wabe 0.6.9 → 0.6.11

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 (162) hide show
  1. package/README.md +156 -50
  2. package/bucket/b.txt +1 -0
  3. package/dev/index.ts +215 -0
  4. package/dist/authentication/Session.d.ts +4 -1
  5. package/dist/authentication/interface.d.ts +16 -0
  6. package/dist/cron/index.d.ts +0 -1
  7. package/dist/database/DatabaseController.d.ts +41 -13
  8. package/dist/database/interface.d.ts +1 -0
  9. package/dist/email/DevAdapter.d.ts +0 -1
  10. package/dist/email/interface.d.ts +1 -1
  11. package/dist/graphql/resolvers.d.ts +4 -2
  12. package/dist/hooks/index.d.ts +8 -2
  13. package/dist/index.d.ts +0 -1
  14. package/dist/index.js +32144 -32058
  15. package/dist/schema/Schema.d.ts +2 -1
  16. package/dist/server/index.d.ts +4 -2
  17. package/dist/utils/crypto.d.ts +7 -0
  18. package/dist/utils/helper.d.ts +5 -1
  19. package/generated/schema.graphql +22 -14
  20. package/generated/wabe.ts +4 -4
  21. package/package.json +23 -23
  22. package/src/authentication/OTP.test.ts +69 -0
  23. package/src/authentication/OTP.ts +64 -0
  24. package/src/authentication/Session.test.ts +629 -0
  25. package/src/authentication/Session.ts +493 -0
  26. package/src/authentication/defaultAuthentication.ts +209 -0
  27. package/src/authentication/index.ts +3 -0
  28. package/src/authentication/interface.ts +155 -0
  29. package/src/authentication/oauth/GitHub.test.ts +91 -0
  30. package/src/authentication/oauth/GitHub.ts +121 -0
  31. package/src/authentication/oauth/Google.test.ts +91 -0
  32. package/src/authentication/oauth/Google.ts +101 -0
  33. package/src/authentication/oauth/Oauth2Client.test.ts +219 -0
  34. package/src/authentication/oauth/Oauth2Client.ts +135 -0
  35. package/src/authentication/oauth/index.ts +2 -0
  36. package/src/authentication/oauth/utils.test.ts +33 -0
  37. package/src/authentication/oauth/utils.ts +27 -0
  38. package/src/authentication/providers/EmailOTP.test.ts +127 -0
  39. package/src/authentication/providers/EmailOTP.ts +84 -0
  40. package/src/authentication/providers/EmailPassword.test.ts +176 -0
  41. package/src/authentication/providers/EmailPassword.ts +116 -0
  42. package/src/authentication/providers/EmailPasswordSRP.test.ts +208 -0
  43. package/src/authentication/providers/EmailPasswordSRP.ts +179 -0
  44. package/src/authentication/providers/GitHub.ts +24 -0
  45. package/src/authentication/providers/Google.ts +24 -0
  46. package/src/authentication/providers/OAuth.test.ts +185 -0
  47. package/src/authentication/providers/OAuth.ts +106 -0
  48. package/src/authentication/providers/PhonePassword.test.ts +176 -0
  49. package/src/authentication/providers/PhonePassword.ts +115 -0
  50. package/src/authentication/providers/QRCodeOTP.test.ts +77 -0
  51. package/src/authentication/providers/QRCodeOTP.ts +58 -0
  52. package/src/authentication/providers/index.ts +6 -0
  53. package/src/authentication/resolvers/refreshResolver.test.ts +30 -0
  54. package/src/authentication/resolvers/refreshResolver.ts +19 -0
  55. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +59 -0
  56. package/src/authentication/resolvers/signInWithResolver.test.ts +293 -0
  57. package/src/authentication/resolvers/signInWithResolver.ts +92 -0
  58. package/src/authentication/resolvers/signOutResolver.test.ts +38 -0
  59. package/src/authentication/resolvers/signOutResolver.ts +18 -0
  60. package/src/authentication/resolvers/signUpWithResolver.test.ts +180 -0
  61. package/src/authentication/resolvers/signUpWithResolver.ts +65 -0
  62. package/src/authentication/resolvers/verifyChallenge.test.ts +133 -0
  63. package/src/authentication/resolvers/verifyChallenge.ts +62 -0
  64. package/src/authentication/roles.test.ts +49 -0
  65. package/src/authentication/roles.ts +40 -0
  66. package/src/authentication/utils.test.ts +97 -0
  67. package/src/authentication/utils.ts +39 -0
  68. package/src/cache/InMemoryCache.test.ts +62 -0
  69. package/src/cache/InMemoryCache.ts +45 -0
  70. package/src/cron/index.test.ts +17 -0
  71. package/src/cron/index.ts +43 -0
  72. package/src/database/DatabaseController.test.ts +613 -0
  73. package/src/database/DatabaseController.ts +1007 -0
  74. package/src/database/index.test.ts +1372 -0
  75. package/src/database/index.ts +9 -0
  76. package/src/database/interface.ts +302 -0
  77. package/src/email/DevAdapter.ts +7 -0
  78. package/src/email/EmailController.test.ts +29 -0
  79. package/src/email/EmailController.ts +13 -0
  80. package/src/email/index.ts +2 -0
  81. package/src/email/interface.ts +36 -0
  82. package/src/email/templates/sendOtpCode.ts +120 -0
  83. package/src/file/FileController.ts +28 -0
  84. package/src/file/FileDevAdapter.ts +51 -0
  85. package/src/file/hookDeleteFile.ts +25 -0
  86. package/src/file/hookReadFile.ts +66 -0
  87. package/src/file/hookUploadFile.ts +50 -0
  88. package/src/file/index.test.ts +932 -0
  89. package/src/file/index.ts +2 -0
  90. package/src/file/interface.ts +39 -0
  91. package/src/graphql/GraphQLSchema.test.ts +4408 -0
  92. package/src/graphql/GraphQLSchema.ts +880 -0
  93. package/src/graphql/index.ts +2 -0
  94. package/src/graphql/parseGraphqlSchema.ts +85 -0
  95. package/src/graphql/parser.test.ts +203 -0
  96. package/src/graphql/parser.ts +542 -0
  97. package/src/graphql/pointerAndRelationFunction.ts +191 -0
  98. package/src/graphql/resolvers.ts +442 -0
  99. package/src/graphql/tests/aggregation.test.ts +1115 -0
  100. package/src/graphql/tests/e2e.test.ts +590 -0
  101. package/src/graphql/tests/scalars.test.ts +250 -0
  102. package/src/graphql/types.ts +227 -0
  103. package/src/hooks/HookObject.test.ts +122 -0
  104. package/src/hooks/HookObject.ts +165 -0
  105. package/src/hooks/authentication.ts +67 -0
  106. package/src/hooks/createUser.test.ts +77 -0
  107. package/src/hooks/createUser.ts +10 -0
  108. package/src/hooks/defaultFields.test.ts +176 -0
  109. package/src/hooks/defaultFields.ts +32 -0
  110. package/src/hooks/deleteSession.test.ts +181 -0
  111. package/src/hooks/deleteSession.ts +20 -0
  112. package/src/hooks/hashFieldHook.test.ts +152 -0
  113. package/src/hooks/hashFieldHook.ts +89 -0
  114. package/src/hooks/index.test.ts +258 -0
  115. package/src/hooks/index.ts +414 -0
  116. package/src/hooks/permissions.test.ts +412 -0
  117. package/src/hooks/permissions.ts +93 -0
  118. package/src/hooks/protected.test.ts +551 -0
  119. package/src/hooks/protected.ts +60 -0
  120. package/src/hooks/searchableFields.test.ts +147 -0
  121. package/src/hooks/searchableFields.ts +86 -0
  122. package/src/hooks/session.test.ts +134 -0
  123. package/src/hooks/session.ts +76 -0
  124. package/src/hooks/setEmail.test.ts +216 -0
  125. package/src/hooks/setEmail.ts +33 -0
  126. package/src/hooks/setupAcl.test.ts +618 -0
  127. package/src/hooks/setupAcl.ts +25 -0
  128. package/src/index.ts +9 -0
  129. package/src/schema/Schema.test.ts +482 -0
  130. package/src/schema/Schema.ts +757 -0
  131. package/src/schema/defaultResolvers.ts +93 -0
  132. package/src/schema/index.ts +1 -0
  133. package/src/schema/resolvers/meResolver.test.ts +62 -0
  134. package/src/schema/resolvers/meResolver.ts +10 -0
  135. package/src/schema/resolvers/resetPassword.test.ts +341 -0
  136. package/src/schema/resolvers/resetPassword.ts +63 -0
  137. package/src/schema/resolvers/sendEmail.test.ts +118 -0
  138. package/src/schema/resolvers/sendEmail.ts +21 -0
  139. package/src/schema/resolvers/sendOtpCode.test.ts +141 -0
  140. package/src/schema/resolvers/sendOtpCode.ts +52 -0
  141. package/src/security.test.ts +3434 -0
  142. package/src/server/defaultSessionHandler.test.ts +62 -0
  143. package/src/server/defaultSessionHandler.ts +105 -0
  144. package/src/server/generateCodegen.ts +433 -0
  145. package/src/server/index.test.ts +532 -0
  146. package/src/server/index.ts +334 -0
  147. package/src/server/interface.ts +11 -0
  148. package/src/server/routes/authHandler.ts +169 -0
  149. package/src/server/routes/index.ts +39 -0
  150. package/src/utils/crypto.test.ts +41 -0
  151. package/src/utils/crypto.ts +105 -0
  152. package/src/utils/export.ts +11 -0
  153. package/src/utils/helper.ts +204 -0
  154. package/src/utils/index.test.ts +11 -0
  155. package/src/utils/index.ts +189 -0
  156. package/src/utils/preload.ts +8 -0
  157. package/src/utils/testHelper.ts +116 -0
  158. package/tsconfig.json +32 -0
  159. package/bunfig.toml +0 -4
  160. package/dist/ai/index.d.ts +0 -1
  161. package/dist/ai/interface.d.ts +0 -9
  162. /package/dist/server/{defaultHandlers.d.ts → defaultSessionHandler.d.ts} +0 -0
