This commit is contained in:
Girish Ramakrishnan
2024-06-13 16:43:57 +02:00
parent e67324b05c
commit 6b4df0bd65
2 changed files with 11 additions and 14 deletions
+8 -10
View File
@@ -33,11 +33,10 @@ async function login(req, res, next) {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || null;
const userAgent = req.headers['user-agent'] || '';
let error = tokens.validateTokenType(type);
if (error) return next(new HttpError(400, error.message));
const tokenTypeError = tokens.validateTokenType(type);
if (tokenTypeError) return next(new HttpError(400, tokenTypeError.message));
let token;
[error, token] = await safe(tokens.add({ clientId: type, identifier: req.user.id, expires: Date.now() + constants.DEFAULT_TOKEN_EXPIRATION_MSECS }));
const [error, token] = await safe(tokens.add({ clientId: type, identifier: req.user.id, expires: Date.now() + constants.DEFAULT_TOKEN_EXPIRATION_MSECS }));
if (error) return next(new HttpError(500, error));
const auditSource = AuditSource.fromRequest(req);
@@ -71,8 +70,8 @@ async function passwordReset(req, res, next) {
if (typeof req.body.resetToken !== 'string') return next(new HttpError(400, 'Missing resetToken'));
if (typeof req.body.password !== 'string') return next(new HttpError(400, 'Missing password'));
let [error, userObject] = await safe(users.getByResetToken(req.body.resetToken));
if (error) return next(new HttpError(401, 'Invalid resetToken'));
const [getError, userObject] = await safe(users.getByResetToken(req.body.resetToken));
if (getError) return next(new HttpError(401, 'Invalid resetToken'));
if (!userObject) return next(new HttpError(401, 'Invalid resetToken'));
if (userObject.twoFactorAuthenticationEnabled) {
@@ -87,13 +86,12 @@ async function passwordReset(req, res, next) {
if (!userObject.username) return next(new HttpError(409, 'No username set'));
// setPassword clears the resetToken
[error] = await safe(users.setPassword(userObject, req.body.password, AuditSource.fromRequest(req)));
const [error] = await safe(users.setPassword(userObject, req.body.password, AuditSource.fromRequest(req)));
if (error && error.reason === BoxError.BAD_FIELD) return next(new HttpError(400, error.message));
if (error) return next(BoxError.toHttpError(error));
let result;
[error, result] = await safe(tokens.add({ clientId: tokens.ID_WEBADMIN, identifier: userObject.id, expires: Date.now() + constants.DEFAULT_TOKEN_EXPIRATION_MSECS }));
if (error) return next(BoxError.toHttpError(error));
const [addError, result] = await safe(tokens.add({ clientId: tokens.ID_WEBADMIN, identifier: userObject.id, expires: Date.now() + constants.DEFAULT_TOKEN_EXPIRATION_MSECS }));
if (addError) return next(BoxError.toHttpError(addError));
next(new HttpSuccess(202, { accessToken: result.accessToken }));
}