Skip to content

Commit 9ba7285

Browse files
author
sachin-maheshwari
authored
Create user-privacy-policy.js
1 parent 5757811 commit 9ba7285

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
function (user, context, callback) {
2+
if (context.clientID === configuration.CLIENT_ACCOUNTS_LOGIN) { // client/application specific
3+
console.log("rule:user-privacy-policy:enter");
4+
5+
const _ = require('lodash');
6+
7+
const loginCount = _.get(context, "stats.loginsCount");
8+
const isAuth0 = (_.get(user, "identities[0].provider") === 'auth0') ? true : false;
9+
10+
/**
11+
* Note : Sachin Maheshwari
12+
* This rule should be execute after Custom-Claims Rule,
13+
* to get the userId for social and enterprise login in 'IdToken'
14+
*/
15+
var userId = _.get(context, `idToken['https://${configuration.DOMAIN}/userId']`, null);
16+
17+
if (isAuth0) {
18+
// no need to check futher as this rule is other than Auth0 i.e. social or enterprise
19+
//return callback(null, user, context);
20+
userId = _.get(user, "identities[0].user_id", null);
21+
}
22+
23+
console.log('rule:user-privacy-policy:logincount', loginCount);
24+
console.log('rule:user-privacy-policy:userId', userId);
25+
26+
if (!userId) {
27+
// no need to check futher
28+
console.log('rule:user-privacy-policy:error:user_id null');
29+
return callback(null, user, context);
30+
}
31+
32+
if (loginCount < 3) {
33+
const getToken = function (tokenCB) {
34+
if (global.M2MToken) {
35+
console.log('rule:user-privacy-policy:a M2M token is present');
36+
const jwt = require('jsonwebtoken');
37+
const moment = require('moment');
38+
const decoded = jwt.decode(global.M2MToken);
39+
const exp = moment.unix(decoded.exp);
40+
41+
if (exp > new Date()) {
42+
console.log('M2MToken is still valid. reusing token');
43+
tokenCB(null, global.M2MToken);
44+
return;
45+
}
46+
}
47+
console.log('rule:user-privacy-policy:fetching fresh m2m token');
48+
request.post({
49+
url: `https://auth0proxy.${configuration.DOMAIN}/token`,
50+
headers: 'content-type: application/json',
51+
json: {
52+
client_id: configuration.M2M_CLIENT_ID,
53+
client_secret: configuration.M2M_CLIENT_SECRET,
54+
audience: configuration.M2M_AUDIENCE,
55+
auth0_url: configuration.M2M_TOKEN_URL,
56+
grant_type: 'client_credentials'
57+
}
58+
}, function (err, response, body) {
59+
if (err) {
60+
tokenCB(err);
61+
return;
62+
}
63+
64+
global.M2MToken = body.access_token;
65+
console.log('rule:user-privacy-policy:setting the M2MToken globally');
66+
tokenCB(null, global.M2MToken);
67+
});
68+
};
69+
70+
const callTermApi = function (token) {
71+
console.log("rule:user-privacy-policy:calling Term API");
72+
73+
var payload = { userId };
74+
75+
request.post({
76+
url: `https://api.${configuration.DOMAIN}/v5/terms/${configuration.TERMS_USER_PRIVACY_POLICY_UUID}/users`,
77+
headers: {
78+
'Authorization': 'Bearer ' + token,
79+
'Content-Type': 'application/json'
80+
},
81+
json: payload
82+
},
83+
function (err, response, body) {
84+
// swallow error
85+
if (err) {
86+
//console.log(err);
87+
} else {
88+
console.log('rule:user-privacy-policy:called term API!');
89+
console.log('rule:user-privacy-policy:response - ', body);
90+
}
91+
callback(null, user, context);
92+
});
93+
};
94+
95+
getToken(function (err, token) {
96+
if (err) {
97+
console.log(err);
98+
} else {
99+
console.log('rule:user-privacy-policy:m2m token', token);
100+
callTermApi(token);
101+
}
102+
});
103+
} // if login count
104+
105+
} // if end
106+
return callback(null, user, context);
107+
}

0 commit comments

Comments
 (0)