wabe 0.6.12 → 0.6.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/dist/database/DatabaseController.d.ts +2 -0
  2. package/dist/file/FileDevAdapter.d.ts +1 -0
  3. package/dist/graphql/pointerAndRelationFunction.d.ts +6 -0
  4. package/dist/index.js +3827 -3541
  5. package/dist/schema/Schema.d.ts +2 -2
  6. package/dist/server/generateCodegen.d.ts +10 -0
  7. package/dist/server/index.d.ts +2 -1
  8. package/dist/utils/objectKeys.d.ts +1 -0
  9. package/package.json +7 -4
  10. package/dev/index.ts +0 -215
  11. package/dist/schema/resolvers/sendEmail.d.ts +0 -1
  12. package/generated/schema.graphql +0 -1945
  13. package/generated/wabe.ts +0 -448
  14. package/src/authentication/OTP.test.ts +0 -69
  15. package/src/authentication/OTP.ts +0 -64
  16. package/src/authentication/Session.test.ts +0 -629
  17. package/src/authentication/Session.ts +0 -517
  18. package/src/authentication/cookies.ts +0 -10
  19. package/src/authentication/defaultAuthentication.ts +0 -209
  20. package/src/authentication/index.ts +0 -4
  21. package/src/authentication/interface.ts +0 -177
  22. package/src/authentication/oauth/GitHub.test.ts +0 -91
  23. package/src/authentication/oauth/GitHub.ts +0 -121
  24. package/src/authentication/oauth/Google.test.ts +0 -91
  25. package/src/authentication/oauth/Google.ts +0 -101
  26. package/src/authentication/oauth/Oauth2Client.test.ts +0 -219
  27. package/src/authentication/oauth/Oauth2Client.ts +0 -135
  28. package/src/authentication/oauth/index.ts +0 -2
  29. package/src/authentication/oauth/utils.test.ts +0 -33
  30. package/src/authentication/oauth/utils.ts +0 -27
  31. package/src/authentication/providers/EmailOTP.test.ts +0 -127
  32. package/src/authentication/providers/EmailOTP.ts +0 -95
  33. package/src/authentication/providers/EmailPassword.test.ts +0 -263
  34. package/src/authentication/providers/EmailPassword.ts +0 -138
  35. package/src/authentication/providers/EmailPasswordSRP.test.ts +0 -208
  36. package/src/authentication/providers/EmailPasswordSRP.ts +0 -191
  37. package/src/authentication/providers/GitHub.ts +0 -24
  38. package/src/authentication/providers/Google.ts +0 -24
  39. package/src/authentication/providers/OAuth.test.ts +0 -185
  40. package/src/authentication/providers/OAuth.ts +0 -106
  41. package/src/authentication/providers/PhonePassword.test.ts +0 -221
  42. package/src/authentication/providers/PhonePassword.ts +0 -136
  43. package/src/authentication/providers/QRCodeOTP.test.ts +0 -77
  44. package/src/authentication/providers/QRCodeOTP.ts +0 -69
  45. package/src/authentication/providers/index.ts +0 -6
  46. package/src/authentication/resolvers/refreshResolver.test.ts +0 -30
  47. package/src/authentication/resolvers/refreshResolver.ts +0 -19
  48. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +0 -59
  49. package/src/authentication/resolvers/signInWithResolver.test.ts +0 -306
  50. package/src/authentication/resolvers/signInWithResolver.ts +0 -106
  51. package/src/authentication/resolvers/signOutResolver.test.ts +0 -38
  52. package/src/authentication/resolvers/signOutResolver.ts +0 -18
  53. package/src/authentication/resolvers/signUpWithResolver.test.ts +0 -180
  54. package/src/authentication/resolvers/signUpWithResolver.ts +0 -68
  55. package/src/authentication/resolvers/verifyChallenge.test.ts +0 -230
  56. package/src/authentication/resolvers/verifyChallenge.ts +0 -78
  57. package/src/authentication/roles.test.ts +0 -49
  58. package/src/authentication/roles.ts +0 -40
  59. package/src/authentication/security.ts +0 -278
  60. package/src/authentication/utils.test.ts +0 -97
  61. package/src/authentication/utils.ts +0 -39
  62. package/src/cache/InMemoryCache.test.ts +0 -62
  63. package/src/cache/InMemoryCache.ts +0 -45
  64. package/src/cron/index.test.ts +0 -17
  65. package/src/cron/index.ts +0 -43
  66. package/src/database/DatabaseController.test.ts +0 -613
  67. package/src/database/DatabaseController.ts +0 -1415
  68. package/src/database/index.test.ts +0 -1551
  69. package/src/database/index.ts +0 -9
  70. package/src/database/interface.ts +0 -308
  71. package/src/email/DevAdapter.ts +0 -7
  72. package/src/email/EmailController.test.ts +0 -29
  73. package/src/email/EmailController.ts +0 -13
  74. package/src/email/index.ts +0 -2
  75. package/src/email/interface.ts +0 -36
  76. package/src/email/templates/sendOtpCode.ts +0 -120
  77. package/src/file/FileController.ts +0 -28
  78. package/src/file/FileDevAdapter.ts +0 -51
  79. package/src/file/hookDeleteFile.ts +0 -25
  80. package/src/file/hookReadFile.ts +0 -66
  81. package/src/file/hookUploadFile.ts +0 -52
  82. package/src/file/index.test.ts +0 -1031
  83. package/src/file/index.ts +0 -2
  84. package/src/file/interface.ts +0 -63
  85. package/src/file/security.ts +0 -156
  86. package/src/graphql/GraphQLSchema.test.ts +0 -5099
  87. package/src/graphql/GraphQLSchema.ts +0 -886
  88. package/src/graphql/index.ts +0 -2
  89. package/src/graphql/parseGraphqlSchema.ts +0 -85
  90. package/src/graphql/parser.test.ts +0 -203
  91. package/src/graphql/parser.ts +0 -707
  92. package/src/graphql/pointerAndRelationFunction.ts +0 -191
  93. package/src/graphql/resolvers.ts +0 -464
  94. package/src/graphql/tests/aggregation.test.ts +0 -1115
  95. package/src/graphql/tests/e2e.test.ts +0 -590
  96. package/src/graphql/tests/scalars.test.ts +0 -250
  97. package/src/graphql/types.ts +0 -227
  98. package/src/hooks/HookObject.test.ts +0 -122
  99. package/src/hooks/HookObject.ts +0 -165
  100. package/src/hooks/authentication.ts +0 -67
  101. package/src/hooks/createUser.test.ts +0 -77
  102. package/src/hooks/createUser.ts +0 -10
  103. package/src/hooks/defaultFields.test.ts +0 -176
  104. package/src/hooks/defaultFields.ts +0 -32
  105. package/src/hooks/deleteSession.test.ts +0 -181
  106. package/src/hooks/deleteSession.ts +0 -20
  107. package/src/hooks/hashFieldHook.test.ts +0 -152
  108. package/src/hooks/hashFieldHook.ts +0 -89
  109. package/src/hooks/index.test.ts +0 -258
  110. package/src/hooks/index.ts +0 -420
  111. package/src/hooks/permissions.test.ts +0 -412
  112. package/src/hooks/permissions.ts +0 -93
  113. package/src/hooks/protected.test.ts +0 -551
  114. package/src/hooks/protected.ts +0 -74
  115. package/src/hooks/searchableFields.test.ts +0 -147
  116. package/src/hooks/searchableFields.ts +0 -86
  117. package/src/hooks/session.test.ts +0 -134
  118. package/src/hooks/session.ts +0 -76
  119. package/src/hooks/setEmail.test.ts +0 -216
  120. package/src/hooks/setEmail.ts +0 -33
  121. package/src/hooks/setupAcl.test.ts +0 -618
  122. package/src/hooks/setupAcl.ts +0 -25
  123. package/src/hooks/virtualFields.test.ts +0 -228
  124. package/src/hooks/virtualFields.ts +0 -48
  125. package/src/index.ts +0 -9
  126. package/src/schema/Schema.test.ts +0 -482
  127. package/src/schema/Schema.ts +0 -839
  128. package/src/schema/defaultResolvers.ts +0 -93
  129. package/src/schema/index.ts +0 -1
  130. package/src/schema/resolvers/meResolver.test.ts +0 -62
  131. package/src/schema/resolvers/meResolver.ts +0 -10
  132. package/src/schema/resolvers/resetPassword.test.ts +0 -341
  133. package/src/schema/resolvers/resetPassword.ts +0 -63
  134. package/src/schema/resolvers/sendEmail.test.ts +0 -118
  135. package/src/schema/resolvers/sendEmail.ts +0 -21
  136. package/src/schema/resolvers/sendOtpCode.test.ts +0 -141
  137. package/src/schema/resolvers/sendOtpCode.ts +0 -52
  138. package/src/security.test.ts +0 -4136
  139. package/src/server/defaultSessionHandler.test.ts +0 -62
  140. package/src/server/defaultSessionHandler.ts +0 -104
  141. package/src/server/generateCodegen.ts +0 -433
  142. package/src/server/index.test.ts +0 -843
  143. package/src/server/index.ts +0 -336
  144. package/src/server/interface.ts +0 -11
  145. package/src/server/routes/authHandler.ts +0 -171
  146. package/src/server/routes/index.ts +0 -48
  147. package/src/utils/crypto.test.ts +0 -41
  148. package/src/utils/crypto.ts +0 -105
  149. package/src/utils/database.ts +0 -8
  150. package/src/utils/export.ts +0 -12
  151. package/src/utils/helper.ts +0 -204
  152. package/src/utils/index.test.ts +0 -11
  153. package/src/utils/index.ts +0 -196
  154. package/src/utils/preload.ts +0 -8
  155. package/src/utils/testHelper.ts +0 -124
  156. package/tsconfig.json +0 -32
