Skip to content

Commit

Permalink
asd
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop committed Feb 3, 2024
1 parent 6ebe9e6 commit 51c8a8b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
30 changes: 19 additions & 11 deletions src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,26 @@ export const authenticationGate = (
return next();
}

const { authUserSecurityKeys } = await gqlSdk.getUserSecurityKeys({
id: auth.userId,
});

if (authUserSecurityKeys.length === 0 && ENV.AUTH_REQUIRE_ELEVATED_CLAIM === 'recommended') {
return next();
}

if (authUserSecurityKeys.length === 0 && bypassIfNoKeys) {
return next();
if (await failsElevatedCheck(auth.userId, bypassIfNoKeys)) {
return sendError(res, 'elevated-claim-required');
}

return sendError(res, 'elevated-claim-required');
return next();
};
}

export const failsElevatedCheck = async (userId: string, bypassIfNoKeys = false) => {
const response = await gqlSdk.getUserSecurityKeys({
id: userId,
});

if (response.authUserSecurityKeys.length === 0 && ENV.AUTH_REQUIRE_ELEVATED_CLAIM === 'recommended') {
return false;
}

if (response.authUserSecurityKeys.length === 0 && bypassIfNoKeys) {
return false;
}

return true;
};
2 changes: 1 addition & 1 deletion src/routes/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ router.post(
router.post(
'/user/password',
bodyValidator(userPasswordSchema),
authenticationGate(true, false, (req) => req.body.ticket !== undefined),
// authenticationGate(true, false, (req) => req.body.ticket !== undefined), // this is done in the handler because the handler has an auhtenticated and unauthenticated mode.............
aw(userPasswordHandler)
);

Expand Down
7 changes: 7 additions & 0 deletions src/routes/user/password.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { RequestHandler } from 'express';
import { ReasonPhrases } from 'http-status-codes';

import { failsElevatedCheck } from '@/middleware/auth';

import { gqlSdk, hashPassword, getUserByTicket } from '@/utils';
import { sendError } from '@/errors';
import { Joi, password } from '@/validation';
Expand All @@ -27,6 +29,11 @@ export const userPasswordHandler: RequestHandler<
if (!req.auth?.userId) {
return sendError(res, 'unauthenticated-user');
}

if (await failsElevatedCheck(req.auth?.userId)) {
return sendError(res, 'elevated-claim-required');
}

user = (await gqlSdk.user({ id: req.auth?.userId })).user;
}

Expand Down

0 comments on commit 51c8a8b

Please sign in to comment.