MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / post

Function post

pages/mfa-verify.tsx:63–159  ·  view source on GitHub ↗
({ request, match, session, isRunningLocally }: RequestHandlerParams)

Source from the content-addressed store, hash-verified

61}
62
63async function post({ request, match, session, isRunningLocally }: RequestHandlerParams) {
64 const isMultiFactorAuthEnabled = await AppConfig.isMultiFactorAuthEnabled();
65
66 if (!isMultiFactorAuthEnabled) {
67 return new Response('Redirect', { status: 303, headers: { 'Location': '/login' } });
68 }
69
70 const searchParams = new URL(request.url).searchParams;
71 const redirectUrl = searchParams.get('redirect') || '/';
72
73 const { user } = (await MultiFactorAuthModel.getDataFromRequest(request)) || {};
74
75 if (!user) {
76 return new Response('Redirect', { status: 303, headers: { 'Location': '/login' } });
77 }
78
79 const hasMultiFactorAuthEnabled = isMultiFactorAuthEnabledForUser(user);
80
81 if (!hasMultiFactorAuthEnabled) {
82 return new Response('Redirect', { status: 303, headers: { 'Location': '/login' } });
83 }
84
85 const enabledMethods = getEnabledMultiFactorAuthMethodsFromUser(user);
86 const availableMethods = enabledMethods.map((method) => method.type);
87
88 try {
89 const formData = await request.formData();
90 const code = getFormDataField(formData, 'code');
91 const token = getFormDataField(formData, 'token');
92
93 if (!code && !token) {
94 throw new Error('Authentication code/token is required');
95 }
96
97 let isValid = false;
98 let updateUser = false;
99
100 for (const method of enabledMethods) {
101 // Passkey verification is handled in a separate process
102 if (method.type === 'passkey') {
103 continue;
104 }
105
106 if (method.type === 'totp') {
107 const verification = await TOTPModel.verifyMethodToken(method.metadata, token);
108 if (verification.isValid) {
109 isValid = true;
110
111 if (verification.remainingCodes && method.type === 'totp' && method.metadata.totp) {
112 method.metadata.totp.hashed_backup_codes = verification.remainingCodes;
113 updateUser = true;
114 }
115 break;
116 }
117 }
118
119 if (method.type === 'email') {
120 const verification = await EmailModel.verifyCode(method.id, code, user);

Callers

nothing calls this directly

Calls 12

getFormDataFieldFunction · 0.90
createSessionResponseFunction · 0.90
basicLayoutResponseFunction · 0.90
verifyMethodTokenMethod · 0.80
verifyCodeMethod · 0.80
defaultHtmlContentFunction · 0.70
getMethod · 0.45
getDataFromRequestMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected