Skip to content

Commit

Permalink
Last minute fixes and cleanup
Browse files Browse the repository at this point in the history
        * Added missing semicolons and remove unnecessary one
        * Removed publishSNSMessage function which was a legacy alert
          component and not longer used.
        * Rearranged function declaration and call to proper
          declare->call order
  • Loading branch information
dividehex committed May 30, 2024
1 parent 6a6d645 commit 8435755
Showing 1 changed file with 36 additions and 72 deletions.
108 changes: 36 additions & 72 deletions rules/link-users-by-email-with-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ function linkUsersByEmailWithMetadata(user, context, callback) {
headers: {
Authorization: 'Bearer ' + auth0.accessToken,
},
}
};

// Since email addresses within auth0 are allowed to be mixed case and the /user-by-email search endpoint
// is case sensitive, we need to search for both situations. In the first search we search by "this" users email
// which might be mixed case (or not). Our second search is for the lowercase equivalent but only if two searches
// would be different.
const searchMultipleEmailCases = async () => {
const emailUrl = new URL('/users-by-email', auth0.baseUrl)
const emailUrl = new URL('/users-by-email', auth0.baseUrl);
emailUrl.searchParams.append('email', user.email);

const emailUrlToLower = new URL('/users-by-email', auth0.baseUrl);
Expand Down Expand Up @@ -63,46 +63,7 @@ function linkUsersByEmailWithMetadata(user, context, callback) {
const mergedProfiles = allResponses.flat();

return mergedProfiles;
}

const data = searchMultipleEmailCases();

try {
// Ignore non-verified users
data = data.filter((u) => u.email_verified);

if (data.length <= 1) {
// The user logged in with an identity which is the only one Auth0 knows about
// or no data returned
// Do not perform any account linking
return callback(null, user, context);
}

if (data.length === 2) {
// Auth0 is aware of 2 identities with the same email address which means
// that the user just logged in with a new identity that hasn't been linked
// into the other existing identity. Here we pass the other account to the
// linking function
linkAccount(data.filter((u) => u.user_id !== user.user_id)[0]);
} else {
// data.length is > 2 which, post November 2020 when all identities were
// force linked manually, shouldn't be possible
var error_message =
`Error linking account ${user.user_id} as there are ` +
`over 2 identities with the email address ${user.email} ` +
data.map((x) => x.user_id).join();
console.log(error_message);
publishSNSMessage(
`${error_message}\n\ndata : ${JSON.stringify(
data
)}\nuser : ${JSON.stringify(user)}`
);
return callback(new Error(error_message));
}
} catch (err) {
console.log('An unknown error occurred while linking accounts: ' + err);
return callback(err);
}
};

const linkAccount = (otherProfile) => {
// sanity check if both accounts have LDAP as primary
Expand Down Expand Up @@ -162,38 +123,41 @@ function linkUsersByEmailWithMetadata(user, context, callback) {
} catch(err) {
console.log('An unknown error occurred while linking accounts: ' + err);
return callback(err);
};
}
};

const publishSNSMessage = (message) => {
if (
!('aws_logging_sns_topic_arn' in configuration) ||
!('aws_logging_access_key_id' in configuration) ||
!('aws_logging_secret_key' in configuration)
) {
console.log('Missing Auth0 AWS SNS logging configuration values');
return false;
// Search for multiple accounts of the same user to link
let data = searchMultipleEmailCases();

try {
// Ignore non-verified users
data = data.filter((u) => u.email_verified);

if (data.length <= 1) {
// The user logged in with an identity which is the only one Auth0 knows about
// or no data returned
// Do not perform any account linking
return callback(null, user, context);
}

const SNS_TOPIC_ARN = configuration.aws_logging_sns_topic_arn;
const ACCESS_KEY_ID = configuration.aws_logging_access_key_id;
const SECRET_KEY = configuration.aws_logging_secret_key;

let AWS = require('[email protected]');
let sns = new AWS.SNS({
apiVersion: '2010-03-31',
accessKeyId: ACCESS_KEY_ID,
secretAccessKey: SECRET_KEY,
region: 'us-west-2',
logger: console,
});
const params = {
Message: message,
TopicArn: SNS_TOPIC_ARN,
};
sns.publish(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
};
if (data.length === 2) {
// Auth0 is aware of 2 identities with the same email address which means
// that the user just logged in with a new identity that hasn't been linked
// into the other existing identity. Here we pass the other account to the
// linking function
linkAccount(data.filter((u) => u.user_id !== user.user_id)[0]);
} else {
// data.length is > 2 which, post November 2020 when all identities were
// force linked manually, shouldn't be possible
var error_message =
`Error linking account ${user.user_id} as there are ` +
`over 2 identities with the email address ${user.email} ` +
data.map((x) => x.user_id).join();
console.log(error_message);
return callback(new Error(error_message));
}
} catch (err) {
console.log('An unknown error occurred while linking accounts: ' + err);
return callback(err);
}
}

0 comments on commit 8435755

Please sign in to comment.