|
1
|
import { |
|
2
|
AuthenticationInput, |
|
3
|
AuthenticationResponse, |
|
4
|
AuthIdentityProviderService, |
|
5
|
IAuthProvider, |
|
6
|
} from "@medusajs/types" |
|
7
|
|
|
8
|
/** |
|
9
|
* ### constructor |
|
10
|
* |
|
11
|
* The constructor allows you to access resources from the module's container using the first parameter, |
|
12
|
* and the module's options using the second parameter. |
|
13
|
* |
|
14
|
* If you're creating a client or establishing a connection with a third-party service, do it in the constructor. |
|
15
|
* |
|
16
|
* #### Example |
|
17
|
* |
|
18
|
* ```ts |
|
19
|
* import { AbstractAuthModuleProvider } from "@medusajs/framework/utils" |
|
20
|
* import { Logger } from "@medusajs/framework/types" |
|
21
|
* |
|
22
|
* type InjectedDependencies = { |
|
23
|
* logger: Logger |
|
24
|
* } |
|
25
|
* |
|
26
|
* type Options = { |
|
27
|
* apiKey: string |
|
28
|
* } |
|
29
|
* |
|
30
|
* class MyAuthProviderService extends AbstractAuthModuleProvider { |
|
31
|
* static identifier = "my-auth" |
|
32
|
* protected logger_: Logger |
|
33
|
* protected options_: Options |
|
34
|
* // assuming you're initializing a client |
|
35
|
* protected client |
|
36
|
* |
|
37
|
* constructor ( |
|
38
|
* { logger }: InjectedDependencies, |
|
39
|
* options: Options |
|
40
|
* ) { |
|
41
|
* super(...arguments) |
|
42
|
* |
|
43
|
* this.logger_ = logger |
|
44
|
* this.options_ = options |
|
45
|
* |
|
46
|
* // assuming you're initializing a client |
|
47
|
* this.client = new Client(options) |
|
48
|
* } |
|
49
|
* |
|
50
|
* // ... |
|
51
|
* } |
|
52
|
* |
|
53
|
* export default MyAuthProviderService |
|
54
|
* ``` |
|
55
|
*/ |
|
56
|
export abstract class AbstractAuthModuleProvider implements IAuthProvider { |
|
57
|
/** |
|
58
|
* Every auth provider must have an `identifier` static property. The provider's ID |
|
59
|
* will be stored as `au_{identifier}_{id}`, where `{id}` is the provider's `id` |
|
60
|
* property in the `medusa-config.ts`. |
|
61
|
* |
|
62
|
* @example |
|
63
|
* class MyAuthProviderService extends AbstractAuthModuleProvider { |
|
64
|
* static identifier = "my-auth" |
|
65
|
* // ... |
|
66
|
* } |
|
67
|
*/ |
|
68
|
static identifier: string |
|
69
|
/** |
|
70
|
* This property indicates the name used when displaying the provider on a frontend application. |
|
71
|
* |
|
72
|
* @example |
|
73
|
* class MyAuthProviderService extends AbstractAuthModuleProvider { |
|
74
|
* static DISPLAY_NAME = "My Auth" |
|
75
|
* // ... |
|
76
|
* } |
|
77
|
* |
|
78
|
*/ |
|
79
|
static DISPLAY_NAME: string |
|
80
|
|
|
81
|
/** |
|
82
|
* @ignore |
|
83
|
*/ |
|
84
|
protected readonly container_: any |
|
85
|
|
|
86
|
/** |
|
87
|
* @deprecated Use `identifier` instead. |
|
88
|
* @ignore |
|
89
|
*/ |
|
90
|
public get provider() { |
|
91
|
return (this.constructor as typeof AbstractAuthModuleProvider).identifier |
|
92
|
} |
|
93
|
|
|
94
|
/** |
|
95
|
* @ignore |
|
96
|
*/ |
|
97
|
public get identifier() { |
|
98
|
return (this.constructor as typeof AbstractAuthModuleProvider).identifier |
|
99
|
} |
|
100
|
|
|
101
|
/** |
|
102
|
* @ignore |
|
103
|
*/ |
|
104
|
public get displayName() { |
|
105
|
return (this.constructor as typeof AbstractAuthModuleProvider).DISPLAY_NAME |
|
106
|
} |
|
107
|
|
|
108
|
/** |
|
109
|
* This method validates the options of the provider set in `medusa-config.ts`. |
|
110
|
* Implementing this method is optional. It's useful if your provider requires custom validation. |
|
111
|
* |
|
112
|
* If the options aren't valid, throw an error. |
|
113
|
* |
|
114
|
* @param options - The provider's options. |
|
115
|
* |
|
116
|
* @example |
|
117
|
* class MyAuthProviderService extends AbstractAuthModuleProvider { |
|
118
|
* static validateOptions(options: Record<any, any>) { |
|
119
|
* if (!options.apiKey) { |
|
120
|
* throw new MedusaError( |
|
121
|
* MedusaError.Types.INVALID_DATA, |
|
122
|
* "API key is required in the provider's options." |
|
123
|
* ) |
|
124
|
* } |
|
125
|
* } |
|
126
|
* // ... |
|
127
|
* } |
|
128
|
*/ |
|
129
|
static validateOptions(options: Record<any, any>): void | never {} |
|
130
|
|
|
131
|
/** |
|
132
|
* This method authenticates the user. |
|
133
|
* |
|
134
|
* The authentication happens either by directly authenticating or returning a redirect URL to continue |
|
135
|
* the authentication with a third party provider. |
|
136
|
* |
|
137
|
* Related Read: [Learn about the different authentication flows in Medusa](https://docs.medusajs.com/resources/commerce-modules/auth/authentication-route). |
|
138
|
* |
|
139
|
* @param {AuthenticationInput} data - The details of the authentication request. |
|
140
|
* @param {AuthIdentityProviderService} authIdentityProviderService - The service used to retrieve or |
|
141
|
* create an auth identity. It has two methods: `create` to create an auth identity, |
|
142
|
* and `retrieve` to retrieve an auth identity. When you authenticate the user, you can create an auth identity |
|
143
|
* using this service. |
|
144
|
* @returns {Promise<AuthenticationResponse>} The authentication response. |
|
145
|
* |
|
146
|
* @example |
|
147
|
* For example, if your authentication provider doesn't require validating a callback: |
|
148
|
* |
|
149
|
* ```ts |
|
150
|
* import { |
|
151
|
* AuthIdentityProviderService, |
|
152
|
* AuthenticationInput, |
|
153
|
* AuthenticationResponse |
|
154
|
* } from "@medusajs/framework/types" |
|
155
|
* // ... |
|
156
|
* |
|
157
|
* class MyAuthProviderService extends AbstractAuthModuleProvider { |
|
158
|
* // ... |
|
159
|
* async authenticate( |
|
160
|
* data: AuthenticationInput, |
|
161
|
* authIdentityProviderService: AuthIdentityProviderService |
|
162
|
* ): Promise<AuthenticationResponse> { |
|
163
|
* const isAuthenticated = false |
|
164
|
* // TODO perform custom logic to authenticate the user |
|
165
|
* // for example, verifying a password |
|
166
|
* |
|
167
|
* if (!isAuthenticated) { |
|
168
|
* // if the authentication didn't succeed, return |
|
169
|
* // an object of the following format |
|
170
|
* return { |
|
171
|
* success: false, |
|
172
|
* error: "Incorrect credentials" |
|
173
|
* } |
|
174
|
* } |
|
175
|
* |
|
176
|
* // authentication is successful, retrieve the identity |
|
177
|
* const authIdentity = await authIdentityProviderService.retrieve({ |
|
178
|
* entity_id: data.body.email, // email or some ID |
|
179
|
* }) |
|
180
|
* |
|
181
|
* return { |
|
182
|
* success: true, |
|
183
|
* authIdentity |
|
184
|
* } |
|
185
|
* } |
|
186
|
* } |
|
187
|
* ``` |
|
188
|
* |
|
189
|
* If your authentication provider requires validating callback: |
|
190
|
* |
|
191
|
* ```ts |
|
192
|
* import { |
|
193
|
* AuthIdentityProviderService, |
|
194
|
* AuthenticationInput, |
|
195
|
* AuthenticationResponse |
|
196
|
* } from "@medusajs/framework/types" |
|
197
|
* // ... |
|
198
|
* |
|
199
|
* class MyAuthProviderService extends AbstractAuthModuleProvider { |
|
200
|
* // ... |
|
201
|
* async authenticate( |
|
202
|
* data: AuthenticationInput, |
|
203
|
* authIdentityProviderService: AuthIdentityProviderService |
|
204
|
* ): Promise<AuthenticationResponse> { |
|
205
|
* const isAuthenticated = false |
|
206
|
* // TODO perform custom logic to authenticate the user |
|
207
|
* // ... |
|
208
|
* |
|
209
|
* if (!isAuthenticated) { |
|
210
|
* // if the authentication didn't succeed, return |
|
211
|
* // an object of the following format |
|
212
|
* return { |
|
213
|
* success: false, |
|
214
|
* error: "Incorrect credentials" |
|
215
|
* } |
|
216
|
* } |
|
217
|
* |
|
218
|
* // optional, useful for third-party providers that require a redirect URL |
|
219
|
* // to continue the authentication flow |
|
220
|
* // const stateKey = crypto.randomBytes(32).toString("hex") |
|
221
|
* // const state = { |
|
222
|
* // callback_url: body?.callback_url ?? this.config_.callbackUrl, |
|
223
|
* // } |
|
224
|
* |
|
225
|
* // verify this later in the `validateCallback` method |
|
226
|
* // await authIdentityService.setState(stateKey, state) |
|
227
|
* |
|
228
|
* return { |
|
229
|
* success: true, |
|
230
|
* location: "some-url.com" |
|
231
|
* } |
|
232
|
* } |
|
233
|
* } |
|
234
|
* ``` |
|
235
|
*/ |
|
236
|
abstract authenticate( |
|
237
|
data: AuthenticationInput, |
|
238
|
authIdentityProviderService: AuthIdentityProviderService |
|
239
|
): Promise<AuthenticationResponse> |
|
240
|
|
|
241
|
/** |
|
242
|
* This method receives credentails to create a new auth identity. It performs any validation necessary |
|
243
|
* before creating the auth identity. |
|
244
|
* |
|
245
|
* For example, in the `emailpass` provider, this method ensures that the provided email doesn't exist |
|
246
|
* before creating the auth identity. |
|
247
|
* |
|
248
|
* This method is only used in a basic authentication flow, such as when using an email and password |
|
249
|
* to register and authenticate a user. |
|
250
|
* |
|
251
|
* Related Read: [Learn about the different authentication flows in Medusa](https://docs.medusajs.com/resources/commerce-modules/auth/authentication-route). |
|
252
|
* |
|
253
|
* @param {AuthenticationInput} data - The details of the authentication request. |
|
254
|
* @param {AuthIdentityProviderService} authIdentityProviderService - The service used to retrieve or |
|
255
|
* create an auth identity. It has two methods: `create` to create an auth identity, |
|
256
|
* and `retrieve` to retrieve an auth identity. When you authenticate the user, you can create an auth identity |
|
257
|
* using this service. |
|
258
|
* @returns The created authentication identity if no errors occur. |
|
259
|
* |
|
260
|
* @example |
|
261
|
* import { |
|
262
|
* AuthIdentityProviderService, |
|
263
|
* AuthenticationInput, |
|
264
|
* AuthenticationResponse |
|
265
|
* } from "@medusajs/framework/types" |
|
266
|
* import { MedusaError } from "@medusajs/framework/utils" |
|
267
|
* // ... |
|
268
|
* |
|
269
|
* class MyAuthProviderService extends AbstractAuthModuleProvider { |
|
270
|
* // ... |
|
271
|
* async register( |
|
272
|
* data: AuthenticationInput, |
|
273
|
* authIdentityProviderService: AuthIdentityProviderService |
|
274
|
* ): Promise<AuthenticationResponse> { |
|
275
|
* try { |
|
276
|
* await authIdentityService.retrieve({ |
|
277
|
* entity_id: data.body.email, // email or some ID |
|
278
|
* }) |
|
279
|
* |
|
280
|
* return { |
|
281
|
* success: false, |
|
282
|
* error: "Identity with email already exists", |
|
283
|
* } |
|
284
|
* } catch (error) { |
|
285
|
* if (error.type === MedusaError.Types.NOT_FOUND) { |
|
286
|
* const createdAuthIdentity = await authIdentityProviderService.create({ |
|
287
|
* entity_id: data.body.email, // email or some ID |
|
288
|
* provider: this.provider, |
|
289
|
* provider_metadata: { |
|
290
|
* // can include password or any other relevant information |
|
291
|
* } |
|
292
|
* }) |
|
293
|
* |
|
294
|
* return { |
|
295
|
* success: true, |
|
296
|
* authIdentity: createdAuthIdentity, |
|
297
|
* } |
|
298
|
* } |
|
299
|
* |
|
300
|
* return { success: false, error: error.message } |
|
301
|
* } |
|
302
|
* } |
|
303
|
* } |
|
304
|
*/ |
|
305
|
register( |
|
306
|
data: AuthenticationInput, |
|
307
|
authIdentityProviderService: AuthIdentityProviderService |
|
308
|
): Promise<AuthenticationResponse> { |
|
309
|
throw new Error( |
|
310
|
`Method 'register' not implemented for provider ${this.provider}` |
|
311
|
) |
|
312
|
} |
|
313
|
|
|
314
|
/** |
|
315
|
* This method is used to update an auth identity's details. |
|
316
|
* |
|
317
|
* For example, the `emailpass` provider's implementation of this method updates a user's password. |
|
318
|
* |
|
319
|
* @param data - Data relevant to identify the auth identity and what to update in it. For example, |
|
320
|
* the `emailpass` provider expects in this object an `email` and `password` properties. |
|
321
|
* @param authIdentityProviderService - The service used to retrieve or |
|
322
|
* create an auth identity. It has two methods: `create` to create an auth identity, |
|
323
|
* and `retrieve` to retrieve an auth identity. When you authenticate the user, you can create an auth identity |
|
324
|
* using this service. |
|
325
|
* @returns The updated authentication identity if no errors occur. |
|
326
|
* |
|
327
|
* @example |
|
328
|
* import { |
|
329
|
* AuthIdentityProviderService, |
|
330
|
* AuthenticationInput, |
|
331
|
* AuthenticationResponse |
|
332
|
* } from "@medusajs/framework/types" |
|
333
|
* import { MedusaError } from "@medusajs/framework/utils" |
|
334
|
* // ... |
|
335
|
* |
|
336
|
* class MyAuthProviderService extends AbstractAuthModuleProvider { |
|
337
|
* // ... |
|
338
|
* async update( |
|
339
|
* data: Record<string, unknown>, |
|
340
|
* authIdentityProviderService: AuthIdentityProviderService |
|
341
|
* ): Promise<AuthenticationResponse> { |
|
342
|
* try { |
|
343
|
* const authIdentity = await authIdentityService.update( |
|
344
|
* data.email, // email or some ID used to identify the auth identity |
|
345
|
* { |
|
346
|
* user: data.user // example |
|
347
|
* } |
|
348
|
* ) |
|
349
|
* |
|
350
|
* return { success: true, authIdentity } |
|
351
|
* } catch (error) { |
|
352
|
* return { success: false, error: error.message } |
|
353
|
* } |
|
354
|
* } |
|
355
|
* } |
|
356
|
*/ |
|
357
|
update( |
|
358
|
data: Record<string, unknown>, |
|
359
|
authIdentityProviderService: AuthIdentityProviderService |
|
360
|
): Promise<AuthenticationResponse> { |
|
361
|
throw new Error( |
|
362
|
`Method 'update' not implemented for provider ${this.provider}` |
|
363
|
) |
|
364
|
} |
|
365
|
|
|
366
|
/** |
|
367
|
* This method validates the callback of an authentication request. |
|
368
|
* |
|
369
|
* In an authentication flow that requires performing an action with a third-party service, such as login |
|
370
|
* with a social account, the {@link authenticate} method is called first. |
|
371
|
* |
|
372
|
* Then, the third-party service redirects to a frontend URL passing it a `code` query parameter. |
|
373
|
* The frontend should then send a request to the Medusa application's validate callback API route, passing it the code. |
|
374
|
* That route uses this method to verify the callback's code. |
|
375
|
* |
|
376
|
* If the callback is verified successfully, the provider creates an auth identity for the user, or updates the auth identity's user information. |
|
377
|
* |
|
378
|
* In the auth identity, use the following properties to store additional data: |
|
379
|
* |
|
380
|
* - `provider_metadata`: Store metadata useful for the provider, such as a password hash. |
|
381
|
* - `user_metadata`: Store metadata of the user's details. For example, if the third-party service returns the user's information such as email |
|
382
|
* or name, you store this data in this property. |
|
383
|
* |
|
384
|
* Related Guide: [Learn about the different authentication flows in Medusa](https://docs.medusajs.com/resources/commerce-modules/auth/authentication-route). |
|
385
|
* |
|
386
|
* @param {AuthenticationInput} data - The details of the authentication request. |
|
387
|
* @param {AuthIdentityProviderService} authIdentityProviderService - The service used to retrieve or |
|
388
|
* create an auth identity. It has two methods: `create` to create an auth identity, |
|
389
|
* and `retrieve` to retrieve an auth identity. When you authenticate the user, you can create an auth identity |
|
390
|
* using this service. |
|
391
|
* @returns {Promise<AuthenticationResponse>} The authentication response. |
|
392
|
* |
|
393
|
* @example |
|
394
|
* import { |
|
395
|
* AuthIdentityProviderService, |
|
396
|
* AuthenticationInput, |
|
397
|
* AuthenticationResponse |
|
398
|
* } from "@medusajs/framework/types" |
|
399
|
* // ... |
|
400
|
* |
|
401
|
* class MyAuthProviderService extends AbstractAuthModuleProvider { |
|
402
|
* // ... |
|
403
|
* async validateCallback( |
|
404
|
* data: AuthenticationInput, |
|
405
|
* authIdentityProviderService: AuthIdentityProviderService |
|
406
|
* ): Promise<AuthenticationResponse> { |
|
407
|
* const isAuthenticated = false |
|
408
|
* |
|
409
|
* // optional, useful for third-party providers that require a redirect URL |
|
410
|
* // to continue the authentication flow |
|
411
|
* // const stateKey = data.query?.state as string |
|
412
|
* // const state = await authIdentityService.getState(stateKey) |
|
413
|
* // if (!state) { |
|
414
|
* // return { success: false, error: "No state provided, or session expired" } |
|
415
|
* // } |
|
416
|
* // // use callback URL with the third-party service |
|
417
|
* // const callbackUrl = state.callback_url |
|
418
|
* // TODO perform custom logic to authenticate the user |
|
419
|
* // ... |
|
420
|
* |
|
421
|
* if (!isAuthenticated) { |
|
422
|
* // if the authentication didn't succeed, return |
|
423
|
* // an object of the following format |
|
424
|
* return { |
|
425
|
* success: false, |
|
426
|
* error: "Something went wrong" |
|
427
|
* } |
|
428
|
* } |
|
429
|
* |
|
430
|
* // authentication is successful, create an auth identity |
|
431
|
* // if doesn't exist |
|
432
|
* let authIdentity |
|
433
|
* |
|
434
|
* try { |
|
435
|
* authIdentity = await authIdentityProviderService.retrieve({ |
|
436
|
* entity_id: data.body.email, // email or some ID |
|
437
|
* provider: this.provider |
|
438
|
* }) |
|
439
|
* } catch (e) { |
|
440
|
* // The auth identity doesn't exist so create it |
|
441
|
* authIdentity = await authIdentityProviderService.create({ |
|
442
|
* entity_id: data.body.email, // email or some ID |
|
443
|
* provider: this.provider, |
|
444
|
* provider_metadata: { |
|
445
|
* // can include password or any other relevant information |
|
446
|
* }, |
|
447
|
* user_metadata: { |
|
448
|
* // can include data retrieved from the third-party service |
|
449
|
* } |
|
450
|
* }) |
|
451
|
* } |
|
452
|
* |
|
453
|
* return { |
|
454
|
* success: true, |
|
455
|
* authIdentity |
|
456
|
* } |
|
457
|
* } |
|
458
|
* } |
|
459
|
*/ |
|
460
|
validateCallback( |
|
461
|
data: AuthenticationInput, |
|
462
|
authIdentityProviderService: AuthIdentityProviderService |
|
463
|
): Promise<AuthenticationResponse> { |
|
464
|
throw new Error( |
|
465
|
`Callback authentication not implemented for provider ${this.provider}` |
|
466
|
) |
|
467
|
} |
|
468
|
} |