wabe 0.6.9 → 0.6.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.md +156 -50
  2. package/bucket/b.txt +1 -0
  3. package/dev/index.ts +215 -0
  4. package/dist/authentication/Session.d.ts +4 -1
  5. package/dist/authentication/interface.d.ts +16 -0
  6. package/dist/cron/index.d.ts +0 -1
  7. package/dist/database/DatabaseController.d.ts +41 -13
  8. package/dist/database/interface.d.ts +1 -0
  9. package/dist/email/DevAdapter.d.ts +0 -1
  10. package/dist/email/interface.d.ts +1 -1
  11. package/dist/graphql/resolvers.d.ts +4 -2
  12. package/dist/hooks/index.d.ts +8 -2
  13. package/dist/index.d.ts +0 -1
  14. package/dist/index.js +32144 -32058
  15. package/dist/schema/Schema.d.ts +2 -1
  16. package/dist/server/index.d.ts +4 -2
  17. package/dist/utils/crypto.d.ts +7 -0
  18. package/dist/utils/helper.d.ts +5 -1
  19. package/generated/schema.graphql +22 -14
  20. package/generated/wabe.ts +4 -4
  21. package/package.json +23 -23
  22. package/src/authentication/OTP.test.ts +69 -0
  23. package/src/authentication/OTP.ts +64 -0
  24. package/src/authentication/Session.test.ts +629 -0
  25. package/src/authentication/Session.ts +493 -0
  26. package/src/authentication/defaultAuthentication.ts +209 -0
  27. package/src/authentication/index.ts +3 -0
  28. package/src/authentication/interface.ts +155 -0
  29. package/src/authentication/oauth/GitHub.test.ts +91 -0
  30. package/src/authentication/oauth/GitHub.ts +121 -0
  31. package/src/authentication/oauth/Google.test.ts +91 -0
  32. package/src/authentication/oauth/Google.ts +101 -0
  33. package/src/authentication/oauth/Oauth2Client.test.ts +219 -0
  34. package/src/authentication/oauth/Oauth2Client.ts +135 -0
  35. package/src/authentication/oauth/index.ts +2 -0
  36. package/src/authentication/oauth/utils.test.ts +33 -0
  37. package/src/authentication/oauth/utils.ts +27 -0
  38. package/src/authentication/providers/EmailOTP.test.ts +127 -0
  39. package/src/authentication/providers/EmailOTP.ts +84 -0
  40. package/src/authentication/providers/EmailPassword.test.ts +176 -0
  41. package/src/authentication/providers/EmailPassword.ts +116 -0
  42. package/src/authentication/providers/EmailPasswordSRP.test.ts +208 -0
  43. package/src/authentication/providers/EmailPasswordSRP.ts +179 -0
  44. package/src/authentication/providers/GitHub.ts +24 -0
  45. package/src/authentication/providers/Google.ts +24 -0
  46. package/src/authentication/providers/OAuth.test.ts +185 -0
  47. package/src/authentication/providers/OAuth.ts +106 -0
  48. package/src/authentication/providers/PhonePassword.test.ts +176 -0
  49. package/src/authentication/providers/PhonePassword.ts +115 -0
  50. package/src/authentication/providers/QRCodeOTP.test.ts +77 -0
  51. package/src/authentication/providers/QRCodeOTP.ts +58 -0
  52. package/src/authentication/providers/index.ts +6 -0
  53. package/src/authentication/resolvers/refreshResolver.test.ts +30 -0
  54. package/src/authentication/resolvers/refreshResolver.ts +19 -0
  55. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +59 -0
  56. package/src/authentication/resolvers/signInWithResolver.test.ts +293 -0
  57. package/src/authentication/resolvers/signInWithResolver.ts +92 -0
  58. package/src/authentication/resolvers/signOutResolver.test.ts +38 -0
  59. package/src/authentication/resolvers/signOutResolver.ts +18 -0
  60. package/src/authentication/resolvers/signUpWithResolver.test.ts +180 -0
  61. package/src/authentication/resolvers/signUpWithResolver.ts +65 -0
  62. package/src/authentication/resolvers/verifyChallenge.test.ts +133 -0
  63. package/src/authentication/resolvers/verifyChallenge.ts +62 -0
  64. package/src/authentication/roles.test.ts +49 -0
  65. package/src/authentication/roles.ts +40 -0
  66. package/src/authentication/utils.test.ts +97 -0
  67. package/src/authentication/utils.ts +39 -0
  68. package/src/cache/InMemoryCache.test.ts +62 -0
  69. package/src/cache/InMemoryCache.ts +45 -0
  70. package/src/cron/index.test.ts +17 -0
  71. package/src/cron/index.ts +43 -0
  72. package/src/database/DatabaseController.test.ts +613 -0
  73. package/src/database/DatabaseController.ts +1007 -0
  74. package/src/database/index.test.ts +1372 -0
  75. package/src/database/index.ts +9 -0
  76. package/src/database/interface.ts +302 -0
  77. package/src/email/DevAdapter.ts +7 -0
  78. package/src/email/EmailController.test.ts +29 -0
  79. package/src/email/EmailController.ts +13 -0
  80. package/src/email/index.ts +2 -0
  81. package/src/email/interface.ts +36 -0
  82. package/src/email/templates/sendOtpCode.ts +120 -0
  83. package/src/file/FileController.ts +28 -0
  84. package/src/file/FileDevAdapter.ts +51 -0
  85. package/src/file/hookDeleteFile.ts +25 -0
  86. package/src/file/hookReadFile.ts +66 -0
  87. package/src/file/hookUploadFile.ts +50 -0
  88. package/src/file/index.test.ts +932 -0
  89. package/src/file/index.ts +2 -0
  90. package/src/file/interface.ts +39 -0
  91. package/src/graphql/GraphQLSchema.test.ts +4408 -0
  92. package/src/graphql/GraphQLSchema.ts +880 -0
  93. package/src/graphql/index.ts +2 -0
  94. package/src/graphql/parseGraphqlSchema.ts +85 -0
  95. package/src/graphql/parser.test.ts +203 -0
  96. package/src/graphql/parser.ts +542 -0
  97. package/src/graphql/pointerAndRelationFunction.ts +191 -0
  98. package/src/graphql/resolvers.ts +442 -0
  99. package/src/graphql/tests/aggregation.test.ts +1115 -0
  100. package/src/graphql/tests/e2e.test.ts +590 -0
  101. package/src/graphql/tests/scalars.test.ts +250 -0
  102. package/src/graphql/types.ts +227 -0
  103. package/src/hooks/HookObject.test.ts +122 -0
  104. package/src/hooks/HookObject.ts +165 -0
  105. package/src/hooks/authentication.ts +67 -0
  106. package/src/hooks/createUser.test.ts +77 -0
  107. package/src/hooks/createUser.ts +10 -0
  108. package/src/hooks/defaultFields.test.ts +176 -0
  109. package/src/hooks/defaultFields.ts +32 -0
  110. package/src/hooks/deleteSession.test.ts +181 -0
  111. package/src/hooks/deleteSession.ts +20 -0
  112. package/src/hooks/hashFieldHook.test.ts +152 -0
  113. package/src/hooks/hashFieldHook.ts +89 -0
  114. package/src/hooks/index.test.ts +258 -0
  115. package/src/hooks/index.ts +414 -0
  116. package/src/hooks/permissions.test.ts +412 -0
  117. package/src/hooks/permissions.ts +93 -0
  118. package/src/hooks/protected.test.ts +551 -0
  119. package/src/hooks/protected.ts +60 -0
  120. package/src/hooks/searchableFields.test.ts +147 -0
  121. package/src/hooks/searchableFields.ts +86 -0
  122. package/src/hooks/session.test.ts +134 -0
  123. package/src/hooks/session.ts +76 -0
  124. package/src/hooks/setEmail.test.ts +216 -0
  125. package/src/hooks/setEmail.ts +33 -0
  126. package/src/hooks/setupAcl.test.ts +618 -0
  127. package/src/hooks/setupAcl.ts +25 -0
  128. package/src/index.ts +9 -0
  129. package/src/schema/Schema.test.ts +482 -0
  130. package/src/schema/Schema.ts +757 -0
  131. package/src/schema/defaultResolvers.ts +93 -0
  132. package/src/schema/index.ts +1 -0
  133. package/src/schema/resolvers/meResolver.test.ts +62 -0
  134. package/src/schema/resolvers/meResolver.ts +10 -0
  135. package/src/schema/resolvers/resetPassword.test.ts +341 -0
  136. package/src/schema/resolvers/resetPassword.ts +63 -0
  137. package/src/schema/resolvers/sendEmail.test.ts +118 -0
  138. package/src/schema/resolvers/sendEmail.ts +21 -0
  139. package/src/schema/resolvers/sendOtpCode.test.ts +141 -0
  140. package/src/schema/resolvers/sendOtpCode.ts +52 -0
  141. package/src/security.test.ts +3434 -0
  142. package/src/server/defaultSessionHandler.test.ts +62 -0
  143. package/src/server/defaultSessionHandler.ts +105 -0
  144. package/src/server/generateCodegen.ts +433 -0
  145. package/src/server/index.test.ts +532 -0
  146. package/src/server/index.ts +334 -0
  147. package/src/server/interface.ts +11 -0
  148. package/src/server/routes/authHandler.ts +169 -0
  149. package/src/server/routes/index.ts +39 -0
  150. package/src/utils/crypto.test.ts +41 -0
  151. package/src/utils/crypto.ts +105 -0
  152. package/src/utils/export.ts +11 -0
  153. package/src/utils/helper.ts +204 -0
  154. package/src/utils/index.test.ts +11 -0
  155. package/src/utils/index.ts +189 -0
  156. package/src/utils/preload.ts +8 -0
  157. package/src/utils/testHelper.ts +116 -0
  158. package/tsconfig.json +32 -0
  159. package/bunfig.toml +0 -4
  160. package/dist/ai/index.d.ts +0 -1
  161. package/dist/ai/interface.d.ts +0 -9
  162. /package/dist/server/{defaultHandlers.d.ts → defaultSessionHandler.d.ts} +0 -0
