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

Function post

pages/api/auth/multi-factor/enable.ts:20–141  ·  view source on GitHub ↗
({ request, user }: RequestHandlerParams)

Source from the content-addressed store, hash-verified

18}
19
20async function post({ request, user }: RequestHandlerParams) {
21 const isMultiFactorAuthEnabled = await AppConfig.isMultiFactorAuthEnabled();
22
23 if (!isMultiFactorAuthEnabled) {
24 const responseBody: ResponseBody = {
25 success: false,
26 error: 'Multi-factor authentication is not enabled on this server',
27 };
28
29 return new Response(JSON.stringify(responseBody), { status: 403 });
30 }
31
32 const body = await request.clone().json() as RequestBody;
33 const { methodId, code } = body;
34
35 if (!methodId || !code) {
36 const responseBody: ResponseBody = {
37 success: false,
38 error: 'Method ID and verification code are required',
39 };
40
41 return new Response(JSON.stringify(responseBody), { status: 400 });
42 }
43
44 const method = getMultiFactorAuthMethodByIdFromUser(user!, methodId);
45 if (!method) {
46 const responseBody: ResponseBody = {
47 success: false,
48 error: 'Multi-factor authentication method not found',
49 };
50
51 return new Response(JSON.stringify(responseBody), { status: 404 });
52 }
53
54 if (method.enabled) {
55 const responseBody: ResponseBody = {
56 success: false,
57 error: 'Multi-factor authentication method is already enabled',
58 };
59
60 return new Response(JSON.stringify(responseBody), { status: 400 });
61 }
62
63 if (method.type === 'totp') {
64 const hashedSecret = method.metadata.totp?.hashed_secret;
65 if (!hashedSecret) {
66 const responseBody: ResponseBody = {
67 success: false,
68 error: 'TOTP secret not found',
69 };
70
71 return new Response(JSON.stringify(responseBody), { status: 400 });
72 }
73
74 try {
75 const secret = await TOTPModel.decryptTOTPSecret(hashedSecret);
76 const isValid = TOTPModel.verifyTOTP(secret, code);
77 if (!isValid) {

Callers

nothing calls this directly

Calls 8

cloneMethod · 0.80
decryptTOTPSecretMethod · 0.80
verifyTOTPMethod · 0.80
verifyCodeMethod · 0.80
enableMethodForUserMethod · 0.80
updateMethod · 0.45

Tested by

no test coverage detected