@rebasepro/server-core 0.7.0 → 0.9.0

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 (300) hide show
  1. package/README.md +2 -2
  2. package/dist/api/ast-schema-editor.d.ts +21 -0
  3. package/dist/api/collections_for_test/callbacks_test_collection.d.ts +2 -0
  4. package/dist/api/errors.d.ts +53 -0
  5. package/dist/api/index.d.ts +7 -0
  6. package/dist/api/logs-routes.d.ts +37 -0
  7. package/dist/api/openapi-generator.d.ts +16 -0
  8. package/dist/api/rest/api-generator.d.ts +68 -0
  9. package/{src/api/rest/index.ts → dist/api/rest/index.d.ts} +0 -1
  10. package/dist/api/rest/query-parser.d.ts +6 -0
  11. package/dist/api/schema-editor-routes.d.ts +3 -0
  12. package/dist/api/types.d.ts +56 -0
  13. package/dist/auth/adapter-middleware.d.ts +43 -0
  14. package/dist/auth/admin-roles-route.d.ts +18 -0
  15. package/dist/auth/admin-user-ops.d.ts +79 -0
  16. package/dist/auth/admin-users-route.d.ts +28 -0
  17. package/dist/auth/api-keys/api-key-middleware.d.ts +39 -0
  18. package/{src/auth/api-keys/api-key-permission-guard.ts → dist/auth/api-keys/api-key-permission-guard.d.ts} +2 -34
  19. package/dist/auth/api-keys/api-key-routes.d.ts +20 -0
  20. package/dist/auth/api-keys/api-key-store.d.ts +35 -0
  21. package/{src/auth/api-keys/api-key-types.ts → dist/auth/api-keys/api-key-types.d.ts} +0 -6
  22. package/{src/auth/api-keys/index.ts → dist/auth/api-keys/index.d.ts} +2 -22
  23. package/dist/auth/apple-oauth.d.ts +30 -0
  24. package/{src/auth/auth-hooks.ts → dist/auth/auth-hooks.d.ts} +15 -73
  25. package/dist/auth/bitbucket-oauth.d.ts +11 -0
  26. package/dist/auth/builtin-auth-adapter.d.ts +55 -0
  27. package/dist/auth/cookie-utils.d.ts +32 -0
  28. package/{src/auth/crypto-utils.ts → dist/auth/crypto-utils.d.ts} +1 -16
  29. package/dist/auth/custom-auth-adapter.d.ts +39 -0
  30. package/dist/auth/discord-oauth.d.ts +14 -0
  31. package/dist/auth/facebook-oauth.d.ts +14 -0
  32. package/dist/auth/github-oauth.d.ts +15 -0
  33. package/dist/auth/gitlab-oauth.d.ts +13 -0
  34. package/dist/auth/google-oauth.d.ts +47 -0
  35. package/{src/auth/index.ts → dist/auth/index.d.ts} +4 -22
  36. package/{src/auth/interfaces.ts → dist/auth/interfaces.d.ts} +20 -89
  37. package/dist/auth/jwt.d.ts +67 -0
  38. package/dist/auth/linkedin-oauth.d.ts +18 -0
  39. package/dist/auth/magic-link-routes.d.ts +32 -0
  40. package/dist/auth/mfa-crypto.d.ts +23 -0
  41. package/dist/auth/mfa-routes.d.ts +7 -0
  42. package/dist/auth/mfa.d.ts +49 -0
  43. package/dist/auth/microsoft-oauth.d.ts +16 -0
  44. package/dist/auth/middleware.d.ts +158 -0
  45. package/dist/auth/password.d.ts +22 -0
  46. package/dist/auth/rate-limiter.d.ts +50 -0
  47. package/dist/auth/reset-password-admin.d.ts +29 -0
  48. package/dist/auth/rls-scope.d.ts +31 -0
  49. package/dist/auth/routes.d.ts +66 -0
  50. package/dist/auth/session-routes.d.ts +29 -0
  51. package/dist/auth/slack-oauth.d.ts +12 -0
  52. package/dist/auth/spotify-oauth.d.ts +12 -0
  53. package/dist/auth/twitter-oauth.d.ts +18 -0
  54. package/dist/collections/BackendCollectionRegistry.d.ts +13 -0
  55. package/dist/collections/loader.d.ts +5 -0
  56. package/dist/cron/cron-loader.d.ts +18 -0
  57. package/dist/cron/cron-routes.d.ts +14 -0
  58. package/dist/cron/cron-scheduler.d.ts +106 -0
  59. package/dist/cron/cron-store.d.ts +32 -0
  60. package/dist/cron/define-cron.d.ts +28 -0
  61. package/{src/cron/index.ts → dist/cron/index.d.ts} +1 -0
  62. package/dist/db/interfaces.d.ts +18 -0
  63. package/dist/dynamic-import-Dvh-K5fl.js +22 -0
  64. package/dist/dynamic-import-Dvh-K5fl.js.map +1 -0
  65. package/dist/email/index.d.ts +6 -0
  66. package/dist/email/smtp-email-service.d.ts +30 -0
  67. package/dist/email/templates.d.ts +50 -0
  68. package/{src/email/types.ts → dist/email/types.d.ts} +40 -36
  69. package/dist/env.d.ts +102 -0
  70. package/dist/functions/define-function.d.ts +55 -0
  71. package/dist/functions/function-loader.d.ts +18 -0
  72. package/dist/functions/function-routes.d.ts +10 -0
  73. package/{src/functions/index.ts → dist/functions/index.d.ts} +2 -0
  74. package/dist/history/history-routes.d.ts +23 -0
  75. package/{src/history/index.ts → dist/history/index.d.ts} +0 -1
  76. package/dist/index.d.ts +35 -0
  77. package/dist/index.es.js +18184 -0
  78. package/dist/index.es.js.map +1 -0
  79. package/dist/index.umd.js +48629 -0
  80. package/dist/index.umd.js.map +1 -0
  81. package/dist/init/docs.d.ts +4 -0
  82. package/dist/init/health.d.ts +2 -0
  83. package/dist/init/middlewares.d.ts +10 -0
  84. package/dist/init/shutdown.d.ts +65 -0
  85. package/dist/init/storage.d.ts +5 -0
  86. package/dist/init.d.ts +344 -0
  87. package/dist/jwt-BwIn8xmk.js +4200 -0
  88. package/dist/jwt-BwIn8xmk.js.map +1 -0
  89. package/dist/logger-BYU66ENZ.js +94 -0
  90. package/dist/logger-BYU66ENZ.js.map +1 -0
  91. package/dist/ms-DnYXB-Wd.js +162 -0
  92. package/dist/ms-DnYXB-Wd.js.map +1 -0
  93. package/dist/openapi-generator-Z9oYWLf_.js +586 -0
  94. package/dist/openapi-generator-Z9oYWLf_.js.map +1 -0
  95. package/dist/schema-editor-routes-D3ef8zu1.js +219 -0
  96. package/dist/schema-editor-routes-D3ef8zu1.js.map +1 -0
  97. package/dist/serve-spa.d.ts +36 -0
  98. package/dist/services/driver-registry.d.ts +78 -0
  99. package/dist/services/routed-realtime-service.d.ts +43 -0
  100. package/dist/services/webhook-service.d.ts +29 -0
  101. package/dist/singleton.d.ts +51 -0
  102. package/dist/src-B4OLmNVa.js +437 -0
  103. package/dist/src-B4OLmNVa.js.map +1 -0
  104. package/dist/src-B5WkP1Rv.js +24778 -0
  105. package/dist/src-B5WkP1Rv.js.map +1 -0
  106. package/dist/storage/GCSStorageController.d.ts +43 -0
  107. package/dist/storage/LocalStorageController.d.ts +46 -0
  108. package/dist/storage/S3StorageController.d.ts +40 -0
  109. package/dist/storage/image-transform.d.ts +50 -0
  110. package/{src/storage/index.ts → dist/storage/index.d.ts} +6 -21
  111. package/dist/storage/routes.d.ts +70 -0
  112. package/dist/storage/storage-registry.d.ts +78 -0
  113. package/dist/storage/tus-handler.d.ts +53 -0
  114. package/{src/storage/types.ts → dist/storage/types.d.ts} +32 -38
  115. package/dist/types/index.d.ts +11 -0
  116. package/dist/utils/dev-port.d.ts +45 -0
  117. package/dist/utils/dynamic-import.d.ts +25 -0
  118. package/dist/utils/logger.d.ts +31 -0
  119. package/dist/utils/logging.d.ts +9 -0
  120. package/dist/utils/request-id.d.ts +4 -0
  121. package/dist/utils/request-logger.d.ts +19 -0
  122. package/{src/utils/sql.ts → dist/utils/sql.d.ts} +5 -16
  123. package/package.json +49 -22
  124. package/coverage/clover.xml +0 -3739
  125. package/coverage/coverage-final.json +0 -31
  126. package/coverage/lcov-report/base.css +0 -224
  127. package/coverage/lcov-report/block-navigation.js +0 -87
  128. package/coverage/lcov-report/favicon.png +0 -0
  129. package/coverage/lcov-report/index.html +0 -266
  130. package/coverage/lcov-report/prettify.css +0 -1
  131. package/coverage/lcov-report/prettify.js +0 -2
  132. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  133. package/coverage/lcov-report/sorter.js +0 -210
  134. package/coverage/lcov-report/src/api/ast-schema-editor.ts.html +0 -952
  135. package/coverage/lcov-report/src/api/errors.ts.html +0 -472
  136. package/coverage/lcov-report/src/api/graphql/graphql-schema-generator.ts.html +0 -1069
  137. package/coverage/lcov-report/src/api/graphql/index.html +0 -116
  138. package/coverage/lcov-report/src/api/index.html +0 -176
  139. package/coverage/lcov-report/src/api/openapi-generator.ts.html +0 -565
  140. package/coverage/lcov-report/src/api/rest/api-generator.ts.html +0 -994
  141. package/coverage/lcov-report/src/api/rest/index.html +0 -131
  142. package/coverage/lcov-report/src/api/rest/query-parser.ts.html +0 -550
  143. package/coverage/lcov-report/src/api/schema-editor-routes.ts.html +0 -202
  144. package/coverage/lcov-report/src/api/server.ts.html +0 -823
  145. package/coverage/lcov-report/src/auth/admin-routes.ts.html +0 -973
  146. package/coverage/lcov-report/src/auth/index.html +0 -176
  147. package/coverage/lcov-report/src/auth/jwt.ts.html +0 -574
  148. package/coverage/lcov-report/src/auth/middleware.ts.html +0 -745
  149. package/coverage/lcov-report/src/auth/password.ts.html +0 -310
  150. package/coverage/lcov-report/src/auth/services.ts.html +0 -2074
  151. package/coverage/lcov-report/src/collections/index.html +0 -116
  152. package/coverage/lcov-report/src/collections/loader.ts.html +0 -232
  153. package/coverage/lcov-report/src/db/auth-schema.ts.html +0 -523
  154. package/coverage/lcov-report/src/db/data-transformer.ts.html +0 -1753
  155. package/coverage/lcov-report/src/db/entityService.ts.html +0 -700
  156. package/coverage/lcov-report/src/db/index.html +0 -146
  157. package/coverage/lcov-report/src/db/services/EntityFetchService.ts.html +0 -4048
  158. package/coverage/lcov-report/src/db/services/EntityPersistService.ts.html +0 -883
  159. package/coverage/lcov-report/src/db/services/RelationService.ts.html +0 -3121
  160. package/coverage/lcov-report/src/db/services/entity-helpers.ts.html +0 -442
  161. package/coverage/lcov-report/src/db/services/index.html +0 -176
  162. package/coverage/lcov-report/src/db/services/index.ts.html +0 -124
  163. package/coverage/lcov-report/src/generate-drizzle-schema-logic.ts.html +0 -1960
  164. package/coverage/lcov-report/src/index.html +0 -116
  165. package/coverage/lcov-report/src/services/driver-registry.ts.html +0 -631
  166. package/coverage/lcov-report/src/services/index.html +0 -131
  167. package/coverage/lcov-report/src/services/postgresDataDriver.ts.html +0 -3025
  168. package/coverage/lcov-report/src/storage/LocalStorageController.ts.html +0 -1189
  169. package/coverage/lcov-report/src/storage/S3StorageController.ts.html +0 -970
  170. package/coverage/lcov-report/src/storage/index.html +0 -161
  171. package/coverage/lcov-report/src/storage/storage-registry.ts.html +0 -646
  172. package/coverage/lcov-report/src/storage/types.ts.html +0 -451
  173. package/coverage/lcov-report/src/utils/drizzle-conditions.ts.html +0 -3082
  174. package/coverage/lcov-report/src/utils/index.html +0 -116
  175. package/coverage/lcov.info +0 -7179
  176. package/jest.config.cjs +0 -19
  177. package/src/api/ast-schema-editor.ts +0 -289
  178. package/src/api/collections_for_test/callbacks_test_collection.ts +0 -61
  179. package/src/api/errors.ts +0 -278
  180. package/src/api/graphql/graphql-schema-generator.ts +0 -383
  181. package/src/api/graphql/index.ts +0 -2
  182. package/src/api/index.ts +0 -11
  183. package/src/api/logs-routes.ts +0 -132
  184. package/src/api/openapi-generator.ts +0 -727
  185. package/src/api/rest/api-generator-count.test.ts +0 -126
  186. package/src/api/rest/api-generator.ts +0 -747
  187. package/src/api/rest/query-parser.ts +0 -300
  188. package/src/api/schema-editor-routes.ts +0 -41
  189. package/src/api/server.ts +0 -271
  190. package/src/api/types.ts +0 -104
  191. package/src/auth/adapter-middleware.ts +0 -114
  192. package/src/auth/admin-roles-route.ts +0 -36
  193. package/src/auth/admin-user-ops.ts +0 -236
  194. package/src/auth/admin-users-route.ts +0 -302
  195. package/src/auth/api-keys/api-key-middleware.ts +0 -133
  196. package/src/auth/api-keys/api-key-routes.ts +0 -193
  197. package/src/auth/api-keys/api-key-store.ts +0 -340
  198. package/src/auth/apple-oauth.ts +0 -122
  199. package/src/auth/bitbucket-oauth.ts +0 -84
  200. package/src/auth/builtin-auth-adapter.ts +0 -380
  201. package/src/auth/custom-auth-adapter.ts +0 -86
  202. package/src/auth/discord-oauth.ts +0 -85
  203. package/src/auth/facebook-oauth.ts +0 -74
  204. package/src/auth/github-oauth.ts +0 -112
  205. package/src/auth/gitlab-oauth.ts +0 -72
  206. package/src/auth/google-oauth.ts +0 -223
  207. package/src/auth/jwt.ts +0 -208
  208. package/src/auth/linkedin-oauth.ts +0 -83
  209. package/src/auth/magic-link-routes.ts +0 -167
  210. package/src/auth/mfa-crypto.ts +0 -91
  211. package/src/auth/mfa-routes.ts +0 -323
  212. package/src/auth/mfa.ts +0 -161
  213. package/src/auth/microsoft-oauth.ts +0 -90
  214. package/src/auth/middleware.ts +0 -369
  215. package/src/auth/password.ts +0 -77
  216. package/src/auth/rate-limiter.ts +0 -225
  217. package/src/auth/reset-password-admin.ts +0 -160
  218. package/src/auth/rls-scope.ts +0 -58
  219. package/src/auth/routes.ts +0 -802
  220. package/src/auth/session-routes.ts +0 -353
  221. package/src/auth/slack-oauth.ts +0 -73
  222. package/src/auth/spotify-oauth.ts +0 -69
  223. package/src/auth/twitter-oauth.ts +0 -128
  224. package/src/collections/BackendCollectionRegistry.ts +0 -20
  225. package/src/collections/loader.ts +0 -50
  226. package/src/cron/cron-loader.ts +0 -84
  227. package/src/cron/cron-routes.test.ts +0 -265
  228. package/src/cron/cron-routes.ts +0 -85
  229. package/src/cron/cron-scheduler.test.ts +0 -559
  230. package/src/cron/cron-scheduler.ts +0 -581
  231. package/src/cron/cron-store.ts +0 -166
  232. package/src/db/interfaces.ts +0 -60
  233. package/src/email/index.ts +0 -19
  234. package/src/email/smtp-email-service.ts +0 -123
  235. package/src/email/templates.ts +0 -470
  236. package/src/env.ts +0 -227
  237. package/src/functions/function-loader.ts +0 -116
  238. package/src/functions/function-routes.ts +0 -31
  239. package/src/history/history-routes.ts +0 -129
  240. package/src/index.ts +0 -70
  241. package/src/init/docs.ts +0 -47
  242. package/src/init/health.ts +0 -37
  243. package/src/init/middlewares.ts +0 -61
  244. package/src/init/shutdown.ts +0 -56
  245. package/src/init/storage.ts +0 -57
  246. package/src/init.ts +0 -1041
  247. package/src/serve-spa.ts +0 -89
  248. package/src/services/driver-registry.ts +0 -183
  249. package/src/services/routed-realtime-service.ts +0 -113
  250. package/src/services/webhook-service.ts +0 -155
  251. package/src/singleton.ts +0 -70
  252. package/src/storage/LocalStorageController.ts +0 -389
  253. package/src/storage/S3StorageController.ts +0 -300
  254. package/src/storage/image-transform.ts +0 -218
  255. package/src/storage/routes.ts +0 -368
  256. package/src/storage/storage-registry.ts +0 -188
  257. package/src/storage/tus-handler.ts +0 -315
  258. package/src/types/index.ts +0 -27
  259. package/src/utils/dev-port.ts +0 -196
  260. package/src/utils/logger.ts +0 -143
  261. package/src/utils/logging.ts +0 -40
  262. package/src/utils/request-id.ts +0 -40
  263. package/src/utils/request-logger.ts +0 -72
  264. package/test/api-generator.test.ts +0 -588
  265. package/test/api-key-permission-guard.test.ts +0 -144
  266. package/test/ast-schema-editor.test.ts +0 -89
  267. package/test/auth-middleware-hono.test.ts +0 -556
  268. package/test/auth-routes.test.ts +0 -1098
  269. package/test/backend-hooks-data.test.ts +0 -477
  270. package/test/custom-auth-adapter.test.ts +0 -189
  271. package/test/driver-registry.test.ts +0 -282
  272. package/test/email-templates.test.ts +0 -174
  273. package/test/env.test.ts +0 -128
  274. package/test/error-propagation.test.ts +0 -226
  275. package/test/errors-hono.test.ts +0 -133
  276. package/test/errors.test.ts +0 -155
  277. package/test/function-loader.test.ts +0 -127
  278. package/test/graphql-schema-generator.test.ts +0 -554
  279. package/test/jwt-security.test.ts +0 -182
  280. package/test/jwt.test.ts +0 -326
  281. package/test/mfa.test.ts +0 -197
  282. package/test/middleware.test.ts +0 -305
  283. package/test/multi-datasource-routing.test.ts +0 -113
  284. package/test/password.test.ts +0 -165
  285. package/test/query-parser.test.ts +0 -233
  286. package/test/rate-limiter.test.ts +0 -102
  287. package/test/reset-password-admin.test.ts +0 -113
  288. package/test/routed-realtime-service.test.ts +0 -86
  289. package/test/safe-compare.test.ts +0 -66
  290. package/test/singleton.test.ts +0 -59
  291. package/test/smtp-email-service.test.ts +0 -169
  292. package/test/storage-local.test.ts +0 -271
  293. package/test/storage-registry.test.ts +0 -282
  294. package/test/storage-routes.test.ts +0 -222
  295. package/test/storage-s3.test.ts +0 -304
  296. package/test/transform-auth-response.test.ts +0 -305
  297. package/test/webhook-service.test.ts +0 -260
  298. package/tsconfig.json +0 -54
  299. package/tsconfig.prod.json +0 -23
  300. package/vite.config.ts +0 -86
