1 import { IAuthModuleService, IUserModuleService } from "@medusajs/framework/types"
2 import { Modules } from "@medusajs/framework/utils"
3 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
4 import jwt from "jsonwebtoken"
5 import path from "path"
6 import { generateTotpCode } from "../../../../../packages/modules/auth/src/utils/totp"
7
8 jest.setTimeout(100000)
9
10 const createUserWithMfa = async (
11 api,
12 container,
13 email: string,
14 password: string
15 ) => {
16 const signup = await api.post("/auth/user/emailpass/register", {
17 email,
18 password,
19 })
20
21 expect(signup.status).toEqual(200)
22
23 const { auth_identity_id } = jwt.decode(signup.data.token) as {
24 auth_identity_id: string
25 }
26
27 const userModule: IUserModuleService = container.resolve(Modules.USER)
28 const authModule: IAuthModuleService = container.resolve(Modules.AUTH)
29
30 const user = await userModule.createUsers({
31 email,
32 first_name: "MFA",
33 last_name: "User",
34 })
35
36 await authModule.updateAuthIdentities({
37 id: auth_identity_id,
38 app_metadata: { user_id: user.id },
39 })
40
41 const authHeader = {
42 headers: { authorization: `Bearer ${signup.data.token}` },
43 }
44
45 const setup = await api.post(
46 "/auth/mfa/factors",
47 { provider: "totp", label: "Authenticator app" },
48 authHeader
49 )
50
51 const code = generateTotpCode({ secret: setup.data.secret })
52 await api.post(
53 `/auth/mfa/factors/${setup.data.mfa_factor.id}/verify`,
54 { code },
55 authHeader
56 )
57
58 return {
59 email,
60 password,
61 secret: setup.data.secret as string,
62 }
63 }
64
65 medusaIntegrationTestRunner({
66 medusaConfigFile: path.join(
67 __dirname,
68 "../../../__fixtures__/auth-mfa-verification"
69 ),
70 testSuite: ({ getContainer, api }) => {
71 describe("MFA with email verification", () => {
72 it("requires email verification before MFA during login", async () => {
73 const container = getContainer()
74 const email = "mfa-verify@medusa.js"
75 const password = "secret_password"
76 const { secret } = await createUserWithMfa(
77 api,
78 container,
79 email,
80 password
81 )
82
83 const login = await api.post("/auth/user/emailpass", {
84 email,
85 password,
86 })
87
88 expect(login.status).toEqual(200)
89 expect(login.data.verification_required).toEqual(true)
90 expect(login.data.token).toEqual(expect.any(String))
91 expect(login.data.mfa_required).toBeUndefined()
92 expect(login.data.mfa_challenge).toBeUndefined()
93
94 const loginToken = jwt.decode(login.data.token) as Record<
95 string,
96 unknown
97 >
98 expect(loginToken.actor_id).toEqual("")
99
100 const authModule: IAuthModuleService = container.resolve(Modules.AUTH)
101 const verification = await authModule.requestAuthVerification({
102 auth_identity_id: loginToken.auth_identity_id as string,
103 entity_id: email,
104 entity_type: "email",
105 code_provider: "token",
106 })
107
108 const confirmed = await api.post(
109 "/auth/verification/confirm",
110 { code: verification.code },
111 { headers: { authorization: `Bearer ${login.data.token}` } }
112 )
113
114 expect(confirmed.status).toEqual(200)
115 expect(confirmed.data.verified_at).toEqual(expect.any(String))
116
117 const refresh = await api.post(
118 "/auth/token/refresh",
119 {},
120 { headers: { authorization: `Bearer ${login.data.token}` } }
121 )
122
123 expect(refresh.status).toEqual(200)
124 expect(refresh.data).toEqual({
125 mfa_required: true,
126 mfa_challenge: expect.objectContaining({
127 auth_identity_id: loginToken.auth_identity_id,
128 methods: expect.arrayContaining(["totp"]),
129 }),
130 token: expect.any(String),
131 })
132
133 const totpCode = generateTotpCode({ secret })
134 const authenticated = await api.post(
135 `/auth/mfa/challenges/${refresh.data.mfa_challenge.id}/verify`,
136 { method: "totp", code: totpCode },
137 { headers: { authorization: `Bearer ${refresh.data.token}` } }
138 )
139
140 expect(authenticated.status).toEqual(200)
141
142 const decoded = jwt.decode(authenticated.data.token) as Record<
143 string,
144 unknown
145 >
146 expect(decoded).toEqual(
147 expect.objectContaining({
148 actor_id: expect.any(String),
149 actor_type: "user",
150 auth_identity_id: loginToken.auth_identity_id,
151 auth_provider: "emailpass",
152 })
153 )
154 expect(decoded.actor_id).not.toEqual("")
155 })
156 })
157 },
158 })