From d837e138a6858581c47a473e16965c34e20d7688 Mon Sep 17 00:00:00 2001 From: Drew Fyock Date: Mon, 29 Jul 2019 09:31:16 -0400 Subject: [PATCH] 0.8.6 --- package-lock.json | 2 +- package.json | 2 +- rules.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3aa6ad4e..af68fcd3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "rules-templates", - "version": "0.8.5", + "version": "0.8.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 56493fe1..62f8adb4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rules-templates", - "version": "0.8.5", + "version": "0.8.6", "description": "Auth0 Rules Repository", "main": "./rules", "scripts": { diff --git a/rules.json b/rules.json index db15ccdb..fd75b1c0 100644 --- a/rules.json +++ b/rules.json @@ -100,7 +100,7 @@ "access control" ], "description": "

This rule will link any accounts that have the same email address while merging metadata.

", - "code": "function (user, context, callback) {\n const request = require('request');\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\n user.app_metadata = user.app_metadata || {};\n user.user_metadata = user.user_metadata || {};\n auth0.users.updateAppMetadata(originalUser.user_id, user.app_metadata)\n .then(auth0.users.updateUserMetadata(originalUser.user_id, user.user_metadata))\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}" + "code": "function (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",