@@ -0,0 +1,176 @@
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
+ mockCreateObject.mockClear()
27
+ spyArgonPasswordVerify.mockClear()
28
+ spyBunPasswordHash.mockClear()
29
+ })
30
+
31
+ afterAll(() => {
32
+ spyArgonPasswordVerify.mockRestore()
33
+ spyBunPasswordHash.mockRestore()
34
+ })
35
+
36
+ const emailPassword = new EmailPassword()
37
+
38
+ it('should signUp with email password', async () => {
39
+ spyBunPasswordHash.mockResolvedValueOnce('$argon2id$hashedPassword')
40
+
41
+ const {
42
+ authenticationDataToSave: { email },
43
+ } = await emailPassword.onSignUp({
44
+ context: { wabe: controllers } as any,
45
+ input: { email: 'email@test.fr', password: 'password' },
46
+ })
47
+
48
+ expect(email).toBe('email@test.fr')
49
+ })
50
+
51
+ it('should signIn with email password', async () => {
52
+ mockGetObjects.mockResolvedValue([
53
+ {
54
+ id: 'userId',
55
+ authentication: {
56
+ emailPassword: {
57
+ email: 'email@test.fr',
58
+ password: 'hashedPassword',
59
+ },
60
+ },
61
+ } as never,
62
+ ])
63
+
64
+ spyArgonPasswordVerify.mockResolvedValueOnce(true)
65
+
66
+ const { user } = await emailPassword.onSignIn({
67
+ context: { wabe: controllers } as any,
68
+ input: { email: 'email@test.fr', password: 'password' },
69
+ })
70
+
71
+ expect(user).toEqual({
72
+ id: 'userId',
73
+ authentication: {
74
+ emailPassword: {
75
+ email: 'email@test.fr',
76
+ password: 'hashedPassword',
77
+ },
78
+ },
79
+ })
80
+
81
+ expect(spyArgonPasswordVerify).toHaveBeenCalledTimes(1)
82
+ expect(spyArgonPasswordVerify).toHaveBeenCalledWith('password', 'hashedPassword')
83
+ })
84
+
85
+ it('should not signIn with email password if password is undefined', () => {
86
+ spyArgonPasswordVerify.mockResolvedValueOnce(false)
87
+
88
+ expect(
89
+ emailPassword.onSignIn({
90
+ context: { wabe: controllers } as any,
91
+ // @ts-expect-error
92
+ input: { email: 'email@test.fr' },
93
+ }),
94
+ ).rejects.toThrow('Invalid authentication credentials')
95
+ })
96
+
97
+ it('should not signIn with email password if there is no user found', () => {
98
+ mockGetObjects.mockResolvedValue([])
99
+
100
+ expect(
101
+ emailPassword.onSignIn({
102
+ context: { wabe: controllers } as any,
103
+ input: {
104
+ email: 'invalidEmail@test.fr',
105
+ password: 'password',
106
+ },
107
+ }),
108
+ ).rejects.toThrow('Invalid authentication credentials')
109
+
110
+ expect(spyArgonPasswordVerify).toHaveBeenCalledTimes(1)
111
+ })
112
+
113
+ it('should not signIn with email password if there is email is invalid', () => {
114
+ mockGetObjects.mockResolvedValue([
115
+ {
116
+ authentication: {
117
+ emailPassword: {
118
+ password: 'hashedPassword',
119
+ },
120
+ },
121
+ } as never,
122
+ ])
123
+
124
+ spyArgonPasswordVerify.mockResolvedValueOnce(true)
125
+
126
+ expect(
127
+ emailPassword.onSignIn({
128
+ context: { wabe: controllers } as any,
129
+ input: {
130
+ email: 'invalidEmail@test.fr',
131
+ password: 'password',
132
+ },
133
+ }),
134
+ ).rejects.toThrow('Invalid authentication credentials')
135
+
136
+ expect(spyArgonPasswordVerify).toHaveBeenCalledTimes(1)
137
+ })
138
+
139
+ it('should not update authentication data if there is no user found', () => {
140
+ mockGetObjects.mockResolvedValue([])
141
+
142
+ spyArgonPasswordVerify.mockResolvedValueOnce(true)
143
+
144
+ expect(
145
+ emailPassword.onUpdateAuthenticationData?.({
146
+ context: { wabe: controllers } as any,
147
+ input: {
148
+ email: 'email@test.fr',
149
+ password: 'password',
150
+ },
151
+ userId: 'userId',
152
+ }),
153
+ ).rejects.toThrow('User not found')
154
+ })
155
+
156
+ it('should update authentication data if the userId match with an user', async () => {
157
+ mockGetObjects.mockResolvedValue([
158
+ {
159
+ id: 'id',
160
+ },
161
+ ] as any)
162
+
163
+ spyBunPasswordHash.mockResolvedValueOnce('$argon2id$hashedPassword')
164
+
165
+ const res = await emailPassword.onUpdateAuthenticationData?.({
166
+ context: { wabe: controllers } as any,
167
+ input: {
168
+ email: 'email@test.fr',
169
+ password: 'password',
170
+ },
171
+ userId: 'userId',
172
+ })
173
+
174
+ expect(res.authenticationDataToSave.email).toBe('email@test.fr')
175
+ })
176
+ })
@@ -0,0 +1,116 @@
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
+
9
+ type EmailPasswordInterface = {
10
+ password: string
11
+ email: string
12
+ otp?: string
13
+ }
14
+
15
+ const DUMMY_PASSWORD_HASH =
16
+ '$argon2id$v=19$m=65536,t=2,p=1$wHZB9xRS/Mbo7L3SL9e935Ag5K+T2EuT/XgB8akwZgo$SPf8EZ4T1HYkuIll4v2hSzNCH7woX3VrZJo3yWg5u8U'
17
+
18
+ export class EmailPassword implements ProviderInterface<DevWabeTypes, EmailPasswordInterface> {
19
+ async onSignIn({
20
+ input,
21
+ context,
22
+ }: AuthenticationEventsOptions<DevWabeTypes, EmailPasswordInterface>) {
23
+ const users = await context.wabe.controllers.database.getObjects({
24
+ className: 'User',
25
+ where: {
26
+ authentication: {
27
+ emailPassword: {
28
+ email: { equalTo: input.email },
29
+ },
30
+ },
31
+ },
32
+ context: contextWithRoot(context),
33
+ select: {
34
+ authentication: true,
35
+ role: true,
36
+ secondFA: true,
37
+ email: true,
38
+ id: true,
39
+ provider: true,
40
+ isOauth: true,
41
+ createdAt: true,
42
+ updatedAt: true,
43
+ },
44
+ first: 1,
45
+ })
46
+
47
+ const user = users[0]
48
+ const userDatabasePassword = user?.authentication?.emailPassword?.password
49
+
50
+ const passwordHashToCheck = userDatabasePassword ?? DUMMY_PASSWORD_HASH
51
+
52
+ const isPasswordEquals = await verifyArgon2(input.password, passwordHashToCheck)
53
+
54
+ if (!user || !isPasswordEquals || input.email !== user.authentication?.emailPassword?.email)
55
+ throw new Error('Invalid authentication credentials')
56
+
57
+ return {
58
+ user,
59
+ }
60
+ }
61
+
62
+ async onSignUp({
63
+ input,
64
+ context,
65
+ }: AuthenticationEventsOptions<DevWabeTypes, EmailPasswordInterface>) {
66
+ const users = await context.wabe.controllers.database.count({
67
+ className: 'User',
68
+ where: {
69
+ authentication: {
70
+ emailPassword: {
71
+ email: { equalTo: input.email },
72
+ },
73
+ },
74
+ },
75
+ context: contextWithRoot(context),
76
+ })
77
+
78
+ // Hide real message
79
+ if (users > 0) throw new Error('Not authorized to create user')
80
+
81
+ return {
82
+ authenticationDataToSave: {
83
+ email: input.email,
84
+ password: input.password,
85
+ },
86
+ }
87
+ }
88
+
89
+ async onUpdateAuthenticationData({
90
+ userId,
91
+ input,
92
+ context,
93
+ }: AuthenticationEventsOptionsWithUserId<DevWabeTypes, EmailPasswordInterface>) {
94
+ const users = await context.wabe.controllers.database.getObjects({
95
+ className: 'User',
96
+ where: {
97
+ id: {
98
+ equalTo: userId,
99
+ },
100
+ },
101
+ context,
102
+ select: { authentication: true },
103
+ })
104
+
105
+ if (users.length === 0) throw new Error('User not found')
106
+
107
+ const user = users[0]
108
+
109
+ return {
110
+ authenticationDataToSave: {
111
+ email: input.email ?? user?.authentication?.emailPassword?.email,
112
+ password: input.password ? input.password : user?.authentication?.emailPassword?.password,
113
+ },
114
+ }
115
+ }
116
+ }
@@ -0,0 +1,208 @@
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
+ })
@@ -0,0 +1,179 @@
1
+ import type {
2
+ AuthenticationEventsOptions,
3
+ OnVerifyChallengeOptions,
4
+ ProviderInterface,
5
+ SecondaryProviderInterface,
6
+ } from '../interface'
7
+ import { contextWithRoot } from '../../utils/export'
8
+ import type { DevWabeTypes } from '../../utils/helper'
9
+ import { createSRPServer, type Ephemeral, type Session } from 'js-srp6a'
10
+
11
+ // 🛡 Valeurs factices pour mitigation des timing attacks
12
+ const DUMMY_SALT = 'deadbeefdeadbeefdeadbeefdeadbeef'
13
+ const DUMMY_VERIFIER =
14
+ '94c8f9b69f44fa0453a8a65129a7865ea2d70b21e645cf185d6fd42a679e524c394d4f02bba2032b10517be8c80f0f58e94302cb57cce7ce1e0a21906b6d22020b84a473d8ef58ea1f53e5204f8b83f05dc334b781fda309ad7cb8fa5c91dc81f64c114b671688b22e0f693a9c97ad2f43e6f1954c83d73e81e3dc8a963b7cbce'
15
+
16
+ type EmailPasswordSRPInterface = {
17
+ clientPublic: string
18
+ email: string
19
+ salt?: string
20
+ verifier?: string
21
+ }
22
+
23
+ export class EmailPasswordSRP implements ProviderInterface<
24
+ DevWabeTypes,
25
+ EmailPasswordSRPInterface
26
+ > {
27
+ async onSignIn({
28
+ input,
29
+ context,
30
+ }: AuthenticationEventsOptions<DevWabeTypes, EmailPasswordSRPInterface>) {
31
+ const server = createSRPServer('SHA-256', 3072)
32
+
33
+ const users = await context.wabe.controllers.database.getObjects({
34
+ className: 'User',
35
+ context: contextWithRoot(context),
36
+ where: {
37
+ email: { equalTo: input.email },
38
+ },
39
+ select: {
40
+ authentication: true,
41
+ role: true,
42
+ secondFA: true,
43
+ email: true,
44
+ id: true,
45
+ provider: true,
46
+ isOauth: true,
47
+ createdAt: true,
48
+ updatedAt: true,
49
+ },
50
+ first: 1,
51
+ })
52
+
53
+ const user = users[0]
54
+
55
+ const salt = user?.authentication?.emailPasswordSRP?.salt ?? DUMMY_SALT
56
+ const verifier = user?.authentication?.emailPasswordSRP?.verifier ?? DUMMY_VERIFIER
57
+
58
+ let ephemeral: Ephemeral
59
+ try {
60
+ ephemeral = await server.generateEphemeral(verifier)
61
+ } catch {
62
+ throw new Error('Invalid authentication credentials')
63
+ }
64
+
65
+ if (!user || !user?.id) {
66
+ // Simulation d'opération pour garder le même temps
67
+ await new Promise((resolve) => setTimeout(resolve, 10))
68
+ throw new Error('Invalid authentication credentials')
69
+ }
70
+
71
+ await context.wabe.controllers.database.updateObject({
72
+ className: 'User',
73
+ context: contextWithRoot(context),
74
+ id: user.id,
75
+ data: {
76
+ authentication: {
77
+ emailPasswordSRP: {
78
+ ...user.authentication?.emailPasswordSRP,
79
+ serverSecret: ephemeral.secret,
80
+ },
81
+ },
82
+ },
83
+ select: {},
84
+ })
85
+
86
+ return { srp: { salt, serverPublic: ephemeral.public }, user }
87
+ }
88
+
89
+ async onSignUp({
90
+ input,
91
+ context,
92
+ }: AuthenticationEventsOptions<DevWabeTypes, EmailPasswordSRPInterface>) {
93
+ const users = await context.wabe.controllers.database.count({
94
+ className: 'User',
95
+ where: {
96
+ email: { equalTo: input.email },
97
+ },
98
+ context: contextWithRoot(context),
99
+ })
100
+
101
+ if (users > 0) throw new Error('Not authorized to create user')
102
+
103
+ return {
104
+ authenticationDataToSave: {
105
+ salt: input.salt,
106
+ verifier: input.verifier,
107
+ email: input.email,
108
+ serverSecret: null,
109
+ },
110
+ }
111
+ }
112
+ }
113
+
114
+ export interface EmailPasswordSRPChallengeInterface {
115
+ email: string
116
+ clientPublic: string
117
+ clientSessionProof: string
118
+ }
119
+
120
+ export class EmailPasswordSRPChallenge implements SecondaryProviderInterface<
121
+ DevWabeTypes,
122
+ EmailPasswordSRPChallengeInterface
123
+ > {
124
+ async onVerifyChallenge({
125
+ context,
126
+ input,
127
+ }: OnVerifyChallengeOptions<DevWabeTypes, EmailPasswordSRPChallengeInterface>) {
128
+ const server = createSRPServer('SHA-256', 3072)
129
+
130
+ const users = await context.wabe.controllers.database.getObjects({
131
+ className: 'User',
132
+ context: contextWithRoot(context),
133
+ where: {
134
+ authentication: {
135
+ emailPasswordSRP: {
136
+ email: { equalTo: input.email },
137
+ },
138
+ },
139
+ },
140
+ select: {
141
+ id: true,
142
+ authentication: true,
143
+ },
144
+ })
145
+
146
+ const user = users[0]
147
+
148
+ const salt = user?.authentication?.emailPasswordSRP?.salt ?? DUMMY_SALT
149
+ const verifier = user?.authentication?.emailPasswordSRP?.verifier ?? DUMMY_VERIFIER
150
+ const serverSecret = user?.authentication?.emailPasswordSRP?.serverSecret ?? 'deadbeef'
151
+
152
+ let serverSession: Session
153
+ try {
154
+ serverSession = await server.deriveSession(
155
+ serverSecret,
156
+ input.clientPublic,
157
+ salt,
158
+ '', // no username
159
+ verifier,
160
+ input.clientSessionProof,
161
+ )
162
+ } catch {
163
+ throw new Error('Invalid authentication credentials')
164
+ }
165
+
166
+ if (!user || !user?.id) {
167
+ // Simulation pour garder un timing constant
168
+ await new Promise((resolve) => setTimeout(resolve, 10))
169
+ throw new Error('Invalid authentication credentials')
170
+ }
171
+
172
+ return {
173
+ userId: user.id,
174
+ srp: {
175
+ serverSessionProof: serverSession.proof,
176
+ },
177
+ }
178
+ }
179
+ }
@@ -0,0 +1,24 @@
1
+ import type { DevWabeTypes } from '../../utils/helper'
2
+ import {
3
+ AuthenticationProvider,
4
+ type AuthenticationEventsOptions,
5
+ type ProviderInterface,
6
+ } from '../interface'
7
+ import { oAuthAuthentication } from './OAuth'
8
+
9
+ type GitHubInterface = {
10
+ authorizationCode: string
11
+ codeVerifier: string
12
+ }
13
+
14
+ export class GitHub implements ProviderInterface<DevWabeTypes, GitHubInterface> {
15
+ name = 'github'
16
+ onSignIn(options: AuthenticationEventsOptions<DevWabeTypes, GitHubInterface>) {
17
+ return oAuthAuthentication(AuthenticationProvider.GitHub)(options)
18
+ }
19
+
20
+ // @ts-expect-error
21
+ onSignUp() {
22
+ throw new Error('SignUp is not implemented for Oauth provider, you should use signIn instead.')
23
+ }
24
+ }