wabe 0.6.12 → 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/package.json +4 -1
- package/dev/index.ts +0 -215
- package/generated/schema.graphql +0 -1945
- package/generated/wabe.ts +0 -448
- 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 -517
- package/src/authentication/cookies.ts +0 -10
- package/src/authentication/defaultAuthentication.ts +0 -209
- package/src/authentication/index.ts +0 -4
- package/src/authentication/interface.ts +0 -177
- 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 -95
- package/src/authentication/providers/EmailPassword.test.ts +0 -263
- package/src/authentication/providers/EmailPassword.ts +0 -138
- package/src/authentication/providers/EmailPasswordSRP.test.ts +0 -208
- package/src/authentication/providers/EmailPasswordSRP.ts +0 -191
- 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 -221
- package/src/authentication/providers/PhonePassword.ts +0 -136
- package/src/authentication/providers/QRCodeOTP.test.ts +0 -77
- package/src/authentication/providers/QRCodeOTP.ts +0 -69
- 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 -306
- package/src/authentication/resolvers/signInWithResolver.ts +0 -106
- 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 -68
- package/src/authentication/resolvers/verifyChallenge.test.ts +0 -230
- package/src/authentication/resolvers/verifyChallenge.ts +0 -78
- package/src/authentication/roles.test.ts +0 -49
- package/src/authentication/roles.ts +0 -40
- package/src/authentication/security.ts +0 -278
- 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 -1415
- package/src/database/index.test.ts +0 -1551
- package/src/database/index.ts +0 -9
- package/src/database/interface.ts +0 -308
- 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 -52
- package/src/file/index.test.ts +0 -1031
- package/src/file/index.ts +0 -2
- package/src/file/interface.ts +0 -63
- package/src/file/security.ts +0 -156
- package/src/graphql/GraphQLSchema.test.ts +0 -5099
- package/src/graphql/GraphQLSchema.ts +0 -886
- 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 -707
- package/src/graphql/pointerAndRelationFunction.ts +0 -191
- package/src/graphql/resolvers.ts +0 -464
- 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 -420
- 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 -74
- 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/hooks/virtualFields.test.ts +0 -228
- package/src/hooks/virtualFields.ts +0 -48
- package/src/index.ts +0 -9
- package/src/schema/Schema.test.ts +0 -482
- package/src/schema/Schema.ts +0 -839
- 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 -4136
- package/src/server/defaultSessionHandler.test.ts +0 -62
- package/src/server/defaultSessionHandler.ts +0 -104
- package/src/server/generateCodegen.ts +0 -433
- package/src/server/index.test.ts +0 -843
- package/src/server/index.ts +0 -336
- package/src/server/interface.ts +0 -11
- package/src/server/routes/authHandler.ts +0 -171
- package/src/server/routes/index.ts +0 -48
- package/src/utils/crypto.test.ts +0 -41
- package/src/utils/crypto.ts +0 -105
- package/src/utils/database.ts +0 -8
- package/src/utils/export.ts +0 -12
- package/src/utils/helper.ts +0 -204
- package/src/utils/index.test.ts +0 -11
- package/src/utils/index.ts +0 -196
- package/src/utils/preload.ts +0 -8
- package/src/utils/testHelper.ts +0 -124
- package/tsconfig.json +0 -32
|
@@ -1,629 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, mock, beforeEach } from 'bun:test'
|
|
2
|
-
import { fail } from 'node:assert'
|
|
3
|
-
import crypto from 'node:crypto'
|
|
4
|
-
import jwt, { type JwtPayload } from 'jsonwebtoken'
|
|
5
|
-
import { Session } from './Session'
|
|
6
|
-
|
|
7
|
-
const encryptToken = (token: string, secret: string) => {
|
|
8
|
-
const key = crypto.createHash('sha256').update(secret).digest()
|
|
9
|
-
const iv = crypto.createHmac('sha256', key).update(token).digest().subarray(0, 12)
|
|
10
|
-
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv)
|
|
11
|
-
const encrypted = Buffer.concat([cipher.update(token, 'utf8'), cipher.final()])
|
|
12
|
-
const tag = cipher.getAuthTag()
|
|
13
|
-
return `${iv.toString('hex')}:${tag.toString('hex')}:${encrypted.toString('hex')}`
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
describe('Session', () => {
|
|
17
|
-
const mockGetObject = mock(() => Promise.resolve({}) as any)
|
|
18
|
-
const mockGetObjects = mock(() => Promise.resolve([]) as any)
|
|
19
|
-
const mockCreateObject = mock(() => Promise.resolve({ id: 'userId' })) as any
|
|
20
|
-
const mockDeleteObject = mock(() => Promise.resolve()) as any
|
|
21
|
-
const mockUpdateObject = mock(() => Promise.resolve()) as any
|
|
22
|
-
|
|
23
|
-
const controllers = {
|
|
24
|
-
database: {
|
|
25
|
-
getObject: mockGetObject,
|
|
26
|
-
getObjects: mockGetObjects,
|
|
27
|
-
createObject: mockCreateObject,
|
|
28
|
-
deleteObject: mockDeleteObject,
|
|
29
|
-
updateObject: mockUpdateObject,
|
|
30
|
-
},
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
beforeEach(() => {
|
|
34
|
-
mockGetObject.mockClear()
|
|
35
|
-
mockGetObjects.mockClear()
|
|
36
|
-
mockCreateObject.mockClear()
|
|
37
|
-
mockDeleteObject.mockClear()
|
|
38
|
-
mockUpdateObject.mockClear()
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
const context = {
|
|
42
|
-
isRoot: true,
|
|
43
|
-
wabe: {
|
|
44
|
-
controllers,
|
|
45
|
-
config: { authentication: { session: { jwtSecret: 'dev' } } },
|
|
46
|
-
},
|
|
47
|
-
} as any
|
|
48
|
-
|
|
49
|
-
it('should set all data set in the jwtTokenFields on create session', async () => {
|
|
50
|
-
mockGetObject.mockResolvedValueOnce({
|
|
51
|
-
id: 'userId',
|
|
52
|
-
email: 'user@email.com',
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
const session = new Session<any>()
|
|
56
|
-
|
|
57
|
-
const jwtTokenFields = {
|
|
58
|
-
id: true,
|
|
59
|
-
email: true,
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const { accessToken, refreshToken } = await session.create('userId', {
|
|
63
|
-
isRoot: true,
|
|
64
|
-
wabe: {
|
|
65
|
-
controllers,
|
|
66
|
-
config: {
|
|
67
|
-
authentication: {
|
|
68
|
-
session: {
|
|
69
|
-
jwtSecret: 'dev',
|
|
70
|
-
jwtTokenFields,
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
} as any)
|
|
76
|
-
|
|
77
|
-
const decodedAccessToken = jwt.decode(accessToken) as JwtPayload
|
|
78
|
-
const decodedRefreshToken = jwt.decode(refreshToken) as JwtPayload
|
|
79
|
-
|
|
80
|
-
expect(decodedAccessToken.user).toEqual({
|
|
81
|
-
id: 'userId',
|
|
82
|
-
email: 'user@email.com',
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
expect(decodedRefreshToken.user).toEqual({
|
|
86
|
-
id: 'userId',
|
|
87
|
-
email: 'user@email.com',
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
expect(mockCreateObject).toHaveBeenCalledWith({
|
|
91
|
-
className: '_Session',
|
|
92
|
-
context: expect.any(Object),
|
|
93
|
-
data: {
|
|
94
|
-
accessTokenEncrypted: expect.any(String),
|
|
95
|
-
accessTokenExpiresAt: expect.any(Date),
|
|
96
|
-
refreshTokenEncrypted: expect.any(String),
|
|
97
|
-
refreshTokenExpiresAt: expect.any(Date),
|
|
98
|
-
user: 'userId',
|
|
99
|
-
},
|
|
100
|
-
select: { id: true },
|
|
101
|
-
})
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
it('should set all data set in the jwtTokenFields on refresh session', async () => {
|
|
105
|
-
const session = new Session<any>()
|
|
106
|
-
|
|
107
|
-
const { accessToken: oldAccessToken, refreshToken: oldRefreshToken } = await session.create(
|
|
108
|
-
'userId',
|
|
109
|
-
context,
|
|
110
|
-
)
|
|
111
|
-
|
|
112
|
-
mockGetObjects.mockResolvedValue([
|
|
113
|
-
{
|
|
114
|
-
id: 'sessionId',
|
|
115
|
-
refreshTokenEncrypted: encryptToken(oldRefreshToken, 'dev'),
|
|
116
|
-
refreshTokenExpiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
|
|
117
|
-
user: {
|
|
118
|
-
id: 'userId',
|
|
119
|
-
email: 'userEmail',
|
|
120
|
-
},
|
|
121
|
-
},
|
|
122
|
-
])
|
|
123
|
-
mockGetObject.mockResolvedValue({
|
|
124
|
-
id: 'userId',
|
|
125
|
-
email: 'user@email.com',
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
const jwtTokenFields = {
|
|
129
|
-
id: true,
|
|
130
|
-
email: true,
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const { accessToken, refreshToken } = await session.refresh(oldAccessToken, oldRefreshToken, {
|
|
134
|
-
isRoot: true,
|
|
135
|
-
wabe: {
|
|
136
|
-
controllers,
|
|
137
|
-
config: {
|
|
138
|
-
authentication: {
|
|
139
|
-
session: {
|
|
140
|
-
jwtSecret: 'dev',
|
|
141
|
-
jwtTokenFields,
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
},
|
|
145
|
-
},
|
|
146
|
-
} as any)
|
|
147
|
-
|
|
148
|
-
if (!accessToken || !refreshToken) fail()
|
|
149
|
-
|
|
150
|
-
const decodedAccessToken = jwt.decode(accessToken) as JwtPayload
|
|
151
|
-
const decodedRefreshToken = jwt.decode(refreshToken) as JwtPayload
|
|
152
|
-
|
|
153
|
-
expect(decodedAccessToken.user).toEqual({
|
|
154
|
-
id: 'userId',
|
|
155
|
-
email: 'user@email.com',
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
expect(decodedRefreshToken.user).toEqual({
|
|
159
|
-
id: 'userId',
|
|
160
|
-
email: 'user@email.com',
|
|
161
|
-
})
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
it('should not set user fields if not jwtTokenFields is set on create session', async () => {
|
|
165
|
-
mockGetObject.mockResolvedValueOnce({
|
|
166
|
-
id: 'userId',
|
|
167
|
-
email: 'user@email.com',
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
const session = new Session<any>()
|
|
171
|
-
|
|
172
|
-
const { accessToken, refreshToken } = await session.create('userId', context)
|
|
173
|
-
|
|
174
|
-
const decodedAccessToken = jwt.decode(accessToken) as JwtPayload
|
|
175
|
-
const decodedRefreshToken = jwt.decode(refreshToken) as JwtPayload
|
|
176
|
-
|
|
177
|
-
expect(decodedAccessToken.user).toBeUndefined()
|
|
178
|
-
|
|
179
|
-
expect(decodedRefreshToken.user).toBeUndefined()
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
it('should not set user fields if not jwtTokenFields is set on refresh session', async () => {
|
|
183
|
-
const session = new Session<any>()
|
|
184
|
-
|
|
185
|
-
const { accessToken: oldAccessToken, refreshToken: oldRefreshToken } = await session.create(
|
|
186
|
-
'userId',
|
|
187
|
-
context,
|
|
188
|
-
)
|
|
189
|
-
|
|
190
|
-
mockGetObjects.mockResolvedValue([
|
|
191
|
-
{
|
|
192
|
-
id: 'sessionId',
|
|
193
|
-
refreshTokenEncrypted: encryptToken(oldRefreshToken, 'dev'),
|
|
194
|
-
refreshTokenExpiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
|
|
195
|
-
user: {
|
|
196
|
-
id: 'userId',
|
|
197
|
-
email: 'userEmail',
|
|
198
|
-
},
|
|
199
|
-
},
|
|
200
|
-
])
|
|
201
|
-
mockGetObject.mockResolvedValue({
|
|
202
|
-
id: 'userId',
|
|
203
|
-
email: 'user@email.com',
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
const { accessToken, refreshToken } = await session.refresh(
|
|
207
|
-
oldAccessToken,
|
|
208
|
-
oldRefreshToken,
|
|
209
|
-
context,
|
|
210
|
-
)
|
|
211
|
-
|
|
212
|
-
if (!accessToken || !refreshToken) fail()
|
|
213
|
-
|
|
214
|
-
const decodedAccessToken = jwt.decode(accessToken) as JwtPayload
|
|
215
|
-
const decodedRefreshToken = jwt.decode(refreshToken) as JwtPayload
|
|
216
|
-
|
|
217
|
-
expect(decodedAccessToken.user).toBeUndefined()
|
|
218
|
-
|
|
219
|
-
expect(decodedRefreshToken.user).toBeUndefined()
|
|
220
|
-
})
|
|
221
|
-
|
|
222
|
-
it('should returns null if no user found', async () => {
|
|
223
|
-
mockGetObjects.mockResolvedValue([])
|
|
224
|
-
|
|
225
|
-
const session = new Session<any>()
|
|
226
|
-
|
|
227
|
-
const { accessToken } = await session.create('userId', context)
|
|
228
|
-
|
|
229
|
-
const res = await session.meFromAccessToken({ accessToken, csrfToken: '' }, context)
|
|
230
|
-
|
|
231
|
-
expect(res.user).toBeNull()
|
|
232
|
-
expect(res.sessionId).toBeNull()
|
|
233
|
-
|
|
234
|
-
expect(mockGetObjects).toHaveBeenCalledTimes(1)
|
|
235
|
-
expect(mockGetObjects).toHaveBeenCalledWith({
|
|
236
|
-
className: '_Session',
|
|
237
|
-
where: {
|
|
238
|
-
accessTokenEncrypted: { equalTo: encryptToken(accessToken, 'dev') },
|
|
239
|
-
OR: [
|
|
240
|
-
{
|
|
241
|
-
accessTokenExpiresAt: {
|
|
242
|
-
greaterThanOrEqualTo: expect.any(Date),
|
|
243
|
-
},
|
|
244
|
-
},
|
|
245
|
-
{
|
|
246
|
-
refreshTokenExpiresAt: {
|
|
247
|
-
greaterThanOrEqualTo: expect.any(Date),
|
|
248
|
-
},
|
|
249
|
-
},
|
|
250
|
-
],
|
|
251
|
-
},
|
|
252
|
-
first: 1,
|
|
253
|
-
select: {
|
|
254
|
-
id: true,
|
|
255
|
-
user: true,
|
|
256
|
-
accessTokenExpiresAt: true,
|
|
257
|
-
refreshTokenExpiresAt: true,
|
|
258
|
-
refreshTokenEncrypted: true,
|
|
259
|
-
},
|
|
260
|
-
context: expect.any(Object),
|
|
261
|
-
})
|
|
262
|
-
})
|
|
263
|
-
|
|
264
|
-
it('should return the user associated with an access token', async () => {
|
|
265
|
-
mockGetObjects.mockResolvedValue([
|
|
266
|
-
{
|
|
267
|
-
id: 'sessionId',
|
|
268
|
-
refreshTokenEncrypted: encryptToken('refreshToken', 'dev'),
|
|
269
|
-
user: {
|
|
270
|
-
id: 'userId',
|
|
271
|
-
email: 'userEmail',
|
|
272
|
-
},
|
|
273
|
-
refreshTokenExpiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30),
|
|
274
|
-
},
|
|
275
|
-
])
|
|
276
|
-
|
|
277
|
-
const session = new Session<any>()
|
|
278
|
-
|
|
279
|
-
const { accessToken } = await session.create('userId', context)
|
|
280
|
-
|
|
281
|
-
const { sessionId, user } = await session.meFromAccessToken(
|
|
282
|
-
{ accessToken, csrfToken: '' },
|
|
283
|
-
{
|
|
284
|
-
...context,
|
|
285
|
-
wabe: {
|
|
286
|
-
...context.wabe,
|
|
287
|
-
config: {
|
|
288
|
-
...context.wabe.config,
|
|
289
|
-
security: { disableCSRFProtection: true },
|
|
290
|
-
},
|
|
291
|
-
},
|
|
292
|
-
},
|
|
293
|
-
)
|
|
294
|
-
|
|
295
|
-
expect(mockGetObjects).toHaveBeenCalledTimes(1)
|
|
296
|
-
expect(mockGetObjects).toHaveBeenCalledWith({
|
|
297
|
-
className: '_Session',
|
|
298
|
-
where: {
|
|
299
|
-
accessTokenEncrypted: { equalTo: encryptToken(accessToken, 'dev') },
|
|
300
|
-
OR: [
|
|
301
|
-
{
|
|
302
|
-
accessTokenExpiresAt: {
|
|
303
|
-
greaterThanOrEqualTo: expect.any(Date),
|
|
304
|
-
},
|
|
305
|
-
},
|
|
306
|
-
{
|
|
307
|
-
refreshTokenExpiresAt: {
|
|
308
|
-
greaterThanOrEqualTo: expect.any(Date),
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
],
|
|
312
|
-
},
|
|
313
|
-
first: 1,
|
|
314
|
-
select: {
|
|
315
|
-
id: true,
|
|
316
|
-
user: true,
|
|
317
|
-
accessTokenExpiresAt: true,
|
|
318
|
-
refreshTokenExpiresAt: true,
|
|
319
|
-
refreshTokenEncrypted: true,
|
|
320
|
-
},
|
|
321
|
-
context: expect.any(Object),
|
|
322
|
-
})
|
|
323
|
-
|
|
324
|
-
expect(sessionId).toEqual('sessionId')
|
|
325
|
-
expect(user?.id).toEqual('userId')
|
|
326
|
-
expect(user?.email).toEqual('userEmail')
|
|
327
|
-
})
|
|
328
|
-
|
|
329
|
-
it('should create a new session', async () => {
|
|
330
|
-
const session = new Session<any>()
|
|
331
|
-
|
|
332
|
-
const fifteenMinutes = new Date(Date.now() + 1000 * 60 * 15)
|
|
333
|
-
const sevenDays = new Date(Date.now() + 1000 * 60 * 60 * 24 * 7)
|
|
334
|
-
|
|
335
|
-
const { accessToken, refreshToken } = await session.create('userId', context)
|
|
336
|
-
|
|
337
|
-
expect(accessToken).not.toBeUndefined()
|
|
338
|
-
expect(refreshToken).not.toBeUndefined()
|
|
339
|
-
|
|
340
|
-
if (!accessToken || !refreshToken) fail()
|
|
341
|
-
|
|
342
|
-
const decodedAccessToken = jwt.decode(accessToken) as JwtPayload
|
|
343
|
-
const decodedRefreshToken = jwt.decode(refreshToken) as JwtPayload
|
|
344
|
-
|
|
345
|
-
expect(decodedAccessToken).not.toBeNull()
|
|
346
|
-
expect(decodedAccessToken.userId).toEqual('userId')
|
|
347
|
-
expect(decodedAccessToken.exp).toBeGreaterThanOrEqual(
|
|
348
|
-
Math.floor(fifteenMinutes.getTime() / 1000),
|
|
349
|
-
)
|
|
350
|
-
expect(decodedAccessToken.iat).toBeGreaterThanOrEqual(Math.floor((Date.now() - 500) / 1000)) // minus 500ms to avoid flaky
|
|
351
|
-
|
|
352
|
-
expect(decodedRefreshToken).not.toBeNull()
|
|
353
|
-
expect(decodedRefreshToken.userId).toEqual('userId')
|
|
354
|
-
expect(decodedRefreshToken.exp).toBeGreaterThanOrEqual(Math.floor(sevenDays.getTime() / 1000))
|
|
355
|
-
expect(decodedRefreshToken.iat).toBeGreaterThanOrEqual(Math.floor((Date.now() - 500) / 1000)) // minus 500ms to avoid flaky
|
|
356
|
-
|
|
357
|
-
expect(mockCreateObject).toHaveBeenCalledTimes(1)
|
|
358
|
-
expect(mockCreateObject).toHaveBeenCalledWith({
|
|
359
|
-
className: '_Session',
|
|
360
|
-
context: expect.any(Object),
|
|
361
|
-
data: {
|
|
362
|
-
accessTokenEncrypted: expect.any(String),
|
|
363
|
-
accessTokenExpiresAt: expect.any(Date),
|
|
364
|
-
refreshTokenEncrypted: expect.any(String),
|
|
365
|
-
refreshTokenExpiresAt: expect.any(Date),
|
|
366
|
-
user: 'userId',
|
|
367
|
-
},
|
|
368
|
-
select: { id: true },
|
|
369
|
-
})
|
|
370
|
-
})
|
|
371
|
-
|
|
372
|
-
it('should delete a session', async () => {
|
|
373
|
-
const session = new Session<any>()
|
|
374
|
-
|
|
375
|
-
await session.delete({
|
|
376
|
-
sessionId: 'sessionId',
|
|
377
|
-
wabe: {
|
|
378
|
-
controllers,
|
|
379
|
-
},
|
|
380
|
-
} as any)
|
|
381
|
-
|
|
382
|
-
expect(mockDeleteObject).toHaveBeenCalledTimes(1)
|
|
383
|
-
expect(mockDeleteObject).toHaveBeenCalledWith({
|
|
384
|
-
className: '_Session',
|
|
385
|
-
context: {
|
|
386
|
-
sessionId: 'sessionId',
|
|
387
|
-
wabe: { controllers },
|
|
388
|
-
isRoot: true,
|
|
389
|
-
},
|
|
390
|
-
id: 'sessionId',
|
|
391
|
-
select: {},
|
|
392
|
-
})
|
|
393
|
-
})
|
|
394
|
-
|
|
395
|
-
it('should refresh a session', async () => {
|
|
396
|
-
const session = new Session<any>()
|
|
397
|
-
|
|
398
|
-
const { accessToken: oldAccessToken, refreshToken: oldRefreshToken } = await session.create(
|
|
399
|
-
'userId',
|
|
400
|
-
context,
|
|
401
|
-
)
|
|
402
|
-
|
|
403
|
-
mockGetObjects.mockResolvedValue([
|
|
404
|
-
{
|
|
405
|
-
id: 'sessionId',
|
|
406
|
-
refreshTokenEncrypted: encryptToken(oldRefreshToken, 'dev'),
|
|
407
|
-
refreshTokenExpiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
|
|
408
|
-
user: {
|
|
409
|
-
id: 'userId',
|
|
410
|
-
email: 'userEmail',
|
|
411
|
-
},
|
|
412
|
-
},
|
|
413
|
-
])
|
|
414
|
-
|
|
415
|
-
const { accessToken, refreshToken } = await session.refresh(
|
|
416
|
-
oldAccessToken,
|
|
417
|
-
oldRefreshToken,
|
|
418
|
-
context,
|
|
419
|
-
)
|
|
420
|
-
|
|
421
|
-
expect(accessToken).not.toBeUndefined()
|
|
422
|
-
expect(refreshToken).not.toBeUndefined()
|
|
423
|
-
|
|
424
|
-
expect(mockGetObjects).toHaveBeenCalledTimes(1)
|
|
425
|
-
expect(mockGetObjects).toHaveBeenCalledWith({
|
|
426
|
-
className: '_Session',
|
|
427
|
-
where: {
|
|
428
|
-
accessTokenEncrypted: { equalTo: encryptToken(oldAccessToken, 'dev') },
|
|
429
|
-
refreshTokenEncrypted: {
|
|
430
|
-
equalTo: encryptToken(oldRefreshToken, 'dev'),
|
|
431
|
-
},
|
|
432
|
-
},
|
|
433
|
-
select: {
|
|
434
|
-
id: true,
|
|
435
|
-
user: {
|
|
436
|
-
id: true,
|
|
437
|
-
role: {
|
|
438
|
-
id: true,
|
|
439
|
-
name: true,
|
|
440
|
-
},
|
|
441
|
-
},
|
|
442
|
-
refreshTokenEncrypted: true,
|
|
443
|
-
refreshTokenExpiresAt: true,
|
|
444
|
-
},
|
|
445
|
-
context: expect.any(Object),
|
|
446
|
-
})
|
|
447
|
-
|
|
448
|
-
expect(mockUpdateObject).toHaveBeenCalledTimes(1)
|
|
449
|
-
expect(mockUpdateObject).toHaveBeenCalledWith({
|
|
450
|
-
className: '_Session',
|
|
451
|
-
context: expect.any(Object),
|
|
452
|
-
id: 'sessionId',
|
|
453
|
-
data: {
|
|
454
|
-
accessTokenEncrypted: expect.any(String),
|
|
455
|
-
accessTokenExpiresAt: expect.any(Date),
|
|
456
|
-
refreshTokenEncrypted: expect.any(String),
|
|
457
|
-
refreshTokenExpiresAt: expect.any(Date),
|
|
458
|
-
},
|
|
459
|
-
select: {},
|
|
460
|
-
})
|
|
461
|
-
|
|
462
|
-
const accessTokenExpiresAt = mockUpdateObject.mock.calls[0][0].data.accessTokenExpiresAt as Date
|
|
463
|
-
|
|
464
|
-
const refreshTokenExpiresAt = mockUpdateObject.mock.calls[0][0].data
|
|
465
|
-
.refreshTokenExpiresAt as Date
|
|
466
|
-
|
|
467
|
-
// -1000 to avoid flaky
|
|
468
|
-
expect(accessTokenExpiresAt.getTime()).toBeGreaterThan(Date.now() + 1000 * 60 * 15 - 1000)
|
|
469
|
-
|
|
470
|
-
// -1000 to avoid flaky
|
|
471
|
-
expect(refreshTokenExpiresAt.getTime()).toBeGreaterThan(
|
|
472
|
-
Date.now() + 1000 * 60 * 60 * 24 * 7 - 1000,
|
|
473
|
-
)
|
|
474
|
-
})
|
|
475
|
-
|
|
476
|
-
it('should return null if access token is invalid (malformed)', async () => {
|
|
477
|
-
const session = new Session<any>()
|
|
478
|
-
|
|
479
|
-
const res = await session.meFromAccessToken(
|
|
480
|
-
{ accessToken: 'not-a-jwt', csrfToken: '' },
|
|
481
|
-
context,
|
|
482
|
-
)
|
|
483
|
-
|
|
484
|
-
expect(res.accessToken).toBeNull()
|
|
485
|
-
expect(res.user).toBeNull()
|
|
486
|
-
expect(res.sessionId).toBeNull()
|
|
487
|
-
})
|
|
488
|
-
|
|
489
|
-
it('should enforce CSRF by default when cookies are used', async () => {
|
|
490
|
-
const session = new Session<any>()
|
|
491
|
-
|
|
492
|
-
mockGetObjects.mockResolvedValue([
|
|
493
|
-
{
|
|
494
|
-
id: 'sessionId',
|
|
495
|
-
refreshTokenEncrypted: encryptToken('refreshToken', 'dev'),
|
|
496
|
-
refreshTokenExpiresAt: new Date(Date.now() + 1000 * 60 * 60),
|
|
497
|
-
accessTokenExpiresAt: new Date(Date.now() + 1000 * 60 * 15),
|
|
498
|
-
user: {
|
|
499
|
-
id: 'userId',
|
|
500
|
-
},
|
|
501
|
-
},
|
|
502
|
-
])
|
|
503
|
-
|
|
504
|
-
const res = await session.meFromAccessToken({ accessToken: 'valid.jwt.token', csrfToken: '' }, {
|
|
505
|
-
isRoot: true,
|
|
506
|
-
wabe: {
|
|
507
|
-
controllers,
|
|
508
|
-
config: {
|
|
509
|
-
authentication: {
|
|
510
|
-
session: { jwtSecret: 'dev' },
|
|
511
|
-
},
|
|
512
|
-
security: {
|
|
513
|
-
// disableCSRFProtection undefined => protection ON
|
|
514
|
-
},
|
|
515
|
-
},
|
|
516
|
-
},
|
|
517
|
-
} as any)
|
|
518
|
-
|
|
519
|
-
expect(res.accessToken).toBeNull()
|
|
520
|
-
expect(res.user).toBeNull()
|
|
521
|
-
expect(res.sessionId).toBeNull()
|
|
522
|
-
})
|
|
523
|
-
|
|
524
|
-
it('should not refresh session if the access token does not already take 75% of time', () => {
|
|
525
|
-
const session = new Session<any>()
|
|
526
|
-
|
|
527
|
-
// 1 hour
|
|
528
|
-
const refreshTokenAgeInMs = 1000 * 60 * 60
|
|
529
|
-
|
|
530
|
-
// Expires in 1 hour
|
|
531
|
-
const date1 = new Date(Date.now() + 1000 * 60 * 60)
|
|
532
|
-
// Expires in 20 minutes
|
|
533
|
-
const date2 = new Date(Date.now() + 1000 * 60 * 15)
|
|
534
|
-
// Expired since 20 minutes
|
|
535
|
-
const date3 = new Date(Date.now() - 1000 * 60 * 20)
|
|
536
|
-
|
|
537
|
-
expect(session._isRefreshTokenExpired(date1, refreshTokenAgeInMs)).toBe(false)
|
|
538
|
-
expect(session._isRefreshTokenExpired(date2, refreshTokenAgeInMs)).toBe(true)
|
|
539
|
-
expect(session._isRefreshTokenExpired(date3, refreshTokenAgeInMs)).toBe(true)
|
|
540
|
-
})
|
|
541
|
-
|
|
542
|
-
it('should return null on refresh session if session not found', async () => {
|
|
543
|
-
mockGetObjects.mockResolvedValue([])
|
|
544
|
-
|
|
545
|
-
const session = new Session<any>()
|
|
546
|
-
|
|
547
|
-
const { accessToken: oldAccessToken, refreshToken: oldRefreshToken } = await session.create(
|
|
548
|
-
'userId',
|
|
549
|
-
context,
|
|
550
|
-
)
|
|
551
|
-
|
|
552
|
-
const { accessToken, refreshToken } = await session.refresh(
|
|
553
|
-
oldAccessToken,
|
|
554
|
-
oldRefreshToken,
|
|
555
|
-
context,
|
|
556
|
-
)
|
|
557
|
-
|
|
558
|
-
expect(accessToken).toBeNull()
|
|
559
|
-
expect(refreshToken).toBeNull()
|
|
560
|
-
|
|
561
|
-
expect(mockGetObjects).toHaveBeenCalledTimes(1)
|
|
562
|
-
expect(mockGetObjects).toHaveBeenCalledWith({
|
|
563
|
-
className: '_Session',
|
|
564
|
-
where: {
|
|
565
|
-
accessTokenEncrypted: { equalTo: encryptToken(oldAccessToken, 'dev') },
|
|
566
|
-
refreshTokenEncrypted: {
|
|
567
|
-
equalTo: encryptToken(oldRefreshToken, 'dev'),
|
|
568
|
-
},
|
|
569
|
-
},
|
|
570
|
-
select: {
|
|
571
|
-
id: true,
|
|
572
|
-
user: {
|
|
573
|
-
id: true,
|
|
574
|
-
role: {
|
|
575
|
-
id: true,
|
|
576
|
-
name: true,
|
|
577
|
-
},
|
|
578
|
-
},
|
|
579
|
-
refreshTokenEncrypted: true,
|
|
580
|
-
refreshTokenExpiresAt: true,
|
|
581
|
-
},
|
|
582
|
-
context: expect.any(Object),
|
|
583
|
-
})
|
|
584
|
-
})
|
|
585
|
-
|
|
586
|
-
it("should throw an error on refresh session if session's refresh token is expired", async () => {
|
|
587
|
-
mockGetObjects.mockResolvedValue([
|
|
588
|
-
{
|
|
589
|
-
id: 'sessionId',
|
|
590
|
-
refreshTokenEncrypted: encryptToken('refreshToken', 'dev'),
|
|
591
|
-
refreshTokenExpiresAt: new Date(Date.now() - 1000),
|
|
592
|
-
user: {
|
|
593
|
-
id: 'userId',
|
|
594
|
-
email: 'userEmail',
|
|
595
|
-
},
|
|
596
|
-
},
|
|
597
|
-
])
|
|
598
|
-
|
|
599
|
-
const session = new Session<any>()
|
|
600
|
-
|
|
601
|
-
const { refreshToken, accessToken } = await session.create('userId', context)
|
|
602
|
-
|
|
603
|
-
expect(session.refresh(accessToken, refreshToken, context)).rejects.toThrow(
|
|
604
|
-
'Refresh token expired',
|
|
605
|
-
)
|
|
606
|
-
})
|
|
607
|
-
|
|
608
|
-
it("should throw an error on refresh session if session's refresh token is not the same as the one in the database", async () => {
|
|
609
|
-
mockGetObjects.mockResolvedValue([
|
|
610
|
-
{
|
|
611
|
-
id: 'sessionId',
|
|
612
|
-
refreshTokenEncrypted: encryptToken('otherRefreshToken', 'dev'),
|
|
613
|
-
refreshTokenExpiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
|
|
614
|
-
user: {
|
|
615
|
-
id: 'userId',
|
|
616
|
-
email: 'userEmail',
|
|
617
|
-
},
|
|
618
|
-
},
|
|
619
|
-
])
|
|
620
|
-
|
|
621
|
-
const session = new Session<any>()
|
|
622
|
-
|
|
623
|
-
const { refreshToken, accessToken } = await session.create('userId', context)
|
|
624
|
-
|
|
625
|
-
expect(session.refresh(accessToken, refreshToken, context)).rejects.toThrow(
|
|
626
|
-
'Invalid refresh token',
|
|
627
|
-
)
|
|
628
|
-
})
|
|
629
|
-
})
|