Skip to content

Commit d7083b2

Browse files
author
sachin-maheshwari
authored
Merge pull request #282 from topcoder-platform/dev
Plat-JIRA-152
2 parents 28bc700 + f9db7b0 commit d7083b2

File tree

9 files changed

+942
-5
lines changed

9 files changed

+942
-5
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
25+
const msg = 'Please implement the Create script for this database connection ' +
26+
'at https://manage.auth0.com/#/connections/database';
27+
return callback(new Error(msg)); */
28+
function create(user, callback) {
29+
//console.log("landed here...................................");
30+
var countryObj = JSON.parse(user.user_metadata.country);
31+
var regSource = user.user_metadata.regSource;
32+
var utmSource = user.user_metadata.utmSource;
33+
var utmMedium = user.user_metadata.utmMedium;
34+
var utmCampaign = user.user_metadata.utmCampaign;
35+
var retUrl = user.user_metadata.returnUrl;
36+
var afterActivationURL = retUrl != null ? retUrl : "https://" + configuration.DOMAIN + "/home";
37+
if (regSource === configuration.REG_BUSINESS) {
38+
afterActivationURL = "https://connect." + configuration.DOMAIN;
39+
}
40+
var data = {
41+
"param": {
42+
"handle": user.username,
43+
"email": user.email,
44+
"credential": {
45+
"password": user.password
46+
},
47+
"firstName": user.user_metadata.firstName,
48+
"lastName": user.user_metadata.lastName,
49+
"country": {
50+
"code": countryObj.code,
51+
"isoAlpha3Code": countryObj.alpha3,
52+
"isoAlpha2Code": countryObj.alpha2
53+
},
54+
"regSource": regSource,
55+
"utmSource": utmSource,
56+
"utmMedium": utmMedium,
57+
"utmCampaign": utmCampaign,
58+
},
59+
"options": {
60+
"afterActivationURL": encodeURIComponent(afterActivationURL)
61+
}
62+
};
63+
//console.log("SignUp....", user, data);
64+
request.post({
65+
url: "https://api." + configuration.DOMAIN + "/v3/users",
66+
json: data
67+
//for more options check:
68+
//https://github.com/mikeal/request#requestoptions-callback
69+
}, function (err, response, body) {
70+
71+
// console.log(err);
72+
// console.log(response.statusCode);
73+
// console.log(body.result.content);
74+
75+
if (err) return callback(err);
76+
console.log(body.result.content);
77+
if (response.statusCode !== 200) {
78+
//return callback(new ValidationError("lock.fallback",body.result.content));
79+
const error_message = body.result.content;
80+
let code = "lock.fallback";
81+
82+
if (error_message.search("Handle may not contain a space") !== -1) {
83+
code = "handle_invalid_space";
84+
} else if (error_message.search("Length of Handle in character should be between 2 and 15") !== -1) {
85+
code = "handle_invalid_length";
86+
} else if (error_message.search("Please choose another handle, not starting with admin") !== -1) {
87+
code = "handle_invalid_startwith_admin";
88+
} else if (error_message.search('Handle may contain only letters, numbers and') !== -1) {
89+
code = "handle_invalid_constains_forbidden_char";
90+
} else if (error_message.search("Handle may not contain only punctuation") !== -1) {
91+
code = "handle_invalid_conatins_only_punctuation";
92+
} else if (error_message.search("The user already exists") !== -1) {
93+
code = "user_exists";
94+
} else if (error_message.search("has already been taken") !== -1) {
95+
code = "user_exists";
96+
}
97+
98+
99+
100+
return callback(new ValidationError(code, error_message));
101+
102+
//return callback(new Error(body.result.content));
103+
}
104+
//if (response.statusCode === 401) return callback();
105+
/* const Analytics = require('analytics-node');
106+
const _ = require('lodash');
107+
var analytics = new Analytics('bkPtWMUMTYDhww2zsJluzxtdhtmSsyd9');
108+
analytics.identify({
109+
anonymousId: 'signup',
110+
traits: {
111+
user: _.omit(user, ['credential', 'password'])
112+
}
113+
});
114+
analytics.track({
115+
anonymousId: 'BXWXUWnilVUPdN01t2Se29Tw2ZYNGZvH',
116+
event: 'signUp',
117+
properties: _.omit(user, ['credential', 'password'])
118+
});*/
119+
callback(null);
120+
}); //end post request
121+
//callback(null);
122+
}
123+
124+
//}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function login(handleOrEmail, password, callback) {
2+
// This script should authenticate a user against the credentials stored in
3+
// your database.
4+
// It is executed when a user attempts to log in or immediately after signing
5+
// up (as a verification that the user was successfully signed up).
6+
//
7+
// Everything returned by this script will be set as part of the user profile
8+
// and will be visible by any of the tenant admins. Avoid adding attributes
9+
// with values such as passwords, keys, secrets, etc.
10+
//
11+
// The `password` parameter of this function is in plain text. It must be
12+
// hashed/salted to match whatever is stored in your database. For example:
13+
//
14+
// var bcrypt = require('bcrypt@0.8.5');
15+
// bcrypt.compare(password, dbPasswordHash, function(err, res)) { ... }
16+
//
17+
// There are three ways this script can finish:
18+
// 1. The user's credentials are valid. The returned user profile should be in
19+
// the following format: https://auth0.com/docs/users/normalized/auth0/normalized-user-profile-schema
20+
// var profile = {
21+
// user_id: ..., // user_id is mandatory
22+
// email: ...,
23+
// [...]
24+
// };
25+
// callback(null, profile);
26+
// 2. The user's credentials are invalid
27+
// callback(new WrongUsernameOrPasswordError(email, "my error message"));
28+
// 3. Something went wrong while trying to reach your database
29+
// callback(new Error("my error message"));
30+
//
31+
// A list of Node.js modules which can be referenced is available here:
32+
//
33+
// https://tehsis.github.io/webtaskio-canirequire/
34+
request.post({
35+
url: "https://api."+configuration.DOMAIN+"/v3/users/login",
36+
form: {
37+
handleOrEmail: handleOrEmail,
38+
password: password
39+
}
40+
//for more options check: https://github.com/mikeal/request#requestoptions-callback
41+
}, function (err, response, body) {
42+
console.log("response..............", err,response.statusCode);
43+
if (err) return callback(err);
44+
if (response.statusCode === 401) return callback();
45+
var user = JSON.parse(body);
46+
user.result.content.roles = user.result.content.roles.map(function(role) {
47+
return role.roleName;
48+
});
49+
50+
callback(null, {
51+
user_id: user.result.content.id,
52+
nickname: user.result.content.handle,
53+
email: user.result.content.email,
54+
handle:user.result.content.handle,
55+
roles: user.result.content.roles,
56+
email_verified: user.result.content.emailActive,
57+
created_at: user.result.content.createdAt
58+
});
59+
});
60+
}
61+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
function (user, context, callback) {
3+
if (context.clientID === configuration.CLIENT_ACCOUNTS_LOGIN) { //
4+
const _ = require('lodash');
5+
6+
// TODO: implement your rule
7+
// if (context.protocol === "redirect-callback") {
8+
// User was redirected to the /continue endpoint
9+
if (context.redirect) {
10+
return callback(null, user, context);
11+
// returnning from here no need to check further
12+
}
13+
// otherwise to nothing
14+
15+
console.log("Enter Rule: Custom-Claims");
16+
let handle = _.get(user, "handle", null);
17+
const provider = _.get(user, "identities[0].provider", null);
18+
if (!handle && provider === "auth0") {
19+
handle = _.get(user, "nickname", null);
20+
}
21+
console.log("Fetch roles for email/handle: ", user.email, handle, provider);
22+
23+
global.AUTH0_CLAIM_NAMESPACE = "https://" + configuration.DOMAIN + "/";
24+
try {
25+
request.post({
26+
url: 'https://api.' + configuration.DOMAIN + '/v3/users/roles',
27+
form: {
28+
email: user.email,
29+
handle: handle
30+
}
31+
}, function (err, response, body) {
32+
console.log("called topcoder api for role: response status - ", response.statusCode);
33+
if (err) return callback(err, user, context);
34+
if (response.statusCode !== 200) {
35+
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:support@topcoder.com">support@topcoder.com</a>. Back to application ', user, context);
36+
}
37+
38+
let res = JSON.parse(body);
39+
// TODO need to double sure about multiple result or no result
40+
let userId = res.result.content.id;
41+
let handle = res.result.content.handle;
42+
let roles = res.result.content.roles.map(function (role) {
43+
return role.roleName;
44+
});
45+
let userStatus = res.result.content.active; // true/false
46+
47+
// TEMP
48+
let tcsso = res.result.content.regSource || '';
49+
50+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'roles'] = roles;
51+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'userId'] = userId;
52+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'handle'] = handle;
53+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'user_id'] = user.identities[0].provider + "|" + userId;
54+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'tcsso'] = tcsso;
55+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'active'] = userStatus;
56+
context.idToken.nickname = handle;
57+
//console.log(user, context);
58+
if (!userStatus) {
59+
context.redirect = {
60+
url: `https://accounts-auth0.${configuration.DOMAIN}/check_email.html`
61+
};
62+
return callback(null, user, context);
63+
}
64+
if (!userStatus && context.login_counts > 1) {
65+
return callback('Login Alert: Please verify your email first! Please connect to our support <a href="mailto:support@topcoder.com">support@topcoder.com</a>. Back to application ', user, context);
66+
}
67+
return callback(null, user, context);
68+
}
69+
);
70+
} catch (e) {
71+
console.log("Error in calling user roles" + e);
72+
return callback("Something went worng!. Please retry.", user, context);
73+
}
74+
} else {
75+
// for other apps do nothing
76+
return callback(null, user, context);
77+
}
78+
}

0 commit comments

Comments
 (0)