({ request, match, session, isRunningLocally }: RequestHandlerParams)
| 61 | } |
| 62 | |
| 63 | async 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); |
nothing calls this directly
no test coverage detected