Skip to content

Commit

Permalink
Merge pull request #224 from aaguiarz/aa-account-linking
Browse files Browse the repository at this point in the history
Remove account linking rules
  • Loading branch information
faroceann authored May 11, 2020
2 parents e2dd84f + 5d38646 commit 3842800
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 608 deletions.
20 changes: 0 additions & 20 deletions rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,6 @@
"description": "<p>This rule will only allow access to an app from a specific set of IP addresses</p>",
"code": "function ipAddressWhitelist(user, context, callback) {\n const whitelist = ['1.2.3.4', '2.3.4.5']; // authorized IPs\n const userHasAccess = whitelist.some(function (ip) {\n return context.request.ip === ip;\n });\n\n if (!userHasAccess) {\n return callback(new Error('Access denied from this IP address.'));\n }\n\n return callback(null, user, context);\n}"
},
{
"id": "link-users-by-email-with-metadata",
"title": "Link Accounts with Same Email Address while Merging Metadata",
"overview": "Link any accounts that have the same email address while merging metadata.",
"categories": [
"access control"
],
"description": "<p>This rule will link any accounts that have the same email address while merging metadata.</p>",
"code": "function linkUsersByEmailWithMetadata(user, context, callback) {\n const request = require('request');\n const _ = require('lodash');\n\n // Check if email is verified, we shouldn't automatically\n // merge accounts if this is not the case.\n if (!user.email || !user.email_verified) {\n return callback(null, user, context);\n }\n\n const userApiUrl = auth0.baseUrl + '/users';\n const userSearchApiUrl = auth0.baseUrl + '/users-by-email';\n\n request({\n url: userSearchApiUrl,\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n qs: {\n email: user.email\n }\n },\n function (err, response, body) {\n if (err) return callback(err);\n if (response.statusCode !== 200) return callback(new Error(body));\n\n var data = JSON.parse(body);\n // Ignore non-verified users and current user, if present\n data = data.filter(function (u) {\n return u.email_verified && (u.user_id !== user.user_id);\n });\n\n if (data.length > 1) {\n return callback(new Error('[!] Rule: Multiple user profiles already exist - cannot select base profile to link with'));\n }\n if (data.length === 0) {\n console.log('[-] Skipping link rule');\n return callback(null, user, context);\n }\n\n const originalUser = data[0];\n const provider = user.identities[0].provider;\n const providerUserId = user.identities[0].user_id;\n const mergeCustomizer = function(objectValue, sourceValue){\n if (_.isArray(objectValue)){\n return sourceValue.concat(objectValue);\n }\n };\n const mergedUserMetadata = _.merge({}, user.user_metadata, originalUser.user_metadata, mergeCustomizer);\n const mergedAppMetadata = _.merge({}, user.app_metadata, originalUser.app_metadata, mergeCustomizer);\n \n auth0.users.updateAppMetadata(originalUser.user_id, mergedAppMetadata)\n .then(auth0.users.updateUserMetadata(originalUser.user_id, mergedUserMetadata))\n .then(function() {\n request.post({\n url: userApiUrl + '/' + originalUser.user_id + '/identities',\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n json: { provider: provider, user_id: String(providerUserId) }\n }, function (err, response, body) {\n if (response && response.statusCode >= 400) {\n return callback(new Error('Error linking account: ' + response.statusMessage));\n }\n context.primaryUser = originalUser.user_id;\n callback(null, user, context);\n });\n })\n .catch(function (err) {\n callback(err);\n });\n });\n}"
},
{
"id": "link-users-by-email",
"title": "Link Accounts with Same Email Address",
"overview": "Link any accounts that have the same email address.",
"categories": [
"access control"
],
"description": "<p>This rule will link any accounts that have the same email address.</p>\n<blockquote>\n <p>Note: When linking accounts, only the metadata of the target user is saved. If you want to merge the metadata of the two accounts you must do that manually. See the document on <a href=\"https://auth0.com/docs/link-accounts\">Linking Accounts</a> for more details.</p>\n</blockquote>",
"code": "function linkUsersByEmail(user, context, callback) {\n const request = require('request');\n // Check if email is verified, we shouldn't automatically\n // merge accounts if this is not the case.\n if (!user.email || !user.email_verified) {\n return callback(null, user, context);\n }\n const userApiUrl = auth0.baseUrl + '/users';\n const userSearchApiUrl = auth0.baseUrl + '/users-by-email';\n\n request({\n url: userSearchApiUrl,\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n qs: {\n email: user.email\n }\n },\n function(err, response, body) {\n if (err) return callback(err);\n if (response.statusCode !== 200) return callback(new Error(body));\n\n var data = JSON.parse(body);\n // Ignore non-verified users and current user, if present\n data = data.filter(function(u) {\n return u.email_verified && (u.user_id !== user.user_id);\n });\n\n if (data.length > 1) {\n return callback(new Error('[!] Rule: Multiple user profiles already exist - cannot select base profile to link with'));\n }\n if (data.length === 0) {\n console.log('[-] Skipping link rule');\n return callback(null, user, context);\n }\n\n const originalUser = data[0];\n const provider = user.identities[0].provider;\n const providerUserId = user.identities[0].user_id;\n\n request.post({\n url: userApiUrl + '/' + originalUser.user_id + '/identities',\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n json: {\n provider: provider,\n user_id: String(providerUserId)\n }\n }, function(err, response, body) {\n if (response.statusCode >= 400) {\n return callback(new Error('Error linking account: ' + response.statusMessage));\n }\n context.primaryUser = originalUser.user_id;\n callback(null, user, context);\n });\n });\n}"
},
{
"id": "roles-creation",
"title": "Set roles to a user",
Expand Down
83 changes: 0 additions & 83 deletions src/rules/link-users-by-email-with-metadata.js

This file was deleted.

71 changes: 0 additions & 71 deletions src/rules/link-users-by-email.js

This file was deleted.

Loading

0 comments on commit 3842800

Please sign in to comment.