Invitation is now also just a single route like password reset

This commit is contained in:
Johannes Zellner
2021-09-16 14:56:10 +02:00
parent 074ce574dd
commit 6785253377
4 changed files with 18 additions and 58 deletions
+14 -31
View File
@@ -27,7 +27,6 @@ exports = module.exports = {
del,
createInvite,
sendInvite,
setTwoFactorAuthenticationSecret,
@@ -555,7 +554,7 @@ async function sendPasswordResetByIdentifier(identifier, auditSource) {
user.resetToken = resetToken;
user.resetTokenCreationTime = resetTokenCreationTime;
await update(user, { resetToken, resetTokenCreationTime }, auditSource);
await update(user, { resetToken,resetTokenCreationTime }, auditSource);
const resetLink = `${settings.dashboardOrigin()}/login.html?resetToken=${user.resetToken}`;
await mailer.passwordReset(user, resetLink);
@@ -645,43 +644,27 @@ async function createOwner(email, username, password, displayName, auditSource)
return await add(email, { username, password, displayName, role: exports.ROLE_OWNER }, auditSource);
}
function inviteLink(user, directoryConfig) {
let link = `${settings.dashboardOrigin()}/setupaccount.html?resetToken=${user.resetToken}&email=${encodeURIComponent(user.email)}`;
if (user.username) link += `&username=${encodeURIComponent(user.username)}`;
if (user.displayName) link += `&displayName=${encodeURIComponent(user.displayName)}`;
if (directoryConfig.lockUserProfiles) link += '&profileLocked=true';
return link;
}
async function createInvite(user, auditSource) {
assert.strictEqual(typeof user, 'object');
assert.strictEqual(typeof auditSource, 'object');
if (user.source) throw new BoxError(BoxError.CONFLICT, 'User is from an external directory');
const resetToken = hat(256), resetTokenCreationTime = new Date();
const directoryConfig = await settings.getDirectoryConfig();
await update(user, { resetToken, resetTokenCreationTime }, auditSource);
user.resetToken = resetToken;
return { resetToken, inviteLink: inviteLink(user, directoryConfig) };
}
async function sendInvite(user, options) {
async function sendInvite(user, options, auditSource) {
assert.strictEqual(typeof user, 'object');
assert.strictEqual(typeof options, 'object');
if (user.source) throw new BoxError(BoxError.CONFLICT, 'User is from an external directory');
if (!user.resetToken) throw new BoxError(BoxError.CONFLICT, 'Must generate resetToken to send invitation');
// if (!user.resetToken) throw new BoxError(BoxError.CONFLICT, 'Must generate resetToken to send invitation');
const resetToken = hat(256);
const resetTokenCreationTime = new Date();
await update(user, { resetToken, resetTokenCreationTime }, auditSource);
const directoryConfig = await settings.getDirectoryConfig();
let inviteLink = `${settings.dashboardOrigin()}/setupaccount.html?resetToken=${user.resetToken}&email=${encodeURIComponent(user.email)}`;
if (user.username) inviteLink += `&username=${encodeURIComponent(user.username)}`;
if (user.displayName) inviteLink += `&displayName=${encodeURIComponent(user.displayName)}`;
if (directoryConfig.lockUserProfiles) inviteLink += '&profileLocked=true';
await mailer.sendInvite(user, options.invitor || null, inviteLink(user, directoryConfig));
return inviteLink;
}
async function setupAccount(user, data, auditSource) {