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,95 +0,0 @@
1
- import { contextWithRoot } from '../..'
2
- import { sendOtpCodeTemplate } from '../../email/templates/sendOtpCode'
3
- import type { DevWabeTypes } from '../../utils/helper'
4
- import type {
5
- OnSendChallengeOptions,
6
- OnVerifyChallengeOptions,
7
- SecondaryProviderInterface,
8
- } from '../interface'
9
- import { OTP } from '../OTP'
10
- import { clearRateLimit, isRateLimited, registerRateLimitFailure } from '../security'
11
-
12
- const DUMMY_USER_ID = '00000000-0000-0000-0000-000000000000'
13
-
14
- type EmailOTPInterface = {
15
- email: string
16
- otp: string
17
- }
18
-
19
- export class EmailOTP implements SecondaryProviderInterface<DevWabeTypes, EmailOTPInterface> {
20
- async onSendChallenge({ context, user }: OnSendChallengeOptions<DevWabeTypes>) {
21
- const emailController = context.wabe.controllers.email
22
-
23
- if (!emailController) throw new Error('Email controller not found')
24
-
25
- const mainEmail = context.wabe.config.email?.mainEmail
26
-
27
- if (!mainEmail) throw new Error('No main email found')
28
-
29
- if (!user.email) throw new Error('No user email found')
30
-
31
- const otpClass = new OTP(context.wabe.config.rootKey)
32
-
33
- const otp = otpClass.generate(user.id)
34
-
35
- const template = context.wabe.config.email?.htmlTemplates?.sendOTPCode
36
-
37
- await emailController.send({
38
- from: mainEmail,
39
- to: [user.email],
40
- subject: template?.subject || 'Your OTP code',
41
- html: template?.fn ? await template.fn({ otp }) : sendOtpCodeTemplate(otp),
42
- })
43
- }
44
-
45
- async onVerifyChallenge({
46
- context,
47
- input,
48
- }: OnVerifyChallengeOptions<DevWabeTypes, EmailOTPInterface>) {
49
- const normalizedEmail = input.email.trim().toLowerCase()
50
- const rateLimitKey = `emailOtp:${normalizedEmail}`
51
-
52
- if (isRateLimited(context, 'verifyChallenge', rateLimitKey)) return null
53
-
54
- const users = await context.wabe.controllers.database.getObjects({
55
- className: 'User',
56
- where: {
57
- email: {
58
- equalTo: input.email,
59
- },
60
- },
61
- select: {
62
- authentication: true,
63
- role: true,
64
- secondFA: true,
65
- email: true,
66
- id: true,
67
- provider: true,
68
- isOauth: true,
69
- createdAt: true,
70
- updatedAt: true,
71
- },
72
- first: 1,
73
- context: contextWithRoot(context),
74
- })
75
-
76
- const realUser = users.length > 0 ? users[0] : null
77
- const userId = realUser?.id ?? DUMMY_USER_ID
78
-
79
- const isDevBypass =
80
- !context.wabe.config.isProduction && input.otp === '000000' && realUser !== null
81
-
82
- const otpClass = new OTP(context.wabe.config.rootKey)
83
-
84
- const isOtpValid = otpClass.verify(input.otp, userId)
85
-
86
- if (realUser && (isOtpValid || isDevBypass)) {
87
- clearRateLimit(context, 'verifyChallenge', rateLimitKey)
88
- return { userId: realUser.id }
89
- }
90
-
91
- registerRateLimitFailure(context, 'verifyChallenge', rateLimitKey)
92
-
93
- return null
94
- }
95
- }
@@ -1,263 +0,0 @@
1
- import { describe, expect, it, mock, spyOn, afterEach, afterAll } from 'bun:test'
2
- import * as crypto from '../../utils/crypto'
3
-
4
- import { EmailPassword } from './EmailPassword'
5
-
6
- describe('Email password', () => {
7
- const mockGetObjects = mock(() => Promise.resolve([]))
8
- const mockCount = mock(() => Promise.resolve(0)) as any
9
- const mockCreateObject = mock(() => Promise.resolve({ id: 'userId' })) as any
10
-
11
- const spyArgonPasswordVerify = spyOn(crypto, 'verifyArgon2')
12
- const spyBunPasswordHash = spyOn(crypto, 'hashArgon2')
13
-
14
- const controllers = {
15
- controllers: {
16
- database: {
17
- getObjects: mockGetObjects,
18
- createObject: mockCreateObject,
19
- count: mockCount,
20
- },
21
- },
22
- } as any
23
-
24
- afterEach(() => {
25
- mockGetObjects.mockClear()
26
- mockCount.mockClear()
27
- mockCreateObject.mockClear()
28
- spyArgonPasswordVerify.mockClear()
29
- spyBunPasswordHash.mockClear()
30
- })
31
-
32
- afterAll(() => {
33
- spyArgonPasswordVerify.mockRestore()
34
- spyBunPasswordHash.mockRestore()
35
- })
36
-
37
- const emailPassword = new EmailPassword()
38
-
39
- it('should signUp with email password', async () => {
40
- spyBunPasswordHash.mockResolvedValueOnce('$argon2id$hashedPassword')
41
-
42
- const {
43
- authenticationDataToSave: { email },
44
- } = await emailPassword.onSignUp({
45
- context: { wabe: controllers } as any,
46
- input: { email: 'email@test.fr', password: 'password' },
47
- })
48
-
49
- expect(email).toBe('email@test.fr')
50
- })
51
-
52
- it('should signIn with email password', async () => {
53
- mockGetObjects.mockResolvedValue([
54
- {
55
- id: 'userId',
56
- authentication: {
57
- emailPassword: {
58
- email: 'email@test.fr',
59
- password: 'hashedPassword',
60
- },
61
- },
62
- } as never,
63
- ])
64
-
65
- spyArgonPasswordVerify.mockResolvedValueOnce(true)
66
-
67
- const { user } = await emailPassword.onSignIn({
68
- context: { wabe: controllers } as any,
69
- input: { email: 'email@test.fr', password: 'password' },
70
- })
71
-
72
- expect(user).toEqual({
73
- id: 'userId',
74
- authentication: {
75
- emailPassword: {
76
- email: 'email@test.fr',
77
- password: 'hashedPassword',
78
- },
79
- },
80
- })
81
-
82
- expect(spyArgonPasswordVerify).toHaveBeenCalledTimes(1)
83
- expect(spyArgonPasswordVerify).toHaveBeenCalledWith('password', 'hashedPassword')
84
- })
85
-
86
- it('should not signIn with email password if password is undefined', () => {
87
- spyArgonPasswordVerify.mockResolvedValueOnce(false)
88
-
89
- expect(
90
- emailPassword.onSignIn({
91
- context: { wabe: controllers } as any,
92
- // @ts-expect-error
93
- input: { email: 'email@test.fr' },
94
- }),
95
- ).rejects.toThrow('Invalid authentication credentials')
96
- })
97
-
98
- it('should not signIn with email password if there is no user found', () => {
99
- mockGetObjects.mockResolvedValue([])
100
-
101
- expect(
102
- emailPassword.onSignIn({
103
- context: { wabe: controllers } as any,
104
- input: {
105
- email: 'invalidEmail@test.fr',
106
- password: 'password',
107
- },
108
- }),
109
- ).rejects.toThrow('Invalid authentication credentials')
110
-
111
- expect(spyArgonPasswordVerify).toHaveBeenCalledTimes(1)
112
- })
113
-
114
- it('should not signIn with email password if there is email is invalid', () => {
115
- mockGetObjects.mockResolvedValue([
116
- {
117
- authentication: {
118
- emailPassword: {
119
- password: 'hashedPassword',
120
- },
121
- },
122
- } as never,
123
- ])
124
-
125
- spyArgonPasswordVerify.mockResolvedValueOnce(true)
126
-
127
- expect(
128
- emailPassword.onSignIn({
129
- context: { wabe: controllers } as any,
130
- input: {
131
- email: 'invalidEmail@test.fr',
132
- password: 'password',
133
- },
134
- }),
135
- ).rejects.toThrow('Invalid authentication credentials')
136
-
137
- expect(spyArgonPasswordVerify).toHaveBeenCalledTimes(1)
138
- })
139
-
140
- it('should rate limit signIn attempts in production', async () => {
141
- mockGetObjects.mockResolvedValue([])
142
-
143
- const context = {
144
- wabe: {
145
- ...controllers,
146
- config: {
147
- isProduction: true,
148
- authentication: {
149
- security: {
150
- signInRateLimit: {
151
- enabled: true,
152
- maxAttempts: 2,
153
- windowMs: 60_000,
154
- blockDurationMs: 60_000,
155
- },
156
- },
157
- },
158
- },
159
- },
160
- } as any
161
-
162
- const input = {
163
- email: 'ratelimit-email-password@test.fr',
164
- password: 'password',
165
- }
166
-
167
- await expect(emailPassword.onSignIn({ context, input })).rejects.toThrow(
168
- 'Invalid authentication credentials',
169
- )
170
- await expect(emailPassword.onSignIn({ context, input })).rejects.toThrow(
171
- 'Invalid authentication credentials',
172
- )
173
-
174
- const callsBeforeBlockedAttempt = mockGetObjects.mock.calls.length
175
-
176
- await expect(emailPassword.onSignIn({ context, input })).rejects.toThrow(
177
- 'Invalid authentication credentials',
178
- )
179
-
180
- expect(mockGetObjects.mock.calls.length).toBe(callsBeforeBlockedAttempt)
181
- })
182
-
183
- it('should rate limit signUp attempts in production', async () => {
184
- mockCount.mockResolvedValue(1)
185
-
186
- const context = {
187
- wabe: {
188
- ...controllers,
189
- config: {
190
- isProduction: true,
191
- authentication: {
192
- security: {
193
- signUpRateLimit: {
194
- enabled: true,
195
- maxAttempts: 2,
196
- windowMs: 60_000,
197
- blockDurationMs: 60_000,
198
- },
199
- },
200
- },
201
- },
202
- },
203
- } as any
204
-
205
- const input = {
206
- email: 'ratelimit-signup-email-password@test.fr',
207
- password: 'password',
208
- }
209
-
210
- await expect(emailPassword.onSignUp({ context, input })).rejects.toThrow(
211
- 'Not authorized to create user',
212
- )
213
- await expect(emailPassword.onSignUp({ context, input })).rejects.toThrow(
214
- 'Not authorized to create user',
215
- )
216
-
217
- const callsBeforeBlockedAttempt = mockCount.mock.calls.length
218
-
219
- await expect(emailPassword.onSignUp({ context, input })).rejects.toThrow(
220
- 'Not authorized to create user',
221
- )
222
-
223
- expect(mockCount.mock.calls.length).toBe(callsBeforeBlockedAttempt)
224
- })
225
-
226
- it('should not update authentication data if there is no user found', () => {
227
- mockGetObjects.mockResolvedValue([])
228
-
229
- spyArgonPasswordVerify.mockResolvedValueOnce(true)
230
-
231
- expect(
232
- emailPassword.onUpdateAuthenticationData?.({
233
- context: { wabe: controllers } as any,
234
- input: {
235
- email: 'email@test.fr',
236
- password: 'password',
237
- },
238
- userId: 'userId',
239
- }),
240
- ).rejects.toThrow('User not found')
241
- })
242
-
243
- it('should update authentication data if the userId match with an user', async () => {
244
- mockGetObjects.mockResolvedValue([
245
- {
246
- id: 'id',
247
- },
248
- ] as any)
249
-
250
- spyBunPasswordHash.mockResolvedValueOnce('$argon2id$hashedPassword')
251
-
252
- const res = await emailPassword.onUpdateAuthenticationData?.({
253
- context: { wabe: controllers } as any,
254
- input: {
255
- email: 'email@test.fr',
256
- password: 'password',
257
- },
258
- userId: 'userId',
259
- })
260
-
261
- expect(res.authenticationDataToSave.email).toBe('email@test.fr')
262
- })
263
- })
@@ -1,138 +0,0 @@
1
- import type {
2
- AuthenticationEventsOptions,
3
- AuthenticationEventsOptionsWithUserId,
4
- ProviderInterface,
5
- } from '../interface'
6
- import { contextWithRoot, verifyArgon2 } from '../../utils/export'
7
- import type { DevWabeTypes } from '../../utils/helper'
8
- import { clearRateLimit, isRateLimited, registerRateLimitFailure } from '../security'
9
-
10
- type EmailPasswordInterface = {
11
- password: string
12
- email: string
13
- otp?: string
14
- }
15
-
16
- const DUMMY_PASSWORD_HASH =
17
- '$argon2id$v=19$m=65536,t=2,p=1$wHZB9xRS/Mbo7L3SL9e935Ag5K+T2EuT/XgB8akwZgo$SPf8EZ4T1HYkuIll4v2hSzNCH7woX3VrZJo3yWg5u8U'
18
-
19
- export class EmailPassword implements ProviderInterface<DevWabeTypes, EmailPasswordInterface> {
20
- async onSignIn({
21
- input,
22
- context,
23
- }: AuthenticationEventsOptions<DevWabeTypes, EmailPasswordInterface>) {
24
- const normalizedEmail = input.email.trim().toLowerCase()
25
- const rateLimitKey = `emailPassword:${normalizedEmail}`
26
-
27
- if (isRateLimited(context, 'signIn', rateLimitKey))
28
- throw new Error('Invalid authentication credentials')
29
-
30
- const users = await context.wabe.controllers.database.getObjects({
31
- className: 'User',
32
- where: {
33
- authentication: {
34
- emailPassword: {
35
- email: { equalTo: input.email },
36
- },
37
- },
38
- },
39
- context: contextWithRoot(context),
40
- select: {
41
- authentication: true,
42
- role: true,
43
- secondFA: true,
44
- email: true,
45
- id: true,
46
- provider: true,
47
- isOauth: true,
48
- createdAt: true,
49
- updatedAt: true,
50
- },
51
- first: 1,
52
- })
53
-
54
- const user = users[0]
55
- const userDatabasePassword = user?.authentication?.emailPassword?.password
56
-
57
- const passwordHashToCheck = userDatabasePassword ?? DUMMY_PASSWORD_HASH
58
-
59
- const isPasswordEquals = await verifyArgon2(input.password, passwordHashToCheck)
60
-
61
- if (!user || !isPasswordEquals || input.email !== user.authentication?.emailPassword?.email) {
62
- registerRateLimitFailure(context, 'signIn', rateLimitKey)
63
- throw new Error('Invalid authentication credentials')
64
- }
65
-
66
- clearRateLimit(context, 'signIn', rateLimitKey)
67
-
68
- return {
69
- user,
70
- }
71
- }
72
-
73
- async onSignUp({
74
- input,
75
- context,
76
- }: AuthenticationEventsOptions<DevWabeTypes, EmailPasswordInterface>) {
77
- const normalizedEmail = input.email.trim().toLowerCase()
78
- const rateLimitKey = `emailPassword:${normalizedEmail}`
79
-
80
- if (isRateLimited(context, 'signUp', rateLimitKey))
81
- throw new Error('Not authorized to create user')
82
-
83
- const users = await context.wabe.controllers.database.count({
84
- className: 'User',
85
- where: {
86
- authentication: {
87
- emailPassword: {
88
- email: { equalTo: input.email },
89
- },
90
- },
91
- },
92
- context: contextWithRoot(context),
93
- })
94
-
95
- // Hide real message
96
- if (users > 0) {
97
- registerRateLimitFailure(context, 'signUp', rateLimitKey)
98
- throw new Error('Not authorized to create user')
99
- }
100
-
101
- clearRateLimit(context, 'signUp', rateLimitKey)
102
-
103
- return {
104
- authenticationDataToSave: {
105
- email: input.email,
106
- password: input.password,
107
- },
108
- }
109
- }
110
-
111
- async onUpdateAuthenticationData({
112
- userId,
113
- input,
114
- context,
115
- }: AuthenticationEventsOptionsWithUserId<DevWabeTypes, EmailPasswordInterface>) {
116
- const users = await context.wabe.controllers.database.getObjects({
117
- className: 'User',
118
- where: {
119
- id: {
120
- equalTo: userId,
121
- },
122
- },
123
- context,
124
- select: { authentication: true },
125
- })
126
-
127
- if (users.length === 0) throw new Error('User not found')
128
-
129
- const user = users[0]
130
-
131
- return {
132
- authenticationDataToSave: {
133
- email: input.email ?? user?.authentication?.emailPassword?.email,
134
- password: input.password ? input.password : user?.authentication?.emailPassword?.password,
135
- },
136
- }
137
- }
138
- }
@@ -1,208 +0,0 @@
1
- import { afterAll, beforeAll, describe, it, expect } from 'bun:test'
2
- import { createSRPClient } from 'js-srp6a'
3
- import type { Wabe } from '../../server'
4
- import { type DevWabeTypes, getAnonymousClient } from '../../utils/helper'
5
- import { setupTests, closeTests } from '../../utils/testHelper'
6
- import { gql } from 'graphql-request'
7
-
8
- describe('EmailPasswordSRP', () => {
9
- let wabe: Wabe<DevWabeTypes>
10
-
11
- beforeAll(async () => {
12
- const setup = await setupTests()
13
- wabe = setup.wabe
14
- })
15
-
16
- afterAll(async () => {
17
- await closeTests(wabe)
18
- })
19
-
20
- it('should authenticate an user with SRP', async () => {
21
- const anonymousClient = getAnonymousClient(wabe.config.port)
22
- const email = 'test@gmail.com'
23
- const password = 'password'
24
-
25
- const client = createSRPClient('SHA-256', 3072)
26
-
27
- // Sign up
28
- const salt = client.generateSalt()
29
- const privateKey = await client.deriveSafePrivateKey(salt, password)
30
- const verifier = client.deriveVerifier(privateKey)
31
-
32
- await anonymousClient.request<any>(
33
- gql`
34
- mutation signUpWith($input: SignUpWithInput!) {
35
- signUpWith(input: $input) {
36
- accessToken
37
- }
38
- }
39
- `,
40
- {
41
- input: {
42
- authentication: {
43
- emailPasswordSRP: {
44
- email,
45
- salt,
46
- verifier,
47
- },
48
- },
49
- },
50
- },
51
- )
52
-
53
- // Sign in
54
- const clientEphemeral = client.generateEphemeral()
55
-
56
- const { signInWith } = await anonymousClient.request<any>(
57
- gql`
58
- mutation signInWith($input: SignInWithInput!) {
59
- signInWith(input: $input) {
60
- srp {
61
- salt
62
- serverPublic
63
- }
64
- }
65
- }
66
- `,
67
- {
68
- input: {
69
- authentication: {
70
- emailPasswordSRP: {
71
- email,
72
- clientPublic: clientEphemeral.public,
73
- },
74
- },
75
- },
76
- },
77
- )
78
-
79
- const clientSession = await client.deriveSession(
80
- clientEphemeral.secret,
81
- signInWith.srp.serverPublic,
82
- salt,
83
- '', // Because we don't hash the username
84
- privateKey,
85
- )
86
-
87
- const { verifyChallenge } = await anonymousClient.request<any>(
88
- gql`
89
- mutation verifyChallenge($input: VerifyChallengeInput!) {
90
- verifyChallenge(input: $input) {
91
- srp {
92
- serverSessionProof
93
- }
94
- }
95
- }
96
- `,
97
- {
98
- input: {
99
- secondFA: {
100
- emailPasswordSRPChallenge: {
101
- email,
102
- clientPublic: clientEphemeral.public,
103
- clientSessionProof: clientSession.proof,
104
- },
105
- },
106
- },
107
- },
108
- )
109
-
110
- expect(
111
- client.verifySession(
112
- clientEphemeral.public,
113
- clientSession,
114
- verifyChallenge.srp.serverSessionProof,
115
- ),
116
- ).resolves.toBeUndefined()
117
- })
118
-
119
- it('should not authenticate with invalid password', async () => {
120
- const anonymousClient = getAnonymousClient(wabe.config.port)
121
- const email = 'invalid@test.com'
122
- const correctPassword = 'correct_password'
123
- const wrongPassword = 'wrong_password'
124
-
125
- const client = createSRPClient('SHA-256', 3072)
126
-
127
- const salt = client.generateSalt()
128
- const privateKey = await client.deriveSafePrivateKey(salt, correctPassword)
129
- const verifier = client.deriveVerifier(privateKey)
130
-
131
- await anonymousClient.request<any>(
132
- gql`
133
- mutation signUpWith($input: SignUpWithInput!) {
134
- signUpWith(input: $input) {
135
- accessToken
136
- }
137
- }
138
- `,
139
- {
140
- input: {
141
- authentication: {
142
- emailPasswordSRP: { email, salt, verifier },
143
- },
144
- },
145
- },
146
- )
147
-
148
- const clientEphemeral = client.generateEphemeral()
149
-
150
- const { signInWith } = await anonymousClient.request<any>(
151
- gql`
152
- mutation signInWith($input: SignInWithInput!) {
153
- signInWith(input: $input) {
154
- srp {
155
- salt
156
- serverPublic
157
- }
158
- }
159
- }
160
- `,
161
- {
162
- input: {
163
- authentication: {
164
- emailPasswordSRP: {
165
- email,
166
- clientPublic: clientEphemeral.public,
167
- },
168
- },
169
- },
170
- },
171
- )
172
-
173
- // Derive with wrong password
174
- const wrongPrivateKey = await client.deriveSafePrivateKey(salt, wrongPassword)
175
- const wrongClientSession = await client.deriveSession(
176
- clientEphemeral.secret,
177
- signInWith.srp.serverPublic,
178
- salt,
179
- '',
180
- wrongPrivateKey,
181
- )
182
-
183
- expect(
184
- anonymousClient.request<any>(
185
- gql`
186
- mutation verifyChallenge($input: VerifyChallengeInput!) {
187
- verifyChallenge(input: $input) {
188
- srp {
189
- serverSessionProof
190
- }
191
- }
192
- }
193
- `,
194
- {
195
- input: {
196
- secondFA: {
197
- emailPasswordSRPChallenge: {
198
- email,
199
- clientPublic: clientEphemeral.public,
200
- clientSessionProof: wrongClientSession.proof,
201
- },
202
- },
203
- },
204
- },
205
- ),
206
- ).rejects.toThrow('Invalid authentication credentials')
207
- })
208
- })