@@ -0,0 +1,141 @@
1
+ import { describe, afterAll, beforeAll, it, spyOn, expect, beforeEach } from 'bun:test'
2
+ import { gql, type GraphQLClient } from 'graphql-request'
3
+ import type { Wabe } from '../../server'
4
+ import { type DevWabeTypes, getGraphqlClient, getAnonymousClient } from '../../utils/helper'
5
+ import { setupTests, closeTests } from '../../utils/testHelper'
6
+ import { EmailDevAdapter } from '../../email/DevAdapter'
7
+
8
+ describe('sendOtpCodeResolver', () => {
9
+ let wabe: Wabe<DevWabeTypes>
10
+ let port: number
11
+ let client: GraphQLClient
12
+
13
+ const spySend = spyOn(EmailDevAdapter.prototype, 'send')
14
+
15
+ beforeAll(async () => {
16
+ const setup = await setupTests()
17
+ wabe = setup.wabe
18
+ port = setup.port
19
+ client = getGraphqlClient(port)
20
+ })
21
+
22
+ afterAll(async () => {
23
+ await closeTests(wabe)
24
+ })
25
+
26
+ beforeEach(() => {
27
+ spySend.mockClear()
28
+ })
29
+
30
+ it('should use the provided email template if provided', async () => {
31
+ const previous = wabe.config.email
32
+ // @ts-expect-error
33
+ wabe.config.email = {
34
+ ...wabe.config.email,
35
+ htmlTemplates: {
36
+ sendOTPCode: {
37
+ fn: () => 'toto',
38
+ subject: 'Confirmation code',
39
+ },
40
+ },
41
+ }
42
+
43
+ await client.request<any>(graphql.createUser, {
44
+ input: {
45
+ fields: {
46
+ authentication: {
47
+ emailPassword: {
48
+ email: 'tata@toto.fr',
49
+ password: 'totototo',
50
+ },
51
+ },
52
+ },
53
+ },
54
+ })
55
+
56
+ await client.request<any>(graphql.sendOtpCode, {
57
+ input: {
58
+ email: 'tata@toto.fr',
59
+ },
60
+ })
61
+
62
+ expect(spySend).toHaveBeenCalledTimes(1)
63
+ expect(spySend).toHaveBeenCalledWith({
64
+ from: 'main.email@wabe.com',
65
+ to: ['tata@toto.fr'],
66
+ subject: 'Confirmation code',
67
+ html: 'toto',
68
+ })
69
+
70
+ wabe.config.email = previous
71
+ })
72
+
73
+ it("should send an OTP code to the user's email as anonymous client", async () => {
74
+ const anonymousClient = getAnonymousClient(port)
75
+
76
+ await anonymousClient.request<any>(graphql.createUserWithAnonymous, {
77
+ input: {
78
+ fields: {
79
+ authentication: {
80
+ emailPassword: {
81
+ email: 'toto@toto.fr',
82
+ password: 'totototo',
83
+ },
84
+ },
85
+ },
86
+ },
87
+ })
88
+
89
+ await anonymousClient.request<any>(graphql.sendOtpCode, {
90
+ input: {
91
+ email: 'toto@toto.fr',
92
+ },
93
+ })
94
+
95
+ expect(spySend).toHaveBeenCalledTimes(1)
96
+ expect(spySend).toHaveBeenCalledWith({
97
+ from: 'main.email@wabe.com',
98
+ to: ['toto@toto.fr'],
99
+ subject: 'Your OTP code',
100
+ html: expect.any(String),
101
+ })
102
+ })
103
+
104
+ it("should return true if the user doesn't exist (hide sensitive data)", async () => {
105
+ const spySend = spyOn(EmailDevAdapter.prototype, 'send')
106
+
107
+ const res = await client.request<any>(graphql.sendOtpCode, {
108
+ input: {
109
+ email: 'invalidUser@toto.fr',
110
+ },
111
+ })
112
+
113
+ expect(res.sendOtpCode).toEqual(true)
114
+
115
+ expect(spySend).toHaveBeenCalledTimes(0)
116
+ })
117
+ })
118
+
119
+ const graphql = {
120
+ createUser: gql`
121
+ mutation createUser($input: CreateUserInput!) {
122
+ createUser(input: $input) {
123
+ user {
124
+ id
125
+ }
126
+ }
127
+ }
128
+ `,
129
+ createUserWithAnonymous: gql`
130
+ mutation createUser($input: CreateUserInput!) {
131
+ createUser(input: $input) {
132
+ ok
133
+ }
134
+ }
135
+ `,
136
+ sendOtpCode: gql`
137
+ mutation sendOtpCode($input: SendOtpCodeInput!) {
138
+ sendOtpCode(input: $input)
139
+ }
140
+ `,
141
+ }
@@ -0,0 +1,52 @@
1
+ import type { MutationSendOtpCodeArgs } from '../../../generated/wabe'
2
+ import type { WabeContext } from '../../server/interface'
3
+ import type { DevWabeTypes } from '../../utils/helper'
4
+ import { sendOtpCodeTemplate } from '../../email/templates/sendOtpCode'
5
+ import { OTP } from '../../authentication/OTP'
6
+ import { contextWithRoot } from '../../utils/export'
7
+
8
+ export const sendOtpCodeResolver = async (
9
+ _: any,
10
+ { input }: MutationSendOtpCodeArgs,
11
+ context: WabeContext<DevWabeTypes>,
12
+ ) => {
13
+ const emailController = context.wabe.controllers.email
14
+
15
+ if (!emailController) throw new Error('Email adapter not defined')
16
+
17
+ const user = await context.wabe.controllers.database.getObjects({
18
+ className: 'User',
19
+ where: {
20
+ email: {
21
+ equalTo: input.email,
22
+ },
23
+ },
24
+ select: { id: true },
25
+ first: 1,
26
+ context: contextWithRoot(context),
27
+ })
28
+
29
+ // We return true if the user doesn't exist to avoid leaking that the user exists or not
30
+ if (user.length === 0) return true
31
+
32
+ const userId = user[0]?.id
33
+
34
+ if (!userId) return false
35
+
36
+ const otpClass = new OTP(context.wabe.config.rootKey)
37
+
38
+ const otp = otpClass.generate(userId)
39
+
40
+ const mainEmail = context.wabe.config.email?.mainEmail || 'noreply@wabe.com'
41
+
42
+ const template = context.wabe.config.email?.htmlTemplates?.sendOTPCode
43
+
44
+ await emailController.send({
45
+ from: mainEmail,
46
+ to: [input.email],
47
+ subject: template?.subject || 'Your OTP code',
48
+ html: template?.fn ? await template.fn({ otp }) : sendOtpCodeTemplate(otp),
49
+ })
50
+
51
+ return true
52
+ }