({ user }: RequestHandlerParams)
| 17 | } |
| 18 | |
| 19 | async function post({ user }: RequestHandlerParams) { |
| 20 | const isMultiFactorAuthEnabled = await AppConfig.isMultiFactorAuthEnabled(); |
| 21 | |
| 22 | if (!isMultiFactorAuthEnabled) { |
| 23 | const responseBody: ResponseBody = { |
| 24 | success: false, |
| 25 | error: 'Multi-factor authentication is not enabled on this server', |
| 26 | }; |
| 27 | |
| 28 | return new Response(JSON.stringify(responseBody), { status: 403 }); |
| 29 | } |
| 30 | |
| 31 | const config = await AppConfig.getConfig(); |
| 32 | const issuer = new URL(config.auth.baseUrl).hostname; |
| 33 | const methodId = MultiFactorAuthModel.generateMethodId(); |
| 34 | const setup = await TOTPModel.createMethod(methodId, 'Authenticator App', issuer, user!.email); |
| 35 | |
| 36 | if (!user!.extra.multi_factor_auth_methods) { |
| 37 | user!.extra.multi_factor_auth_methods = []; |
| 38 | } |
| 39 | |
| 40 | user!.extra.multi_factor_auth_methods.push(setup.method); |
| 41 | |
| 42 | await UserModel.update(user!); |
| 43 | |
| 44 | const responseData: ResponseBody = { |
| 45 | success: true, |
| 46 | data: { |
| 47 | methodId: setup.method.id, |
| 48 | secret: setup.plainTextSecret, |
| 49 | qrCodeUrl: setup.qrCodeUrl, |
| 50 | backupCodes: setup.plainTextBackupCodes, |
| 51 | }, |
| 52 | }; |
| 53 | |
| 54 | return new Response(JSON.stringify(responseData)); |
| 55 | } |
| 56 | |
| 57 | export default page({ |
| 58 | post, |
nothing calls this directly
no test coverage detected