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,181 @@
|
|
|
1
|
+
import { describe, beforeAll, afterAll, expect, it, beforeEach } from 'bun:test'
|
|
2
|
+
import type { Wabe } from '../server'
|
|
3
|
+
import { type DevWabeTypes, getGraphqlClient } from '../utils/helper'
|
|
4
|
+
import { setupTests, closeTests } from '../utils/testHelper'
|
|
5
|
+
import { gql, type GraphQLClient } from 'graphql-request'
|
|
6
|
+
|
|
7
|
+
describe('Delete session on delete user', () => {
|
|
8
|
+
let wabe: Wabe<DevWabeTypes>
|
|
9
|
+
let port: number
|
|
10
|
+
let client: GraphQLClient
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
const setup = await setupTests()
|
|
14
|
+
wabe = setup.wabe
|
|
15
|
+
port = setup.port
|
|
16
|
+
client = getGraphqlClient(port)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
afterAll(async () => {
|
|
20
|
+
await closeTests(wabe)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
beforeEach(async () => {
|
|
24
|
+
await wabe.controllers.database.clearDatabase()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('should delete the session when the user is deleted', async () => {
|
|
28
|
+
const {
|
|
29
|
+
signUpWith: { id: userId },
|
|
30
|
+
} = await client.request<any>(graphql.signUpWith, {
|
|
31
|
+
input: {
|
|
32
|
+
authentication: {
|
|
33
|
+
emailPassword: {
|
|
34
|
+
email: 'email@test.fr',
|
|
35
|
+
password: 'password',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const { _sessions: sessions1 } = await client.request<any>(graphql.sessions)
|
|
42
|
+
|
|
43
|
+
expect(sessions1.edges.length).toEqual(1)
|
|
44
|
+
|
|
45
|
+
await client.request<any>(graphql.deleteUser, {
|
|
46
|
+
input: {
|
|
47
|
+
id: userId,
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const { _sessions: sessions2 } = await client.request<any>(graphql.sessions)
|
|
52
|
+
|
|
53
|
+
expect(sessions2.edges.length).toEqual(0)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('should only delete the sessions of the deleted user', async () => {
|
|
57
|
+
const {
|
|
58
|
+
signUpWith: { id: userId },
|
|
59
|
+
} = await client.request<any>(graphql.signUpWith, {
|
|
60
|
+
input: {
|
|
61
|
+
authentication: {
|
|
62
|
+
emailPassword: {
|
|
63
|
+
email: 'email@test.fr',
|
|
64
|
+
password: 'password',
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
await client.request<any>(graphql.signUpWith, {
|
|
71
|
+
input: {
|
|
72
|
+
authentication: {
|
|
73
|
+
emailPassword: {
|
|
74
|
+
email: 'anotherEmail@test.fr',
|
|
75
|
+
password: 'password',
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const { _sessions: sessions1 } = await client.request<any>(graphql.sessions)
|
|
82
|
+
|
|
83
|
+
expect(sessions1.edges.length).toEqual(2)
|
|
84
|
+
|
|
85
|
+
await client.request<any>(graphql.deleteUser, {
|
|
86
|
+
input: {
|
|
87
|
+
id: userId,
|
|
88
|
+
},
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
const { _sessions: sessions2 } = await client.request<any>(graphql.sessions)
|
|
92
|
+
|
|
93
|
+
expect(sessions2.edges.length).toEqual(1)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it("should not delete any session if the user doesn't have any session", async () => {
|
|
97
|
+
const {
|
|
98
|
+
signUpWith: { id: userId },
|
|
99
|
+
} = await client.request<any>(graphql.signUpWith, {
|
|
100
|
+
input: {
|
|
101
|
+
authentication: {
|
|
102
|
+
emailPassword: {
|
|
103
|
+
email: 'email@test.fr',
|
|
104
|
+
password: 'password',
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
await client.request<any>(graphql.signUpWith, {
|
|
111
|
+
input: {
|
|
112
|
+
authentication: {
|
|
113
|
+
emailPassword: {
|
|
114
|
+
email: 'anotherEmail@test.fr',
|
|
115
|
+
password: 'password',
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
await wabe.controllers.database.deleteObjects({
|
|
122
|
+
className: '_Session',
|
|
123
|
+
context: {
|
|
124
|
+
wabe,
|
|
125
|
+
isRoot: true,
|
|
126
|
+
},
|
|
127
|
+
select: {},
|
|
128
|
+
where: {
|
|
129
|
+
user: {
|
|
130
|
+
id: {
|
|
131
|
+
equalTo: userId,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
// Delete the user
|
|
138
|
+
await client.request<any>(graphql.deleteUser, {
|
|
139
|
+
input: {
|
|
140
|
+
id: userId,
|
|
141
|
+
},
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
const { _sessions: sessions2 } = await client.request<any>(graphql.sessions)
|
|
145
|
+
|
|
146
|
+
expect(sessions2.edges.length).toEqual(1)
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
const graphql = {
|
|
151
|
+
sessions: gql`
|
|
152
|
+
query _sessions {
|
|
153
|
+
_sessions {
|
|
154
|
+
edges {
|
|
155
|
+
node {
|
|
156
|
+
id
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
`,
|
|
162
|
+
signUpWith: gql`
|
|
163
|
+
mutation signUpWith($input: SignUpWithInput!) {
|
|
164
|
+
signUpWith(input: $input){
|
|
165
|
+
id
|
|
166
|
+
accessToken
|
|
167
|
+
refreshToken
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
`,
|
|
171
|
+
deleteUser: gql`
|
|
172
|
+
mutation deleteUser($input: DeleteUserInput!) {
|
|
173
|
+
deleteUser(input: $input) {
|
|
174
|
+
user {
|
|
175
|
+
name
|
|
176
|
+
age
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
`,
|
|
181
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { contextWithRoot } from '../utils/export'
|
|
2
|
+
import type { DevWabeTypes } from '../utils/helper'
|
|
3
|
+
import type { HookObject } from './HookObject'
|
|
4
|
+
|
|
5
|
+
// TODO: It should better to do this in after delete to avoid case when deleteUser failed
|
|
6
|
+
// For the moment KISS
|
|
7
|
+
export const defaultDeleteSessionOnDeleteUser = async (
|
|
8
|
+
object: HookObject<DevWabeTypes, 'User'>,
|
|
9
|
+
) => {
|
|
10
|
+
const userId = object.object?.id
|
|
11
|
+
|
|
12
|
+
await object.context.wabe.controllers.database.deleteObjects({
|
|
13
|
+
className: '_Session',
|
|
14
|
+
context: contextWithRoot(object.context),
|
|
15
|
+
where: {
|
|
16
|
+
user: { id: { equalTo: userId } },
|
|
17
|
+
},
|
|
18
|
+
select: {},
|
|
19
|
+
})
|
|
20
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { hashFieldHook } from './hashFieldHook'
|
|
2
|
+
import { describe, expect, it } from 'bun:test'
|
|
3
|
+
import { HookObject } from './HookObject'
|
|
4
|
+
import type { SchemaFields } from '../schema'
|
|
5
|
+
import { OperationType } from '.'
|
|
6
|
+
import { hashArgon2, verifyArgon2 } from 'src/utils/crypto'
|
|
7
|
+
|
|
8
|
+
const makeHookObject = ({ className, operationType, newData, fields }: any) => {
|
|
9
|
+
const config = {
|
|
10
|
+
port: 0,
|
|
11
|
+
isProduction: false,
|
|
12
|
+
database: {} as any,
|
|
13
|
+
rootKey: '',
|
|
14
|
+
schema: {
|
|
15
|
+
classes: [{ name: className, fields }],
|
|
16
|
+
},
|
|
17
|
+
}
|
|
18
|
+
return new HookObject({
|
|
19
|
+
className,
|
|
20
|
+
operationType,
|
|
21
|
+
newData,
|
|
22
|
+
context: {
|
|
23
|
+
wabe: { config } as any,
|
|
24
|
+
isRoot: false,
|
|
25
|
+
},
|
|
26
|
+
object: { id: 'dummy' },
|
|
27
|
+
select: {},
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe('hashFieldHook', () => {
|
|
32
|
+
const fields: SchemaFields<any> = {
|
|
33
|
+
password: { type: 'Hash' },
|
|
34
|
+
email: { type: 'Email' },
|
|
35
|
+
authentication: {
|
|
36
|
+
type: 'Object',
|
|
37
|
+
object: {
|
|
38
|
+
name: 'Authentication',
|
|
39
|
+
fields: {
|
|
40
|
+
emailPassword: {
|
|
41
|
+
type: 'Object',
|
|
42
|
+
object: {
|
|
43
|
+
name: 'EmailPassword',
|
|
44
|
+
fields: {
|
|
45
|
+
password: { type: 'Hash' },
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
twoHashes: {
|
|
53
|
+
type: 'Object',
|
|
54
|
+
object: {
|
|
55
|
+
name: 'TwoHashes',
|
|
56
|
+
fields: {
|
|
57
|
+
hash1: { type: 'Hash' },
|
|
58
|
+
hash2: { type: 'Hash' },
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
it('should hashed a plain value in a sub object in beforeCreate', async () => {
|
|
65
|
+
const newData = {
|
|
66
|
+
authentication: { emailPassword: { password: 'mysecret' } },
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const hookObject = makeHookObject({
|
|
70
|
+
className: 'User',
|
|
71
|
+
operationType: OperationType.BeforeCreate,
|
|
72
|
+
newData,
|
|
73
|
+
fields,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
await hashFieldHook(hookObject)
|
|
77
|
+
|
|
78
|
+
expect(
|
|
79
|
+
hookObject.getNewData().authentication.emailPassword.password,
|
|
80
|
+
).not.toBe('mysecret')
|
|
81
|
+
|
|
82
|
+
const isValid = await verifyArgon2(
|
|
83
|
+
'mysecret',
|
|
84
|
+
hookObject.getNewData().authentication.emailPassword.password,
|
|
85
|
+
)
|
|
86
|
+
expect(isValid).toBe(true)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('should hashed a plain value in a sub object with 2 hash fields in beforeCreate', async () => {
|
|
90
|
+
const newData = {
|
|
91
|
+
twoHashes: { hash1: 'mysecret', hash2: 'mysecret' },
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const hookObject = makeHookObject({
|
|
95
|
+
className: 'User',
|
|
96
|
+
operationType: OperationType.BeforeCreate,
|
|
97
|
+
newData,
|
|
98
|
+
fields,
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
await hashFieldHook(hookObject)
|
|
102
|
+
|
|
103
|
+
expect(hookObject.getNewData().twoHashes.hash1).not.toBe('mysecret')
|
|
104
|
+
|
|
105
|
+
const isValid = await verifyArgon2(
|
|
106
|
+
'mysecret',
|
|
107
|
+
hookObject.getNewData().twoHashes.hash1,
|
|
108
|
+
)
|
|
109
|
+
expect(isValid).toBe(true)
|
|
110
|
+
|
|
111
|
+
expect(hookObject.getNewData().twoHashes.hash2).not.toBe('mysecret')
|
|
112
|
+
|
|
113
|
+
const isValid2 = await verifyArgon2(
|
|
114
|
+
'mysecret',
|
|
115
|
+
hookObject.getNewData().twoHashes.hash2,
|
|
116
|
+
)
|
|
117
|
+
expect(isValid2).toBe(true)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('hashes a plain value on beforeCreate', async () => {
|
|
121
|
+
const newData = { password: 'mysecret', email: 'test@example.com' }
|
|
122
|
+
const hookObject = makeHookObject({
|
|
123
|
+
className: 'User',
|
|
124
|
+
operationType: 'beforeCreate',
|
|
125
|
+
newData,
|
|
126
|
+
fields,
|
|
127
|
+
})
|
|
128
|
+
await hashFieldHook(hookObject)
|
|
129
|
+
expect(hookObject.getNewData().password).not.toBe('mysecret')
|
|
130
|
+
expect(hookObject.getNewData().email).toBe('test@example.com')
|
|
131
|
+
|
|
132
|
+
const isValid = await verifyArgon2(
|
|
133
|
+
'mysecret',
|
|
134
|
+
hookObject.getNewData().password,
|
|
135
|
+
)
|
|
136
|
+
expect(isValid).toBe(true)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('does not double-hash an already hashed value', async () => {
|
|
140
|
+
const hashed = await hashArgon2('alreadyhashed')
|
|
141
|
+
const newData = { password: hashed }
|
|
142
|
+
const hookObject = makeHookObject({
|
|
143
|
+
className: 'User',
|
|
144
|
+
operationType: 'beforeUpdate',
|
|
145
|
+
newData,
|
|
146
|
+
fields,
|
|
147
|
+
})
|
|
148
|
+
await hashFieldHook(hookObject)
|
|
149
|
+
expect(hookObject.getNewData().password).toBe(hashed)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
it('does nothing if not beforeCreate or beforeUpdate', async () => {
|
|
153
|
+
const newData = { password: 'shouldnotchange' }
|
|
154
|
+
const hookObject = makeHookObject({
|
|
155
|
+
className: 'User',
|
|
156
|
+
operationType: 'afterRead',
|
|
157
|
+
newData,
|
|
158
|
+
fields,
|
|
159
|
+
})
|
|
160
|
+
await hashFieldHook(hookObject)
|
|
161
|
+
expect(hookObject.getNewData().password).toBe('shouldnotchange')
|
|
162
|
+
})
|
|
163
|
+
})
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { HookObject } from './HookObject'
|
|
2
|
+
import type { WabeTypes } from '../server'
|
|
3
|
+
import {
|
|
4
|
+
getNestedProperty,
|
|
5
|
+
getNewObjectAfterUpdateNestedProperty,
|
|
6
|
+
} from '../utils'
|
|
7
|
+
import { OperationType } from '.'
|
|
8
|
+
import { hashArgon2, isArgon2Hash } from 'src/utils/crypto'
|
|
9
|
+
|
|
10
|
+
const hashField = ({
|
|
11
|
+
value,
|
|
12
|
+
}: {
|
|
13
|
+
value: ReturnType<typeof HookObject.prototype.getNewData>
|
|
14
|
+
}) => {
|
|
15
|
+
if (!value || typeof value !== 'string' || isArgon2Hash(value)) return value
|
|
16
|
+
|
|
17
|
+
return hashArgon2(value)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function hashFieldHook<
|
|
21
|
+
T extends WabeTypes,
|
|
22
|
+
K extends keyof WabeTypes['types'],
|
|
23
|
+
>(hookObject: HookObject<T, K>) {
|
|
24
|
+
if (
|
|
25
|
+
hookObject.operationType !== OperationType.BeforeCreate &&
|
|
26
|
+
hookObject.operationType !== OperationType.BeforeUpdate
|
|
27
|
+
)
|
|
28
|
+
return
|
|
29
|
+
|
|
30
|
+
const fields = hookObject.context.wabe.config.schema?.classes?.find(
|
|
31
|
+
(cls: any) => cls.name === hookObject.className,
|
|
32
|
+
)?.fields
|
|
33
|
+
|
|
34
|
+
if (!fields) return
|
|
35
|
+
|
|
36
|
+
const computeHashForFields = ({
|
|
37
|
+
fieldsToCompute,
|
|
38
|
+
path = '',
|
|
39
|
+
}: {
|
|
40
|
+
fieldsToCompute: typeof fields
|
|
41
|
+
path?: string
|
|
42
|
+
}) =>
|
|
43
|
+
Promise.all(
|
|
44
|
+
Object.entries(fieldsToCompute).map(async ([fieldName, fieldDef]) => {
|
|
45
|
+
if (fieldDef.type === 'Object') {
|
|
46
|
+
await computeHashForFields({
|
|
47
|
+
// @ts-expect-error
|
|
48
|
+
fieldsToCompute: fieldDef.object.fields,
|
|
49
|
+
path: `${path || ''}${path ? '.' : ''}${fieldName}`,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (fieldDef.type !== 'Hash') return
|
|
56
|
+
|
|
57
|
+
// If we don't have nested object
|
|
58
|
+
if (!path) {
|
|
59
|
+
const hashed = await hashField({
|
|
60
|
+
// @ts-expect-error
|
|
61
|
+
value: hookObject.getNewData()[fieldName],
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
// @ts-expect-error
|
|
65
|
+
hookObject.upsertNewData(fieldName, hashed)
|
|
66
|
+
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const objectNameToUpdate = path.split('.')[0] || ''
|
|
71
|
+
// @ts-expect-error
|
|
72
|
+
const objectToUpdate = hookObject.getNewData()[objectNameToUpdate]
|
|
73
|
+
const valueToUpdate = getNestedProperty(hookObject.getNewData(), path)
|
|
74
|
+
|
|
75
|
+
if (!valueToUpdate?.[fieldName]) return
|
|
76
|
+
|
|
77
|
+
const hashed = await hashField({
|
|
78
|
+
value: valueToUpdate[fieldName],
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const newObject = getNewObjectAfterUpdateNestedProperty(
|
|
82
|
+
{ [objectNameToUpdate]: objectToUpdate },
|
|
83
|
+
`${path}.${fieldName}`,
|
|
84
|
+
hashed,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
if (hashed)
|
|
88
|
+
hookObject.upsertNewData(
|
|
89
|
+
// @ts-expect-error
|
|
90
|
+
objectNameToUpdate,
|
|
91
|
+
newObject[objectNameToUpdate],
|
|
92
|
+
)
|
|
93
|
+
}),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
await computeHashForFields({ fieldsToCompute: fields })
|
|
97
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, spyOn, mock } from 'bun:test'
|
|
2
|
+
import * as index from './index'
|
|
3
|
+
import { OperationType } from './index'
|
|
4
|
+
|
|
5
|
+
describe('Hooks', () => {
|
|
6
|
+
const mockGetObject = mock(() => {})
|
|
7
|
+
const mockGetObjects = mock(() => {})
|
|
8
|
+
const mockCallback1 = mock(() => {})
|
|
9
|
+
const mockCallback2 = mock(() => {})
|
|
10
|
+
const mockCallback3 = mock(() => {})
|
|
11
|
+
const mockCallback4 = mock(() => {})
|
|
12
|
+
const mockCallback5 = mock(() => {})
|
|
13
|
+
|
|
14
|
+
const controllers = {
|
|
15
|
+
database: {
|
|
16
|
+
getObject: mockGetObject,
|
|
17
|
+
getObjects: mockGetObjects,
|
|
18
|
+
},
|
|
19
|
+
} as any
|
|
20
|
+
|
|
21
|
+
const config = {
|
|
22
|
+
hooks: [
|
|
23
|
+
{
|
|
24
|
+
callback: mockCallback1,
|
|
25
|
+
operationType: OperationType.BeforeRead,
|
|
26
|
+
priority: 1,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
callback: mockCallback2,
|
|
30
|
+
operationType: OperationType.BeforeRead,
|
|
31
|
+
priority: 2,
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
callback: mockCallback3,
|
|
35
|
+
operationType: OperationType.BeforeCreate,
|
|
36
|
+
priority: 1,
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
callback: mockCallback4,
|
|
40
|
+
operationType: OperationType.BeforeRead,
|
|
41
|
+
priority: 1,
|
|
42
|
+
className: 'ClassTest',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
callback: mockCallback5,
|
|
46
|
+
operationType: OperationType.BeforeRead,
|
|
47
|
+
priority: 1,
|
|
48
|
+
className: 'AnotherClass',
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
} as any
|
|
52
|
+
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
mockGetObjects.mockClear()
|
|
55
|
+
mockGetObject.mockClear()
|
|
56
|
+
mockCallback1.mockClear()
|
|
57
|
+
mockCallback2.mockClear()
|
|
58
|
+
mockCallback3.mockClear()
|
|
59
|
+
mockCallback4.mockClear()
|
|
60
|
+
mockCallback5.mockClear()
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('should only run hook on specified className', async () => {
|
|
64
|
+
const hooks = index.initializeHook({
|
|
65
|
+
className: 'ClassTest',
|
|
66
|
+
context: {
|
|
67
|
+
isRoot: true,
|
|
68
|
+
wabe: { controllers, config } as any,
|
|
69
|
+
},
|
|
70
|
+
newData: { name: 'test' },
|
|
71
|
+
select: {},
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
await hooks.runOnSingleObject({
|
|
75
|
+
operationType: OperationType.BeforeRead,
|
|
76
|
+
id: 'id',
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
expect(mockCallback4).toHaveBeenCalledTimes(1)
|
|
80
|
+
expect(mockCallback5).toHaveBeenCalledTimes(0)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('should run hook on BeforeRead with one object impacted', async () => {
|
|
84
|
+
mockGetObject.mockResolvedValueOnce({ id: 'id', name: 'name' } as never)
|
|
85
|
+
|
|
86
|
+
const hooks = index.initializeHook({
|
|
87
|
+
className: 'ClassName',
|
|
88
|
+
context: {
|
|
89
|
+
isRoot: true,
|
|
90
|
+
wabe: { controllers, config } as any,
|
|
91
|
+
},
|
|
92
|
+
newData: { name: 'test' },
|
|
93
|
+
select: {},
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
await hooks.runOnSingleObject({
|
|
97
|
+
operationType: OperationType.BeforeRead,
|
|
98
|
+
id: 'id',
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
expect(mockGetObject).toHaveBeenCalledTimes(1)
|
|
102
|
+
expect(mockGetObject).toHaveBeenCalledWith({
|
|
103
|
+
className: 'ClassName',
|
|
104
|
+
context: {
|
|
105
|
+
isRoot: true,
|
|
106
|
+
wabe: { controllers, config },
|
|
107
|
+
isGraphQLCall: false,
|
|
108
|
+
},
|
|
109
|
+
id: 'id',
|
|
110
|
+
_skipHooks: true,
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
expect(mockCallback1).toHaveBeenCalledTimes(1)
|
|
114
|
+
|
|
115
|
+
// @ts-expect-error
|
|
116
|
+
const hookObject = mockCallback1.mock.calls[0][0] as any
|
|
117
|
+
|
|
118
|
+
expect(hookObject.object).toEqual({
|
|
119
|
+
id: 'id',
|
|
120
|
+
name: 'name',
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
expect(hookObject.context).toEqual({
|
|
124
|
+
isRoot: true,
|
|
125
|
+
wabe: {
|
|
126
|
+
controllers: expect.any(Object),
|
|
127
|
+
config,
|
|
128
|
+
},
|
|
129
|
+
isGraphQLCall: false,
|
|
130
|
+
})
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('should run hook on BeforeCreate', async () => {
|
|
134
|
+
const hooks = index.initializeHook({
|
|
135
|
+
className: 'ClassName',
|
|
136
|
+
context: {
|
|
137
|
+
isRoot: true,
|
|
138
|
+
wabe: { controllers, config } as any,
|
|
139
|
+
},
|
|
140
|
+
newData: { name: 'test' },
|
|
141
|
+
select: {},
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
await hooks.runOnSingleObject({
|
|
145
|
+
operationType: OperationType.BeforeCreate,
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
expect(mockGetObjects).toHaveBeenCalledTimes(0)
|
|
149
|
+
|
|
150
|
+
// @ts-expect-error
|
|
151
|
+
const hookObject = mockCallback3.mock.calls[0][0] as any
|
|
152
|
+
|
|
153
|
+
// Before create the object is empty
|
|
154
|
+
expect(hookObject.object).toEqual({})
|
|
155
|
+
|
|
156
|
+
expect(hookObject.context).toEqual({
|
|
157
|
+
isRoot: true,
|
|
158
|
+
wabe: { controllers: expect.any(Object), config },
|
|
159
|
+
isGraphQLCall: false,
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('should run callback in priorities order', async () => {
|
|
164
|
+
const spy_findHooksByPriority = spyOn(index, '_findHooksByPriority')
|
|
165
|
+
|
|
166
|
+
mockGetObjects.mockResolvedValueOnce([{ id: 'id', name: 'name' }] as never)
|
|
167
|
+
|
|
168
|
+
const hooks = index.initializeHook({
|
|
169
|
+
className: 'ClassName',
|
|
170
|
+
context: {
|
|
171
|
+
isRoot: true,
|
|
172
|
+
wabe: { controllers, config } as any,
|
|
173
|
+
},
|
|
174
|
+
newData: { name: 'test' },
|
|
175
|
+
select: {},
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
await hooks.runOnSingleObject({
|
|
179
|
+
operationType: OperationType.BeforeRead,
|
|
180
|
+
id: 'id',
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
expect(spy_findHooksByPriority.mock.calls[0]?.[0].priority).toEqual(1)
|
|
184
|
+
expect(spy_findHooksByPriority.mock.calls[1]?.[0].priority).toEqual(2)
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it('should call hook that is trigger by operation type', async () => {
|
|
188
|
+
mockGetObjects.mockResolvedValueOnce([{ id: 'id', name: 'name' }] as never)
|
|
189
|
+
|
|
190
|
+
const hooks = index.initializeHook({
|
|
191
|
+
className: 'ClassName',
|
|
192
|
+
context: {
|
|
193
|
+
isRoot: true,
|
|
194
|
+
wabe: { controllers, config } as any,
|
|
195
|
+
},
|
|
196
|
+
newData: { name: 'test' },
|
|
197
|
+
select: {},
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
await hooks.runOnSingleObject({
|
|
201
|
+
operationType: OperationType.BeforeRead,
|
|
202
|
+
id: 'id',
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
expect(mockCallback3).toHaveBeenCalledTimes(0)
|
|
206
|
+
})
|
|
207
|
+
})
|