|
1
|
import { generateResetPasswordTokenWorkflow } from "@medusajs/core-flows" |
|
2
|
import { IAuthModuleService } from "@medusajs/framework/types" |
|
3
|
import { AuthWorkflowEvents, Modules } from "@medusajs/framework/utils" |
|
4
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
5
|
import jwt from "jsonwebtoken" |
|
6
|
import { |
|
7
|
adminHeaders, |
|
8
|
createAdminUser, |
|
9
|
} from "../../../../helpers/create-admin-user" |
|
10
|
|
|
11
|
jest.setTimeout(100000) |
|
12
|
|
|
13
|
medusaIntegrationTestRunner({ |
|
14
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
15
|
let container |
|
16
|
beforeAll(async () => { |
|
17
|
container = getContainer() |
|
18
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
19
|
|
|
20
|
await dbUtils.snapshot() |
|
21
|
}) |
|
22
|
|
|
23
|
describe("Full authentication lifecycle", () => { |
|
24
|
it("Invite + registration + authentication flow", async () => { |
|
25
|
// Create invite |
|
26
|
const { token: inviteToken } = ( |
|
27
|
await api.post( |
|
28
|
"/admin/invites", |
|
29
|
{ email: "newadmin@medusa.js" }, |
|
30
|
adminHeaders |
|
31
|
) |
|
32
|
).data.invite |
|
33
|
|
|
34
|
// Register identity |
|
35
|
const signup = await api.post("/auth/user/emailpass/register", { |
|
36
|
email: "newadmin@medusa.js", |
|
37
|
password: "secret_password", |
|
38
|
}) |
|
39
|
|
|
40
|
expect(signup.status).toEqual(200) |
|
41
|
expect(signup.data).toEqual({ token: expect.any(String) }) |
|
42
|
|
|
43
|
// Accept invite |
|
44
|
const response = await api.post( |
|
45
|
`/admin/invites/accept?token=${inviteToken}`, |
|
46
|
{ |
|
47
|
email: "newadmin@medusa.js", |
|
48
|
first_name: "John", |
|
49
|
last_name: "Doe", |
|
50
|
}, |
|
51
|
{ |
|
52
|
headers: { |
|
53
|
authorization: `Bearer ${signup.data.token}`, |
|
54
|
}, |
|
55
|
} |
|
56
|
) |
|
57
|
|
|
58
|
expect(response.status).toEqual(200) |
|
59
|
expect(response.data).toEqual({ |
|
60
|
user: expect.objectContaining({ |
|
61
|
email: "newadmin@medusa.js", |
|
62
|
first_name: "John", |
|
63
|
last_name: "Doe", |
|
64
|
}), |
|
65
|
}) |
|
66
|
|
|
67
|
// Sign in |
|
68
|
const login = await api.post("/auth/user/emailpass", { |
|
69
|
email: "newadmin@medusa.js", |
|
70
|
password: "secret_password", |
|
71
|
}) |
|
72
|
expect(login.status).toEqual(200) |
|
73
|
expect(login.data).toEqual({ token: expect.any(String) }) |
|
74
|
|
|
75
|
// Convert token to session |
|
76
|
const createSession = await api.post( |
|
77
|
"/auth/session", |
|
78
|
{}, |
|
79
|
{ headers: { authorization: `Bearer ${login.data.token}` } } |
|
80
|
) |
|
81
|
expect(createSession.status).toEqual(200) |
|
82
|
|
|
83
|
// Extract cookie |
|
84
|
const [cookie] = createSession.headers["set-cookie"][0].split(";") |
|
85
|
expect(cookie).toEqual(expect.stringContaining("connect.sid")) |
|
86
|
|
|
87
|
const cookieHeader = { |
|
88
|
headers: { Cookie: cookie }, |
|
89
|
} |
|
90
|
|
|
91
|
// Perform cookie authenticated request |
|
92
|
const authedRequest = await api.get( |
|
93
|
"/admin/products?limit=1", |
|
94
|
cookieHeader |
|
95
|
) |
|
96
|
expect(authedRequest.status).toEqual(200) |
|
97
|
|
|
98
|
// Sign out |
|
99
|
const signOutRequest = await api.delete("/auth/session", cookieHeader) |
|
100
|
expect(signOutRequest.status).toEqual(200) |
|
101
|
|
|
102
|
// Regression: DELETE /auth/session must clear the session cookie on |
|
103
|
// the client. See https://github.com/medusajs/medusa/issues/15508 |
|
104
|
const signOutSetCookie = signOutRequest.headers["set-cookie"] |
|
105
|
expect(signOutSetCookie).toBeDefined() |
|
106
|
expect(signOutSetCookie[0]).toMatch(/^connect\.sid=;/) |
|
107
|
expect(signOutSetCookie[0]).toContain("Expires=") |
|
108
|
|
|
109
|
// Attempt to perform authenticated request |
|
110
|
const unAuthedRequest = await api |
|
111
|
.get("/admin/products?limit=1", cookieHeader) |
|
112
|
.catch((e) => e) |
|
113
|
|
|
114
|
expect(unAuthedRequest.response.status).toEqual(401) |
|
115
|
}) |
|
116
|
|
|
117
|
it("should respond with 401 on register, if email already exists", async () => { |
|
118
|
const signup = await api |
|
119
|
.post("/auth/user/emailpass/register", { |
|
120
|
email: "admin@medusa.js", |
|
121
|
password: "secret_password", |
|
122
|
}) |
|
123
|
.catch((e) => e) |
|
124
|
|
|
125
|
expect(signup.response.status).toEqual(401) |
|
126
|
expect(signup.response.data.message).toEqual( |
|
127
|
"Identity with email already exists" |
|
128
|
) |
|
129
|
}) |
|
130
|
|
|
131
|
it("should reject re-registering an incomplete signup with a different password", async () => { |
|
132
|
const firstSignup = await api.post("/auth/user/emailpass/register", { |
|
133
|
email: "incomplete@medusa.js", |
|
134
|
password: "first_password", |
|
135
|
}) |
|
136
|
|
|
137
|
expect(firstSignup.status).toEqual(200) |
|
138
|
expect(firstSignup.data).toEqual({ token: expect.any(String) }) |
|
139
|
|
|
140
|
const reclaim = await api |
|
141
|
.post("/auth/user/emailpass/register", { |
|
142
|
email: "incomplete@medusa.js", |
|
143
|
password: "attacker_password", |
|
144
|
}) |
|
145
|
.catch((e) => e.response) |
|
146
|
|
|
147
|
expect(reclaim.status).toEqual(401) |
|
148
|
expect(reclaim.data.message).toEqual( |
|
149
|
"Identity with email already exists" |
|
150
|
) |
|
151
|
|
|
152
|
const attackerSignin = await api |
|
153
|
.post("/auth/user/emailpass", { |
|
154
|
email: "incomplete@medusa.js", |
|
155
|
password: "attacker_password", |
|
156
|
}) |
|
157
|
.catch((e) => e.response) |
|
158
|
|
|
159
|
expect(attackerSignin.status).toEqual(401) |
|
160
|
expect(attackerSignin.data.message).toEqual("Invalid email or password") |
|
161
|
|
|
162
|
const signin = await api.post("/auth/user/emailpass", { |
|
163
|
email: "incomplete@medusa.js", |
|
164
|
password: "first_password", |
|
165
|
}) |
|
166
|
|
|
167
|
expect(signin.status).toEqual(200) |
|
168
|
expect(signin.data).toEqual({ token: expect.any(String) }) |
|
169
|
}) |
|
170
|
|
|
171
|
it("should allow resuming an incomplete signup with the original password", async () => { |
|
172
|
const { token: inviteToken } = ( |
|
173
|
await api.post( |
|
174
|
"/admin/invites", |
|
175
|
{ email: "resumed@medusa.js" }, |
|
176
|
adminHeaders |
|
177
|
) |
|
178
|
).data.invite |
|
179
|
|
|
180
|
const firstSignup = await api.post("/auth/user/emailpass/register", { |
|
181
|
email: "resumed@medusa.js", |
|
182
|
password: "secret_password", |
|
183
|
}) |
|
184
|
|
|
185
|
expect(firstSignup.status).toEqual(200) |
|
186
|
|
|
187
|
// The invite acceptance failed, so the identity is still actorless and |
|
188
|
// the user retries the whole flow from the registration form. |
|
189
|
const retriedSignup = await api.post("/auth/user/emailpass/register", { |
|
190
|
email: "resumed@medusa.js", |
|
191
|
password: "secret_password", |
|
192
|
}) |
|
193
|
|
|
194
|
expect(retriedSignup.status).toEqual(200) |
|
195
|
expect(retriedSignup.data).toEqual({ token: expect.any(String) }) |
|
196
|
|
|
197
|
const accepted = await api.post( |
|
198
|
`/admin/invites/accept?token=${inviteToken}`, |
|
199
|
{ first_name: "John", last_name: "Doe" }, |
|
200
|
{ |
|
201
|
headers: { authorization: `Bearer ${retriedSignup.data.token}` }, |
|
202
|
} |
|
203
|
) |
|
204
|
|
|
205
|
expect(accepted.status).toEqual(200) |
|
206
|
expect(accepted.data.user).toEqual( |
|
207
|
expect.objectContaining({ email: "resumed@medusa.js" }) |
|
208
|
) |
|
209
|
}) |
|
210
|
|
|
211
|
it("should respond with 401 on sign in, if email does not exist", async () => { |
|
212
|
const signup = await api |
|
213
|
.post("/auth/user/emailpass", { |
|
214
|
email: "john@doe.com", |
|
215
|
password: "secret_password", |
|
216
|
}) |
|
217
|
.catch((e) => e) |
|
218
|
|
|
219
|
expect(signup.response.status).toEqual(401) |
|
220
|
expect(signup.response.data.message).toEqual( |
|
221
|
"Invalid email or password" |
|
222
|
) |
|
223
|
}) |
|
224
|
}) |
|
225
|
|
|
226
|
describe("Reset password flows", () => { |
|
227
|
it("should generate a reset password token", async () => { |
|
228
|
const response = await api.post("/auth/user/emailpass/reset-password", { |
|
229
|
identifier: "admin@medusa.js", |
|
230
|
}) |
|
231
|
|
|
232
|
expect(response.status).toEqual(201) |
|
233
|
}) |
|
234
|
|
|
235
|
it("should fail if identifier is not provided", async () => { |
|
236
|
const errResponse = await api |
|
237
|
.post("/auth/user/emailpass/reset-password", {}) |
|
238
|
.catch((e) => e) |
|
239
|
|
|
240
|
expect(errResponse.response.data.message).toEqual( |
|
241
|
"Invalid request: Field 'identifier' is required" |
|
242
|
) |
|
243
|
expect(errResponse.response.status).toEqual(400) |
|
244
|
}) |
|
245
|
|
|
246
|
it("should fail to generate token for non-existing user, but still respond with 201", async () => { |
|
247
|
const response = await api.post("/auth/user/emailpass/reset-password", { |
|
248
|
identifier: "non-existing-user@medusa.js", |
|
249
|
}) |
|
250
|
|
|
251
|
expect(response.status).toEqual(201) |
|
252
|
}) |
|
253
|
|
|
254
|
it("should reject a provider that is not allowed for the user actor type", async () => { |
|
255
|
const response = await api |
|
256
|
.post("/auth/user/non-existing-provider/reset-password", { |
|
257
|
identifier: "admin@medusa.js", |
|
258
|
}) |
|
259
|
.catch((e) => e.response) |
|
260
|
|
|
261
|
expect(response.status).toEqual(400) |
|
262
|
expect(response.data.message).toEqual( |
|
263
|
"The actor type user is not allowed to use the auth provider non-existing-provider" |
|
264
|
) |
|
265
|
}) |
|
266
|
|
|
267
|
it("should successfully reset password", async () => { |
|
268
|
// Register user |
|
269
|
await api.post("/auth/user/emailpass/register", { |
|
270
|
email: "test@medusa-commerce.com", |
|
271
|
password: "secret_password", |
|
272
|
}) |
|
273
|
|
|
274
|
// The token won't be part of the Rest API response, so we need to generate it manually |
|
275
|
const { result } = await generateResetPasswordTokenWorkflow( |
|
276
|
container |
|
277
|
).run({ |
|
278
|
input: { |
|
279
|
entityId: "test@medusa-commerce.com", |
|
280
|
actorType: "user", |
|
281
|
provider: "emailpass", |
|
282
|
secret: "test", |
|
283
|
}, |
|
284
|
}) |
|
285
|
|
|
286
|
const response = await api.post( |
|
287
|
`/auth/user/emailpass/update`, |
|
288
|
{ |
|
289
|
password: "new_password", |
|
290
|
}, |
|
291
|
{ |
|
292
|
headers: { |
|
293
|
authorization: `Bearer ${result}`, |
|
294
|
}, |
|
295
|
} |
|
296
|
) |
|
297
|
|
|
298
|
expect(response.status).toEqual(200) |
|
299
|
expect(response.data).toEqual({ success: true }) |
|
300
|
|
|
301
|
const failedLogin = await api |
|
302
|
.post("/auth/user/emailpass", { |
|
303
|
email: "test@medusa-commerce.com", |
|
304
|
password: "secret_password", |
|
305
|
}) |
|
306
|
.catch((e) => e) |
|
307
|
|
|
308
|
expect(failedLogin.response.status).toEqual(401) |
|
309
|
expect(failedLogin.response.data.message).toEqual( |
|
310
|
"Invalid email or password" |
|
311
|
) |
|
312
|
|
|
313
|
const login = await api.post("/auth/user/emailpass", { |
|
314
|
email: "test@medusa-commerce.com", |
|
315
|
password: "new_password", |
|
316
|
}) |
|
317
|
|
|
318
|
expect(login.status).toEqual(200) |
|
319
|
expect(login.data).toEqual({ token: expect.any(String) }) |
|
320
|
}) |
|
321
|
|
|
322
|
it("should reject replaying a reset token after a successful password update", async () => { |
|
323
|
await api.post("/auth/user/emailpass/register", { |
|
324
|
email: "replay@medusa-commerce.com", |
|
325
|
password: "secret_password", |
|
326
|
}) |
|
327
|
|
|
328
|
const { result: resetToken } = await generateResetPasswordTokenWorkflow( |
|
329
|
container |
|
330
|
).run({ |
|
331
|
input: { |
|
332
|
entityId: "replay@medusa-commerce.com", |
|
333
|
actorType: "user", |
|
334
|
provider: "emailpass", |
|
335
|
secret: "test", |
|
336
|
}, |
|
337
|
}) |
|
338
|
|
|
339
|
const firstUse = await api.post( |
|
340
|
`/auth/user/emailpass/update`, |
|
341
|
{ password: "first_new_password" }, |
|
342
|
{ headers: { authorization: `Bearer ${resetToken}` } } |
|
343
|
) |
|
344
|
expect(firstUse.status).toEqual(200) |
|
345
|
|
|
346
|
const replay = await api |
|
347
|
.post( |
|
348
|
`/auth/user/emailpass/update`, |
|
349
|
{ password: "attacker_password" }, |
|
350
|
{ headers: { authorization: `Bearer ${resetToken}` } } |
|
351
|
) |
|
352
|
.catch((e) => e) |
|
353
|
|
|
354
|
expect(replay.response.status).toEqual(401) |
|
355
|
expect(replay.response.data.message).toEqual("Invalid token") |
|
356
|
|
|
357
|
const attackerLogin = await api |
|
358
|
.post("/auth/user/emailpass", { |
|
359
|
email: "replay@medusa-commerce.com", |
|
360
|
password: "attacker_password", |
|
361
|
}) |
|
362
|
.catch((e) => e) |
|
363
|
expect(attackerLogin.response.status).toEqual(401) |
|
364
|
|
|
365
|
const legitLogin = await api.post("/auth/user/emailpass", { |
|
366
|
email: "replay@medusa-commerce.com", |
|
367
|
password: "first_new_password", |
|
368
|
}) |
|
369
|
expect(legitLogin.status).toEqual(200) |
|
370
|
}) |
|
371
|
|
|
372
|
it("should invalidate previously issued reset tokens when a new one is generated", async () => { |
|
373
|
await api.post("/auth/user/emailpass/register", { |
|
374
|
email: "reissue@medusa-commerce.com", |
|
375
|
password: "secret_password", |
|
376
|
}) |
|
377
|
|
|
378
|
const { result: firstToken } = await generateResetPasswordTokenWorkflow( |
|
379
|
container |
|
380
|
).run({ |
|
381
|
input: { |
|
382
|
entityId: "reissue@medusa-commerce.com", |
|
383
|
actorType: "user", |
|
384
|
provider: "emailpass", |
|
385
|
secret: "test", |
|
386
|
}, |
|
387
|
}) |
|
388
|
|
|
389
|
const { result: secondToken } = |
|
390
|
await generateResetPasswordTokenWorkflow(container).run({ |
|
391
|
input: { |
|
392
|
entityId: "reissue@medusa-commerce.com", |
|
393
|
actorType: "user", |
|
394
|
provider: "emailpass", |
|
395
|
secret: "test", |
|
396
|
}, |
|
397
|
}) |
|
398
|
|
|
399
|
const oldTokenAttempt = await api |
|
400
|
.post( |
|
401
|
`/auth/user/emailpass/update`, |
|
402
|
{ password: "attacker_password" }, |
|
403
|
{ headers: { authorization: `Bearer ${firstToken}` } } |
|
404
|
) |
|
405
|
.catch((e) => e) |
|
406
|
expect(oldTokenAttempt.response.status).toEqual(401) |
|
407
|
expect(oldTokenAttempt.response.data.message).toEqual("Invalid token") |
|
408
|
|
|
409
|
const newTokenUse = await api.post( |
|
410
|
`/auth/user/emailpass/update`, |
|
411
|
{ password: "new_password" }, |
|
412
|
{ headers: { authorization: `Bearer ${secondToken}` } } |
|
413
|
) |
|
414
|
expect(newTokenUse.status).toEqual(200) |
|
415
|
}) |
|
416
|
|
|
417
|
it("should reject session bearer tokens on the password update endpoint", async () => { |
|
418
|
await api.post("/auth/user/emailpass/register", { |
|
419
|
email: "session@medusa-commerce.com", |
|
420
|
password: "secret_password", |
|
421
|
}) |
|
422
|
|
|
423
|
const login = await api.post("/auth/user/emailpass", { |
|
424
|
email: "session@medusa-commerce.com", |
|
425
|
password: "secret_password", |
|
426
|
}) |
|
427
|
|
|
428
|
const attempt = await api |
|
429
|
.post( |
|
430
|
`/auth/user/emailpass/update`, |
|
431
|
{ password: "new_password" }, |
|
432
|
{ headers: { authorization: `Bearer ${login.data.token}` } } |
|
433
|
) |
|
434
|
.catch((e) => e) |
|
435
|
|
|
436
|
expect(attempt.response.status).toEqual(401) |
|
437
|
expect(attempt.response.data.message).toEqual("Invalid token") |
|
438
|
}) |
|
439
|
|
|
440
|
it("should ensure you can only update password", async () => { |
|
441
|
// Register user |
|
442
|
await api.post("/auth/user/emailpass/register", { |
|
443
|
email: "test@medusa-commerce.com", |
|
444
|
password: "secret_password", |
|
445
|
}) |
|
446
|
|
|
447
|
// The token won't be part of the Rest API response, so we need to generate it manually |
|
448
|
const { result } = await generateResetPasswordTokenWorkflow( |
|
449
|
container |
|
450
|
).run({ |
|
451
|
input: { |
|
452
|
entityId: "test@medusa-commerce.com", |
|
453
|
actorType: "user", |
|
454
|
provider: "emailpass", |
|
455
|
secret: "test", |
|
456
|
}, |
|
457
|
}) |
|
458
|
|
|
459
|
const response = await api.post( |
|
460
|
`/auth/user/emailpass/update`, |
|
461
|
{ |
|
462
|
email: "test+new@medusa-commerce.com", |
|
463
|
password: "new_password", |
|
464
|
}, |
|
465
|
{ |
|
466
|
headers: { |
|
467
|
authorization: `Bearer ${result}`, |
|
468
|
}, |
|
469
|
} |
|
470
|
) |
|
471
|
|
|
472
|
expect(response.status).toEqual(200) |
|
473
|
expect(response.data).toEqual({ success: true }) |
|
474
|
|
|
475
|
const failedLogin = await api |
|
476
|
.post("/auth/user/emailpass", { |
|
477
|
email: "test+new@medusa-commerce.com", |
|
478
|
password: "new_password", |
|
479
|
}) |
|
480
|
.catch((e) => e) |
|
481
|
|
|
482
|
expect(failedLogin.response.status).toEqual(401) |
|
483
|
expect(failedLogin.response.data.message).toEqual( |
|
484
|
"Invalid email or password" |
|
485
|
) |
|
486
|
|
|
487
|
const login = await api.post("/auth/user/emailpass", { |
|
488
|
email: "test@medusa-commerce.com", |
|
489
|
password: "new_password", |
|
490
|
}) |
|
491
|
|
|
492
|
expect(login.status).toEqual(200) |
|
493
|
expect(login.data).toEqual({ token: expect.any(String) }) |
|
494
|
}) |
|
495
|
|
|
496
|
it("should fail if token has expired", async () => { |
|
497
|
// Register user |
|
498
|
await api.post("/auth/user/emailpass/register", { |
|
499
|
email: "test@medusa-commerce.com", |
|
500
|
password: "secret_password", |
|
501
|
}) |
|
502
|
|
|
503
|
// The token won't be part of the Rest API response, so we need to generate it manually |
|
504
|
const { result } = await generateResetPasswordTokenWorkflow( |
|
505
|
container |
|
506
|
).run({ |
|
507
|
input: { |
|
508
|
entityId: "test@medusa-commerce.com", |
|
509
|
actorType: "user", |
|
510
|
provider: "emailpass", |
|
511
|
secret: "test", |
|
512
|
}, |
|
513
|
}) |
|
514
|
|
|
515
|
await dbConnection.raw( |
|
516
|
`UPDATE "auth_password_reset_token" SET expires_at = NOW() - INTERVAL '1 day' WHERE entity_id = ? AND deleted_at IS NULL`, |
|
517
|
["test@medusa-commerce.com"] |
|
518
|
) |
|
519
|
|
|
520
|
const response = await api |
|
521
|
.post( |
|
522
|
`/auth/user/emailpass/update`, |
|
523
|
{ |
|
524
|
password: "new_password", |
|
525
|
}, |
|
526
|
{ |
|
527
|
headers: { |
|
528
|
authorization: `Bearer ${result}`, |
|
529
|
}, |
|
530
|
} |
|
531
|
) |
|
532
|
.catch((e) => e) |
|
533
|
|
|
534
|
expect(response.response.status).toEqual(401) |
|
535
|
expect(response.response.data.message).toEqual("Invalid token") |
|
536
|
}) |
|
537
|
|
|
538
|
it("should fail if no token is passed", async () => { |
|
539
|
// Register user |
|
540
|
await api.post("/auth/user/emailpass/register", { |
|
541
|
email: "test@medusa-commerce.com", |
|
542
|
password: "secret_password", |
|
543
|
}) |
|
544
|
|
|
545
|
const response = await api |
|
546
|
.post(`/auth/user/emailpass/update`, { |
|
547
|
email: "test@medusa-commerce.com", |
|
548
|
}) |
|
549
|
.catch((e) => e) |
|
550
|
|
|
551
|
expect(response.response.status).toEqual(401) |
|
552
|
expect(response.response.data.message).toEqual("Invalid token") |
|
553
|
}) |
|
554
|
|
|
555
|
it("should fail if update is attempted on different actor type", async () => { |
|
556
|
// Register user |
|
557
|
await api.post("/auth/user/emailpass/register", { |
|
558
|
email: "test@medusa-commerce.com", |
|
559
|
password: "secret_password", |
|
560
|
}) |
|
561
|
|
|
562
|
// The token won't be part of the Rest API response, so we need to generate it manually |
|
563
|
const { result } = await generateResetPasswordTokenWorkflow( |
|
564
|
container |
|
565
|
).run({ |
|
566
|
input: { |
|
567
|
entityId: "test@medusa-commerce.com", |
|
568
|
actorType: "user", |
|
569
|
provider: "emailpass", |
|
570
|
secret: "test", |
|
571
|
}, |
|
572
|
}) |
|
573
|
|
|
574
|
const response = await api |
|
575
|
.post( |
|
576
|
`/auth/customer/emailpass/update`, |
|
577
|
{ |
|
578
|
password: "new_password", |
|
579
|
}, |
|
580
|
{ |
|
581
|
headers: { |
|
582
|
authorization: `Bearer ${result}`, |
|
583
|
}, |
|
584
|
} |
|
585
|
) |
|
586
|
.catch((e) => e) |
|
587
|
|
|
588
|
expect(response.response.status).toEqual(401) |
|
589
|
expect(response.response.data.message).toEqual("Invalid token") |
|
590
|
}) |
|
591
|
|
|
592
|
it("should fail if token secret is incorrect", async () => { |
|
593
|
// Register user |
|
594
|
await api.post("/auth/user/emailpass/register", { |
|
595
|
email: "test@medusa-commerce.com", |
|
596
|
password: "secret_password", |
|
597
|
}) |
|
598
|
|
|
599
|
// The token won't be part of the Rest API response, so we need to generate it manually |
|
600
|
const { result } = await generateResetPasswordTokenWorkflow( |
|
601
|
container |
|
602
|
).run({ |
|
603
|
input: { |
|
604
|
entityId: "test@medusa-commerce.com", |
|
605
|
actorType: "user", |
|
606
|
provider: "emailpass", |
|
607
|
secret: "incorrect_secret", |
|
608
|
}, |
|
609
|
}) |
|
610
|
|
|
611
|
const response = await api |
|
612
|
.post( |
|
613
|
`/auth/user/emailpass/update`, |
|
614
|
{ |
|
615
|
password: "new_password", |
|
616
|
}, |
|
617
|
{ |
|
618
|
headers: { |
|
619
|
authorization: `Bearer ${result}`, |
|
620
|
}, |
|
621
|
} |
|
622
|
) |
|
623
|
.catch((e) => e) |
|
624
|
|
|
625
|
expect(response.response.status).toEqual(401) |
|
626
|
expect(response.response.data.message).toEqual("Invalid token") |
|
627
|
}) |
|
628
|
|
|
629
|
it("should emit metadata in password reset event", async () => { |
|
630
|
await api.post("/auth/user/emailpass/register", { |
|
631
|
email: "test-metadata@medusa-commerce.com", |
|
632
|
password: "secret_password", |
|
633
|
}) |
|
634
|
|
|
635
|
const eventBus = container.resolve(Modules.EVENT_BUS) |
|
636
|
const subscriber = jest.fn() |
|
637
|
|
|
638
|
eventBus.subscribe(AuthWorkflowEvents.PASSWORD_RESET, subscriber) |
|
639
|
|
|
640
|
const metadata = { |
|
641
|
source: "test", |
|
642
|
userId: "123", |
|
643
|
customField: "customValue", |
|
644
|
} |
|
645
|
|
|
646
|
const response = await api.post("/auth/user/emailpass/reset-password", { |
|
647
|
identifier: "test-metadata@medusa-commerce.com", |
|
648
|
metadata: metadata, |
|
649
|
}) |
|
650
|
|
|
651
|
expect(response.status).toEqual(201) |
|
652
|
|
|
653
|
await new Promise((resolve) => setTimeout(resolve, 100)) |
|
654
|
|
|
655
|
expect(subscriber).toHaveBeenCalledTimes(1) |
|
656
|
|
|
657
|
const eventData = subscriber.mock.calls[0][0] |
|
658
|
|
|
659
|
expect(eventData.data).toMatchObject({ |
|
660
|
entity_id: "test-metadata@medusa-commerce.com", |
|
661
|
actor_type: "user", |
|
662
|
token: expect.any(String), |
|
663
|
metadata, |
|
664
|
}) |
|
665
|
|
|
666
|
eventBus.unsubscribe(AuthWorkflowEvents.PASSWORD_RESET, subscriber) |
|
667
|
}) |
|
668
|
}) |
|
669
|
|
|
670
|
it("should refresh the token successfully", async () => { |
|
671
|
// Make sure issue date is later than the admin one |
|
672
|
await new Promise((resolve) => setTimeout(resolve, 100)) |
|
673
|
|
|
674
|
const resp = await api.post("/auth/token/refresh", {}, adminHeaders) |
|
675
|
const decodedOriginalToken = jwt.decode( |
|
676
|
adminHeaders.headers["authorization"].split(" ")[1] |
|
677
|
) as any |
|
678
|
const decodedRefreshedToken = jwt.decode(resp.data.token) as any |
|
679
|
|
|
680
|
expect(decodedOriginalToken).toEqual( |
|
681
|
expect.objectContaining({ |
|
682
|
actor_id: decodedRefreshedToken.actor_id, |
|
683
|
actor_type: decodedRefreshedToken.actor_type, |
|
684
|
auth_identity_id: decodedRefreshedToken.auth_identity_id, |
|
685
|
}) |
|
686
|
) |
|
687
|
|
|
688
|
expect(decodedOriginalToken.iat).toBeLessThan(decodedRefreshedToken.iat) |
|
689
|
expect(decodedOriginalToken.exp).toBeLessThan(decodedRefreshedToken.exp) |
|
690
|
}) |
|
691
|
|
|
692
|
it("should preserve custom app_metadata fields on token refresh", async () => { |
|
693
|
// Register a new identity via emailpass |
|
694
|
const signup = await api.post("/auth/user/emailpass/register", { |
|
695
|
email: "custom-meta@medusa.js", |
|
696
|
password: "secret_password", |
|
697
|
}) |
|
698
|
expect(signup.status).toEqual(200) |
|
699
|
|
|
700
|
// Add custom fields to the auth identity's app_metadata |
|
701
|
const authModule: IAuthModuleService = container.resolve(Modules.AUTH) |
|
702
|
const { auth_identity_id } = jwt.decode(signup.data.token) as any |
|
703
|
await authModule.updateAuthIdentities({ |
|
704
|
id: auth_identity_id, |
|
705
|
app_metadata: { is_verified: true, custom_role: "manager" }, |
|
706
|
}) |
|
707
|
|
|
708
|
// Login to get a fresh token that reflects the updated app_metadata |
|
709
|
const login = await api.post("/auth/user/emailpass", { |
|
710
|
email: "custom-meta@medusa.js", |
|
711
|
password: "secret_password", |
|
712
|
}) |
|
713
|
expect(login.status).toEqual(200) |
|
714
|
|
|
715
|
const loginDecoded = jwt.decode(login.data.token) as any |
|
716
|
expect(loginDecoded.app_metadata).toMatchObject({ |
|
717
|
is_verified: true, |
|
718
|
custom_role: "manager", |
|
719
|
}) |
|
720
|
|
|
721
|
// Refresh the token and verify custom fields are not stripped |
|
722
|
const refresh = await api.post( |
|
723
|
"/auth/token/refresh", |
|
724
|
{}, |
|
725
|
{ headers: { authorization: `Bearer ${login.data.token}` } } |
|
726
|
) |
|
727
|
expect(refresh.status).toEqual(200) |
|
728
|
|
|
729
|
const refreshDecoded = jwt.decode(refresh.data.token) as any |
|
730
|
expect(refreshDecoded.app_metadata).toMatchObject({ |
|
731
|
is_verified: true, |
|
732
|
custom_role: "manager", |
|
733
|
}) |
|
734
|
}) |
|
735
|
|
|
736
|
it("should include auth_provider in the JWT and preserve it on token refresh", async () => { |
|
737
|
// Register and log in via emailpass |
|
738
|
await api.post("/auth/user/emailpass/register", { |
|
739
|
email: "provider-test@medusa.js", |
|
740
|
password: "secret_password", |
|
741
|
}) |
|
742
|
|
|
743
|
const login = await api.post("/auth/user/emailpass", { |
|
744
|
email: "provider-test@medusa.js", |
|
745
|
password: "secret_password", |
|
746
|
}) |
|
747
|
expect(login.status).toEqual(200) |
|
748
|
|
|
749
|
// Initial token should carry auth_provider so refresh can resolve user_metadata |
|
750
|
const loginDecoded = jwt.decode(login.data.token) as any |
|
751
|
expect(loginDecoded.auth_provider).toEqual("emailpass") |
|
752
|
|
|
753
|
// Refresh — auth_provider must be forwarded so provider identity is resolved |
|
754
|
const refresh = await api.post( |
|
755
|
"/auth/token/refresh", |
|
756
|
{}, |
|
757
|
{ headers: { authorization: `Bearer ${login.data.token}` } } |
|
758
|
) |
|
759
|
expect(refresh.status).toEqual(200) |
|
760
|
|
|
761
|
const refreshDecoded = jwt.decode(refresh.data.token) as any |
|
762
|
expect(refreshDecoded.auth_provider).toEqual("emailpass") |
|
763
|
}) |
|
764
|
|
|
765
|
it("should preserve user_metadata from the provider identity on token refresh", async () => { |
|
766
|
// Register via emailpass to get an auth identity with a provider identity |
|
767
|
const signup = await api.post("/auth/user/emailpass/register", { |
|
768
|
email: "user-meta@medusa.js", |
|
769
|
password: "secret_password", |
|
770
|
}) |
|
771
|
expect(signup.status).toEqual(200) |
|
772
|
|
|
773
|
// Seed user_metadata directly on the provider identity (simulates what an |
|
774
|
// OIDC/Auth0 provider would set in validateCallback) |
|
775
|
const authModule: IAuthModuleService = container.resolve(Modules.AUTH) |
|
776
|
const { auth_identity_id } = jwt.decode(signup.data.token) as any |
|
777
|
const authIdentity = await authModule.retrieveAuthIdentity( |
|
778
|
auth_identity_id, |
|
779
|
{ relations: ["provider_identities"] } |
|
780
|
) |
|
781
|
const providerIdentityId = authIdentity.provider_identities![0].id |
|
782
|
await authModule.updateProviderIdentities({ |
|
783
|
id: providerIdentityId, |
|
784
|
user_metadata: { email: "user-meta@medusa.js", roles: ["editor"] }, |
|
785
|
}) |
|
786
|
|
|
787
|
// Login to get a fresh token that includes the user_metadata |
|
788
|
const login = await api.post("/auth/user/emailpass", { |
|
789
|
email: "user-meta@medusa.js", |
|
790
|
password: "secret_password", |
|
791
|
}) |
|
792
|
expect(login.status).toEqual(200) |
|
793
|
|
|
794
|
const loginDecoded = jwt.decode(login.data.token) as any |
|
795
|
expect(loginDecoded.user_metadata).toEqual({ |
|
796
|
email: "user-meta@medusa.js", |
|
797
|
roles: ["editor"], |
|
798
|
}) |
|
799
|
|
|
800
|
// Refresh — user_metadata must survive because provider_identities is |
|
801
|
// now eagerly loaded and auth_provider is forwarded from the JWT claim |
|
802
|
const refresh = await api.post( |
|
803
|
"/auth/token/refresh", |
|
804
|
{}, |
|
805
|
{ headers: { authorization: `Bearer ${login.data.token}` } } |
|
806
|
) |
|
807
|
expect(refresh.status).toEqual(200) |
|
808
|
|
|
809
|
const refreshDecoded = jwt.decode(refresh.data.token) as any |
|
810
|
expect(refreshDecoded.user_metadata).toEqual({ |
|
811
|
email: "user-meta@medusa.js", |
|
812
|
roles: ["editor"], |
|
813
|
}) |
|
814
|
}) |
|
815
|
}, |
|
816
|
}) |