wabe 0.6.9 → 0.6.10
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.
- package/README.md +138 -32
- package/bucket/b.txt +1 -0
- package/dev/index.ts +215 -0
- package/dist/authentication/Session.d.ts +4 -1
- package/dist/authentication/interface.d.ts +16 -0
- package/dist/email/interface.d.ts +1 -1
- package/dist/graphql/resolvers.d.ts +4 -2
- package/dist/hooks/index.d.ts +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +8713 -8867
- package/dist/server/index.d.ts +4 -2
- package/dist/utils/crypto.d.ts +7 -0
- package/dist/utils/helper.d.ts +4 -1
- package/generated/schema.graphql +16 -14
- package/generated/wabe.ts +4 -4
- package/package.json +15 -15
- package/src/authentication/OTP.test.ts +69 -0
- package/src/authentication/OTP.ts +66 -0
- package/src/authentication/Session.test.ts +665 -0
- package/src/authentication/Session.ts +529 -0
- package/src/authentication/defaultAuthentication.ts +214 -0
- package/src/authentication/index.ts +3 -0
- package/src/authentication/interface.ts +157 -0
- package/src/authentication/oauth/GitHub.test.ts +105 -0
- package/src/authentication/oauth/GitHub.ts +133 -0
- package/src/authentication/oauth/Google.test.ts +105 -0
- package/src/authentication/oauth/Google.ts +110 -0
- package/src/authentication/oauth/Oauth2Client.test.ts +225 -0
- package/src/authentication/oauth/Oauth2Client.ts +140 -0
- package/src/authentication/oauth/index.ts +2 -0
- package/src/authentication/oauth/utils.test.ts +35 -0
- package/src/authentication/oauth/utils.ts +28 -0
- package/src/authentication/providers/EmailOTP.test.ts +138 -0
- package/src/authentication/providers/EmailOTP.ts +93 -0
- package/src/authentication/providers/EmailPassword.test.ts +187 -0
- package/src/authentication/providers/EmailPassword.ts +130 -0
- package/src/authentication/providers/EmailPasswordSRP.test.ts +206 -0
- package/src/authentication/providers/EmailPasswordSRP.ts +184 -0
- package/src/authentication/providers/GitHub.ts +30 -0
- package/src/authentication/providers/Google.ts +30 -0
- package/src/authentication/providers/OAuth.test.ts +185 -0
- package/src/authentication/providers/OAuth.ts +112 -0
- package/src/authentication/providers/PhonePassword.test.ts +187 -0
- package/src/authentication/providers/PhonePassword.ts +129 -0
- package/src/authentication/providers/QRCodeOTP.test.ts +79 -0
- package/src/authentication/providers/QRCodeOTP.ts +65 -0
- package/src/authentication/providers/index.ts +6 -0
- package/src/authentication/resolvers/refreshResolver.test.ts +37 -0
- package/src/authentication/resolvers/refreshResolver.ts +20 -0
- package/src/authentication/resolvers/signInWithResolver.inte.test.ts +59 -0
- package/src/authentication/resolvers/signInWithResolver.test.ts +307 -0
- package/src/authentication/resolvers/signInWithResolver.ts +102 -0
- package/src/authentication/resolvers/signOutResolver.test.ts +41 -0
- package/src/authentication/resolvers/signOutResolver.ts +22 -0
- package/src/authentication/resolvers/signUpWithResolver.test.ts +186 -0
- package/src/authentication/resolvers/signUpWithResolver.ts +69 -0
- package/src/authentication/resolvers/verifyChallenge.test.ts +136 -0
- package/src/authentication/resolvers/verifyChallenge.ts +69 -0
- package/src/authentication/roles.test.ts +59 -0
- package/src/authentication/roles.ts +40 -0
- package/src/authentication/utils.test.ts +99 -0
- package/src/authentication/utils.ts +43 -0
- package/src/cache/InMemoryCache.test.ts +62 -0
- package/src/cache/InMemoryCache.ts +45 -0
- package/src/cron/index.test.ts +17 -0
- package/src/cron/index.ts +46 -0
- package/src/database/DatabaseController.test.ts +625 -0
- package/src/database/DatabaseController.ts +983 -0
- package/src/database/index.test.ts +1230 -0
- package/src/database/index.ts +9 -0
- package/src/database/interface.ts +312 -0
- package/src/email/DevAdapter.ts +8 -0
- package/src/email/EmailController.test.ts +29 -0
- package/src/email/EmailController.ts +13 -0
- package/src/email/index.ts +2 -0
- package/src/email/interface.ts +36 -0
- package/src/email/templates/sendOtpCode.ts +120 -0
- package/src/file/FileController.ts +28 -0
- package/src/file/FileDevAdapter.ts +54 -0
- package/src/file/hookDeleteFile.ts +27 -0
- package/src/file/hookReadFile.ts +70 -0
- package/src/file/hookUploadFile.ts +53 -0
- package/src/file/index.test.ts +979 -0
- package/src/file/index.ts +2 -0
- package/src/file/interface.ts +42 -0
- package/src/graphql/GraphQLSchema.test.ts +4399 -0
- package/src/graphql/GraphQLSchema.ts +928 -0
- package/src/graphql/index.ts +2 -0
- package/src/graphql/parseGraphqlSchema.ts +94 -0
- package/src/graphql/parser.test.ts +217 -0
- package/src/graphql/parser.ts +566 -0
- package/src/graphql/pointerAndRelationFunction.ts +200 -0
- package/src/graphql/resolvers.ts +467 -0
- package/src/graphql/tests/aggregation.test.ts +1123 -0
- package/src/graphql/tests/e2e.test.ts +596 -0
- package/src/graphql/tests/scalars.test.ts +250 -0
- package/src/graphql/types.ts +219 -0
- package/src/hooks/HookObject.test.ts +122 -0
- package/src/hooks/HookObject.ts +168 -0
- package/src/hooks/authentication.ts +76 -0
- package/src/hooks/createUser.test.ts +77 -0
- package/src/hooks/createUser.ts +10 -0
- package/src/hooks/defaultFields.test.ts +187 -0
- package/src/hooks/defaultFields.ts +40 -0
- package/src/hooks/deleteSession.test.ts +181 -0
- package/src/hooks/deleteSession.ts +20 -0
- package/src/hooks/hashFieldHook.test.ts +163 -0
- package/src/hooks/hashFieldHook.ts +97 -0
- package/src/hooks/index.test.ts +207 -0
- package/src/hooks/index.ts +430 -0
- package/src/hooks/permissions.test.ts +424 -0
- package/src/hooks/permissions.ts +113 -0
- package/src/hooks/protected.test.ts +551 -0
- package/src/hooks/protected.ts +72 -0
- package/src/hooks/searchableFields.test.ts +166 -0
- package/src/hooks/searchableFields.ts +98 -0
- package/src/hooks/session.test.ts +138 -0
- package/src/hooks/session.ts +78 -0
- package/src/hooks/setEmail.test.ts +216 -0
- package/src/hooks/setEmail.ts +35 -0
- package/src/hooks/setupAcl.test.ts +589 -0
- package/src/hooks/setupAcl.ts +29 -0
- package/src/index.ts +9 -0
- package/src/schema/Schema.test.ts +484 -0
- package/src/schema/Schema.ts +795 -0
- package/src/schema/defaultResolvers.ts +94 -0
- package/src/schema/index.ts +1 -0
- package/src/schema/resolvers/meResolver.test.ts +62 -0
- package/src/schema/resolvers/meResolver.ts +14 -0
- package/src/schema/resolvers/newFile.ts +0 -0
- package/src/schema/resolvers/resetPassword.test.ts +345 -0
- package/src/schema/resolvers/resetPassword.ts +64 -0
- package/src/schema/resolvers/sendEmail.test.ts +118 -0
- package/src/schema/resolvers/sendEmail.ts +21 -0
- package/src/schema/resolvers/sendOtpCode.test.ts +153 -0
- package/src/schema/resolvers/sendOtpCode.ts +52 -0
- package/src/security.test.ts +3461 -0
- package/src/server/defaultSessionHandler.test.ts +66 -0
- package/src/server/defaultSessionHandler.ts +115 -0
- package/src/server/generateCodegen.ts +476 -0
- package/src/server/index.test.ts +552 -0
- package/src/server/index.ts +354 -0
- package/src/server/interface.ts +11 -0
- package/src/server/routes/authHandler.ts +187 -0
- package/src/server/routes/index.ts +40 -0
- package/src/utils/crypto.test.ts +41 -0
- package/src/utils/crypto.ts +121 -0
- package/src/utils/export.ts +13 -0
- package/src/utils/helper.ts +195 -0
- package/src/utils/index.test.ts +11 -0
- package/src/utils/index.ts +201 -0
- package/src/utils/preload.ts +8 -0
- package/src/utils/testHelper.ts +117 -0
- package/tsconfig.json +32 -0
- package/bunfig.toml +0 -4
- package/dist/ai/index.d.ts +0 -1
- package/dist/ai/interface.d.ts +0 -9
- /package/dist/server/{defaultHandlers.d.ts → defaultSessionHandler.d.ts} +0 -0
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
|
|
11
|
+
const DUMMY_USER_ID = '00000000-0000-0000-0000-000000000000'
|
|
12
|
+
|
|
13
|
+
type EmailOTPInterface = {
|
|
14
|
+
email: string
|
|
15
|
+
otp: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class EmailOTP
|
|
19
|
+
implements SecondaryProviderInterface<DevWabeTypes, EmailOTPInterface>
|
|
20
|
+
{
|
|
21
|
+
async onSendChallenge({
|
|
22
|
+
context,
|
|
23
|
+
user,
|
|
24
|
+
}: OnSendChallengeOptions<DevWabeTypes>) {
|
|
25
|
+
const emailController = context.wabe.controllers.email
|
|
26
|
+
|
|
27
|
+
if (!emailController) throw new Error('Email controller not found')
|
|
28
|
+
|
|
29
|
+
const mainEmail = context.wabe.config.email?.mainEmail
|
|
30
|
+
|
|
31
|
+
if (!mainEmail) throw new Error('No main email found')
|
|
32
|
+
|
|
33
|
+
if (!user.email) throw new Error('No user email found')
|
|
34
|
+
|
|
35
|
+
const otpClass = new OTP(context.wabe.config.rootKey)
|
|
36
|
+
|
|
37
|
+
const otp = otpClass.generate(user.id)
|
|
38
|
+
|
|
39
|
+
const template = context.wabe.config.email?.htmlTemplates?.sendOTPCode
|
|
40
|
+
|
|
41
|
+
await emailController.send({
|
|
42
|
+
from: mainEmail,
|
|
43
|
+
to: [user.email],
|
|
44
|
+
subject: template?.subject || 'Your OTP code',
|
|
45
|
+
html: template?.fn
|
|
46
|
+
? await template.fn({ otp })
|
|
47
|
+
: sendOtpCodeTemplate(otp),
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async onVerifyChallenge({
|
|
52
|
+
context,
|
|
53
|
+
input,
|
|
54
|
+
}: OnVerifyChallengeOptions<DevWabeTypes, EmailOTPInterface>) {
|
|
55
|
+
const users = await context.wabe.controllers.database.getObjects({
|
|
56
|
+
className: 'User',
|
|
57
|
+
where: {
|
|
58
|
+
email: {
|
|
59
|
+
equalTo: input.email,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
select: {
|
|
63
|
+
authentication: true,
|
|
64
|
+
role: true,
|
|
65
|
+
secondFA: true,
|
|
66
|
+
email: true,
|
|
67
|
+
id: true,
|
|
68
|
+
provider: true,
|
|
69
|
+
isOauth: true,
|
|
70
|
+
createdAt: true,
|
|
71
|
+
updatedAt: true,
|
|
72
|
+
},
|
|
73
|
+
first: 1,
|
|
74
|
+
context: contextWithRoot(context),
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
const realUser = users.length > 0 ? users[0] : null
|
|
78
|
+
const userId = realUser?.id ?? DUMMY_USER_ID
|
|
79
|
+
|
|
80
|
+
const isDevBypass =
|
|
81
|
+
!context.wabe.config.isProduction &&
|
|
82
|
+
input.otp === '000000' &&
|
|
83
|
+
realUser !== null
|
|
84
|
+
|
|
85
|
+
const otpClass = new OTP(context.wabe.config.rootKey)
|
|
86
|
+
|
|
87
|
+
const isOtpValid = otpClass.verify(input.otp, userId)
|
|
88
|
+
|
|
89
|
+
if (realUser && (isOtpValid || isDevBypass)) return { userId: realUser.id }
|
|
90
|
+
|
|
91
|
+
return null
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import {
|
|
2
|
+
describe,
|
|
3
|
+
expect,
|
|
4
|
+
it,
|
|
5
|
+
mock,
|
|
6
|
+
spyOn,
|
|
7
|
+
afterEach,
|
|
8
|
+
afterAll,
|
|
9
|
+
} from 'bun:test'
|
|
10
|
+
import * as crypto from '../../utils/crypto'
|
|
11
|
+
|
|
12
|
+
import { EmailPassword } from './EmailPassword'
|
|
13
|
+
|
|
14
|
+
describe('Email password', () => {
|
|
15
|
+
const mockGetObjects = mock(() => Promise.resolve([]))
|
|
16
|
+
const mockCount = mock(() => Promise.resolve(0)) as any
|
|
17
|
+
const mockCreateObject = mock(() => Promise.resolve({ id: 'userId' })) as any
|
|
18
|
+
|
|
19
|
+
const spyArgonPasswordVerify = spyOn(crypto, 'verifyArgon2')
|
|
20
|
+
const spyBunPasswordHash = spyOn(crypto, 'hashArgon2')
|
|
21
|
+
|
|
22
|
+
const controllers = {
|
|
23
|
+
controllers: {
|
|
24
|
+
database: {
|
|
25
|
+
getObjects: mockGetObjects,
|
|
26
|
+
createObject: mockCreateObject,
|
|
27
|
+
count: mockCount,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
} as any
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
mockGetObjects.mockClear()
|
|
34
|
+
mockCreateObject.mockClear()
|
|
35
|
+
spyArgonPasswordVerify.mockClear()
|
|
36
|
+
spyBunPasswordHash.mockClear()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
afterAll(() => {
|
|
40
|
+
spyArgonPasswordVerify.mockRestore()
|
|
41
|
+
spyBunPasswordHash.mockRestore()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const emailPassword = new EmailPassword()
|
|
45
|
+
|
|
46
|
+
it('should signUp with email password', async () => {
|
|
47
|
+
spyBunPasswordHash.mockResolvedValueOnce('$argon2id$hashedPassword')
|
|
48
|
+
|
|
49
|
+
const {
|
|
50
|
+
authenticationDataToSave: { email },
|
|
51
|
+
} = await emailPassword.onSignUp({
|
|
52
|
+
context: { wabe: controllers } as any,
|
|
53
|
+
input: { email: 'email@test.fr', password: 'password' },
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
expect(email).toBe('email@test.fr')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('should signIn with email password', async () => {
|
|
60
|
+
mockGetObjects.mockResolvedValue([
|
|
61
|
+
{
|
|
62
|
+
id: 'userId',
|
|
63
|
+
authentication: {
|
|
64
|
+
emailPassword: {
|
|
65
|
+
email: 'email@test.fr',
|
|
66
|
+
password: 'hashedPassword',
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
} as never,
|
|
70
|
+
])
|
|
71
|
+
|
|
72
|
+
spyArgonPasswordVerify.mockResolvedValueOnce(true)
|
|
73
|
+
|
|
74
|
+
const { user } = await emailPassword.onSignIn({
|
|
75
|
+
context: { wabe: controllers } as any,
|
|
76
|
+
input: { email: 'email@test.fr', password: 'password' },
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
expect(user).toEqual({
|
|
80
|
+
id: 'userId',
|
|
81
|
+
authentication: {
|
|
82
|
+
emailPassword: {
|
|
83
|
+
email: 'email@test.fr',
|
|
84
|
+
password: 'hashedPassword',
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
expect(spyArgonPasswordVerify).toHaveBeenCalledTimes(1)
|
|
90
|
+
expect(spyArgonPasswordVerify).toHaveBeenCalledWith(
|
|
91
|
+
'password',
|
|
92
|
+
'hashedPassword',
|
|
93
|
+
)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('should not signIn with email password if password is undefined', () => {
|
|
97
|
+
spyArgonPasswordVerify.mockResolvedValueOnce(false)
|
|
98
|
+
|
|
99
|
+
expect(
|
|
100
|
+
emailPassword.onSignIn({
|
|
101
|
+
context: { wabe: controllers } as any,
|
|
102
|
+
// @ts-expect-error
|
|
103
|
+
input: { email: 'email@test.fr' },
|
|
104
|
+
}),
|
|
105
|
+
).rejects.toThrow('Invalid authentication credentials')
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('should not signIn with email password if there is no user found', () => {
|
|
109
|
+
mockGetObjects.mockResolvedValue([])
|
|
110
|
+
|
|
111
|
+
expect(
|
|
112
|
+
emailPassword.onSignIn({
|
|
113
|
+
context: { wabe: controllers } as any,
|
|
114
|
+
input: {
|
|
115
|
+
email: 'invalidEmail@test.fr',
|
|
116
|
+
password: 'password',
|
|
117
|
+
},
|
|
118
|
+
}),
|
|
119
|
+
).rejects.toThrow('Invalid authentication credentials')
|
|
120
|
+
|
|
121
|
+
expect(spyArgonPasswordVerify).toHaveBeenCalledTimes(1)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('should not signIn with email password if there is email is invalid', () => {
|
|
125
|
+
mockGetObjects.mockResolvedValue([
|
|
126
|
+
{
|
|
127
|
+
authentication: {
|
|
128
|
+
emailPassword: {
|
|
129
|
+
password: 'hashedPassword',
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
} as never,
|
|
133
|
+
])
|
|
134
|
+
|
|
135
|
+
spyArgonPasswordVerify.mockResolvedValueOnce(true)
|
|
136
|
+
|
|
137
|
+
expect(
|
|
138
|
+
emailPassword.onSignIn({
|
|
139
|
+
context: { wabe: controllers } as any,
|
|
140
|
+
input: {
|
|
141
|
+
email: 'invalidEmail@test.fr',
|
|
142
|
+
password: 'password',
|
|
143
|
+
},
|
|
144
|
+
}),
|
|
145
|
+
).rejects.toThrow('Invalid authentication credentials')
|
|
146
|
+
|
|
147
|
+
expect(spyArgonPasswordVerify).toHaveBeenCalledTimes(1)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('should not update authentication data if there is no user found', () => {
|
|
151
|
+
mockGetObjects.mockResolvedValue([])
|
|
152
|
+
|
|
153
|
+
spyArgonPasswordVerify.mockResolvedValueOnce(true)
|
|
154
|
+
|
|
155
|
+
expect(
|
|
156
|
+
emailPassword.onUpdateAuthenticationData?.({
|
|
157
|
+
context: { wabe: controllers } as any,
|
|
158
|
+
input: {
|
|
159
|
+
email: 'email@test.fr',
|
|
160
|
+
password: 'password',
|
|
161
|
+
},
|
|
162
|
+
userId: 'userId',
|
|
163
|
+
}),
|
|
164
|
+
).rejects.toThrow('User not found')
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it('should update authentication data if the userId match with an user', async () => {
|
|
168
|
+
mockGetObjects.mockResolvedValue([
|
|
169
|
+
{
|
|
170
|
+
id: 'id',
|
|
171
|
+
},
|
|
172
|
+
] as any)
|
|
173
|
+
|
|
174
|
+
spyBunPasswordHash.mockResolvedValueOnce('$argon2id$hashedPassword')
|
|
175
|
+
|
|
176
|
+
const res = await emailPassword.onUpdateAuthenticationData?.({
|
|
177
|
+
context: { wabe: controllers } as any,
|
|
178
|
+
input: {
|
|
179
|
+
email: 'email@test.fr',
|
|
180
|
+
password: 'password',
|
|
181
|
+
},
|
|
182
|
+
userId: 'userId',
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
expect(res.authenticationDataToSave.email).toBe('email@test.fr')
|
|
186
|
+
})
|
|
187
|
+
})
|
|
@@ -0,0 +1,130 @@
|
|
|
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
|
|
19
|
+
implements ProviderInterface<DevWabeTypes, EmailPasswordInterface>
|
|
20
|
+
{
|
|
21
|
+
async onSignIn({
|
|
22
|
+
input,
|
|
23
|
+
context,
|
|
24
|
+
}: AuthenticationEventsOptions<DevWabeTypes, EmailPasswordInterface>) {
|
|
25
|
+
const users = await context.wabe.controllers.database.getObjects({
|
|
26
|
+
className: 'User',
|
|
27
|
+
where: {
|
|
28
|
+
authentication: {
|
|
29
|
+
emailPassword: {
|
|
30
|
+
email: { equalTo: input.email },
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
context: contextWithRoot(context),
|
|
35
|
+
select: {
|
|
36
|
+
authentication: true,
|
|
37
|
+
role: true,
|
|
38
|
+
secondFA: true,
|
|
39
|
+
email: true,
|
|
40
|
+
id: true,
|
|
41
|
+
provider: true,
|
|
42
|
+
isOauth: true,
|
|
43
|
+
createdAt: true,
|
|
44
|
+
updatedAt: true,
|
|
45
|
+
},
|
|
46
|
+
first: 1,
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
const user = users[0]
|
|
50
|
+
const userDatabasePassword = user?.authentication?.emailPassword?.password
|
|
51
|
+
|
|
52
|
+
const passwordHashToCheck = userDatabasePassword ?? DUMMY_PASSWORD_HASH
|
|
53
|
+
|
|
54
|
+
const isPasswordEquals = await verifyArgon2(
|
|
55
|
+
input.password,
|
|
56
|
+
passwordHashToCheck,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
if (
|
|
60
|
+
!user ||
|
|
61
|
+
!isPasswordEquals ||
|
|
62
|
+
input.email !== user.authentication?.emailPassword?.email
|
|
63
|
+
)
|
|
64
|
+
throw new Error('Invalid authentication credentials')
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
user,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async onSignUp({
|
|
72
|
+
input,
|
|
73
|
+
context,
|
|
74
|
+
}: AuthenticationEventsOptions<DevWabeTypes, EmailPasswordInterface>) {
|
|
75
|
+
const users = await context.wabe.controllers.database.count({
|
|
76
|
+
className: 'User',
|
|
77
|
+
where: {
|
|
78
|
+
authentication: {
|
|
79
|
+
emailPassword: {
|
|
80
|
+
email: { equalTo: input.email },
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
context: contextWithRoot(context),
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
// Hide real message
|
|
88
|
+
if (users > 0) throw new Error('Not authorized to create user')
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
authenticationDataToSave: {
|
|
92
|
+
email: input.email,
|
|
93
|
+
password: input.password,
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async onUpdateAuthenticationData({
|
|
99
|
+
userId,
|
|
100
|
+
input,
|
|
101
|
+
context,
|
|
102
|
+
}: AuthenticationEventsOptionsWithUserId<
|
|
103
|
+
DevWabeTypes,
|
|
104
|
+
EmailPasswordInterface
|
|
105
|
+
>) {
|
|
106
|
+
const users = await context.wabe.controllers.database.getObjects({
|
|
107
|
+
className: 'User',
|
|
108
|
+
where: {
|
|
109
|
+
id: {
|
|
110
|
+
equalTo: userId,
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
context,
|
|
114
|
+
select: { authentication: true },
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
if (users.length === 0) throw new Error('User not found')
|
|
118
|
+
|
|
119
|
+
const user = users[0]
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
authenticationDataToSave: {
|
|
123
|
+
email: input.email ?? user?.authentication?.emailPassword?.email,
|
|
124
|
+
password: input.password
|
|
125
|
+
? input.password
|
|
126
|
+
: user?.authentication?.emailPassword?.password,
|
|
127
|
+
},
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
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 { salt serverPublic }
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
`,
|
|
158
|
+
{
|
|
159
|
+
input: {
|
|
160
|
+
authentication: {
|
|
161
|
+
emailPasswordSRP: {
|
|
162
|
+
email,
|
|
163
|
+
clientPublic: clientEphemeral.public,
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
// Derive with wrong password
|
|
171
|
+
const wrongPrivateKey = await client.deriveSafePrivateKey(
|
|
172
|
+
salt,
|
|
173
|
+
wrongPassword,
|
|
174
|
+
)
|
|
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 { serverSessionProof }
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
`,
|
|
192
|
+
{
|
|
193
|
+
input: {
|
|
194
|
+
secondFA: {
|
|
195
|
+
emailPasswordSRPChallenge: {
|
|
196
|
+
email,
|
|
197
|
+
clientPublic: clientEphemeral.public,
|
|
198
|
+
clientSessionProof: wrongClientSession.proof,
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
),
|
|
204
|
+
).rejects.toThrow('Invalid authentication credentials')
|
|
205
|
+
})
|
|
206
|
+
})
|