wabe 0.6.11 → 0.6.13
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/dist/authentication/Session.d.ts +10 -12
- package/dist/authentication/cookies.d.ts +1 -0
- package/dist/authentication/index.d.ts +1 -0
- package/dist/authentication/interface.d.ts +20 -0
- package/dist/authentication/security.d.ts +18 -0
- package/dist/database/DatabaseController.d.ts +57 -3
- package/dist/database/interface.d.ts +5 -0
- package/dist/file/interface.d.ts +23 -0
- package/dist/file/security.d.ts +6 -0
- package/dist/graphql/GraphQLSchema.d.ts +1 -1
- package/dist/graphql/resolvers.d.ts +1 -0
- package/dist/hooks/protected.d.ts +3 -0
- package/dist/hooks/virtualFields.d.ts +3 -0
- package/dist/index.js +1757 -818
- package/dist/schema/Schema.d.ts +66 -7
- package/dist/server/index.d.ts +2 -1
- package/dist/server/routes/index.d.ts +4 -1
- package/dist/utils/database.d.ts +1 -0
- package/dist/utils/export.d.ts +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/package.json +7 -4
- package/bucket/b.txt +0 -1
- package/dev/index.ts +0 -215
- package/generated/schema.graphql +0 -1892
- package/generated/wabe.ts +0 -439
- package/src/authentication/OTP.test.ts +0 -69
- package/src/authentication/OTP.ts +0 -64
- package/src/authentication/Session.test.ts +0 -629
- package/src/authentication/Session.ts +0 -493
- package/src/authentication/defaultAuthentication.ts +0 -209
- package/src/authentication/index.ts +0 -3
- package/src/authentication/interface.ts +0 -155
- package/src/authentication/oauth/GitHub.test.ts +0 -91
- package/src/authentication/oauth/GitHub.ts +0 -121
- package/src/authentication/oauth/Google.test.ts +0 -91
- package/src/authentication/oauth/Google.ts +0 -101
- package/src/authentication/oauth/Oauth2Client.test.ts +0 -219
- package/src/authentication/oauth/Oauth2Client.ts +0 -135
- package/src/authentication/oauth/index.ts +0 -2
- package/src/authentication/oauth/utils.test.ts +0 -33
- package/src/authentication/oauth/utils.ts +0 -27
- package/src/authentication/providers/EmailOTP.test.ts +0 -127
- package/src/authentication/providers/EmailOTP.ts +0 -84
- package/src/authentication/providers/EmailPassword.test.ts +0 -176
- package/src/authentication/providers/EmailPassword.ts +0 -116
- package/src/authentication/providers/EmailPasswordSRP.test.ts +0 -208
- package/src/authentication/providers/EmailPasswordSRP.ts +0 -179
- package/src/authentication/providers/GitHub.ts +0 -24
- package/src/authentication/providers/Google.ts +0 -24
- package/src/authentication/providers/OAuth.test.ts +0 -185
- package/src/authentication/providers/OAuth.ts +0 -106
- package/src/authentication/providers/PhonePassword.test.ts +0 -176
- package/src/authentication/providers/PhonePassword.ts +0 -115
- package/src/authentication/providers/QRCodeOTP.test.ts +0 -77
- package/src/authentication/providers/QRCodeOTP.ts +0 -58
- package/src/authentication/providers/index.ts +0 -6
- package/src/authentication/resolvers/refreshResolver.test.ts +0 -30
- package/src/authentication/resolvers/refreshResolver.ts +0 -19
- package/src/authentication/resolvers/signInWithResolver.inte.test.ts +0 -59
- package/src/authentication/resolvers/signInWithResolver.test.ts +0 -293
- package/src/authentication/resolvers/signInWithResolver.ts +0 -92
- package/src/authentication/resolvers/signOutResolver.test.ts +0 -38
- package/src/authentication/resolvers/signOutResolver.ts +0 -18
- package/src/authentication/resolvers/signUpWithResolver.test.ts +0 -180
- package/src/authentication/resolvers/signUpWithResolver.ts +0 -65
- package/src/authentication/resolvers/verifyChallenge.test.ts +0 -133
- package/src/authentication/resolvers/verifyChallenge.ts +0 -62
- package/src/authentication/roles.test.ts +0 -49
- package/src/authentication/roles.ts +0 -40
- package/src/authentication/utils.test.ts +0 -97
- package/src/authentication/utils.ts +0 -39
- package/src/cache/InMemoryCache.test.ts +0 -62
- package/src/cache/InMemoryCache.ts +0 -45
- package/src/cron/index.test.ts +0 -17
- package/src/cron/index.ts +0 -43
- package/src/database/DatabaseController.test.ts +0 -613
- package/src/database/DatabaseController.ts +0 -1007
- package/src/database/index.test.ts +0 -1372
- package/src/database/index.ts +0 -9
- package/src/database/interface.ts +0 -302
- package/src/email/DevAdapter.ts +0 -7
- package/src/email/EmailController.test.ts +0 -29
- package/src/email/EmailController.ts +0 -13
- package/src/email/index.ts +0 -2
- package/src/email/interface.ts +0 -36
- package/src/email/templates/sendOtpCode.ts +0 -120
- package/src/file/FileController.ts +0 -28
- package/src/file/FileDevAdapter.ts +0 -51
- package/src/file/hookDeleteFile.ts +0 -25
- package/src/file/hookReadFile.ts +0 -66
- package/src/file/hookUploadFile.ts +0 -50
- package/src/file/index.test.ts +0 -932
- package/src/file/index.ts +0 -2
- package/src/file/interface.ts +0 -39
- package/src/graphql/GraphQLSchema.test.ts +0 -4408
- package/src/graphql/GraphQLSchema.ts +0 -880
- package/src/graphql/index.ts +0 -2
- package/src/graphql/parseGraphqlSchema.ts +0 -85
- package/src/graphql/parser.test.ts +0 -203
- package/src/graphql/parser.ts +0 -542
- package/src/graphql/pointerAndRelationFunction.ts +0 -191
- package/src/graphql/resolvers.ts +0 -442
- package/src/graphql/tests/aggregation.test.ts +0 -1115
- package/src/graphql/tests/e2e.test.ts +0 -590
- package/src/graphql/tests/scalars.test.ts +0 -250
- package/src/graphql/types.ts +0 -227
- package/src/hooks/HookObject.test.ts +0 -122
- package/src/hooks/HookObject.ts +0 -165
- package/src/hooks/authentication.ts +0 -67
- package/src/hooks/createUser.test.ts +0 -77
- package/src/hooks/createUser.ts +0 -10
- package/src/hooks/defaultFields.test.ts +0 -176
- package/src/hooks/defaultFields.ts +0 -32
- package/src/hooks/deleteSession.test.ts +0 -181
- package/src/hooks/deleteSession.ts +0 -20
- package/src/hooks/hashFieldHook.test.ts +0 -152
- package/src/hooks/hashFieldHook.ts +0 -89
- package/src/hooks/index.test.ts +0 -258
- package/src/hooks/index.ts +0 -414
- package/src/hooks/permissions.test.ts +0 -412
- package/src/hooks/permissions.ts +0 -93
- package/src/hooks/protected.test.ts +0 -551
- package/src/hooks/protected.ts +0 -60
- package/src/hooks/searchableFields.test.ts +0 -147
- package/src/hooks/searchableFields.ts +0 -86
- package/src/hooks/session.test.ts +0 -134
- package/src/hooks/session.ts +0 -76
- package/src/hooks/setEmail.test.ts +0 -216
- package/src/hooks/setEmail.ts +0 -33
- package/src/hooks/setupAcl.test.ts +0 -618
- package/src/hooks/setupAcl.ts +0 -25
- package/src/index.ts +0 -9
- package/src/schema/Schema.test.ts +0 -482
- package/src/schema/Schema.ts +0 -757
- package/src/schema/defaultResolvers.ts +0 -93
- package/src/schema/index.ts +0 -1
- package/src/schema/resolvers/meResolver.test.ts +0 -62
- package/src/schema/resolvers/meResolver.ts +0 -10
- package/src/schema/resolvers/resetPassword.test.ts +0 -341
- package/src/schema/resolvers/resetPassword.ts +0 -63
- package/src/schema/resolvers/sendEmail.test.ts +0 -118
- package/src/schema/resolvers/sendEmail.ts +0 -21
- package/src/schema/resolvers/sendOtpCode.test.ts +0 -141
- package/src/schema/resolvers/sendOtpCode.ts +0 -52
- package/src/security.test.ts +0 -3434
- package/src/server/defaultSessionHandler.test.ts +0 -62
- package/src/server/defaultSessionHandler.ts +0 -105
- package/src/server/generateCodegen.ts +0 -433
- package/src/server/index.test.ts +0 -532
- package/src/server/index.ts +0 -334
- package/src/server/interface.ts +0 -11
- package/src/server/routes/authHandler.ts +0 -169
- package/src/server/routes/index.ts +0 -39
- package/src/utils/crypto.test.ts +0 -41
- package/src/utils/crypto.ts +0 -105
- package/src/utils/export.ts +0 -11
- package/src/utils/helper.ts +0 -204
- package/src/utils/index.test.ts +0 -11
- package/src/utils/index.ts +0 -189
- package/src/utils/preload.ts +0 -8
- package/src/utils/testHelper.ts +0 -116
- package/tsconfig.json +0 -32
|
@@ -1,493 +0,0 @@
|
|
|
1
|
-
import jwt, { verify, type SignOptions } from 'jsonwebtoken'
|
|
2
|
-
import crypto from 'node:crypto'
|
|
3
|
-
import type { WabeContext } from '../server/interface'
|
|
4
|
-
import type { User } from '../../generated/wabe'
|
|
5
|
-
import type { WabeConfig } from '../server'
|
|
6
|
-
import { contextWithRoot } from '../utils/export'
|
|
7
|
-
import type { DevWabeTypes } from '../utils/helper'
|
|
8
|
-
import { encryptDeterministicToken, decryptDeterministicToken } from '../utils/crypto'
|
|
9
|
-
|
|
10
|
-
const getJwtSecret = (context: WabeContext<DevWabeTypes>): string => {
|
|
11
|
-
const secret = context.wabe.config.authentication?.session?.jwtSecret
|
|
12
|
-
if (!secret) throw new Error('Authentication session requires jwtSecret')
|
|
13
|
-
return secret
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const safeVerify = (
|
|
17
|
-
token: string,
|
|
18
|
-
secret: string,
|
|
19
|
-
options: Pick<SignOptions, 'audience' | 'issuer'> = {},
|
|
20
|
-
) => {
|
|
21
|
-
try {
|
|
22
|
-
return !!verify(token, secret, options)
|
|
23
|
-
} catch {
|
|
24
|
-
return false
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const getTokenSecret = (context: WabeContext<DevWabeTypes>): string =>
|
|
29
|
-
context.wabe.config.authentication?.session?.tokenSecret ?? getJwtSecret(context)
|
|
30
|
-
|
|
31
|
-
const getTokenEncryptionKey = (context: WabeContext<DevWabeTypes>) =>
|
|
32
|
-
crypto.createHash('sha256').update(getTokenSecret(context)).digest()
|
|
33
|
-
|
|
34
|
-
const getJwtVerifyOptions = (context: WabeContext<DevWabeTypes>) => {
|
|
35
|
-
const opts: Pick<SignOptions, 'audience' | 'issuer'> = {}
|
|
36
|
-
const audience = context.wabe.config.authentication?.session?.jwtAudience
|
|
37
|
-
const issuer = context.wabe.config.authentication?.session?.jwtIssuer
|
|
38
|
-
if (audience) opts.audience = audience
|
|
39
|
-
if (issuer) opts.issuer = issuer
|
|
40
|
-
return opts
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export class Session {
|
|
44
|
-
private accessToken: string | undefined = undefined
|
|
45
|
-
private refreshToken: string | undefined = undefined
|
|
46
|
-
|
|
47
|
-
getAccessTokenExpireAt(config: WabeConfig<DevWabeTypes>) {
|
|
48
|
-
const customExpiresInMs = config?.authentication?.session?.accessTokenExpiresInMs
|
|
49
|
-
|
|
50
|
-
if (!customExpiresInMs) return new Date(Date.now() + 1000 * 60 * 15) // 15 minutes in ms
|
|
51
|
-
|
|
52
|
-
return new Date(Date.now() + customExpiresInMs)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
_getRefreshTokenExpiresInMs(config: WabeConfig<DevWabeTypes>) {
|
|
56
|
-
const customExpiresInMs = config?.authentication?.session?.refreshTokenExpiresInMs
|
|
57
|
-
|
|
58
|
-
if (!customExpiresInMs) return 1000 * 60 * 60 * 24 * 7 // 7 days in ms
|
|
59
|
-
|
|
60
|
-
return customExpiresInMs
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
getRefreshTokenExpireAt(config: WabeConfig<DevWabeTypes>) {
|
|
64
|
-
const expiresInMs = this._getRefreshTokenExpiresInMs(config)
|
|
65
|
-
|
|
66
|
-
return new Date(Date.now() + expiresInMs)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async meFromAccessToken(
|
|
70
|
-
{ accessToken, csrfToken }: { accessToken: string; csrfToken: string },
|
|
71
|
-
context: WabeContext<DevWabeTypes>,
|
|
72
|
-
): Promise<{
|
|
73
|
-
sessionId: string | null
|
|
74
|
-
user: User | null
|
|
75
|
-
accessToken: string | null
|
|
76
|
-
refreshToken?: string | null
|
|
77
|
-
}> {
|
|
78
|
-
const verifyOptions = getJwtVerifyOptions(context)
|
|
79
|
-
if (!safeVerify(accessToken, getJwtSecret(context), verifyOptions)) {
|
|
80
|
-
return {
|
|
81
|
-
sessionId: null,
|
|
82
|
-
user: null,
|
|
83
|
-
accessToken: null,
|
|
84
|
-
refreshToken: null,
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const encryptedAccessToken = encryptDeterministicToken(
|
|
89
|
-
accessToken,
|
|
90
|
-
getTokenEncryptionKey(context),
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
const sessions = await context.wabe.controllers.database.getObjects({
|
|
94
|
-
className: '_Session',
|
|
95
|
-
where: {
|
|
96
|
-
accessTokenEncrypted: { equalTo: encryptedAccessToken },
|
|
97
|
-
OR: [
|
|
98
|
-
{
|
|
99
|
-
accessTokenExpiresAt: {
|
|
100
|
-
greaterThanOrEqualTo: new Date(),
|
|
101
|
-
},
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
refreshTokenExpiresAt: {
|
|
105
|
-
greaterThanOrEqualTo: new Date(),
|
|
106
|
-
},
|
|
107
|
-
},
|
|
108
|
-
],
|
|
109
|
-
},
|
|
110
|
-
select: {
|
|
111
|
-
id: true,
|
|
112
|
-
user: true,
|
|
113
|
-
accessTokenExpiresAt: true,
|
|
114
|
-
refreshTokenExpiresAt: true,
|
|
115
|
-
refreshTokenEncrypted: true,
|
|
116
|
-
},
|
|
117
|
-
first: 1,
|
|
118
|
-
context,
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
if (sessions.length === 0)
|
|
122
|
-
return {
|
|
123
|
-
sessionId: null,
|
|
124
|
-
user: null,
|
|
125
|
-
accessToken: null,
|
|
126
|
-
refreshToken: null,
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const session = sessions[0]
|
|
130
|
-
|
|
131
|
-
if (!session || !session?.user)
|
|
132
|
-
return {
|
|
133
|
-
sessionId: null,
|
|
134
|
-
user: null,
|
|
135
|
-
accessToken: null,
|
|
136
|
-
refreshToken: null,
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// CSRF check only for cookie-based sessions (enabled by default unless explicitly disabled)
|
|
140
|
-
if (
|
|
141
|
-
context.wabe.config.authentication?.session?.cookieSession &&
|
|
142
|
-
context.wabe.config.security?.disableCSRFProtection !== true
|
|
143
|
-
) {
|
|
144
|
-
const [receivedHmacHex, receivedRandomValue] = csrfToken.split('.')
|
|
145
|
-
|
|
146
|
-
if (!receivedHmacHex || !receivedRandomValue)
|
|
147
|
-
return {
|
|
148
|
-
sessionId: null,
|
|
149
|
-
user: null,
|
|
150
|
-
accessToken: null,
|
|
151
|
-
refreshToken: null,
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const currentSessionId = session.id
|
|
155
|
-
|
|
156
|
-
const message = `${currentSessionId.length}!${currentSessionId}!${receivedRandomValue?.length}!${receivedRandomValue}`
|
|
157
|
-
|
|
158
|
-
const csrfSecret =
|
|
159
|
-
context.wabe.config.authentication?.session?.csrfSecret || getJwtSecret(context)
|
|
160
|
-
|
|
161
|
-
const expectedHmac = crypto.createHmac('sha256', csrfSecret).update(message).digest('hex')
|
|
162
|
-
|
|
163
|
-
const isValid = crypto.timingSafeEqual(
|
|
164
|
-
Buffer.from(receivedHmacHex || '', 'hex'),
|
|
165
|
-
Buffer.from(expectedHmac, 'hex'),
|
|
166
|
-
)
|
|
167
|
-
|
|
168
|
-
if (!isValid)
|
|
169
|
-
return {
|
|
170
|
-
sessionId: null,
|
|
171
|
-
user: null,
|
|
172
|
-
accessToken: null,
|
|
173
|
-
refreshToken: null,
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// User check
|
|
178
|
-
|
|
179
|
-
const user = session.user
|
|
180
|
-
|
|
181
|
-
const userWithRole = await context.wabe.controllers.database.getObject({
|
|
182
|
-
className: 'User',
|
|
183
|
-
select: {
|
|
184
|
-
role: true,
|
|
185
|
-
},
|
|
186
|
-
context,
|
|
187
|
-
id: user.id,
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
// If access token is expired and refresh token is not expired
|
|
191
|
-
if (
|
|
192
|
-
new Date(session.accessTokenExpiresAt) < new Date() &&
|
|
193
|
-
new Date(session.refreshTokenExpiresAt) >= new Date() &&
|
|
194
|
-
session.refreshTokenEncrypted
|
|
195
|
-
) {
|
|
196
|
-
const decryptedRefreshToken = decryptDeterministicToken(
|
|
197
|
-
session.refreshTokenEncrypted as string,
|
|
198
|
-
getTokenEncryptionKey(context),
|
|
199
|
-
)
|
|
200
|
-
|
|
201
|
-
if (!decryptedRefreshToken)
|
|
202
|
-
return {
|
|
203
|
-
sessionId: null,
|
|
204
|
-
user: null,
|
|
205
|
-
accessToken: null,
|
|
206
|
-
refreshToken: null,
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const { accessToken: newAccessToken, refreshToken: newRefreshToken } = await this.refresh(
|
|
210
|
-
accessToken,
|
|
211
|
-
decryptedRefreshToken,
|
|
212
|
-
context,
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
return {
|
|
216
|
-
sessionId: session.id,
|
|
217
|
-
user: {
|
|
218
|
-
...user,
|
|
219
|
-
role: userWithRole?.role,
|
|
220
|
-
},
|
|
221
|
-
accessToken: newAccessToken,
|
|
222
|
-
refreshToken: newRefreshToken,
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return {
|
|
227
|
-
sessionId: session.id,
|
|
228
|
-
user: {
|
|
229
|
-
...user,
|
|
230
|
-
role: userWithRole?.role,
|
|
231
|
-
},
|
|
232
|
-
accessToken,
|
|
233
|
-
refreshToken: decryptDeterministicToken(
|
|
234
|
-
session.refreshTokenEncrypted as string,
|
|
235
|
-
getTokenEncryptionKey(context),
|
|
236
|
-
),
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
async create(userId: string, context: WabeContext<DevWabeTypes>) {
|
|
241
|
-
const jwtTokenFields = context.wabe.config.authentication?.session?.jwtTokenFields
|
|
242
|
-
|
|
243
|
-
const nowSeconds = Math.floor(Date.now() / 1000)
|
|
244
|
-
|
|
245
|
-
const result = jwtTokenFields
|
|
246
|
-
? await context.wabe.controllers.database.getObject({
|
|
247
|
-
className: 'User',
|
|
248
|
-
select: jwtTokenFields,
|
|
249
|
-
context,
|
|
250
|
-
id: userId,
|
|
251
|
-
})
|
|
252
|
-
: undefined
|
|
253
|
-
|
|
254
|
-
const secretKey = getJwtSecret(context)
|
|
255
|
-
|
|
256
|
-
const signOptions: SignOptions = { jwtid: crypto.randomUUID() }
|
|
257
|
-
const audience = context.wabe.config.authentication?.session?.jwtAudience
|
|
258
|
-
const issuer = context.wabe.config.authentication?.session?.jwtIssuer
|
|
259
|
-
if (audience) signOptions.audience = audience
|
|
260
|
-
if (issuer) signOptions.issuer = issuer
|
|
261
|
-
|
|
262
|
-
this.accessToken = jwt.sign(
|
|
263
|
-
{
|
|
264
|
-
userId,
|
|
265
|
-
user: result,
|
|
266
|
-
iat: nowSeconds,
|
|
267
|
-
exp: Math.floor(this.getAccessTokenExpireAt(context.wabe.config).getTime() / 1000),
|
|
268
|
-
},
|
|
269
|
-
secretKey,
|
|
270
|
-
signOptions,
|
|
271
|
-
)
|
|
272
|
-
|
|
273
|
-
this.refreshToken = jwt.sign(
|
|
274
|
-
{
|
|
275
|
-
userId,
|
|
276
|
-
user: result,
|
|
277
|
-
iat: nowSeconds,
|
|
278
|
-
exp: Math.floor(this.getRefreshTokenExpireAt(context.wabe.config).getTime() / 1000),
|
|
279
|
-
},
|
|
280
|
-
secretKey,
|
|
281
|
-
signOptions,
|
|
282
|
-
)
|
|
283
|
-
|
|
284
|
-
const accessTokenEncrypted = encryptDeterministicToken(
|
|
285
|
-
this.accessToken,
|
|
286
|
-
getTokenEncryptionKey(context),
|
|
287
|
-
)
|
|
288
|
-
const refreshTokenEncrypted = encryptDeterministicToken(
|
|
289
|
-
this.refreshToken,
|
|
290
|
-
getTokenEncryptionKey(context),
|
|
291
|
-
)
|
|
292
|
-
|
|
293
|
-
const res = await context.wabe.controllers.database.createObject({
|
|
294
|
-
className: '_Session',
|
|
295
|
-
context: contextWithRoot(context),
|
|
296
|
-
data: {
|
|
297
|
-
accessTokenEncrypted,
|
|
298
|
-
accessTokenExpiresAt: this.getAccessTokenExpireAt(context.wabe.config),
|
|
299
|
-
refreshTokenEncrypted,
|
|
300
|
-
refreshTokenExpiresAt: this.getRefreshTokenExpireAt(context.wabe.config),
|
|
301
|
-
user: userId,
|
|
302
|
-
},
|
|
303
|
-
select: { id: true },
|
|
304
|
-
})
|
|
305
|
-
|
|
306
|
-
if (!res) throw new Error('Session not created')
|
|
307
|
-
|
|
308
|
-
const sessionId = res.id
|
|
309
|
-
const randomValue = crypto.randomBytes(16).toString('hex')
|
|
310
|
-
const message = `${sessionId.length}!${sessionId}!${randomValue.length}!${randomValue}`
|
|
311
|
-
|
|
312
|
-
const csrfSecret = context.wabe.config.authentication?.session?.csrfSecret || secretKey
|
|
313
|
-
|
|
314
|
-
const hmac = crypto.createHmac('sha256', csrfSecret).update(message).digest('hex')
|
|
315
|
-
|
|
316
|
-
const csrfToken = `${hmac}.${randomValue}`
|
|
317
|
-
|
|
318
|
-
return {
|
|
319
|
-
accessToken: this.accessToken,
|
|
320
|
-
refreshToken: this.refreshToken,
|
|
321
|
-
csrfToken,
|
|
322
|
-
sessionId: res.id,
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
async refresh(accessToken: string, refreshToken: string, context: WabeContext<DevWabeTypes>) {
|
|
327
|
-
const secretKey = getJwtSecret(context)
|
|
328
|
-
|
|
329
|
-
const verifyOptions = getJwtVerifyOptions(context)
|
|
330
|
-
|
|
331
|
-
if (!safeVerify(accessToken, secretKey, verifyOptions))
|
|
332
|
-
return {
|
|
333
|
-
accessToken: null,
|
|
334
|
-
refreshToken: null,
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
if (!safeVerify(refreshToken, secretKey, verifyOptions))
|
|
338
|
-
return {
|
|
339
|
-
accessToken: null,
|
|
340
|
-
refreshToken: null,
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
const accessTokenEncrypted = encryptDeterministicToken(
|
|
344
|
-
accessToken,
|
|
345
|
-
getTokenEncryptionKey(context),
|
|
346
|
-
)
|
|
347
|
-
const incomingRefreshTokenEncrypted = encryptDeterministicToken(
|
|
348
|
-
refreshToken,
|
|
349
|
-
getTokenEncryptionKey(context),
|
|
350
|
-
)
|
|
351
|
-
|
|
352
|
-
const session = await context.wabe.controllers.database.getObjects({
|
|
353
|
-
className: '_Session',
|
|
354
|
-
where: {
|
|
355
|
-
accessTokenEncrypted: { equalTo: accessTokenEncrypted },
|
|
356
|
-
refreshTokenEncrypted: { equalTo: incomingRefreshTokenEncrypted },
|
|
357
|
-
},
|
|
358
|
-
select: {
|
|
359
|
-
id: true,
|
|
360
|
-
user: {
|
|
361
|
-
id: true,
|
|
362
|
-
role: {
|
|
363
|
-
id: true,
|
|
364
|
-
name: true,
|
|
365
|
-
},
|
|
366
|
-
},
|
|
367
|
-
refreshTokenEncrypted: true,
|
|
368
|
-
refreshTokenExpiresAt: true,
|
|
369
|
-
},
|
|
370
|
-
context: contextWithRoot(context),
|
|
371
|
-
})
|
|
372
|
-
|
|
373
|
-
if (!session.length)
|
|
374
|
-
return {
|
|
375
|
-
accessToken: null,
|
|
376
|
-
refreshToken: null,
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
if (!session[0]) throw new Error('Session not found')
|
|
380
|
-
|
|
381
|
-
const {
|
|
382
|
-
refreshTokenExpiresAt,
|
|
383
|
-
user,
|
|
384
|
-
refreshTokenEncrypted: storedRefreshTokenEncrypted,
|
|
385
|
-
id,
|
|
386
|
-
} = session[0]
|
|
387
|
-
|
|
388
|
-
if (new Date(refreshTokenExpiresAt) < new Date(Date.now()))
|
|
389
|
-
throw new Error('Refresh token expired')
|
|
390
|
-
|
|
391
|
-
const decryptedRefreshToken =
|
|
392
|
-
decryptDeterministicToken(storedRefreshTokenEncrypted, getTokenEncryptionKey(context)) ||
|
|
393
|
-
refreshToken
|
|
394
|
-
|
|
395
|
-
if (!decryptedRefreshToken || decryptedRefreshToken !== refreshToken)
|
|
396
|
-
throw new Error('Invalid refresh token')
|
|
397
|
-
|
|
398
|
-
// Always rotate tokens on refresh
|
|
399
|
-
const userId = user?.id
|
|
400
|
-
|
|
401
|
-
if (!userId)
|
|
402
|
-
return {
|
|
403
|
-
accessToken: null,
|
|
404
|
-
refreshToken: null,
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
const jwtTokenFields = context.wabe.config.authentication?.session?.jwtTokenFields
|
|
408
|
-
|
|
409
|
-
const result = jwtTokenFields
|
|
410
|
-
? await context.wabe.controllers.database.getObject({
|
|
411
|
-
className: 'User',
|
|
412
|
-
select: jwtTokenFields,
|
|
413
|
-
context,
|
|
414
|
-
id: userId,
|
|
415
|
-
})
|
|
416
|
-
: undefined
|
|
417
|
-
|
|
418
|
-
const nowSeconds = Math.floor(Date.now() / 1000)
|
|
419
|
-
|
|
420
|
-
const signOptions: SignOptions = { jwtid: crypto.randomUUID() }
|
|
421
|
-
const audience = context.wabe.config.authentication?.session?.jwtAudience
|
|
422
|
-
const issuer = context.wabe.config.authentication?.session?.jwtIssuer
|
|
423
|
-
if (audience) signOptions.audience = audience
|
|
424
|
-
if (issuer) signOptions.issuer = issuer
|
|
425
|
-
|
|
426
|
-
const newAccessToken = jwt.sign(
|
|
427
|
-
{
|
|
428
|
-
userId,
|
|
429
|
-
user: result,
|
|
430
|
-
iat: nowSeconds,
|
|
431
|
-
exp: Math.floor(this.getAccessTokenExpireAt(context.wabe.config).getTime() / 1000),
|
|
432
|
-
},
|
|
433
|
-
secretKey,
|
|
434
|
-
signOptions,
|
|
435
|
-
)
|
|
436
|
-
|
|
437
|
-
const newRefreshToken = jwt.sign(
|
|
438
|
-
{
|
|
439
|
-
userId,
|
|
440
|
-
user: result,
|
|
441
|
-
iat: nowSeconds,
|
|
442
|
-
exp: Math.floor(this.getRefreshTokenExpireAt(context.wabe.config).getTime() / 1000),
|
|
443
|
-
},
|
|
444
|
-
secretKey,
|
|
445
|
-
signOptions,
|
|
446
|
-
)
|
|
447
|
-
|
|
448
|
-
const newAccessTokenEncrypted = encryptDeterministicToken(
|
|
449
|
-
newAccessToken,
|
|
450
|
-
getTokenEncryptionKey(context),
|
|
451
|
-
)
|
|
452
|
-
const newRefreshTokenEncrypted = encryptDeterministicToken(
|
|
453
|
-
newRefreshToken,
|
|
454
|
-
getTokenEncryptionKey(context),
|
|
455
|
-
)
|
|
456
|
-
|
|
457
|
-
await context.wabe.controllers.database.updateObject({
|
|
458
|
-
className: '_Session',
|
|
459
|
-
context: contextWithRoot(context),
|
|
460
|
-
id,
|
|
461
|
-
data: {
|
|
462
|
-
accessTokenEncrypted: newAccessTokenEncrypted,
|
|
463
|
-
accessTokenExpiresAt: this.getAccessTokenExpireAt(context.wabe.config),
|
|
464
|
-
refreshTokenEncrypted: newRefreshTokenEncrypted,
|
|
465
|
-
refreshTokenExpiresAt: this.getRefreshTokenExpireAt(context.wabe.config),
|
|
466
|
-
},
|
|
467
|
-
select: {},
|
|
468
|
-
})
|
|
469
|
-
|
|
470
|
-
return {
|
|
471
|
-
accessToken: newAccessToken,
|
|
472
|
-
refreshToken: newRefreshToken,
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
async delete(context: WabeContext<DevWabeTypes>) {
|
|
477
|
-
if (!context.sessionId) return
|
|
478
|
-
|
|
479
|
-
await context.wabe.controllers.database.deleteObject({
|
|
480
|
-
className: '_Session',
|
|
481
|
-
context: contextWithRoot(context),
|
|
482
|
-
id: context.sessionId,
|
|
483
|
-
select: {},
|
|
484
|
-
})
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
_isRefreshTokenExpired(userRefreshTokenExpiresAt: Date, refreshTokenAgeInMs: number) {
|
|
488
|
-
const refreshTokenEmittedAt = userRefreshTokenExpiresAt.getTime() - refreshTokenAgeInMs
|
|
489
|
-
const numberOfMsSinceRefreshTokenEmitted = Date.now() - refreshTokenEmittedAt
|
|
490
|
-
|
|
491
|
-
return numberOfMsSinceRefreshTokenEmitted >= 0.75 * refreshTokenAgeInMs
|
|
492
|
-
}
|
|
493
|
-
}
|
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
import type { WabeTypes } from '..'
|
|
2
|
-
import type { CustomAuthenticationMethods, ProviderInterface } from './interface'
|
|
3
|
-
import { GitHub, QRCodeOTP } from './providers'
|
|
4
|
-
import { Google } from './providers'
|
|
5
|
-
import { EmailOTP } from './providers/EmailOTP'
|
|
6
|
-
import { EmailPassword } from './providers/EmailPassword'
|
|
7
|
-
import { EmailPasswordSRPChallenge, EmailPasswordSRP } from './providers/EmailPasswordSRP'
|
|
8
|
-
import { PhonePassword } from './providers/PhonePassword'
|
|
9
|
-
|
|
10
|
-
export const defaultAuthenticationMethods = <T extends WabeTypes>(): CustomAuthenticationMethods<
|
|
11
|
-
T,
|
|
12
|
-
ProviderInterface<T>
|
|
13
|
-
>[] => [
|
|
14
|
-
{
|
|
15
|
-
name: 'emailPasswordSRPChallenge',
|
|
16
|
-
input: {
|
|
17
|
-
email: {
|
|
18
|
-
type: 'Email',
|
|
19
|
-
required: true,
|
|
20
|
-
},
|
|
21
|
-
clientPublic: {
|
|
22
|
-
type: 'String',
|
|
23
|
-
required: true,
|
|
24
|
-
},
|
|
25
|
-
clientSessionProof: {
|
|
26
|
-
type: 'String',
|
|
27
|
-
required: true,
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
// @ts-expect-error
|
|
31
|
-
provider: new EmailPasswordSRPChallenge(),
|
|
32
|
-
isSecondaryFactor: true,
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
name: 'emailPasswordSRP',
|
|
36
|
-
input: {
|
|
37
|
-
email: {
|
|
38
|
-
type: 'Email',
|
|
39
|
-
required: true,
|
|
40
|
-
},
|
|
41
|
-
clientPublic: {
|
|
42
|
-
type: 'String',
|
|
43
|
-
},
|
|
44
|
-
salt: {
|
|
45
|
-
type: 'String',
|
|
46
|
-
},
|
|
47
|
-
verifier: {
|
|
48
|
-
type: 'String',
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
dataToStore: {
|
|
52
|
-
email: {
|
|
53
|
-
type: 'Email',
|
|
54
|
-
required: true,
|
|
55
|
-
},
|
|
56
|
-
salt: {
|
|
57
|
-
type: 'String',
|
|
58
|
-
required: true,
|
|
59
|
-
},
|
|
60
|
-
verifier: {
|
|
61
|
-
type: 'String',
|
|
62
|
-
required: true,
|
|
63
|
-
},
|
|
64
|
-
serverSecret: {
|
|
65
|
-
type: 'String',
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
// @ts-expect-error
|
|
69
|
-
provider: new EmailPasswordSRP(),
|
|
70
|
-
},
|
|
71
|
-
{
|
|
72
|
-
name: 'emailOTP',
|
|
73
|
-
input: {
|
|
74
|
-
email: {
|
|
75
|
-
type: 'Email',
|
|
76
|
-
required: true,
|
|
77
|
-
},
|
|
78
|
-
otp: {
|
|
79
|
-
type: 'String',
|
|
80
|
-
required: true,
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
// @ts-expect-error
|
|
84
|
-
provider: new EmailOTP(),
|
|
85
|
-
isSecondaryFactor: true,
|
|
86
|
-
},
|
|
87
|
-
{
|
|
88
|
-
name: 'qrCodeOTP',
|
|
89
|
-
input: {
|
|
90
|
-
email: {
|
|
91
|
-
type: 'Email',
|
|
92
|
-
required: true,
|
|
93
|
-
},
|
|
94
|
-
otp: {
|
|
95
|
-
type: 'String',
|
|
96
|
-
required: true,
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
// @ts-expect-error
|
|
100
|
-
provider: new QRCodeOTP(),
|
|
101
|
-
isSecondaryFactor: true,
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
name: 'phonePassword',
|
|
105
|
-
input: {
|
|
106
|
-
phone: {
|
|
107
|
-
type: 'Phone',
|
|
108
|
-
required: true,
|
|
109
|
-
},
|
|
110
|
-
password: {
|
|
111
|
-
type: 'Hash',
|
|
112
|
-
required: true,
|
|
113
|
-
},
|
|
114
|
-
},
|
|
115
|
-
dataToStore: {
|
|
116
|
-
phone: {
|
|
117
|
-
type: 'Phone',
|
|
118
|
-
required: true,
|
|
119
|
-
},
|
|
120
|
-
password: {
|
|
121
|
-
type: 'Hash',
|
|
122
|
-
required: true,
|
|
123
|
-
},
|
|
124
|
-
},
|
|
125
|
-
// @ts-expect-error
|
|
126
|
-
provider: new PhonePassword(),
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
name: 'emailPassword',
|
|
130
|
-
input: {
|
|
131
|
-
email: {
|
|
132
|
-
type: 'Email',
|
|
133
|
-
required: true,
|
|
134
|
-
},
|
|
135
|
-
password: {
|
|
136
|
-
type: 'Hash',
|
|
137
|
-
required: true,
|
|
138
|
-
},
|
|
139
|
-
},
|
|
140
|
-
dataToStore: {
|
|
141
|
-
email: {
|
|
142
|
-
type: 'Email',
|
|
143
|
-
required: true,
|
|
144
|
-
},
|
|
145
|
-
password: {
|
|
146
|
-
type: 'Hash',
|
|
147
|
-
required: true,
|
|
148
|
-
},
|
|
149
|
-
},
|
|
150
|
-
// @ts-expect-error
|
|
151
|
-
provider: new EmailPassword(),
|
|
152
|
-
},
|
|
153
|
-
{
|
|
154
|
-
name: 'google',
|
|
155
|
-
input: {
|
|
156
|
-
authorizationCode: {
|
|
157
|
-
type: 'String',
|
|
158
|
-
required: true,
|
|
159
|
-
},
|
|
160
|
-
codeVerifier: {
|
|
161
|
-
type: 'String',
|
|
162
|
-
required: true,
|
|
163
|
-
},
|
|
164
|
-
},
|
|
165
|
-
dataToStore: {
|
|
166
|
-
email: {
|
|
167
|
-
type: 'Email',
|
|
168
|
-
required: true,
|
|
169
|
-
},
|
|
170
|
-
verifiedEmail: {
|
|
171
|
-
type: 'Boolean',
|
|
172
|
-
required: true,
|
|
173
|
-
},
|
|
174
|
-
},
|
|
175
|
-
// There is no signUp method for Google provider
|
|
176
|
-
// @ts-expect-error
|
|
177
|
-
provider: new Google(),
|
|
178
|
-
},
|
|
179
|
-
{
|
|
180
|
-
name: 'github',
|
|
181
|
-
input: {
|
|
182
|
-
authorizationCode: {
|
|
183
|
-
type: 'String',
|
|
184
|
-
required: true,
|
|
185
|
-
},
|
|
186
|
-
codeVerifier: {
|
|
187
|
-
type: 'String',
|
|
188
|
-
required: true,
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
dataToStore: {
|
|
192
|
-
email: {
|
|
193
|
-
type: 'Email',
|
|
194
|
-
required: true,
|
|
195
|
-
},
|
|
196
|
-
avatarUrl: {
|
|
197
|
-
type: 'String',
|
|
198
|
-
required: true,
|
|
199
|
-
},
|
|
200
|
-
username: {
|
|
201
|
-
type: 'String',
|
|
202
|
-
required: true,
|
|
203
|
-
},
|
|
204
|
-
},
|
|
205
|
-
// There is no signUp method for Google provider
|
|
206
|
-
// @ts-expect-error
|
|
207
|
-
provider: new GitHub(),
|
|
208
|
-
},
|
|
209
|
-
]
|