|
1
|
import { asFunction, asValue, Lifetime } from "@medusajs/framework/awilix" |
|
2
|
import { moduleProviderLoader } from "@medusajs/framework/modules-sdk" |
|
3
|
import { |
|
4
|
LoaderOptions, |
|
5
|
ModuleProviderExports, |
|
6
|
ModulesSdkTypes, |
|
7
|
} from "@medusajs/framework/types" |
|
8
|
import { MedusaError } from "@medusajs/framework/utils" |
|
9
|
import { |
|
10
|
AuthIdentifiersRegistrationName, |
|
11
|
AuthMfaIdentifiersRegistrationName, |
|
12
|
AuthMfaProviderRegistrationPrefix, |
|
13
|
AuthModuleOptions, |
|
14
|
AuthProviderRegistrationPrefix, |
|
15
|
AuthVerificationIdentifiersRegistrationName, |
|
16
|
AuthVerificationProviderRegistrationPrefix, |
|
17
|
TotpMfaProviderOptions, |
|
18
|
TokenVerificationProviderOptions, |
|
19
|
} from "@types" |
|
20
|
import { RecoveryCodeMfaProvider } from "../providers/mfa/recovery-code" |
|
21
|
import { TotpMfaProvider } from "../providers/mfa/totp" |
|
22
|
import { TokenVerificationProvider } from "../providers/verification/token" |
|
23
|
import { MedusaCloudAuthService } from "../providers/medusa-cloud-auth" |
|
24
|
|
|
25
|
const validateCloudOptions = (options: AuthModuleOptions["cloud"]) => { |
|
26
|
const { |
|
27
|
oauth_authorize_endpoint, |
|
28
|
oauth_token_endpoint, |
|
29
|
oauth_audience, |
|
30
|
environment_handle, |
|
31
|
sandbox_handle, |
|
32
|
api_key, |
|
33
|
callback_url, |
|
34
|
} = options ?? {} |
|
35
|
|
|
36
|
if (!environment_handle && !sandbox_handle) { |
|
37
|
return false |
|
38
|
} |
|
39
|
|
|
40
|
if ( |
|
41
|
!oauth_authorize_endpoint || |
|
42
|
!oauth_token_endpoint || |
|
43
|
!oauth_audience || |
|
44
|
!api_key || |
|
45
|
!callback_url |
|
46
|
) { |
|
47
|
return false |
|
48
|
} |
|
49
|
|
|
50
|
return true |
|
51
|
} |
|
52
|
|
|
53
|
const registrationFn = async (klass, container, pluginOptions) => { |
|
54
|
container.register({ |
|
55
|
[AuthProviderRegistrationPrefix + pluginOptions.id]: asFunction( |
|
56
|
(cradle) => new klass(cradle, pluginOptions.options ?? {}), |
|
57
|
{ |
|
58
|
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, |
|
59
|
} |
|
60
|
), |
|
61
|
}) |
|
62
|
|
|
63
|
container.registerAdd( |
|
64
|
AuthIdentifiersRegistrationName, |
|
65
|
asValue(pluginOptions.id) |
|
66
|
) |
|
67
|
} |
|
68
|
|
|
69
|
const mfaRegistrationFn = async (klass, container, pluginOptions) => { |
|
70
|
if (!klass?.identifier) { |
|
71
|
throw new MedusaError( |
|
72
|
MedusaError.Types.INVALID_ARGUMENT, |
|
73
|
`Trying to register an MFA provider without a provider identifier.` |
|
74
|
) |
|
75
|
} |
|
76
|
|
|
77
|
const id = pluginOptions.id ?? klass.identifier |
|
78
|
const key = AuthMfaProviderRegistrationPrefix + id |
|
79
|
|
|
80
|
container.register({ |
|
81
|
[key]: asFunction( |
|
82
|
(cradle) => new klass(cradle, pluginOptions.options ?? {}), |
|
83
|
{ |
|
84
|
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, |
|
85
|
} |
|
86
|
), |
|
87
|
}) |
|
88
|
|
|
89
|
container.registerAdd(AuthMfaIdentifiersRegistrationName, asValue(id)) |
|
90
|
} |
|
91
|
|
|
92
|
const verificationRegistrationFn = async (klass, container, pluginOptions) => { |
|
93
|
if (!klass?.identifier) { |
|
94
|
throw new MedusaError( |
|
95
|
MedusaError.Types.INVALID_ARGUMENT, |
|
96
|
`Trying to register a verification provider without a provider identifier.` |
|
97
|
) |
|
98
|
} |
|
99
|
|
|
100
|
const id = pluginOptions.id ?? klass.identifier |
|
101
|
const key = AuthVerificationProviderRegistrationPrefix + id |
|
102
|
|
|
103
|
container.register({ |
|
104
|
[key]: asFunction( |
|
105
|
(cradle) => new klass(cradle, pluginOptions.options ?? {}), |
|
106
|
{ |
|
107
|
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, |
|
108
|
} |
|
109
|
), |
|
110
|
}) |
|
111
|
|
|
112
|
container.registerAdd( |
|
113
|
AuthVerificationIdentifiersRegistrationName, |
|
114
|
asValue(id) |
|
115
|
) |
|
116
|
} |
|
117
|
|
|
118
|
const getMfaProviderOptions = ( |
|
119
|
options: AuthModuleOptions | undefined, |
|
120
|
id: string |
|
121
|
): Record<string, unknown> | undefined => { |
|
122
|
return options?.mfa?.providers?.find((provider) => provider.id === id) |
|
123
|
?.options |
|
124
|
} |
|
125
|
|
|
126
|
const getVerificationProviderOptions = ( |
|
127
|
options: AuthModuleOptions | undefined, |
|
128
|
id: string |
|
129
|
): Record<string, unknown> | undefined => { |
|
130
|
return options?.verification?.providers?.find( |
|
131
|
(provider) => provider.id === id |
|
132
|
)?.options |
|
133
|
} |
|
134
|
|
|
135
|
type MfaProviderConfig = NonNullable< |
|
136
|
NonNullable<AuthModuleOptions["mfa"]>["providers"] |
|
137
|
>[number] |
|
138
|
|
|
139
|
type VerificationProviderConfig = NonNullable< |
|
140
|
NonNullable<AuthModuleOptions["verification"]>["providers"] |
|
141
|
>[number] |
|
142
|
|
|
143
|
const hasProviderResolver = <T extends { resolve?: unknown }>( |
|
144
|
provider: T |
|
145
|
): provider is T & { |
|
146
|
resolve: string | ModuleProviderExports |
|
147
|
} => { |
|
148
|
return !!provider.resolve |
|
149
|
} |
|
150
|
|
|
151
|
const validateMfaProviderConfigs = ( |
|
152
|
providers: MfaProviderConfig[] = [] |
|
153
|
): void => { |
|
154
|
const configurableProviderIds = new Set([TotpMfaProvider.identifier]) |
|
155
|
const invalidProvider = providers.find( |
|
156
|
(provider) => !provider.resolve && !configurableProviderIds.has(provider.id) |
|
157
|
) |
|
158
|
|
|
159
|
if (invalidProvider) { |
|
160
|
throw new MedusaError( |
|
161
|
MedusaError.Types.INVALID_ARGUMENT, |
|
162
|
`MFA provider "${invalidProvider.id}" must include a provider resolver.` |
|
163
|
) |
|
164
|
} |
|
165
|
} |
|
166
|
|
|
167
|
const validateVerificationProviderConfigs = ( |
|
168
|
providers: VerificationProviderConfig[] = [] |
|
169
|
): void => { |
|
170
|
const configurableProviderIds = new Set([ |
|
171
|
TokenVerificationProvider.identifier, |
|
172
|
]) |
|
173
|
const invalidProvider = providers.find( |
|
174
|
(provider) => !provider.resolve && !configurableProviderIds.has(provider.id) |
|
175
|
) |
|
176
|
|
|
177
|
if (invalidProvider) { |
|
178
|
throw new MedusaError( |
|
179
|
MedusaError.Types.INVALID_ARGUMENT, |
|
180
|
`Verification provider "${invalidProvider.id}" must include a provider resolver.` |
|
181
|
) |
|
182
|
} |
|
183
|
} |
|
184
|
|
|
185
|
export default async ({ |
|
186
|
container, |
|
187
|
options, |
|
188
|
}: LoaderOptions< |
|
189
|
( |
|
190
|
| ModulesSdkTypes.ModuleServiceInitializeOptions |
|
191
|
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions |
|
192
|
) & |
|
193
|
AuthModuleOptions |
|
194
|
>): Promise<void> => { |
|
195
|
validateMfaProviderConfigs(options?.mfa?.providers) |
|
196
|
validateVerificationProviderConfigs(options?.verification?.providers) |
|
197
|
|
|
198
|
container.registerAdd(AuthIdentifiersRegistrationName, asValue(undefined)) |
|
199
|
|
|
200
|
const totpOptions = getMfaProviderOptions( |
|
201
|
options, |
|
202
|
TotpMfaProvider.identifier |
|
203
|
) as TotpMfaProviderOptions | undefined |
|
204
|
|
|
205
|
await mfaRegistrationFn(TotpMfaProvider, container, { |
|
206
|
options: { |
|
207
|
encryption_key: options?.mfa?.encryption_key, |
|
208
|
...(totpOptions ?? {}), |
|
209
|
}, |
|
210
|
id: TotpMfaProvider.identifier, |
|
211
|
}) |
|
212
|
await mfaRegistrationFn(RecoveryCodeMfaProvider, container, { |
|
213
|
options: options?.mfa, |
|
214
|
id: RecoveryCodeMfaProvider.identifier, |
|
215
|
}) |
|
216
|
|
|
217
|
await moduleProviderLoader({ |
|
218
|
container, |
|
219
|
providers: (options?.mfa?.providers || []).filter(hasProviderResolver), |
|
220
|
registerServiceFn: mfaRegistrationFn, |
|
221
|
}) |
|
222
|
|
|
223
|
const tokenOptions = getVerificationProviderOptions( |
|
224
|
options, |
|
225
|
TokenVerificationProvider.identifier |
|
226
|
) as TokenVerificationProviderOptions | undefined |
|
227
|
|
|
228
|
await verificationRegistrationFn(TokenVerificationProvider, container, { |
|
229
|
options: tokenOptions ?? {}, |
|
230
|
id: TokenVerificationProvider.identifier, |
|
231
|
}) |
|
232
|
|
|
233
|
await moduleProviderLoader({ |
|
234
|
container, |
|
235
|
providers: (options?.verification?.providers || []).filter( |
|
236
|
hasProviderResolver |
|
237
|
), |
|
238
|
registerServiceFn: verificationRegistrationFn, |
|
239
|
}) |
|
240
|
|
|
241
|
if (validateCloudOptions(options?.cloud) && !options?.cloud?.disabled) { |
|
242
|
await registrationFn(MedusaCloudAuthService, container, { |
|
243
|
options: options?.cloud, |
|
244
|
id: "cloud", |
|
245
|
}) |
|
246
|
} |
|
247
|
|
|
248
|
await moduleProviderLoader({ |
|
249
|
container, |
|
250
|
providers: options?.providers || [], |
|
251
|
registerServiceFn: registrationFn, |
|
252
|
}) |
|
253
|
} |