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
package/generated/wabe.ts DELETED
@@ -1,448 +0,0 @@
1
- export enum RoleEnum {
2
- DashboardAdmin = "DashboardAdmin",
3
- Admin = "Admin",
4
- Client = "Client"
5
- }
6
-
7
- export enum AuthenticationProvider {
8
- github = "github",
9
- google = "google",
10
- emailPassword = "emailPassword",
11
- phonePassword = "phonePassword"
12
- }
13
-
14
- export enum SecondaryFactor {
15
- emailOTP = "emailOTP",
16
- qrcodeOTP = "qrcodeOTP"
17
- }
18
-
19
- export type ACLObjectUsersACL = {
20
- userId: string,
21
- read: boolean,
22
- write: boolean
23
- }
24
-
25
- export type ACLObject = {
26
- users?: Array<ACLObjectUsersACL>,
27
- roles?: Array<ACLObjectRolesACL>
28
- }
29
-
30
- export type ACLObjectRolesACL = {
31
- roleId: string,
32
- read: boolean,
33
- write: boolean
34
- }
35
-
36
- export type AuthenticationEmailPasswordSRP = {
37
- email: string,
38
- salt: string,
39
- verifier: string,
40
- serverSecret?: string
41
- }
42
-
43
- export type Authentication = {
44
- emailPasswordSRP?: AuthenticationEmailPasswordSRP,
45
- phonePassword?: AuthenticationPhonePassword,
46
- emailPassword?: AuthenticationEmailPassword,
47
- google?: AuthenticationGoogle,
48
- github?: AuthenticationGithub
49
- }
50
-
51
- export type AuthenticationPhonePassword = {
52
- phone: string,
53
- password: string
54
- }
55
-
56
- export type AuthenticationEmailPassword = {
57
- email: string,
58
- password: string
59
- }
60
-
61
- export type AuthenticationGoogle = {
62
- email: string,
63
- verifiedEmail: boolean
64
- }
65
-
66
- export type AuthenticationGithub = {
67
- email: string,
68
- avatarUrl: string,
69
- username: string
70
- }
71
-
72
- export type SecondFA = {
73
- enabled: boolean,
74
- provider: SecondaryFactor
75
- }
76
-
77
- export type PendingAuthenticationChallenge = {
78
- token: string,
79
- provider: string,
80
- expiresAt: Date
81
- }
82
-
83
- export type User = {
84
- id: string,
85
- name?: string,
86
- age?: number,
87
- email?: string,
88
- acl?: ACLObject,
89
- createdAt?: string,
90
- updatedAt?: string,
91
- search?: Array<string>,
92
- authentication?: Authentication,
93
- provider?: AuthenticationProvider,
94
- isOauth?: boolean,
95
- verifiedEmail?: boolean,
96
- role?: Role,
97
- sessions?: Array<_Session>,
98
- secondFA?: SecondFA,
99
- pendingChallenges?: Array<PendingAuthenticationChallenge>
100
- }
101
-
102
- export type Experience = {
103
- jobTitle: string,
104
- companyName: string,
105
- startDate: string,
106
- endDate: string,
107
- achievements?: Array<string>
108
- }
109
-
110
- export type Post = {
111
- id: string,
112
- name: string,
113
- test2?: RoleEnum,
114
- test3: Array<User>,
115
- test4: User,
116
- experiences?: Array<Experience>,
117
- acl?: ACLObject,
118
- createdAt?: string,
119
- updatedAt?: string,
120
- search?: Array<string>
121
- }
122
-
123
- export type _Session = {
124
- id: string,
125
- user: User,
126
- accessTokenEncrypted: string,
127
- accessTokenExpiresAt: string,
128
- refreshTokenEncrypted: string,
129
- refreshTokenExpiresAt: string,
130
- acl?: ACLObject,
131
- createdAt?: string,
132
- updatedAt?: string,
133
- search?: Array<string>
134
- }
135
-
136
- export type Role = {
137
- id: string,
138
- name: string,
139
- users?: Array<User>,
140
- acl?: ACLObject,
141
- createdAt?: string,
142
- updatedAt?: string,
143
- search?: Array<string>
144
- }
145
-
146
- export type _InternalConfig = {
147
- id: string,
148
- configKey: string,
149
- configValue: string,
150
- description?: string,
151
- acl?: ACLObject,
152
- createdAt?: string,
153
- updatedAt?: string,
154
- search?: Array<string>
155
- }
156
-
157
- export type WhereUser = {
158
- id: string,
159
- name?: string,
160
- age?: number,
161
- email?: string,
162
- acl?: ACLObject,
163
- createdAt?: Date,
164
- updatedAt?: Date,
165
- search?: Array<string>,
166
- authentication?: Authentication,
167
- provider?: AuthenticationProvider,
168
- isOauth?: boolean,
169
- verifiedEmail?: boolean,
170
- role?: Role,
171
- sessions?: Array<_Session>,
172
- secondFA?: SecondFA,
173
- pendingChallenges?: Array<PendingAuthenticationChallenge>
174
- }
175
-
176
- export type WherePost = {
177
- id: string,
178
- name: string,
179
- test2?: RoleEnum,
180
- test3: Array<User>,
181
- test4: User,
182
- experiences?: Array<Experience>,
183
- acl?: ACLObject,
184
- createdAt?: Date,
185
- updatedAt?: Date,
186
- search?: Array<string>
187
- }
188
-
189
- export type Where_Session = {
190
- id: string,
191
- user: User,
192
- accessTokenEncrypted: string,
193
- accessTokenExpiresAt: Date,
194
- refreshTokenEncrypted: string,
195
- refreshTokenExpiresAt: Date,
196
- acl?: ACLObject,
197
- createdAt?: Date,
198
- updatedAt?: Date,
199
- search?: Array<string>
200
- }
201
-
202
- export type WhereRole = {
203
- id: string,
204
- name: string,
205
- users?: Array<User>,
206
- acl?: ACLObject,
207
- createdAt?: Date,
208
- updatedAt?: Date,
209
- search?: Array<string>
210
- }
211
-
212
- export type Where_InternalConfig = {
213
- id: string,
214
- configKey: string,
215
- configValue: string,
216
- description?: string,
217
- acl?: ACLObject,
218
- createdAt?: Date,
219
- updatedAt?: Date,
220
- search?: Array<string>
221
- }
222
-
223
- export type CreateMutationInput = {
224
- name: number
225
- }
226
-
227
- export type MutationCreateMutationArgs = {
228
- input: CreateMutationInput
229
- }
230
-
231
- export type CustomMutationInput = {
232
- a: number,
233
- b: number
234
- }
235
-
236
- export type MutationCustomMutationArgs = {
237
- input: CustomMutationInput
238
- }
239
-
240
- export type SecondCustomMutationInput = {
241
- sum?: SecondCustomMutationSum
242
- }
243
-
244
- export type MutationSecondCustomMutationArgs = {
245
- input: SecondCustomMutationInput
246
- }
247
-
248
- export type SecondCustomMutationSum = {
249
- a: number,
250
- b: number
251
- }
252
-
253
- export type ResetPasswordInput = {
254
- password: string,
255
- email?: string,
256
- phone?: string,
257
- otp: string
258
- }
259
-
260
- export type MutationResetPasswordArgs = {
261
- input: ResetPasswordInput
262
- }
263
-
264
- export type SendOtpCodeInput = {
265
- email: string
266
- }
267
-
268
- export type MutationSendOtpCodeArgs = {
269
- input: SendOtpCodeInput
270
- }
271
-
272
- export type SendEmailInput = {
273
- from: string,
274
- to: Array<string>,
275
- subject: string,
276
- text?: string,
277
- html?: string
278
- }
279
-
280
- export type MutationSendEmailArgs = {
281
- input: SendEmailInput
282
- }
283
-
284
- export type SignInWithInput = {
285
- authentication: SignInWithAuthentication
286
- }
287
-
288
- export type MutationSignInWithArgs = {
289
- input: SignInWithInput
290
- }
291
-
292
- export type SignInWithAuthenticationEmailPasswordSRP = {
293
- email: string,
294
- clientPublic?: string,
295
- salt?: string,
296
- verifier?: string
297
- }
298
-
299
- export type SignInWithAuthentication = {
300
- emailPasswordSRP?: SignInWithAuthenticationEmailPasswordSRP,
301
- phonePassword?: SignInWithAuthenticationPhonePassword,
302
- emailPassword?: SignInWithAuthenticationEmailPassword,
303
- google?: SignInWithAuthenticationGoogle,
304
- github?: SignInWithAuthenticationGithub
305
- }
306
-
307
- export type SignInWithAuthenticationPhonePassword = {
308
- phone: string,
309
- password: string
310
- }
311
-
312
- export type SignInWithAuthenticationEmailPassword = {
313
- email: string,
314
- password: string
315
- }
316
-
317
- export type SignInWithAuthenticationGoogle = {
318
- authorizationCode: string,
319
- codeVerifier: string
320
- }
321
-
322
- export type SignInWithAuthenticationGithub = {
323
- authorizationCode: string,
324
- codeVerifier: string
325
- }
326
-
327
- export type SignUpWithInput = {
328
- authentication: SignUpWithAuthentication
329
- }
330
-
331
- export type MutationSignUpWithArgs = {
332
- input: SignUpWithInput
333
- }
334
-
335
- export type SignUpWithAuthenticationEmailPasswordSRP = {
336
- email: string,
337
- clientPublic?: string,
338
- salt?: string,
339
- verifier?: string
340
- }
341
-
342
- export type SignUpWithAuthentication = {
343
- emailPasswordSRP?: SignUpWithAuthenticationEmailPasswordSRP,
344
- phonePassword?: SignUpWithAuthenticationPhonePassword,
345
- emailPassword?: SignUpWithAuthenticationEmailPassword,
346
- google?: SignUpWithAuthenticationGoogle,
347
- github?: SignUpWithAuthenticationGithub
348
- }
349
-
350
- export type SignUpWithAuthenticationPhonePassword = {
351
- phone: string,
352
- password: string
353
- }
354
-
355
- export type SignUpWithAuthenticationEmailPassword = {
356
- email: string,
357
- password: string
358
- }
359
-
360
- export type SignUpWithAuthenticationGoogle = {
361
- authorizationCode: string,
362
- codeVerifier: string
363
- }
364
-
365
- export type SignUpWithAuthenticationGithub = {
366
- authorizationCode: string,
367
- codeVerifier: string
368
- }
369
-
370
- export type SignOutInput = {
371
-
372
- }
373
-
374
- export type MutationSignOutArgs = {
375
- input: SignOutInput
376
- }
377
-
378
- export type RefreshInput = {
379
- accessToken: string,
380
- refreshToken: string
381
- }
382
-
383
- export type MutationRefreshArgs = {
384
- input: RefreshInput
385
- }
386
-
387
- export type VerifyChallengeInput = {
388
- challengeToken?: string,
389
- secondFA?: VerifyChallengeSecondFA
390
- }
391
-
392
- export type MutationVerifyChallengeArgs = {
393
- input: VerifyChallengeInput
394
- }
395
-
396
- export type VerifyChallengeSecondFAEmailPasswordSRPChallenge = {
397
- email: string,
398
- clientPublic: string,
399
- clientSessionProof: string
400
- }
401
-
402
- export type VerifyChallengeSecondFA = {
403
- emailPasswordSRPChallenge?: VerifyChallengeSecondFAEmailPasswordSRPChallenge,
404
- emailOTP?: VerifyChallengeSecondFAEmailOTP,
405
- qrCodeOTP?: VerifyChallengeSecondFAQrCodeOTP
406
- }
407
-
408
- export type VerifyChallengeSecondFAEmailOTP = {
409
- email: string,
410
- otp: string
411
- }
412
-
413
- export type VerifyChallengeSecondFAQrCodeOTP = {
414
- email: string,
415
- otp: string
416
- }
417
-
418
- export type QueryHelloWorldArgs = {
419
- name: string
420
- }
421
-
422
- export type QueryMeArgs = {
423
-
424
- }
425
-
426
- export type WabeSchemaScalars = ""
427
-
428
- export type WabeSchemaEnums = {
429
- RoleEnum: RoleEnum,
430
- AuthenticationProvider: AuthenticationProvider,
431
- SecondaryFactor: SecondaryFactor
432
- }
433
-
434
- export type WabeSchemaTypes = {
435
- User: User,
436
- Post: Post,
437
- _Session: _Session,
438
- Role: Role,
439
- _InternalConfig: _InternalConfig
440
- }
441
-
442
- export type WabeSchemaWhereTypes = {
443
- User: WhereUser,
444
- Post: WherePost,
445
- _Session: Where_Session,
446
- Role: WhereRole,
447
- _InternalConfig: Where_InternalConfig
448
- }
@@ -1,69 +0,0 @@
1
- import { describe, it, expect } from 'bun:test'
2
- import { OTP } from './OTP'
3
-
4
- describe('OTP', () => {
5
- it('should generate a valid OTP code', () => {
6
- const otp = new OTP('rootKey')
7
-
8
- const otpValue = otp.generate('userId')
9
-
10
- expect(otpValue.length).toBe(6)
11
- })
12
-
13
- it('should verify a valid OTP code', () => {
14
- const otp = new OTP('rootKey')
15
-
16
- const otpValue = otp.generate('userId')
17
-
18
- expect(otpValue.length).toBe(6)
19
-
20
- expect(otp.verify(otpValue, 'userId')).toBe(true)
21
- })
22
-
23
- it('should not verify an invalid OTP code', () => {
24
- const otp = new OTP('rootKey')
25
-
26
- const otpValue = otp.generate('userId')
27
-
28
- expect(otpValue.length).toBe(6)
29
-
30
- expect(otp.verify('invalidOtp', 'userId')).toBe(false)
31
-
32
- const otpValue2 = otp.generate('invalidUserId')
33
-
34
- expect(otpValue2.length).toBe(6)
35
-
36
- expect(otp.verify(otpValue2, 'userId')).toBe(false)
37
- })
38
-
39
- it('should not verify an invalid OTP code (more than 5 minutes)', () => {
40
- // Directly test the timeout is flaky we only test that the correct value is passed to totp
41
- const otp = new OTP('rootKey')
42
-
43
- expect(otp.internalTotp.options.window).toEqual([1, 0])
44
- })
45
-
46
- it('should generate a valid keyuri', () => {
47
- const otp = new OTP('rootKey')
48
-
49
- const keyuri = otp.generateKeyuri({
50
- userId: 'userId',
51
- emailOrUsername: 'email@test.fr',
52
- applicationName: 'Wabe',
53
- })
54
-
55
- expect(keyuri).toBe(
56
- 'otpauth://totp/Wabe:email%40test.fr?secret=O54OZDANWM2YFHJKJMMVMQSV7DUMUZFT3BWE4Z5NOQCAATGGHKYA&period=30&digits=6&algorithm=SHA1&issuer=Wabe',
57
- )
58
- })
59
-
60
- it('should verify an OTP generated from authenticator', () => {
61
- const otp = new OTP('rootKey')
62
-
63
- const code = otp.authenticatorGenerate('userId')
64
-
65
- const isValid = otp.authenticatorVerify(code, 'userId')
66
-
67
- expect(isValid).toBe(true)
68
- })
69
- })
@@ -1,64 +0,0 @@
1
- import { totp, authenticator } from 'otplib'
2
- import type { TOTP } from 'otplib/core'
3
- import { createHash } from 'node:crypto'
4
- import { base32Encode } from 'src/utils'
5
-
6
- const ONE_WINDOW = 1
7
-
8
- export class OTP {
9
- private secret: string
10
- public internalTotp: TOTP
11
-
12
- constructor(rootKey: string) {
13
- this.secret = rootKey
14
- this.internalTotp = totp.clone({
15
- window: [ONE_WINDOW, 0],
16
- })
17
- }
18
-
19
- deriveSecret(userId: string): string {
20
- const hash = createHash('sha256').update(`${this.secret}:${userId}`).digest()
21
-
22
- return base32Encode(hash, 'RFC4648', { padding: false })
23
- }
24
-
25
- generate(userId: string): string {
26
- const secret = this.deriveSecret(userId)
27
-
28
- return this.internalTotp.generate(secret)
29
- }
30
-
31
- verify(otp: string, userId: string): boolean {
32
- const secret = this.deriveSecret(userId)
33
-
34
- return this.internalTotp.verify({ secret, token: otp })
35
- }
36
-
37
- authenticatorGenerate(userId: string): string {
38
- const secret = this.deriveSecret(userId)
39
- return authenticator.generate(secret)
40
- }
41
-
42
- authenticatorVerify(otp: string, userId: string): boolean {
43
- const secret = this.deriveSecret(userId)
44
-
45
- return authenticator.verify({
46
- secret,
47
- token: otp,
48
- })
49
- }
50
-
51
- generateKeyuri({
52
- userId,
53
- emailOrUsername,
54
- applicationName,
55
- }: {
56
- userId: string
57
- emailOrUsername: string
58
- applicationName: string
59
- }): string {
60
- const secret = this.deriveSecret(userId)
61
-
62
- return authenticator.keyuri(emailOrUsername, applicationName, secret)
63
- }
64
- }