|
1
|
import { |
|
2
|
AuthenticationInput, |
|
3
|
AuthenticationResponse, |
|
4
|
AuthIdentityDTO, |
|
5
|
AuthIdentityProviderService, |
|
6
|
AuthTypes, |
|
7
|
IAuthModuleService, |
|
8
|
} from "@medusajs/framework/types" |
|
9
|
import { |
|
10
|
AbstractAuthModuleProvider, |
|
11
|
MedusaError, |
|
12
|
Modules, |
|
13
|
} from "@medusajs/framework/utils" |
|
14
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
15
|
import { generateTotpCode } from "../../../src/utils/totp" |
|
16
|
|
|
17
|
jest.setTimeout(30000) |
|
18
|
|
|
19
|
const mockCache = new Map() |
|
20
|
const inMemoryCache = { |
|
21
|
get: async (key: string) => mockCache.get(key) ?? null, |
|
22
|
set: async (key: string, data: unknown) => { |
|
23
|
mockCache.set(key, data) |
|
24
|
}, |
|
25
|
invalidate: async (key: string) => { |
|
26
|
mockCache.delete(key) |
|
27
|
}, |
|
28
|
} |
|
29
|
|
|
30
|
class EmailPassFixtureProvider extends AbstractAuthModuleProvider { |
|
31
|
static identifier = "emailpass" |
|
32
|
|
|
33
|
constructor() { |
|
34
|
super({}, { provider: "emailpass", displayName: "Emailpass Fixture" }) |
|
35
|
} |
|
36
|
|
|
37
|
async authenticate( |
|
38
|
authenticationData: AuthenticationInput, |
|
39
|
service: AuthIdentityProviderService |
|
40
|
): Promise<AuthenticationResponse> { |
|
41
|
const { email, password } = authenticationData.body ?? {} |
|
42
|
let authIdentity: AuthIdentityDTO | undefined |
|
43
|
|
|
44
|
try { |
|
45
|
authIdentity = await service.retrieve({ |
|
46
|
entity_id: email, |
|
47
|
}) |
|
48
|
|
|
49
|
const providerIdentity = authIdentity.provider_identities?.find( |
|
50
|
(pi) => pi.provider === this.provider |
|
51
|
)! |
|
52
|
|
|
53
|
if (providerIdentity.provider_metadata?.password === password) { |
|
54
|
return { |
|
55
|
success: true, |
|
56
|
authIdentity, |
|
57
|
} |
|
58
|
} |
|
59
|
} catch (error) { |
|
60
|
if (error.type !== MedusaError.Types.NOT_FOUND) { |
|
61
|
return { success: false, error: error.message } |
|
62
|
} |
|
63
|
} |
|
64
|
|
|
65
|
return { |
|
66
|
success: false, |
|
67
|
error: "Invalid email or password", |
|
68
|
} |
|
69
|
} |
|
70
|
} |
|
71
|
|
|
72
|
const parseJson = (value: unknown) => |
|
73
|
typeof value === "string" ? JSON.parse(value) : value |
|
74
|
|
|
75
|
const createUnverifiedIdentity = async (service: IAuthModuleService) => { |
|
76
|
return await service.createAuthIdentities({ |
|
77
|
id: "auth-id", |
|
78
|
provider_identities: [ |
|
79
|
{ |
|
80
|
id: "provider-id", |
|
81
|
entity_id: "verify@test.com", |
|
82
|
provider: "emailpass", |
|
83
|
provider_metadata: { |
|
84
|
password: "plaintext", |
|
85
|
}, |
|
86
|
}, |
|
87
|
], |
|
88
|
}) |
|
89
|
} |
|
90
|
|
|
91
|
moduleIntegrationTestRunner<IAuthModuleService>({ |
|
92
|
moduleName: Modules.AUTH, |
|
93
|
testSuite: ({ MikroOrmWrapper, service }) => { |
|
94
|
describe("AuthModuleService - verification providers", () => { |
|
95
|
beforeEach(async () => { |
|
96
|
jest.spyOn(Date, "now").mockReturnValue(1_710_000_000_000) |
|
97
|
await createUnverifiedIdentity(service) |
|
98
|
}) |
|
99
|
|
|
100
|
afterEach(() => { |
|
101
|
jest.restoreAllMocks() |
|
102
|
}) |
|
103
|
|
|
104
|
it("generates an opaque token and stores only its hash in provider_metadata", async () => { |
|
105
|
const result = await service.requestAuthVerification({ |
|
106
|
entity_type: "email", |
|
107
|
code_provider: "token", |
|
108
|
auth_identity_id: "auth-id", |
|
109
|
entity_id: "verify@test.com", |
|
110
|
metadata: { |
|
111
|
source: "test", |
|
112
|
}, |
|
113
|
}) |
|
114
|
|
|
115
|
expect(result).toEqual( |
|
116
|
expect.objectContaining({ |
|
117
|
code: expect.any(String), |
|
118
|
expires_at: new Date(1_710_000_900_000), |
|
119
|
entity_id: "verify@test.com", |
|
120
|
auth_identity_id: "auth-id", |
|
121
|
entity_type: "email", |
|
122
|
code_provider: "token", |
|
123
|
metadata: { |
|
124
|
source: "test", |
|
125
|
}, |
|
126
|
requested_at: new Date(1_710_000_000_000), |
|
127
|
provider_metadata: { |
|
128
|
token_hash: expect.stringMatching(/^[a-f0-9]{64}$/), |
|
129
|
}, |
|
130
|
}) |
|
131
|
) |
|
132
|
expect(result.provider_metadata?.token_hash).not.toEqual(result.code) |
|
133
|
|
|
134
|
const [storedVerification] = |
|
135
|
await MikroOrmWrapper.forkManager().execute( |
|
136
|
"select * from auth_verification where auth_identity_id = ?", |
|
137
|
["auth-id"] |
|
138
|
) |
|
139
|
|
|
140
|
expect( |
|
141
|
parseJson(storedVerification.provider_metadata).token_hash |
|
142
|
).toEqual(result.provider_metadata?.token_hash) |
|
143
|
expect(storedVerification).not.toHaveProperty("token_hash") |
|
144
|
}) |
|
145
|
|
|
146
|
it("reuses the same verification record when requesting again", async () => { |
|
147
|
const first = await service.requestAuthVerification({ |
|
148
|
entity_type: "email", |
|
149
|
code_provider: "token", |
|
150
|
auth_identity_id: "auth-id", |
|
151
|
entity_id: "verify@test.com", |
|
152
|
}) |
|
153
|
const second = await service.requestAuthVerification({ |
|
154
|
entity_type: "email", |
|
155
|
code_provider: "token", |
|
156
|
auth_identity_id: "auth-id", |
|
157
|
entity_id: "verify@test.com", |
|
158
|
}) |
|
159
|
|
|
160
|
const storedVerifications = await MikroOrmWrapper.forkManager().execute( |
|
161
|
"select * from auth_verification where auth_identity_id = ?", |
|
162
|
["auth-id"] |
|
163
|
) |
|
164
|
|
|
165
|
expect(first.code).not.toEqual(second.code) |
|
166
|
expect(storedVerifications).toHaveLength(1) |
|
167
|
expect(storedVerifications[0].deleted_at).toBeNull() |
|
168
|
|
|
169
|
await expect( |
|
170
|
service.confirmAuthVerification({ code: first.code }) |
|
171
|
).rejects.toThrow("Verification code is invalid or already used") |
|
172
|
}) |
|
173
|
|
|
174
|
it("confirms a token without requiring auth identity context", async () => { |
|
175
|
const { code } = await service.requestAuthVerification({ |
|
176
|
entity_type: "email", |
|
177
|
code_provider: "token", |
|
178
|
auth_identity_id: "auth-id", |
|
179
|
entity_id: "verify@test.com", |
|
180
|
}) |
|
181
|
|
|
182
|
await expect( |
|
183
|
service.confirmAuthVerification({ code }) |
|
184
|
).resolves.toEqual( |
|
185
|
expect.objectContaining({ |
|
186
|
auth_identity_id: "auth-id", |
|
187
|
entity_id: "verify@test.com", |
|
188
|
entity_type: "email", |
|
189
|
code_provider: "token", |
|
190
|
verified_at: new Date(1_710_000_000_000), |
|
191
|
}) |
|
192
|
) |
|
193
|
}) |
|
194
|
|
|
195
|
it("rejects an unregistered verification provider", async () => { |
|
196
|
const { code } = await service.requestAuthVerification({ |
|
197
|
entity_type: "email", |
|
198
|
code_provider: "token", |
|
199
|
auth_identity_id: "auth-id", |
|
200
|
entity_id: "verify@test.com", |
|
201
|
}) |
|
202
|
|
|
203
|
await expect( |
|
204
|
service.confirmAuthVerification({ |
|
205
|
code, |
|
206
|
code_provider: "unknown", |
|
207
|
}) |
|
208
|
).rejects.toThrow( |
|
209
|
"Unable to retrieve the verification provider with id: unknown" |
|
210
|
) |
|
211
|
|
|
212
|
await expect( |
|
213
|
service.confirmAuthVerification({ |
|
214
|
code, |
|
215
|
code_provider: "token", |
|
216
|
}) |
|
217
|
).resolves.toEqual( |
|
218
|
expect.objectContaining({ |
|
219
|
auth_identity_id: "auth-id", |
|
220
|
entity_id: "verify@test.com", |
|
221
|
entity_type: "email", |
|
222
|
code_provider: "token", |
|
223
|
verified_at: new Date(1_710_000_000_000), |
|
224
|
}) |
|
225
|
) |
|
226
|
}) |
|
227
|
|
|
228
|
it("rejects expired, used, and unknown tokens", async () => { |
|
229
|
const { code: expiredCode } = await service.requestAuthVerification({ |
|
230
|
entity_type: "email", |
|
231
|
code_provider: "token", |
|
232
|
auth_identity_id: "auth-id", |
|
233
|
entity_id: "verify@test.com", |
|
234
|
}) |
|
235
|
|
|
236
|
await MikroOrmWrapper.forkManager().execute( |
|
237
|
"update auth_verification set requested_at = ? where auth_identity_id = ?", |
|
238
|
[new Date(1_709_999_000_000), "auth-id"] |
|
239
|
) |
|
240
|
|
|
241
|
await expect( |
|
242
|
service.confirmAuthVerification({ code: expiredCode }) |
|
243
|
).rejects.toThrow("Verification code has expired") |
|
244
|
|
|
245
|
const active = await service.requestAuthVerification({ |
|
246
|
entity_type: "email", |
|
247
|
code_provider: "token", |
|
248
|
auth_identity_id: "auth-id", |
|
249
|
entity_id: "verify@test.com", |
|
250
|
}) |
|
251
|
|
|
252
|
await service.confirmAuthVerification({ code: active.code }) |
|
253
|
await expect( |
|
254
|
service.confirmAuthVerification({ code: active.code }) |
|
255
|
).rejects.toThrow("Verification code is invalid or already used") |
|
256
|
await expect( |
|
257
|
service.confirmAuthVerification({ code: "missing" }) |
|
258
|
).rejects.toThrow("Verification code is invalid or already used") |
|
259
|
}) |
|
260
|
}) |
|
261
|
}, |
|
262
|
}) |
|
263
|
|
|
264
|
moduleIntegrationTestRunner<IAuthModuleService>({ |
|
265
|
moduleName: Modules.AUTH, |
|
266
|
moduleOptions: { |
|
267
|
providers: [ |
|
268
|
{ |
|
269
|
resolve: { |
|
270
|
services: [EmailPassFixtureProvider], |
|
271
|
}, |
|
272
|
id: "emailpass", |
|
273
|
}, |
|
274
|
], |
|
275
|
mfa: { |
|
276
|
encryption_key: "test-mfa-encryption-key", |
|
277
|
recovery_code_count: 2, |
|
278
|
challenge_ttl_seconds: 60, |
|
279
|
challenge_max_attempts: 2, |
|
280
|
providers: [ |
|
281
|
{ |
|
282
|
id: "totp", |
|
283
|
options: { |
|
284
|
issuer: "Medusa Test", |
|
285
|
window: 0, |
|
286
|
}, |
|
287
|
}, |
|
288
|
], |
|
289
|
}, |
|
290
|
}, |
|
291
|
moduleDependencies: [Modules.CACHE], |
|
292
|
injectedDependencies: { |
|
293
|
[Modules.CACHE]: inMemoryCache, |
|
294
|
}, |
|
295
|
testSuite: ({ service }) => { |
|
296
|
describe("AuthModuleService - verification authentication flow", () => { |
|
297
|
beforeEach(async () => { |
|
298
|
jest.spyOn(Date, "now").mockReturnValue(1_710_000_000_000) |
|
299
|
await createUnverifiedIdentity(service) |
|
300
|
}) |
|
301
|
|
|
302
|
afterEach(() => { |
|
303
|
mockCache.clear() |
|
304
|
jest.restoreAllMocks() |
|
305
|
}) |
|
306
|
|
|
307
|
it("continues to MFA once the entity is verified", async () => { |
|
308
|
const setup = await service.startAuthMfa({ |
|
309
|
auth_identity_id: "auth-id", |
|
310
|
provider: "totp", |
|
311
|
}) |
|
312
|
const code = generateTotpCode({ |
|
313
|
secret: setup.secret, |
|
314
|
timestamp: Date.now(), |
|
315
|
}) |
|
316
|
const verification = await service.requestAuthVerification({ |
|
317
|
entity_type: "email", |
|
318
|
code_provider: "token", |
|
319
|
auth_identity_id: "auth-id", |
|
320
|
entity_id: "verify@test.com", |
|
321
|
}) |
|
322
|
|
|
323
|
await service.verifyAuthMfa({ |
|
324
|
id: setup.mfa.id, |
|
325
|
code, |
|
326
|
}) |
|
327
|
await service.confirmAuthVerification({ |
|
328
|
code: verification.code, |
|
329
|
}) |
|
330
|
|
|
331
|
const result = await service.authenticate("emailpass", { |
|
332
|
body: { |
|
333
|
email: "verify@test.com", |
|
334
|
password: "plaintext", |
|
335
|
}, |
|
336
|
}) |
|
337
|
|
|
338
|
expect(result).toEqual( |
|
339
|
expect.objectContaining({ |
|
340
|
success: true, |
|
341
|
mfaChallenge: expect.objectContaining({ |
|
342
|
auth_identity_id: "auth-id", |
|
343
|
auth_provider: "emailpass", |
|
344
|
}), |
|
345
|
authIdentity: expect.objectContaining({ |
|
346
|
id: "auth-id", |
|
347
|
provider_identities: [ |
|
348
|
expect.objectContaining({ entity_id: "verify@test.com" }), |
|
349
|
], |
|
350
|
}), |
|
351
|
}) |
|
352
|
) |
|
353
|
expect(result.verification).toBeUndefined() |
|
354
|
}) |
|
355
|
}) |
|
356
|
}, |
|
357
|
}) |