1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import jwt from "jsonwebtoken"
3 import {
4 adminHeaders,
5 createAdminUser,
6 } from "../../../../helpers/create-admin-user"
7 import { generateTotpCode } from "../../../../../packages/modules/auth/src/utils/totp"
8
9 jest.setTimeout(100000)
10
11 const enrollTotpFactor = async (api) => {
12 const setup = await api.post(
13 "/auth/mfa/factors",
14 { provider: "totp", label: "Authenticator app" },
15 adminHeaders
16 )
17
18 expect(setup.status).toEqual(200)
19 expect(setup.data).toEqual({
20 mfa_factor: expect.objectContaining({
21 provider: "totp",
22 status: "pending",
23 }),
24 secret: expect.any(String),
25 otpauth_url: expect.stringContaining("otpauth://totp/"),
26 })
27
28 const code = generateTotpCode({ secret: setup.data.secret })
29 const verified = await api.post(
30 `/auth/mfa/factors/${setup.data.mfa_factor.id}/verify`,
31 { code },
32 adminHeaders
33 )
34
35 expect(verified.status).toEqual(200)
36 expect(verified.data.mfa_factor.status).toEqual("enabled")
37
38 return {
39 secret: setup.data.secret as string,
40 factorId: setup.data.mfa_factor.id as string,
41 }
42 }
43
44 medusaIntegrationTestRunner({
45 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
46 beforeAll(async () => {
47 await createAdminUser(dbConnection, adminHeaders, getContainer())
48
49 await dbUtils.snapshot()
50 })
51
52 describe("MFA HTTP routes", () => {
53 it("enrolls and verifies a TOTP factor", async () => {
54 const { factorId } = await enrollTotpFactor(api)
55
56 const factors = await api.get("/auth/mfa/factors", adminHeaders)
57
58 expect(factors.status).toEqual(200)
59 expect(factors.data.mfa_factors).toEqual([
60 expect.objectContaining({
61 id: factorId,
62 provider: "totp",
63 status: "enabled",
64 }),
65 ])
66 })
67
68 it("requires an MFA challenge when logging in with an enabled factor", async () => {
69 await enrollTotpFactor(api)
70
71 const login = await api.post("/auth/user/emailpass", {
72 email: "admin@medusa.js",
73 password: "somepassword",
74 })
75
76 expect(login.status).toEqual(200)
77 expect(login.data).toEqual({
78 mfa_required: true,
79 mfa_challenge: expect.objectContaining({
80 auth_identity_id: expect.any(String),
81 methods: expect.arrayContaining(["totp"]),
82 attempts: 0,
83 }),
84 token: expect.any(String),
85 })
86
87 const decoded = jwt.decode(login.data.token) as Record<string, unknown>
88 expect(decoded.actor_id).toEqual("")
89 })
90
91 it("completes an MFA challenge and issues an authenticated token", async () => {
92 const { secret } = await enrollTotpFactor(api)
93
94 const login = await api.post("/auth/user/emailpass", {
95 email: "admin@medusa.js",
96 password: "somepassword",
97 })
98
99 const code = generateTotpCode({ secret })
100 const verified = await api.post(
101 `/auth/mfa/challenges/${login.data.mfa_challenge.id}/verify`,
102 { method: "totp", code },
103 { headers: { authorization: `Bearer ${login.data.token}` } }
104 )
105
106 expect(verified.status).toEqual(200)
107 expect(verified.data).toEqual({ token: expect.any(String) })
108
109 const decoded = jwt.decode(verified.data.token) as Record<
110 string,
111 unknown
112 >
113 expect(decoded).toEqual(
114 expect.objectContaining({
115 actor_id: expect.any(String),
116 actor_type: "user",
117 auth_identity_id: login.data.mfa_challenge.auth_identity_id,
118 auth_provider: "emailpass",
119 })
120 )
121 })
122
123 it("rejects an invalid TOTP code during challenge verification", async () => {
124 await enrollTotpFactor(api)
125
126 const login = await api.post("/auth/user/emailpass", {
127 email: "admin@medusa.js",
128 password: "somepassword",
129 })
130
131 const err = await api
132 .post(
133 `/auth/mfa/challenges/${login.data.mfa_challenge.id}/verify`,
134 { method: "totp", code: "000000" },
135 { headers: { authorization: `Bearer ${login.data.token}` } }
136 )
137 .catch((e) => e)
138
139 expect(err.response.status).toEqual(400)
140 })
141 })
142 },
143 })