@@ -1,112 +0,0 @@
1
- import type { OAuthProvider, OAuthProviderProfile } from "./interfaces";
2
- import { z } from "zod";
3
- import { logger } from "../utils/logger";
4
-
5
- /**
6
- * Creates a GitHub OAuth Provider integration.
7
- *
8
- * Flow: Frontend receives an authorization `code` via the GitHub OAuth redirect.
9
- * This provider exchanges the code for an access token, then fetches the user's
10
- * profile and primary email from the GitHub API.
11
- */
12
- export function createGitHubProvider(config: { clientId: string; clientSecret: string }): OAuthProvider<{ code: string; redirectUri: string }> {
13
- return {
14
- id: "github",
15
- schema: z.object({
16
- code: z.string().min(1, "Auth code is required"),
17
- redirectUri: z.string().url("Valid redirect URI is required")
18
- }),
19
- verify: async (payload: { code: string; redirectUri: string }): Promise<OAuthProviderProfile | null> => {
20
- try {
21
- // Exchange code for access token
22
- const tokenResponse = await fetch("https://github.com/login/oauth/access_token", {
23
- method: "POST",
24
- headers: {
25
- "Content-Type": "application/json",
26
- "Accept": "application/json"
27
- },
28
- body: JSON.stringify({
29
- client_id: config.clientId,
30
- client_secret: config.clientSecret,
31
- code: payload.code,
32
- redirect_uri: payload.redirectUri
33
- })
34
- });
35
-
36
- if (!tokenResponse.ok) {
37
- logger.error("Failed to get GitHub access token", { detail: await tokenResponse.text() });
38
- return null;
39
- }
40
-
41
- const tokenData = await tokenResponse.json() as { access_token?: string; error?: string };
42
- if (tokenData.error || !tokenData.access_token) {
43
- logger.error("GitHub token exchange error", { detail: tokenData.error });
44
- return null;
45
- }
46
-
47
- const accessToken = tokenData.access_token;
48
-
49
- // Fetch user profile
50
- const profileResponse = await fetch("https://api.github.com/user", {
51
- headers: {
52
- "Authorization": `Bearer ${accessToken}`,
53
- "Accept": "application/vnd.github+json",
54
- "User-Agent": "Rebase-Auth"
55
- }
56
- });
57
-
58
- if (!profileResponse.ok) {
59
- logger.error("Failed to get GitHub user info", { detail: await profileResponse.text() });
60
- return null;
61
- }
62
-
63
- const profileData = await profileResponse.json() as {
64
- id: number;
65
- login: string;
66
- name?: string | null;
67
- avatar_url?: string | null;
68
- email?: string | null;
69
- };
70
-
71
- // GitHub may not return email in profile if it's private.
72
- // Fetch from /user/emails endpoint instead.
73
- let email = profileData.email;
74
- if (!email) {
75
- const emailsResponse = await fetch("https://api.github.com/user/emails", {
76
- headers: {
77
- "Authorization": `Bearer ${accessToken}`,
78
- "Accept": "application/vnd.github+json",
79
- "User-Agent": "Rebase-Auth"
80
- }
81
- });
82
-
83
- if (emailsResponse.ok) {
84
- const emails = await emailsResponse.json() as Array<{
85
- email: string;
86
- primary: boolean;
87
- verified: boolean;
88
- }>;
89
- const primary = emails.find(e => e.primary && e.verified) || emails.find(e => e.verified);
90
- email = primary?.email || null;
91
- }
92
- }
93
-
94
- if (!email) {
95
- logger.error("GitHub user has no verified email");
96
- return null;
97
- }
98
-
99
- return {
100
- providerId: String(profileData.id),
101
- email,
102
- displayName: profileData.name || profileData.login || null,
103
- photoUrl: profileData.avatar_url || null,
104
- emailVerified: true
105
- };
106
- } catch (error) {
107
- logger.error("GitHub OAuth error", { error: error });
108
- return null;
109
- }
110
- }
111
- };
112
- }
@@ -1,72 +0,0 @@
1
- import type { OAuthProvider, OAuthProviderProfile } from "./interfaces";
2
- import { z } from "zod";
3
- import { logger } from "../utils/logger";
4
-
5
- /**
6
- * Creates a GitLab OAuth Provider integration.
7
- * Works with both GitLab.com and self-hosted instances.
8
- */
9
- export function createGitLabProvider(config: {
10
- clientId: string;
11
- clientSecret: string;
12
- baseUrl?: string;
13
- }): OAuthProvider<{ code: string; redirectUri: string }> {
14
- const gitlabUrl = (config.baseUrl || "https://gitlab.com").replace(/\/$/, "");
15
-
16
- return {
17
- id: "gitlab",
18
- schema: z.object({
19
- code: z.string().min(1, "Auth code is required"),
20
- redirectUri: z.string().url("Valid redirect URI is required")
21
- }),
22
- verify: async (payload: { code: string; redirectUri: string }): Promise<OAuthProviderProfile | null> => {
23
- try {
24
- const tokenResponse = await fetch(`${gitlabUrl}/oauth/token`, {
25
- method: "POST",
26
- headers: { "Content-Type": "application/json" },
27
- body: JSON.stringify({
28
- client_id: config.clientId,
29
- client_secret: config.clientSecret,
30
- code: payload.code,
31
- grant_type: "authorization_code",
32
- redirect_uri: payload.redirectUri
33
- })
34
- });
35
-
36
- if (!tokenResponse.ok) {
37
- logger.error("Failed to get GitLab access token", { detail: await tokenResponse.text() });
38
- return null;
39
- }
40
-
41
- const tokenData = await tokenResponse.json() as { access_token: string };
42
-
43
- const profileResponse = await fetch(`${gitlabUrl}/api/v4/user`, {
44
- headers: { "Authorization": `Bearer ${tokenData.access_token}` }
45
- });
46
-
47
- if (!profileResponse.ok) {
48
- logger.error("Failed to get GitLab user info", { detail: await profileResponse.text() });
49
- return null;
50
- }
51
-
52
- const p = await profileResponse.json() as {
53
- id: number; username: string; name?: string;
54
- email: string; avatar_url?: string | null;
55
- };
56
-
57
- if (!p.email) { logger.error("GitLab user has no email"); return null; }
58
-
59
- return {
60
- providerId: String(p.id),
61
- email: p.email,
62
- displayName: p.name || p.username || null,
63
- photoUrl: p.avatar_url || null,
64
- emailVerified: true
65
- };
66
- } catch (error) {
67
- logger.error("GitLab OAuth error", { error: error });
68
- return null;
69
- }
70
- }
71
- };
72
- }
@@ -1,223 +0,0 @@
1
- import { OAuth2Client } from "google-auth-library/build/src/index.js";
2
- import type { OAuthProvider, OAuthProviderProfile } from "./interfaces";
3
- import { z } from "zod";
4
- import { logger } from "../utils/logger";
5
-
6
- export interface GoogleUserInfo {
7
- googleId: string;
8
- email: string;
9
- displayName: string | null;
10
- photoUrl: string | null;
11
- emailVerified: boolean;
12
- }
13
-
14
- export interface GoogleProviderConfig {
15
- clientId: string;
16
- /**
17
- * The OAuth 2.0 client secret from Google Cloud Console.
18
- *
19
- * Required for the **authorization code flow** (Path 3), where the
20
- * frontend sends an authorization `code` and the backend exchanges it
21
- * server-side for tokens. This is the most secure flow because tokens
22
- * never touch the browser.
23
- *
24
- * When omitted, only ID-token and access-token verification are available
25
- * (Paths 1 & 2), which rely on the frontend obtaining tokens directly.
26
- */
27
- clientSecret?: string;
28
- }
29
-
30
- /**
31
- * Creates a Google OAuth Provider integration.
32
- *
33
- * Supports three verification paths:
34
- *
35
- * **Path 1 – ID Token** (One Tap / Sign In With Google button):
36
- * Frontend sends `idToken`. Backend verifies cryptographically using
37
- * Google's public keys. No secret required.
38
- *
39
- * **Path 2 – Access Token** (popup via `initTokenClient`):
40
- * Frontend sends `accessToken`. Backend validates by calling Google's
41
- * userinfo endpoint. No secret required.
42
- *
43
- * **Path 3 – Authorization Code** (most secure, requires `clientSecret`):
44
- * Frontend sends `code` + `redirectUri`. Backend exchanges the code
45
- * server-side for an ID token using `clientId` + `clientSecret`, then
46
- * verifies the ID token. Tokens never touch the browser.
47
- */
48
- export function createGoogleProvider(config: GoogleProviderConfig | string): OAuthProvider<{
49
- idToken?: string;
50
- accessToken?: string;
51
- code?: string;
52
- redirectUri?: string;
53
- }> {
54
- const clientId = typeof config === "string" ? config : config.clientId;
55
- const clientSecret = typeof config === "string" ? undefined : config.clientSecret;
56
- const googleClient = new OAuth2Client(clientId, clientSecret);
57
-
58
- return {
59
- id: "google",
60
- schema: z.object({
61
- idToken: z.string().min(1).optional(),
62
- accessToken: z.string().min(1).optional(),
63
- code: z.string().min(1).optional(),
64
- redirectUri: z.string().min(1).optional()
65
- }).refine(
66
- (data) => data.idToken || data.accessToken || (data.code && data.redirectUri),
67
- { message: "One of idToken, accessToken, or code+redirectUri is required" }
68
- ),
69
- verify: async (payload: {
70
- idToken?: string;
71
- accessToken?: string;
72
- code?: string;
73
- redirectUri?: string;
74
- }): Promise<OAuthProviderProfile | null> => {
75
- try {
76
- // Path 1: verify an ID token (One Tap / renderButton)
77
- if (payload.idToken) {
78
- const ticket = await googleClient.verifyIdToken({
79
- idToken: payload.idToken,
80
- audience: clientId
81
- });
82
-
83
- const content = ticket.getPayload();
84
- if (!content) {
85
- throw new Error("Google ID token payload was empty");
86
- }
87
-
88
- return {
89
- providerId: content.sub,
90
- email: content.email || "",
91
- displayName: content.name || null,
92
- photoUrl: content.picture || null,
93
- emailVerified: true
94
- };
95
- }
96
-
97
- // Path 2: verify an access token via Google's userinfo endpoint
98
- if (payload.accessToken) {
99
- const res = await fetch(
100
- "https://www.googleapis.com/oauth2/v3/userinfo",
101
- { headers: { Authorization: `Bearer ${payload.accessToken}` } }
102
- );
103
- if (!res.ok) {
104
- throw new Error(`Google userinfo request failed with status ${res.status}`);
105
- }
106
- const info = await res.json() as {
107
- sub: string;
108
- email?: string;
109
- name?: string;
110
- picture?: string;
111
- };
112
- if (!info.sub || !info.email) {
113
- throw new Error("Google userinfo response missing sub or email");
114
- }
115
- return {
116
- providerId: info.sub,
117
- email: info.email,
118
- displayName: info.name || null,
119
- photoUrl: info.picture || null,
120
- emailVerified: true
121
- };
122
- }
123
-
124
- // Path 3: authorization code exchange (most secure)
125
- // The frontend obtained a one-time authorization code via the
126
- // Google OAuth consent screen. We exchange it server-side for
127
- // tokens, so the access/id tokens never touch the browser.
128
- if (payload.code && payload.redirectUri) {
129
- if (!clientSecret) {
130
- throw new Error(
131
- "Google authorization code flow requires clientSecret. " +
132
- "Configure GOOGLE_CLIENT_SECRET in your environment."
133
- );
134
- }
135
-
136
- // Exchange the authorization code for tokens
137
- const tokenResponse = await fetch("https://oauth2.googleapis.com/token", {
138
- method: "POST",
139
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
140
- body: new URLSearchParams({
141
- code: payload.code,
142
- client_id: clientId,
143
- client_secret: clientSecret,
144
- redirect_uri: payload.redirectUri,
145
- grant_type: "authorization_code"
146
- })
147
- });
148
-
149
- if (!tokenResponse.ok) {
150
- const errorBody = await tokenResponse.text();
151
- throw new Error(`Google token exchange failed (${tokenResponse.status}): ${errorBody}`);
152
- }
153
-
154
- const tokenData = await tokenResponse.json() as {
155
- id_token?: string;
156
- access_token?: string;
157
- error?: string;
158
- error_description?: string;
159
- };
160
-
161
- if (tokenData.error) {
162
- throw new Error(`Google token exchange error: ${tokenData.error} – ${tokenData.error_description || "no details"}`);
163
- }
164
-
165
- // Prefer verifying the ID token (cryptographic verification)
166
- if (tokenData.id_token) {
167
- const ticket = await googleClient.verifyIdToken({
168
- idToken: tokenData.id_token,
169
- audience: clientId
170
- });
171
-
172
- const content = ticket.getPayload();
173
- if (!content) {
174
- throw new Error("Google ID token payload was empty after code exchange");
175
- }
176
-
177
- return {
178
- providerId: content.sub,
179
- email: content.email || "",
180
- displayName: content.name || null,
181
- photoUrl: content.picture || null,
182
- emailVerified: true
183
- };
184
- }
185
-
186
- // Fallback: use the access token to fetch userinfo
187
- if (tokenData.access_token) {
188
- const userInfoRes = await fetch(
189
- "https://www.googleapis.com/oauth2/v3/userinfo",
190
- { headers: { Authorization: `Bearer ${tokenData.access_token}` } }
191
- );
192
- if (!userInfoRes.ok) {
193
- throw new Error(`Google userinfo request failed after code exchange (${userInfoRes.status})`);
194
- }
195
- const info = await userInfoRes.json() as {
196
- sub: string;
197
- email?: string;
198
- name?: string;
199
- picture?: string;
200
- };
201
- if (!info.sub || !info.email) {
202
- return null;
203
- }
204
- return {
205
- providerId: info.sub,
206
- email: info.email,
207
- displayName: info.name || null,
208
- photoUrl: info.picture || null,
209
- emailVerified: true
210
- };
211
- }
212
-
213
- throw new Error("Google token exchange returned neither id_token nor access_token");
214
- }
215
-
216
- throw new Error("No valid Google credential provided (expected idToken, accessToken, or code+redirectUri)");
217
- } catch (error) {
218
- logger.error("Google OAuth verification failed", { error: error });
219
- throw error;
220
- }
221
- }
222
- };
223
- }
package/src/auth/jwt.ts DELETED
@@ -1,208 +0,0 @@
1
- import jwt from "jsonwebtoken";
2
- import { createHash, randomBytes } from "crypto";
3
- import { logger } from "../utils/logger";
4
-
5
- export interface JwtConfig {
6
- secret: string;
7
- accessExpiresIn?: string;
8
- refreshExpiresIn?: string;
9
- }
10
-
11
- export interface AccessTokenPayload {
12
- userId: string;
13
- roles: string[];
14
- uid?: string;
15
- /** Authentication Assurance Level: aal1 = password/oauth, aal2 = MFA verified */
16
- aal?: "aal1" | "aal2";
17
- /** Email claim from the JWT, if present */
18
- email?: string;
19
- /** Display name claim from the JWT, if present */
20
- displayName?: string;
21
- /** Photo URL claim from the JWT, if present */
22
- photoURL?: string;
23
- /** Whether MFA has been verified for this session */
24
- mfa_verified?: boolean;
25
- /** Authentication Methods Reference — list of methods used (e.g. 'pwd', 'otp') */
26
- amr?: string[];
27
- }
28
-
29
- let jwtConfig: JwtConfig = {
30
- secret: "",
31
- accessExpiresIn: "1h",
32
- refreshExpiresIn: "30d"
33
- };
34
-
35
- /**
36
- * Configure JWT settings - call this during initialization.
37
- * Validates the secret strength to prevent deployment with default/weak secrets.
38
- */
39
- export function configureJwt(config: JwtConfig): void {
40
- // Reject obviously weak/default secrets
41
- const weakSecrets = new Set([
42
- "secret",
43
- "jwt-secret",
44
- "jwt_secret",
45
- "your-secret",
46
- "your-super-secret-jwt-key-change-in-production",
47
- "super-secret-jwt-key-change-in-production",
48
- "change-me",
49
- "changeme",
50
- "password",
51
- "test",
52
- "mysecret",
53
- "my-secret",
54
- "my_secret",
55
- "example-secret",
56
- "please-change-me",
57
- "replace-this-with-a-real-secret",
58
- "default-secret",
59
- "rebase_saas_jwt_secret_must_be_long_long_long_long",
60
- "rebase_saas_service_key_must_be_long_long_long_long"
61
- ]);
62
-
63
- if (!config.secret || config.secret.length < 32) {
64
- throw new Error(
65
- "JWT secret is too short. Must be at least 32 characters. " +
66
- "Generate one with: node -e \"logger.info(require('crypto').randomBytes(48).toString('base64'))\""
67
- );
68
- }
69
-
70
- if (weakSecrets.has(config.secret.toLowerCase())) {
71
- throw new Error(
72
- "JWT secret is a known default/weak value. Please use a strong, randomly generated secret. " +
73
- "Generate one with: node -e \"logger.info(require('crypto').randomBytes(48).toString('base64'))\""
74
- );
75
- }
76
-
77
- jwtConfig = {
78
- ...jwtConfig,
79
- ...config
80
- };
81
- }
82
-
83
- /**
84
- * Generate an access token (short-lived, 1 hour by default)
85
- */
86
- export function generateAccessToken(
87
- userId: string,
88
- roles: string[],
89
- aal: "aal1" | "aal2" = "aal1",
90
- customClaims?: Record<string, unknown>
91
- ): string {
92
- if (!jwtConfig.secret) {
93
- throw new Error("JWT secret not configured. Call configureJwt() first.");
94
- }
95
-
96
- const payload: Record<string, unknown> = {
97
- userId,
98
- roles,
99
- aal,
100
- ...customClaims
101
- };
102
-
103
- return jwt.sign(payload, jwtConfig.secret, {
104
- expiresIn: jwtConfig.accessExpiresIn as jwt.SignOptions["expiresIn"],
105
- algorithm: "HS256"
106
- });
107
- }
108
-
109
- /**
110
- * Get the expiration time of an access token in milliseconds from now
111
- */
112
- export function getAccessTokenExpiryMs(): number {
113
- const duration = jwtConfig.accessExpiresIn || "1h";
114
- const match = duration.match(/^(\d+)([dhms])$/);
115
-
116
- if (!match) {
117
- // Default to 1 hour
118
- return 60 * 60 * 1000;
119
- }
120
-
121
- const value = parseInt(match[1], 10);
122
- const unit = match[2];
123
-
124
- switch (unit) {
125
- case "d": return value * 24 * 60 * 60 * 1000;
126
- case "h": return value * 60 * 60 * 1000;
127
- case "m": return value * 60 * 1000;
128
- case "s": return value * 1000;
129
- default: return 60 * 60 * 1000;
130
- }
131
- }
132
-
133
- /**
134
- * Get the expiration timestamp for an access token
135
- */
136
- export function getAccessTokenExpiry(): number {
137
- return Date.now() + getAccessTokenExpiryMs();
138
- }
139
-
140
- /**
141
- * Verify and decode an access token
142
- */
143
- export function verifyAccessToken(token: string): AccessTokenPayload | null {
144
- if (!jwtConfig.secret) {
145
- throw new Error("JWT secret not configured. Call configureJwt() first.");
146
- }
147
-
148
- try {
149
- const decoded = jwt.verify(token, jwtConfig.secret, { algorithms: ["HS256"] }) as { userId?: string; uid?: string; sub?: string; roles?: string[]; aal?: string };
150
- const id = decoded.userId || decoded.uid || decoded.sub;
151
- if (!id) {
152
- logger.error("[JWT] Verification failed: missing id in payload", { detail: decoded });
153
- return null;
154
- }
155
-
156
- const aal = (decoded.aal === "aal1" || decoded.aal === "aal2") ? decoded.aal : "aal1";
157
-
158
- return {
159
- userId: id,
160
- roles: decoded.roles || [],
161
- aal
162
- };
163
- } catch (error) {
164
- logger.error("[JWT] Verification failed", { error: error, detail: token.substring(0, 15) });
165
- return null;
166
- }
167
- }
168
-
169
- /**
170
- * Generate a random refresh token (long-lived, 30 days by default)
171
- */
172
- export function generateRefreshToken(): string {
173
- return randomBytes(40).toString("hex");
174
- }
175
-
176
- /**
177
- * Hash a refresh token for database storage (don't store raw tokens)
178
- */
179
- export function hashRefreshToken(token: string): string {
180
- return createHash("sha256").update(token).digest("hex");
181
- }
182
-
183
- /**
184
- * Calculate refresh token expiration date
185
- */
186
- export function getRefreshTokenExpiry(): Date {
187
- const duration = jwtConfig.refreshExpiresIn || "30d";
188
- const match = duration.match(/^(\d+)([dhms])$/);
189
-
190
- if (!match) {
191
- // Default to 30 days
192
- return new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);
193
- }
194
-
195
- const value = parseInt(match[1], 10);
196
- const unit = match[2];
197
-
198
- let ms: number;
199
- switch (unit) {
200
- case "d": ms = value * 24 * 60 * 60 * 1000; break;
201
- case "h": ms = value * 60 * 60 * 1000; break;
202
- case "m": ms = value * 60 * 1000; break;
203
- case "s": ms = value * 1000; break;
204
- default: ms = 30 * 24 * 60 * 60 * 1000;
205
- }
206
-
207
- return new Date(Date.now() + ms);
208
- }