@@ -1,839 +0,0 @@
1
- import { AuthenticationProvider, SecondaryFactor } from '../authentication'
2
- import { refreshResolver } from '../authentication/resolvers/refreshResolver'
3
- import { signOutResolver } from '../authentication/resolvers/signOutResolver'
4
- import { verifyChallengeResolver } from '../authentication/resolvers/verifyChallenge'
5
- import type { WabeConfig, WabeTypes } from '../server'
6
- import { defaultMutations, defaultQueries } from './defaultResolvers'
7
- import type { HookObject } from '../hooks/HookObject'
8
- import { signUpWithResolver } from '../authentication/resolvers/signUpWithResolver'
9
- import { signInWithResolver } from '../authentication/resolvers/signInWithResolver'
10
-
11
- export const defaultPrivateFields = ['acl']
12
-
13
- export type WabePrimaryTypes =
14
- | 'String'
15
- | 'Int'
16
- | 'Float'
17
- | 'Boolean'
18
- | 'Email'
19
- | 'Phone'
20
- | 'Date'
21
- | 'File'
22
- | 'Hash'
23
-
24
- export type WabeCustomTypes = 'Array' | 'Object'
25
-
26
- export type WabeRelationTypes = 'Pointer' | 'Relation'
27
-
28
- export type WabeFieldTypes = WabeCustomTypes | WabePrimaryTypes | WabeRelationTypes
29
-
30
- export type VirtualReturnType =
31
- | 'String'
32
- | 'Int'
33
- | 'Float'
34
- | 'Boolean'
35
- | 'Date'
36
- | 'Email'
37
- | 'Phone'
38
- | 'Object'
39
- | 'Array'
40
-
41
- export type WabeObject<T extends WabeTypes> = {
42
- name: string
43
- fields: SchemaFields<T>
44
- description?: string
45
- required?: boolean
46
- }
47
-
48
- export type ProtectedFieldConfig<TRole = string> = {
49
- authorizedRoles: Array<TRole>
50
- protectedOperations: Array<'create' | 'read' | 'update'>
51
- }
52
-
53
- export type SchemaClassWithProtectedFields = {
54
- fields: Record<string, { protected?: ProtectedFieldConfig }>
55
- }
56
-
57
- type FieldBase<T extends WabeTypes> = {
58
- required?: boolean
59
- description?: string
60
- protected?: ProtectedFieldConfig<T['enums']['RoleEnum'] | 'rootOnly'>
61
- }
62
-
63
- type TypeFieldBase<U, K extends WabeFieldTypes> = {
64
- type: K
65
- defaultValue?: U
66
- }
67
-
68
- type TypeFieldArray<T extends WabeTypes> = {
69
- type: 'Array'
70
- requiredValue?: boolean
71
- defaultValue?: any[]
72
- } & (
73
- | {
74
- // For the moment we only keep object and not array because we don't
75
- // support array of array
76
- typeValue: WabePrimaryTypes
77
- }
78
- | { typeValue: 'Object'; object: WabeObject<T> }
79
- )
80
-
81
- type TypeFieldObject<T extends WabeTypes> = {
82
- type: 'Object'
83
- object: WabeObject<T>
84
- defaultValue?: any
85
- }
86
-
87
- type TypeFieldPointer<T extends WabeTypes> = {
88
- type: 'Pointer'
89
- class: keyof T['types']
90
- }
91
-
92
- type TypeFieldRelation<T extends WabeTypes> = {
93
- type: 'Relation'
94
- class: keyof T['types']
95
- }
96
-
97
- type TypeFieldFile = {
98
- type: 'File'
99
- }
100
-
101
- type TypeFieldCustomScalars<T extends WabeTypes> = {
102
- type: T['scalars'] extends '' ? never : T['scalars']
103
- required?: boolean
104
- defaultValue?: any
105
- }
106
-
107
- type TypeFieldCustomEnums<T extends WabeTypes> = {
108
- type: keyof T['enums']
109
- defaultValue?: any
110
- }
111
-
112
- type TypeFieldVirtualScalar<T extends WabeTypes, K extends keyof T['types']> = {
113
- type: 'Virtual'
114
- returnType: 'String' | 'Int' | 'Float' | 'Boolean' | 'Date' | 'Email' | 'Phone'
115
- defaultValue?: never
116
- dependsOn: Array<keyof T['types'][K]>
117
- callback: (object: T['types'][K] & { id: string }) => string | number | boolean | Date | null
118
- }
119
-
120
- type TypeFieldVirtualObject<T extends WabeTypes, K extends keyof T['types']> = {
121
- type: 'Virtual'
122
- returnType: 'Object'
123
- object: WabeObject<T>
124
- defaultValue?: never
125
- dependsOn: Array<keyof T['types'][K]>
126
- callback: (object: T['types'][K] & { id: string }) => Record<string, unknown> | null
127
- }
128
-
129
- type TypeFieldVirtualArray<T extends WabeTypes, K extends keyof T['types']> = {
130
- type: 'Virtual'
131
- returnType: 'Array'
132
- defaultValue?: never
133
- dependsOn: Array<keyof T['types'][K]>
134
- callback: (object: T['types'][K] & { id: string }) => unknown[] | null
135
- } & ({ typeValue: WabePrimaryTypes } | { typeValue: 'Object'; object: WabeObject<T> })
136
-
137
- type TypeFieldVirtual<T extends WabeTypes, K extends keyof T['types']> =
138
- | TypeFieldVirtualScalar<T, K>
139
- | TypeFieldVirtualObject<T, K>
140
- | TypeFieldVirtualArray<T, K>
141
-
142
- export type TypeField<T extends WabeTypes, K extends keyof T['types'] = keyof T['types']> = (
143
- | TypeFieldBase<string, 'String'>
144
- | TypeFieldBase<number, 'Int'>
145
- | TypeFieldBase<number, 'Float'>
146
- | TypeFieldBase<boolean, 'Boolean'>
147
- | TypeFieldBase<Date, 'Date'>
148
- | TypeFieldBase<string, 'Email'>
149
- | TypeFieldBase<string, 'Phone'>
150
- | TypeFieldBase<string, 'Hash'>
151
- | TypeFieldArray<T>
152
- | TypeFieldObject<T>
153
- | TypeFieldPointer<T>
154
- | TypeFieldRelation<T>
155
- | TypeFieldFile
156
- | TypeFieldCustomScalars<T>
157
- | TypeFieldCustomEnums<T>
158
- | TypeFieldVirtual<T, K>
159
- ) &
160
- FieldBase<T>
161
-
162
- export type SchemaFields<
163
- T extends WabeTypes,
164
- K extends keyof T['types'] = keyof T['types'],
165
- > = Record<string, TypeField<T, K>>
166
-
167
- export type ResolverType<T extends WabeTypes> = {
168
- required?: boolean
169
- description?: string
170
- resolve: (...args: any) => any
171
- } & (
172
- | { type: WabePrimaryTypes | T['enums'] | T['scalars'] }
173
- | { type: 'Object'; outputObject: ClassInterface<T> }
174
- | {
175
- type: 'Array'
176
- typeValue: WabePrimaryTypes
177
- typeValueRequired?: boolean
178
- }
179
- | {
180
- type: 'Array'
181
- typeValue: 'Object'
182
- outputObject: ClassInterface<T>
183
- typeValueRequired?: boolean
184
- }
185
- )
186
-
187
- export type QueryResolver<T extends WabeTypes> = {
188
- args?: SchemaFields<T>
189
- } & ResolverType<T>
190
-
191
- export type MutationResolver<T extends WabeTypes> = {
192
- args?: { input: SchemaFields<T> }
193
- } & ResolverType<T>
194
-
195
- export type TypeResolver<T extends WabeTypes> = {
196
- queries?: {
197
- [key: string]: QueryResolver<T>
198
- }
199
- mutations?: {
200
- [key: string]: MutationResolver<T>
201
- }
202
- }
203
-
204
- export type PermissionsOperations = 'create' | 'read' | 'update' | 'delete'
205
-
206
- export interface PermissionProperties<T extends WabeTypes> {
207
- requireAuthentication?: boolean
208
- /**
209
- * An empty array means that none role is authorized (except root client)
210
- */
211
- authorizedRoles?: Array<T['enums']['RoleEnum'] | 'everyone'>
212
- }
213
-
214
- /**
215
- * ACL properties
216
- * Callback to define the ACL object before insert of the object in the database
217
- * Can be done with a beforeCreate hook but for simplicity we can define it here
218
- */
219
- export type ACLProperties = (hookObject: HookObject<any, any>) => void | Promise<void>
220
-
221
- export type ClassPermissions<T extends WabeTypes> = Partial<
222
- Record<PermissionsOperations, PermissionProperties<T>> & {
223
- acl: ACLProperties
224
- }
225
- >
226
-
227
- export type SearchableFields = Array<string>
228
-
229
- export type ClassIndexes = Array<{
230
- field: string
231
- order: 'ASC' | 'DESC'
232
- unique?: boolean
233
- }>
234
-
235
- export interface ClassInterface<T extends WabeTypes> {
236
- name: string
237
- fields: SchemaFields<T, keyof T['types']>
238
- description?: string
239
- permissions?: ClassPermissions<T>
240
- searchableFields?: SearchableFields
241
- indexes?: ClassIndexes
242
- }
243
-
244
- export interface ScalarInterface {
245
- name: string
246
- description?: string
247
- parseValue?: (value: any) => any
248
- serialize?: (value: any) => any
249
- parseLiteral?: (ast: any) => any
250
- }
251
-
252
- export interface EnumInterface {
253
- name: string
254
- values: Record<string, string>
255
- description?: string
256
- }
257
-
258
- export interface SchemaInterface<T extends WabeTypes> {
259
- classes?: ClassInterface<T>[]
260
- scalars?: ScalarInterface[]
261
- enums?: EnumInterface[]
262
- resolvers?: TypeResolver<T>
263
- }
264
-
265
- export class Schema<T extends WabeTypes> {
266
- public schema: SchemaInterface<T>
267
- private config: WabeConfig<T>
268
-
269
- constructor(config: WabeConfig<T>) {
270
- this.config = config
271
- // TODO : Add default scalars here
272
- this.schema = {
273
- ...config.schema,
274
- classes: this.defaultClass(config.schema),
275
- enums: [...(config.schema?.enums || []), ...this.defaultEnum()],
276
- resolvers: this.mergeResolvers(this.defaultResolvers()),
277
- }
278
- }
279
-
280
- defaultEnum(): EnumInterface[] {
281
- return [
282
- {
283
- name: 'AuthenticationProvider',
284
- values: Object.fromEntries(Object.values(AuthenticationProvider).map((key) => [key, key])),
285
- },
286
- {
287
- name: 'SecondaryFactor',
288
- values: Object.fromEntries(Object.values(SecondaryFactor).map((key) => [key, key])),
289
- },
290
- ]
291
- }
292
-
293
- mergeResolvers(defaultResolvers: TypeResolver<T>): TypeResolver<T> {
294
- return {
295
- mutations: {
296
- ...this.config.schema?.resolvers?.mutations,
297
- ...defaultResolvers.mutations,
298
- },
299
- queries: {
300
- ...this.config.schema?.resolvers?.queries,
301
- ...defaultResolvers.queries,
302
- },
303
- }
304
- }
305
-
306
- defaultResolvers(): TypeResolver<T> {
307
- const customAuthenticationConfig = this.config.authentication?.customAuthenticationMethods || []
308
-
309
- const allPrimaryAuthenticationMethodsInput = customAuthenticationConfig
310
- .filter((authenticationMethod) => !authenticationMethod.isSecondaryFactor)
311
- .reduce((acc, authenticationMethod) => {
312
- acc[authenticationMethod.name] = {
313
- type: 'Object',
314
- object: {
315
- name: authenticationMethod.name,
316
- fields: authenticationMethod.input,
317
- },
318
- }
319
-
320
- return acc
321
- }, {} as SchemaFields<T>)
322
-
323
- const allSecondaryFactorAuthenticationMethodsInput = customAuthenticationConfig
324
- .filter((authenticationMethod) => authenticationMethod.isSecondaryFactor)
325
- .reduce((acc, authenticationMethod) => {
326
- acc[authenticationMethod.name] = {
327
- type: 'Object',
328
- object: {
329
- name: authenticationMethod.name,
330
- fields: authenticationMethod.input,
331
- },
332
- }
333
-
334
- return acc
335
- }, {} as SchemaFields<T>)
336
-
337
- const authenticationInputObject: TypeFieldObject<T> = {
338
- type: 'Object',
339
- object: {
340
- name: 'Authentication',
341
- fields: allPrimaryAuthenticationMethodsInput,
342
- },
343
- }
344
-
345
- const secondaryFactorAuthenticationInputObject: TypeFieldObject<T> = {
346
- type: 'Object',
347
- object: {
348
- name: 'SecondaryFactorAuthentication',
349
- fields: allSecondaryFactorAuthenticationMethodsInput,
350
- },
351
- }
352
-
353
- const authenticationInput: TypeField<T> = {
354
- type: 'Object',
355
- object: {
356
- name: 'Authentication',
357
- fields: authenticationInputObject.object.fields,
358
- required: true,
359
- },
360
- required: true,
361
- }
362
-
363
- return {
364
- queries: defaultQueries,
365
- mutations: {
366
- ...defaultMutations,
367
- ...(customAuthenticationConfig.length > 0
368
- ? {
369
- signInWith: {
370
- type: 'Object',
371
- outputObject: {
372
- name: 'SignInWithOutput',
373
- fields: {
374
- user: {
375
- type: 'Pointer',
376
- class: 'User',
377
- },
378
- challengeToken: {
379
- type: 'String',
380
- },
381
- accessToken: {
382
- type: 'String',
383
- },
384
- refreshToken: {
385
- type: 'String',
386
- },
387
-
388
- srp: {
389
- type: 'Object',
390
- object: {
391
- name: 'SRPOutputSignInWith',
392
- fields: {
393
- salt: {
394
- type: 'String',
395
- },
396
- serverPublic: {
397
- type: 'String',
398
- },
399
- },
400
- },
401
- },
402
- },
403
- },
404
- args: {
405
- input: {
406
- authentication: authenticationInput,
407
- },
408
- },
409
- resolve: signInWithResolver,
410
- },
411
- signUpWith: {
412
- type: 'Object',
413
- outputObject: {
414
- name: 'SignUpWithOutput',
415
- fields: {
416
- id: { type: 'String' },
417
- accessToken: {
418
- type: 'String',
419
- required: true,
420
- },
421
- refreshToken: {
422
- type: 'String',
423
- required: true,
424
- },
425
- },
426
- },
427
- args: {
428
- input: {
429
- authentication: authenticationInput,
430
- },
431
- },
432
- resolve: signUpWithResolver,
433
- },
434
- signOut: {
435
- type: 'Boolean',
436
- resolve: signOutResolver,
437
- },
438
- refresh: {
439
- type: 'Object',
440
- args: {
441
- input: {
442
- accessToken: {
443
- type: 'String',
444
- required: true,
445
- },
446
- refreshToken: {
447
- type: 'String',
448
- required: true,
449
- },
450
- },
451
- },
452
- outputObject: {
453
- name: 'RefreshSessionOutput',
454
- fields: {
455
- accessToken: {
456
- type: 'String',
457
- required: true,
458
- },
459
- refreshToken: {
460
- type: 'String',
461
- required: true,
462
- },
463
- },
464
- },
465
- resolve: refreshResolver,
466
- },
467
- verifyChallenge: {
468
- type: 'Object',
469
- outputObject: {
470
- name: 'VerifyChallengeOutput',
471
- fields: {
472
- accessToken: {
473
- type: 'String',
474
- },
475
- srp: {
476
- type: 'Object',
477
- object: {
478
- name: 'SRPOutputVerifyChallenge',
479
- fields: {
480
- serverSessionProof: {
481
- type: 'String',
482
- required: true,
483
- },
484
- },
485
- },
486
- },
487
- },
488
- },
489
- args: {
490
- input: {
491
- challengeToken: {
492
- type: 'String',
493
- },
494
- secondFA: secondaryFactorAuthenticationInputObject,
495
- },
496
- },
497
- resolve: verifyChallengeResolver,
498
- },
499
- }
500
- : {}),
501
- },
502
- }
503
- }
504
-
505
- sessionClass(): ClassInterface<T> {
506
- return {
507
- name: '_Session',
508
- fields: {
509
- user: {
510
- type: 'Pointer',
511
- required: true,
512
- class: 'User',
513
- },
514
- accessTokenEncrypted: {
515
- type: 'String',
516
- required: true,
517
- },
518
- accessTokenExpiresAt: {
519
- type: 'Date',
520
- required: true,
521
- },
522
- refreshTokenEncrypted: {
523
- type: 'String',
524
- required: true,
525
- },
526
- refreshTokenExpiresAt: {
527
- type: 'Date',
528
- required: true,
529
- },
530
- },
531
- }
532
- }
533
-
534
- roleClass(): ClassInterface<T> {
535
- return {
536
- name: 'Role',
537
- fields: {
538
- name: {
539
- type: 'String',
540
- required: true,
541
- },
542
- users: {
543
- type: 'Relation',
544
- class: 'User',
545
- },
546
- },
547
- permissions: {
548
- create: {
549
- authorizedRoles: [],
550
- requireAuthentication: true,
551
- },
552
- read: {
553
- authorizedRoles: ['everyone'],
554
- requireAuthentication: true,
555
- },
556
- update: {
557
- authorizedRoles: [],
558
- requireAuthentication: true,
559
- },
560
- delete: {
561
- authorizedRoles: [],
562
- requireAuthentication: true,
563
- },
564
- },
565
- }
566
- }
567
-
568
- internalConfigClass(): ClassInterface<T> {
569
- return {
570
- name: '_InternalConfig',
571
- fields: {
572
- configKey: {
573
- type: 'String',
574
- required: true,
575
- },
576
- configValue: {
577
- type: 'String',
578
- required: true,
579
- },
580
- description: {
581
- type: 'String',
582
- },
583
- },
584
- // Only root key
585
- permissions: {
586
- create: {
587
- authorizedRoles: [],
588
- requireAuthentication: true,
589
- },
590
- read: {
591
- authorizedRoles: [],
592
- requireAuthentication: true,
593
- },
594
- update: {
595
- authorizedRoles: [],
596
- requireAuthentication: true,
597
- },
598
- delete: {
599
- authorizedRoles: [],
600
- requireAuthentication: true,
601
- },
602
- },
603
- }
604
- }
605
-
606
- userClass(): ClassInterface<T> {
607
- const customAuthenticationConfig = this.config.authentication?.customAuthenticationMethods || []
608
-
609
- const allAuthenticationDataToStoreObject = customAuthenticationConfig
610
- .filter(
611
- (authenticationMethod) =>
612
- authenticationMethod.dataToStore && !authenticationMethod.isSecondaryFactor,
613
- )
614
- .reduce((acc, authenticationMethod) => {
615
- if (authenticationMethod.dataToStore)
616
- acc[authenticationMethod.name] = {
617
- type: 'Object',
618
- object: {
619
- name: authenticationMethod.name,
620
- fields: authenticationMethod.dataToStore,
621
- },
622
- }
623
-
624
- return acc
625
- }, {} as SchemaFields<T>)
626
-
627
- const authenticationObject: TypeFieldObject<T> = {
628
- type: 'Object',
629
- object: {
630
- name: 'Authentication',
631
- fields: allAuthenticationDataToStoreObject,
632
- },
633
- }
634
-
635
- const fields: SchemaFields<T> = {
636
- ...(customAuthenticationConfig.length > 0 ? { authentication: authenticationObject } : {}),
637
- provider: {
638
- type: 'AuthenticationProvider',
639
- },
640
- isOauth: {
641
- type: 'Boolean',
642
- },
643
- email: {
644
- type: 'Email',
645
- },
646
- verifiedEmail: {
647
- type: 'Boolean',
648
- },
649
- role: {
650
- type: 'Pointer',
651
- class: 'Role',
652
- protected: {
653
- authorizedRoles: ['rootOnly'],
654
- protectedOperations: ['create', 'update'],
655
- },
656
- },
657
- sessions: {
658
- type: 'Relation',
659
- class: '_Session',
660
- protected: {
661
- authorizedRoles: ['rootOnly'],
662
- protectedOperations: ['create', 'update'],
663
- },
664
- },
665
- secondFA: {
666
- type: 'Object',
667
- object: {
668
- name: 'SecondFA',
669
- fields: {
670
- enabled: {
671
- type: 'Boolean',
672
- required: true,
673
- },
674
- provider: {
675
- type: 'SecondaryFactor',
676
- required: true,
677
- },
678
- },
679
- },
680
- },
681
- pendingChallenges: {
682
- type: 'Array',
683
- typeValue: 'Object',
684
- object: {
685
- name: 'PendingAuthenticationChallenge',
686
- fields: {
687
- token: {
688
- type: 'String',
689
- required: true,
690
- },
691
- provider: {
692
- type: 'String',
693
- required: true,
694
- },
695
- expiresAt: {
696
- type: 'Date',
697
- required: true,
698
- },
699
- },
700
- },
701
- protected: {
702
- authorizedRoles: ['rootOnly'],
703
- protectedOperations: ['create', 'read', 'update'],
704
- },
705
- },
706
- }
707
-
708
- return {
709
- name: 'User',
710
- fields,
711
- permissions: {
712
- delete: {
713
- authorizedRoles: [],
714
- requireAuthentication: true,
715
- },
716
- update: {
717
- authorizedRoles: [],
718
- requireAuthentication: true,
719
- },
720
- create: {
721
- requireAuthentication: false,
722
- },
723
- read: {
724
- authorizedRoles: [],
725
- requireAuthentication: true,
726
- },
727
- },
728
- }
729
- }
730
-
731
- defaultFields(): SchemaFields<T> {
732
- return {
733
- acl: {
734
- type: 'Object',
735
- object: {
736
- name: 'ACLObject',
737
- fields: {
738
- users: {
739
- type: 'Array',
740
- typeValue: 'Object',
741
- object: {
742
- name: 'UsersACL',
743
- fields: {
744
- userId: {
745
- type: 'String',
746
- required: true,
747
- },
748
- read: {
749
- type: 'Boolean',
750
- required: true,
751
- },
752
- write: {
753
- type: 'Boolean',
754
- required: true,
755
- },
756
- },
757
- },
758
- },
759
- roles: {
760
- type: 'Array',
761
- typeValue: 'Object',
762
- object: {
763
- name: 'RolesACL',
764
- fields: {
765
- roleId: {
766
- type: 'String',
767
- required: true,
768
- },
769
- read: {
770
- type: 'Boolean',
771
- required: true,
772
- },
773
- write: {
774
- type: 'Boolean',
775
- required: true,
776
- },
777
- },
778
- },
779
- },
780
- },
781
- },
782
- protected: {
783
- authorizedRoles: ['rootOnly'],
784
- protectedOperations: ['create', 'update'],
785
- },
786
- },
787
- createdAt: {
788
- type: 'Date',
789
- },
790
- updatedAt: {
791
- type: 'Date',
792
- },
793
- search: {
794
- type: 'Array',
795
- typeValue: 'String',
796
- },
797
- }
798
- }
799
-
800
- mergeClass(newClass: ClassInterface<T>[]): ClassInterface<T>[] {
801
- const allUniqueClassName = [...new Set(newClass.map((classItem) => classItem.name))]
802
-
803
- return allUniqueClassName.map((uniqueClass) => {
804
- const allClassWithSameName = newClass.filter((localClass) => localClass.name === uniqueClass)
805
-
806
- return allClassWithSameName.reduce((acc, classItem) => {
807
- return {
808
- ...acc,
809
- ...classItem,
810
- fields: {
811
- // We merge fields that have the same name and then we add the new fields
812
- ...acc.fields,
813
- ...classItem.fields,
814
- ...this.defaultFields(),
815
- },
816
- permissions:
817
- classItem.permissions || acc.permissions
818
- ? {
819
- // Order is important because we put the provided schema before so we always consider
820
- // the provided schema as the source of truth
821
- ...classItem.permissions,
822
- ...acc.permissions,
823
- }
824
- : undefined,
825
- }
826
- }, allClassWithSameName[0] as ClassInterface<T>)
827
- })
828
- }
829
-
830
- defaultClass(schema?: SchemaInterface<T>): ClassInterface<T>[] {
831
- return this.mergeClass([
832
- ...(schema?.classes || []),
833
- this.userClass(),
834
- this.sessionClass(),
835
- this.roleClass(),
836
- this.internalConfigClass(),
837
- ])
838
- }
839
- }