({ request, user }: RequestHandlerParams)
| 19 | } |
| 20 | |
| 21 | async function post({ request, user }: RequestHandlerParams) { |
| 22 | const isMultiFactorAuthEnabled = await AppConfig.isMultiFactorAuthEnabled(); |
| 23 | |
| 24 | if (!isMultiFactorAuthEnabled) { |
| 25 | const responseBody: ResponseBody = { |
| 26 | success: false, |
| 27 | error: 'Multi-factor authentication is not enabled on this server', |
| 28 | }; |
| 29 | |
| 30 | return new Response(JSON.stringify(responseBody), { status: 403 }); |
| 31 | } |
| 32 | |
| 33 | const body = await request.clone().json() as RequestBody; |
| 34 | const { methodId, password, disableAll } = body; |
| 35 | |
| 36 | if (!password) { |
| 37 | const responseBody: ResponseBody = { |
| 38 | success: false, |
| 39 | error: 'Password is required', |
| 40 | }; |
| 41 | |
| 42 | return new Response(JSON.stringify(responseBody), { status: 400 }); |
| 43 | } |
| 44 | |
| 45 | const hashedPassword = await generateHash(`${password}:${PASSWORD_SALT}`, 'SHA-256'); |
| 46 | |
| 47 | if (user!.hashed_password !== hashedPassword) { |
| 48 | const responseBody: ResponseBody = { |
| 49 | success: false, |
| 50 | error: 'Invalid password', |
| 51 | }; |
| 52 | |
| 53 | return new Response(JSON.stringify(responseBody), { status: 400 }); |
| 54 | } |
| 55 | |
| 56 | if (disableAll) { |
| 57 | user!.extra.multi_factor_auth_methods = []; |
| 58 | |
| 59 | await UserModel.update(user!); |
| 60 | |
| 61 | const responseBody: ResponseBody = { |
| 62 | success: true, |
| 63 | }; |
| 64 | |
| 65 | return new Response(JSON.stringify(responseBody)); |
| 66 | } |
| 67 | |
| 68 | if (!methodId) { |
| 69 | const responseBody: ResponseBody = { |
| 70 | success: false, |
| 71 | error: 'Method ID is required when not disabling all methods', |
| 72 | }; |
| 73 | |
| 74 | return new Response(JSON.stringify(responseBody), { status: 400 }); |
| 75 | } |
| 76 | |
| 77 | const method = getMultiFactorAuthMethodByIdFromUser(user!, methodId); |
| 78 |
nothing calls this directly
no test coverage detected