Created
August 17, 2020 19:07
-
-
Save yangchenyun/3b28869bfcafc57ad53e848af77c9c14 to your computer and use it in GitHub Desktop.
Test Authentication for JD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function UnauthorizedError(msg) { | |
return new Error(msg); | |
} | |
let runrule = function (user, context, callback) { | |
// do not restrict social login users | |
if (user.identities[0].isSocial){ | |
return callback(null, user, context); | |
} | |
// if the user has meta data | |
if (user.hasOwnProperty('app_metadata')){ | |
// if the user has property instance | |
if (user.app_metadata.hasOwnProperty('instance')){ | |
// if the client has an instance | |
if (context.clientMetadata.hasOwnProperty('instance')){ | |
// if users have the instance or have * in their thingy | |
// or the client has * in which case all users can access it | |
if (context.clientMetadata.instance !== '*' && user.app_metadata.instance.indexOf(context.clientMetadata.instance) === -1 && user.app_metadata.instance.indexOf("*") === -1) { | |
return callback(new UnauthorizedError('Whoops - You ('+ user.email + ') are currently logged into an account that doesn\'t have access to this FetchCore instance. Please log out and log back in with an authorized account.')); | |
} | |
} else { | |
return callback(new UnauthorizedError('Client must have the instance property')); | |
} | |
} else { | |
return callback(new UnauthorizedError('Users must have the instance property')); | |
} | |
} else { | |
return callback(new UnauthorizedError('App metadata must exist')); | |
} | |
callback(null, user, context); | |
} | |
let user = | |
{ | |
"username": "0e6f439695cfe44a08ea6f9e1069f5b773388eaf4e1e7fec7e41b003af1fe03c7a3429f26d17df46e6774525a59379127774ddcc4fa344a6a3db0c88b0fd3291", | |
"email_verified": true, | |
"email": "leiphondavida@johndeere.com", | |
"updated_at": "2020-08-17T17:42:04.476Z", | |
"picture": "https://s.gravatar.com/avatar/1f65336394b578cc98c8242a0900b043?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fle.png", | |
"user_id": "auth0|5c129fc0cf8f34453d864c42", | |
"nickname": "0e6f439695cfe44a08ea6f9e1069f5b773388eaf4e1e7fec7e41b003af1fe03c7a3429f26d17df46e6774525a59379127774ddcc4fa344a6a3db0c88b0fd3291", | |
"identities": [ | |
{ | |
"user_id": "5c129fc0cf8f34453d864c42", | |
"provider": "auth0", | |
"connection": "Username-Password-Authentication", | |
"isSocial": false | |
} | |
], | |
"created_at": "2018-12-13T18:06:56.327Z", | |
"name": "leiphondavida@johndeere.com", | |
"last_password_reset": "2020-08-13T16:55:43.385Z", | |
"user_metadata": {}, | |
"app_metadata": { | |
"instance": [ | |
"jd" | |
], | |
"first_name": "Dave", | |
"last_name": "Leiphon", | |
"roles": [ | |
"admin" | |
], | |
"user_type": "human" | |
}, | |
"last_ip": "167.99.98.62", | |
"last_login": "2020-08-17T17:42:04.475Z", | |
"logins_count": 60, | |
"blocked_for": [], | |
"guardian_authenticators": [] | |
}; | |
let context = { | |
clientMetadata: { | |
"instance": ["jd"], | |
}, | |
}; | |
runrule(user, context, console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment