Skip to content

Commit 26050c4

Browse files
author
sachin-maheshwari
authored
Merge branch 'master' into dev
2 parents c118abe + 82588e3 commit 26050c4

File tree

8 files changed

+750
-1
lines changed

8 files changed

+750
-1
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*function create(user, callback) {
2+
// This script should create a user entry in your existing database. It will
3+
// be executed when a user attempts to sign up, or when a user is created
4+
// through the Auth0 dashboard or API.
5+
// When this script has finished executing, the Login script will be
6+
// executed immediately afterwards, to verify that the user was created
7+
// successfully.
8+
//
9+
// The user object will always contain the following properties:
10+
// * email: the user's email
11+
// * password: the password entered by the user, in plain text
12+
// * tenant: the name of this Auth0 account
13+
// * client_id: the client ID of the application where the user signed up, or
14+
// API key if created through the API or Auth0 dashboard
15+
// * connection: the name of this database connection
16+
//
17+
// There are three ways this script can finish:
18+
// 1. A user was successfully created
19+
// callback(null);
20+
// 2. This user already exists in your database
21+
// callback(new ValidationError("user_exists", "my error message"));
22+
// 3. Something went wrong while trying to reach your database
23+
// callback(new Error("my error message"));
24+
const msg = 'Please implement the Create script for this database connection ' +
25+
'at https://manage.auth0.com/#/connections/database';
26+
return callback(new Error(msg)); */
27+
function create(user, callback) {
28+
29+
var countryObj = JSON.parse(user.user_metadata.country);
30+
var regSource = user.user_metadata.regSource;
31+
var utmSource = user.user_metadata.utmSource;
32+
var utmMedium = user.user_metadata.utmMedium;
33+
var utmCampaign = user.user_metadata.utmCampaign;
34+
var retUrl = user.user_metadata.returnUrl;
35+
var afterActivationURL = retUrl ? retUrl : "https://platform."+configuration.DOMAIN+"/onboard";
36+
if (regSource === configuration.REG_BUSINESS) {
37+
afterActivationURL = "https://connect."+configuration.DOMAIN;
38+
}
39+
var data = {
40+
"param": {
41+
"handle": user.username,
42+
"email": user.email,
43+
"credential": {
44+
"password": user.password
45+
},
46+
"firstName": user.user_metadata.firstName,
47+
"lastName": user.user_metadata.lastName,
48+
"country": {
49+
"code": countryObj.code,
50+
"isoAlpha3Code": countryObj.alpha3,
51+
"isoAlpha2Code": countryObj.alpha2
52+
},
53+
"regSource": regSource,
54+
"utmSource": utmSource,
55+
"utmMedium": utmMedium,
56+
"utmCampaign": utmCampaign,
57+
},
58+
"options": {
59+
"afterActivationURL": encodeURIComponent(afterActivationURL)
60+
}
61+
};
62+
console.log("SignUp....", user, data);
63+
request.post({
64+
url: "https://api."+configuration.DOMAIN+"/v3/users",
65+
json: data
66+
//for more options check:
67+
//https://github.com/mikeal/request#requestoptions-callback
68+
}, function (err, response, body) {
69+
70+
console.log(err);
71+
console.log(response.statusCode);
72+
console.log(body);
73+
74+
if (err) return callback(err);
75+
76+
if (response.statusCode !== 200) {
77+
//return callback(new ValidationError('user_exists', body.result.content));
78+
const error_message = body.result.content;
79+
let code = "lock.fallback";
80+
81+
if (error_message.search("Handle may not contain a space") !== -1) {
82+
code = "handle_invalid_space";
83+
} else if (error_message.search("Length of Handle in character should be between 2 and 15") !== -1){
84+
code = "handle_invalid_length";
85+
} else if (error_message.search("Please choose another handle, not starting with admin") !== -1) {
86+
code = "handle_invalid_startwith_admin";
87+
} else if (error_message.search('Handle may contain only letters, numbers and') !== -1) {
88+
code = "handle_invalid_constains_forbidden_char";
89+
} else if (error_message.search("Handle may not contain only punctuation") !== -1) {
90+
code = "handle_invalid_conatins_only_punctuation";
91+
} else if (error_message.search("has already been taken") !== -1) {
92+
code = "user_exists";
93+
}
94+
95+
return callback(new ValidationError(code,error_message));
96+
}
97+
//if (response.statusCode === 401) return callback();
98+
callback(null);
99+
}); //end post request
100+
//callback(null);
101+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function login(handleOrEmail, password, callback) {
2+
request.post({
3+
url: "https://api."+configuration.DOMAIN+"/v3/users/login",
4+
form: {
5+
handleOrEmail: handleOrEmail,
6+
password: password
7+
}
8+
//for more options check: https://github.com/mikeal/request#requestoptions-callback
9+
}, function (err, response, body) {
10+
console.log(body);
11+
//console.log("context", context);
12+
if (err) return callback(err);
13+
if (response.statusCode === 401) return callback();
14+
var user = JSON.parse(body);
15+
user.result.content.roles = user.result.content.roles.map(function(role) {
16+
return role.roleName;
17+
});
18+
19+
callback(null, {
20+
user_id: user.result.content.id,
21+
nickname: user.result.content.handle,
22+
email: user.result.content.email,
23+
roles: user.result.content.roles,
24+
email_verified: user.result.content.emailActive,
25+
});
26+
});
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
function (user, context, callback) {
3+
if ((context.clientID === configuration.CLIENT_ACCOUNTS_LOGIN)) { // client/application specific
4+
// TODO: implement your rule
5+
if (context.redirect) {
6+
console.log("context redirect called, existing from custom -claims");
7+
return callback(null, user, context);
8+
// returnning from here no need to check further
9+
}
10+
const _ = require('lodash');
11+
console.log("Enter Rule: Custom-Claims");
12+
let handle = _.get(user, "handle", null);
13+
const provider = _.get(user, "identities[0].provider", null);
14+
if (!handle && provider === "auth0") {
15+
handle = _.get(user, "nickname", null);
16+
}
17+
console.log("Fetch roles for email/handle: ", user.email, handle, provider);
18+
global.AUTH0_CLAIM_NAMESPACE = "https://" + configuration.DOMAIN + "/";
19+
try {
20+
request.post({
21+
url: 'https://api.' + configuration.DOMAIN + '/v3/users/roles',
22+
form: {
23+
email: user.email,
24+
handle: handle
25+
}
26+
}, function (err, response, body) {
27+
console.log("called topcoder api for role: response status - ", response.statusCode);
28+
if (err) return callback(err, user, context);
29+
if (response.statusCode !== 200) {
30+
return callback('Login Error: Whoops! Something went wrong. Looks like your registered email has discrepancy with Authentication. Please connect to our support <a href="mailto:[email protected]">[email protected]</a>. Back to application ', user, context);
31+
}
32+
33+
let res = JSON.parse(body);
34+
// TODO need to double sure about multiple result or no result
35+
let userId = res.result.content.id;
36+
let handle = res.result.content.handle;
37+
let roles = res.result.content.roles.map(function (role) {
38+
return role.roleName;
39+
});
40+
let userStatus = res.result.content.active; // true/false
41+
42+
// TEMP
43+
let tcsso = res.result.content.regSource || '';
44+
45+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'roles'] = roles;
46+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'userId'] = userId;
47+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'handle'] = handle;
48+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'user_id'] = user.identities[0].provider + "|" + userId;
49+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'tcsso'] = tcsso;
50+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'active'] = userStatus;
51+
context.idToken.nickname = handle;
52+
53+
if (!userStatus) {
54+
context.redirect = {
55+
url: `https://accounts-auth0.${configuration.DOMAIN}/check_email.html`
56+
};
57+
return callback(null, user, context);
58+
}
59+
60+
//console.log(user, context);
61+
return callback(null, user, context);
62+
}
63+
);
64+
} catch (e) {
65+
console.log("Error in calling user roles" + e);
66+
return callback("Something went worng!. Please retry.", user, context);
67+
}
68+
} else {
69+
// for other apps do nothing
70+
return callback(null, user, context);
71+
}
72+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
function (user, context, callback) {
2+
if (context.clientID === configuration.CLIENT_ACCOUNTS_LOGIN) { // client/application specific
3+
global.AUTH0_CLAIM_NAMESPACE = "https://" + configuration.DOMAIN + "/";
4+
const _ = require('lodash');
5+
console.log("Enter Rule: Enterprise-User-Registration");
6+
7+
const baseApiUrl = "https://api." + configuration.DOMAIN + "/v3";
8+
//console.log("register user rule executed- user", user);
9+
//console.log("register user rule executed - context", context);
10+
11+
const isEnterprise = (_.get(user, "identities[0].provider") !== 'auth0') &&
12+
!(_.get(user, "identities[0].isSocial")) ? true : false;
13+
14+
console.log("Is enterprise login: ", isEnterprise);
15+
if (isEnterprise) {
16+
let provider = _.get(user, "identities[0].connection");
17+
const providerType = _.get(user, "identities[0].provider");
18+
let userId = _.get(user, "identities[0].user_id");
19+
userId = userId.substring(userId.lastIndexOf('|') + 1);
20+
21+
let handle = _.get(user, "nickname", "");
22+
const lastName = _.get(user, "family_name");
23+
const firstName = _.get(user, "given_name");
24+
const email = _.get(user, "email");
25+
//const emailVerified = _.get(user, "email_verified", true);
26+
const name = _.get(user, "name");
27+
28+
let isoAlpha2Code = _.get(context, "request.geoip.country_code");
29+
let isoAlpha3Code = _.get(context, "request.geoip.country_code3");
30+
let countryCode = _.get(context, "request.geoip.country_name");
31+
let regSource = _.get(context, "request.query.regSource", null);
32+
let retUrl = _.get(context, "request.query.returnUrl", null);
33+
let utmSource = _.get(context, "request.query.utmSource", null);
34+
let utmMedium = _.get(context, "request.query.utmMedium", null);
35+
let utmCampaign = _.get(context, "request.query.utmCampaign", null);
36+
37+
const resourcePath = '/identityproviders?filter=handle=' + email;
38+
const afterActivationURL = configuration.DEFAULT_AFTER_ACTIVATION_URL;
39+
const hostName = _.get(context, "request.hostname", null);
40+
const registrationCompletetUrl = "https://" + hostName + "/continue";
41+
//const userHandleRedirectUrl = configuration.CUSTOM_PAGES_BASE_URL + '/signup.html?source='+ utmSource + '&formAction=' + registrationCompletetUrl;
42+
const userHandleRedirectUrl = configuration.CUSTOM_PAGES_BASE_URL +
43+
"/signup.html?regSource=" + regSource +
44+
"&firstName=" + encodeURIComponent(firstName) +
45+
"&lastName=" + encodeURIComponent(lastName) +
46+
"&utmSource=" + encodeURIComponent(utmSource) +
47+
"&utmMedium=" + encodeURIComponent(utmMedium) +
48+
"&utmCampaign=" + encodeURIComponent(utmCampaign) +
49+
"&formAction=" + registrationCompletetUrl +
50+
"&returnUrl=" + retUrl;
51+
52+
console.log("provider", provider, email);
53+
try {
54+
request.get({
55+
url: baseApiUrl + resourcePath
56+
}, function (err, response, body) {
57+
console.log("Enterprise user check - responseBody", body);
58+
59+
if (err) {
60+
console.log("Enterprise validation error:", err);
61+
}
62+
63+
/**
64+
* check if enterprise profile is valid for our TC database
65+
*/
66+
/*
67+
Aug 2021 adding new wipro-sso connection with name wipro_azuread
68+
*/
69+
if (_.includes([configuration.WIPRO_SSO_AZURE_AD_CONNECTION_NAME], provider)) {
70+
provider = configuration.WIPRO_SSO_ADFS_CONNECTION_NAME;
71+
}
72+
73+
let isSSOUserExist = (_.get(JSON.parse(body), "result.content.name") === provider) ?true : false;
74+
75+
console.log("Enterprise customer alreday available:", isSSOUserExist);
76+
77+
if (!isSSOUserExist) {
78+
console.log("register enterprise user.");
79+
if (context.protocol === "redirect-callback") {
80+
// User was redirected to the /continue endpoint
81+
console.log("print data", context, user);
82+
console.log("get user extra data from query param");
83+
handle = _.get(context, "request.query.handle", handle);
84+
const countryStr = _.get(context, "request.query.country", null);
85+
const countryObj = JSON.parse(countryStr);
86+
if (countryObj) {
87+
countryCode = _.get(countryObj, "code", countryCode);
88+
isoAlpha2Code = _.get(countryObj, "alpha2", isoAlpha2Code);
89+
isoAlpha3Code = _.get(countryObj, "alpha3", isoAlpha3Code);
90+
}
91+
utmSource = _.get(context, "request.query.source", utmSource);
92+
utmMedium = _.get(context, "request.query.utmMedium", utmMedium);
93+
utmCampaign = _.get(context, "request.query.utmCampaign", utmCampaign);
94+
} else {
95+
console.log('Redirect to choose user handle page.');
96+
context.redirect = {
97+
url: userHandleRedirectUrl
98+
};
99+
return callback(null, user, context);
100+
}
101+
// Enterprise profile will be active default
102+
let data = {
103+
"param": {
104+
"handle": handle,
105+
"firstName": firstName,
106+
"lastName": lastName,
107+
"email": email,
108+
"country": {
109+
"code": countryCode,
110+
"isoAlpha3Code": isoAlpha3Code,
111+
"isoAlpha2Code": isoAlpha2Code
112+
},
113+
"utmSource": utmSource,
114+
"utmMedium": utmMedium,
115+
"utmCampaign": utmCampaign,
116+
"active": true,
117+
"profile": {
118+
"name": name,
119+
"email": email,
120+
"providerType": providerType,
121+
"provider": provider,
122+
"userId": userId
123+
}
124+
},
125+
"options": {
126+
"afterActivationURL": afterActivationURL
127+
}
128+
};
129+
request.post({
130+
url: "https://api." + configuration.DOMAIN + "/v3/users",
131+
json: data
132+
}, function (error, response, body) {
133+
if (response.statusCode !== 200) {
134+
console.log("Enterprise registration error", error);
135+
}
136+
// on success
137+
return callback(null, user, context);
138+
//if (response.statusCode === 401) return callback();
139+
});
140+
} else { // valid social user if block end
141+
return callback(null, user, context);
142+
}
143+
}
144+
); // end validatesocial request
145+
} catch (e) {
146+
console.log(`Error in calling validate enterprise user ${e}`);
147+
return callback(null, user, context);
148+
}
149+
} else {// end isSocial if-block
150+
console.log("existing from Enterprise-User-Registration rule.");
151+
return callback(null, user, context);
152+
}
153+
} else { // END client-id check
154+
return callback(null, user, context);
155+
}
156+
}

0 commit comments

Comments
 (0)