@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,556 +0,0 @@
1
- import { describe, it, expect, beforeAll, jest } from "@jest/globals";
2
- import { Hono, Context } from "hono";
3
- import { configureJwt, generateAccessToken } from "../src/auth/jwt";
4
- import { requireAuth, optionalAuth, requireAdmin, createAuthMiddleware, createRequireAuth } from "../src/auth/middleware";
5
- import type { HonoEnv } from "../src/api/types";
6
- import type { DataDriver } from "../../types/src/controllers/data_driver";
7
-
8
- const TEST_SECRET = "test-secret-key-for-hono-middleware-testing-1234567890";
9
-
10
- describe("Auth Middleware (Hono)", () => {
11
-
12
- beforeAll(() => {
13
- configureJwt({ secret: TEST_SECRET,
14
- accessExpiresIn: "1h" });
15
- });
16
-
17
- // ── requireAuth ─────────────────────────────────────────
18
- describe("requireAuth", () => {
19
- function createApp() {
20
- const app = new Hono<HonoEnv>();
21
- app.use("/protected/*", requireAuth);
22
- app.get("/protected/resource", (c: Context<HonoEnv>) => {
23
- const user = c.get("user");
24
- return c.json({ user });
25
- });
26
- return app;
27
- }
28
-
29
- it("passes with valid Bearer token", async () => {
30
- const app = createApp();
31
- const token = generateAccessToken("user-1", ["admin"]);
32
- const res = await app.request("/protected/resource", {
33
- headers: { Authorization: `Bearer ${token}` }
34
- });
35
- expect(res.status).toBe(200);
36
- const body = await res.json() as any;
37
- expect(body.user.userId).toBe("user-1");
38
- expect(body.user.roles).toEqual(["admin"]);
39
- });
40
-
41
- it("returns 401 for missing Authorization header", async () => {
42
- const app = createApp();
43
- const res = await app.request("/protected/resource");
44
- expect(res.status).toBe(401);
45
- const body = await res.json() as any;
46
- expect(body.error.code).toBe("UNAUTHORIZED");
47
- });
48
-
49
- it("returns 401 for non-Bearer prefix", async () => {
50
- const app = createApp();
51
- const res = await app.request("/protected/resource", {
52
- headers: { Authorization: "Basic abc123" }
53
- });
54
- expect(res.status).toBe(401);
55
- });
56
-
57
- it("returns 401 for invalid/expired token", async () => {
58
- const app = createApp();
59
- const res = await app.request("/protected/resource", {
60
- headers: { Authorization: "Bearer invalid.token.here" }
61
- });
62
- expect(res.status).toBe(401);
63
- const body = await res.json() as any;
64
- expect(body.error.code).toBe("UNAUTHORIZED");
65
- });
66
-
67
- it("accepts token via query parameter", async () => {
68
- const app = createApp();
69
- const token = generateAccessToken("user-2", ["editor"]);
70
- const res = await app.request(`/protected/resource?token=${token}`);
71
- expect(res.status).toBe(200);
72
- const body = await res.json() as any;
73
- expect(body.user.userId).toBe("user-2");
74
- });
75
-
76
- it("prefers Bearer token over query parameter", async () => {
77
- const app = createApp();
78
- const bearerToken = generateAccessToken("bearer-user", ["admin"]);
79
- const queryToken = generateAccessToken("query-user", ["viewer"]);
80
- const res = await app.request(`/protected/resource?token=${queryToken}`, {
81
- headers: { Authorization: `Bearer ${bearerToken}` }
82
- });
83
- expect(res.status).toBe(200);
84
- const body = await res.json() as any;
85
- expect(body.user.userId).toBe("bearer-user");
86
- });
87
- });
88
-
89
- // ── requireAdmin ────────────────────────────────────────
90
- describe("requireAdmin", () => {
91
- function createApp() {
92
- const app = new Hono<HonoEnv>();
93
- app.use("/admin/*", requireAuth, requireAdmin);
94
- app.get("/admin/dashboard", (c: Context<HonoEnv>) => c.json({ ok: true }));
95
- return app;
96
- }
97
-
98
- it("allows admin users", async () => {
99
- const app = createApp();
100
- const token = generateAccessToken("admin-1", ["admin"]);
101
- const res = await app.request("/admin/dashboard", {
102
- headers: { Authorization: `Bearer ${token}` }
103
- });
104
- expect(res.status).toBe(200);
105
- });
106
-
107
- it("allows schema-admin users", async () => {
108
- const app = createApp();
109
- const token = generateAccessToken("schema-admin-1", ["schema-admin"]);
110
- const res = await app.request("/admin/dashboard", {
111
- headers: { Authorization: `Bearer ${token}` }
112
- });
113
- expect(res.status).toBe(200);
114
- });
115
-
116
- it("returns 403 for non-admin users", async () => {
117
- const app = createApp();
118
- const token = generateAccessToken("user-1", ["editor"]);
119
- const res = await app.request("/admin/dashboard", {
120
- headers: { Authorization: `Bearer ${token}` }
121
- });
122
- expect(res.status).toBe(403);
123
- const body = await res.json() as any;
124
- expect(body.error.code).toBe("FORBIDDEN");
125
- });
126
-
127
- it("returns 401 when requireAdmin is used without requireAuth", async () => {
128
- const app = new Hono<HonoEnv>();
129
- app.use("/admin/*", requireAdmin);
130
- app.get("/admin/dashboard", (c: Context<HonoEnv>) => c.json({ ok: true }));
131
-
132
- const res = await app.request("/admin/dashboard");
133
- expect(res.status).toBe(401);
134
- });
135
- });
136
-
137
- // ── optionalAuth ────────────────────────────────────────
138
- describe("optionalAuth", () => {
139
- function createApp() {
140
- const app = new Hono<HonoEnv>();
141
- app.use("/public/*", optionalAuth);
142
- app.get("/public/feed", (c: Context<HonoEnv>) => {
143
- const user = c.get("user");
144
- return c.json({ authenticated: !!user,
145
- user: user ?? null });
146
- });
147
- return app;
148
- }
149
-
150
- it("sets user when valid token is present", async () => {
151
- const app = createApp();
152
- const token = generateAccessToken("opt-user", ["viewer"]);
153
- const res = await app.request("/public/feed", {
154
- headers: { Authorization: `Bearer ${token}` }
155
- });
156
- const body = await res.json() as any;
157
- expect(res.status).toBe(200);
158
- expect(body.authenticated).toBe(true);
159
- expect(body.user.userId).toBe("opt-user");
160
- });
161
-
162
- it("proceeds without user when no token", async () => {
163
- const app = createApp();
164
- const res = await app.request("/public/feed");
165
- const body = await res.json() as any;
166
- expect(res.status).toBe(200);
167
- expect(body.authenticated).toBe(false);
168
- expect(body.user).toBeNull();
169
- });
170
-
171
- it("proceeds without user when token is invalid", async () => {
172
- const app = createApp();
173
- const res = await app.request("/public/feed", {
174
- headers: { Authorization: "Bearer garbage.token.value" }
175
- });
176
- const body = await res.json() as any;
177
- expect(res.status).toBe(200);
178
- expect(body.authenticated).toBe(false);
179
- });
180
- });
181
-
182
- // ── createAuthMiddleware ────────────────────────────────
183
- describe("createAuthMiddleware", () => {
184
- const mockDriver: DataDriver = {
185
- fetchCollection: jest.fn() as any,
186
- fetchEntity: jest.fn() as any,
187
- saveEntity: jest.fn() as any,
188
- deleteEntity: jest.fn() as any
189
- };
190
-
191
- it("sets driver in context (requireAuth: false)", async () => {
192
- const app = new Hono<HonoEnv>();
193
- app.use("/*", createAuthMiddleware({ driver: mockDriver,
194
- requireAuth: false }));
195
- app.get("/test", (c) => {
196
- const driver = c.get("driver");
197
- return c.json({ hasDriver: !!driver });
198
- });
199
-
200
- const res = await app.request("/test");
201
- expect(res.status).toBe(200);
202
- const body = await res.json() as any;
203
- expect(body.hasDriver).toBe(true);
204
- });
205
-
206
- it("enforces auth by default (requireAuth defaults to true)", async () => {
207
- const app = new Hono<HonoEnv>();
208
- app.use("/*", createAuthMiddleware({ driver: mockDriver }));
209
- app.get("/test", (c) => c.json({ ok: true }));
210
-
211
- const res = await app.request("/test");
212
- expect(res.status).toBe(401);
213
- });
214
-
215
- it("enforces auth when requireAuth is true", async () => {
216
- const app = new Hono<HonoEnv>();
217
- app.use("/*", createAuthMiddleware({ driver: mockDriver,
218
- requireAuth: true }));
219
- app.get("/test", (c) => c.json({ ok: true }));
220
-
221
- const res = await app.request("/test");
222
- expect(res.status).toBe(401);
223
- });
224
-
225
- it("allows anonymous when requireAuth is false", async () => {
226
- const app = new Hono<HonoEnv>();
227
- app.use("/*", createAuthMiddleware({ driver: mockDriver,
228
- requireAuth: false }));
229
- app.get("/test", (c) => c.json({ ok: true }));
230
-
231
- const res = await app.request("/test");
232
- expect(res.status).toBe(200);
233
- });
234
-
235
- it("extracts JWT and sets user in context", async () => {
236
- const app = new Hono<HonoEnv>();
237
- // Default requireAuth=true, but providing a valid token satisfies it
238
- app.use("/*", createAuthMiddleware({ driver: mockDriver }));
239
- app.get("/test", (c) => {
240
- const user = c.get("user");
241
- return c.json({ user });
242
- });
243
-
244
- const token = generateAccessToken("jwt-user", ["admin"]);
245
- const res = await app.request("/test", {
246
- headers: { Authorization: `Bearer ${token}` }
247
- });
248
- const body = await res.json() as any;
249
- expect(body.user.userId).toBe("jwt-user");
250
- expect(body.user.roles).toEqual(["admin"]);
251
- });
252
-
253
- it("rejects invalid tokens even with requireAuth: false (fail closed)", async () => {
254
- const app = new Hono<HonoEnv>();
255
- app.use("/*", createAuthMiddleware({ driver: mockDriver,
256
- requireAuth: false }));
257
- app.get("/test", (c) => c.json({ ok: true }));
258
-
259
- const res = await app.request("/test", {
260
- headers: { Authorization: "Bearer invalid.garbage.token" }
261
- });
262
- expect(res.status).toBe(401);
263
- const body = await res.json() as any;
264
- expect(body.error.code).toBe("UNAUTHORIZED");
265
- });
266
-
267
- it("uses custom validator when provided", async () => {
268
- const app = new Hono<HonoEnv>();
269
- app.use("/*", createAuthMiddleware({
270
- driver: mockDriver,
271
- validator: async (c: Context<HonoEnv>) => {
272
- const apiKey = c.req.header("x-api-key");
273
- if (apiKey === "valid-key") {
274
- return { userId: "api-user",
275
- roles: ["api"] };
276
- }
277
- return false;
278
- }
279
- }));
280
- app.get("/test", (c) => {
281
- const user = c.get("user");
282
- return c.json({ user: user ?? null });
283
- });
284
-
285
- // Valid API key
286
- const res = await app.request("/test", {
287
- headers: { "x-api-key": "valid-key" }
288
- });
289
- const body = await res.json() as any;
290
- expect(body.user.userId).toBe("api-user");
291
-
292
- // Invalid API key — requireAuth not set, defaults to true,
293
- // so unauthenticated requests are rejected
294
- const res2 = await app.request("/test", {
295
- headers: { "x-api-key": "bad-key" }
296
- });
297
- expect(res2.status).toBe(401); // Secure by default
298
- });
299
-
300
- it("returns 401 when custom validator throws", async () => {
301
- const app = new Hono<HonoEnv>();
302
- app.use("/*", createAuthMiddleware({
303
- driver: mockDriver,
304
- validator: async () => { throw new Error("auth failed"); }
305
- }));
306
- app.get("/test", (c) => c.json({ ok: true }));
307
-
308
- const res = await app.request("/test");
309
- expect(res.status).toBe(401);
310
- });
311
-
312
- it("calls withAuth on driver when available", async () => {
313
- const scopedDriver = { ...mockDriver,
314
- isScopedDriver: true };
315
- const driverWithAuth = {
316
- ...mockDriver,
317
- withAuth: jest.fn().mockResolvedValue(scopedDriver)
318
- };
319
-
320
- const app = new Hono<HonoEnv>();
321
- app.use("/*", createAuthMiddleware({ driver: driverWithAuth as any }));
322
- app.get("/test", (c) => {
323
- const driver = c.get("driver") as any;
324
- return c.json({ scoped: !!driver?.isScopedDriver });
325
- });
326
-
327
- const token = generateAccessToken("rls-user", ["editor"]);
328
- const res = await app.request("/test", {
329
- headers: { Authorization: `Bearer ${token}` }
330
- });
331
- const body = await res.json() as any;
332
- expect(body.scoped).toBe(true);
333
- expect(driverWithAuth.withAuth).toHaveBeenCalledWith(
334
- expect.objectContaining({ uid: "rls-user",
335
- roles: ["editor"] })
336
- );
337
- });
338
-
339
- it("handles validator returning true (default user)", async () => {
340
- const app = new Hono<HonoEnv>();
341
- app.use("/*", createAuthMiddleware({
342
- driver: mockDriver,
343
- requireAuth: false, // Explicit opt-out for this test
344
- validator: async () => true
345
- }));
346
- app.get("/test", (c) => {
347
- const user = c.get("user");
348
- return c.json({ user });
349
- });
350
-
351
- const res = await app.request("/test");
352
- const body = await res.json() as any;
353
- expect(body.user.userId).toBe("default");
354
- });
355
- });
356
-
357
- describe("Service Key Authentication", () => {
358
- const SERVICE_KEY = "a-very-secure-service-key-that-is-at-least-32-characters-long";
359
- const mockDriver: DataDriver = {
360
- fetchCollection: jest.fn() as any,
361
- fetchEntity: jest.fn() as any,
362
- saveEntity: jest.fn() as any,
363
- deleteEntity: jest.fn() as any
364
- };
365
-
366
- it("grants admin access when Bearer token matches the service key", async () => {
367
- const app = new Hono<HonoEnv>();
368
- app.use("/*", createAuthMiddleware({
369
- driver: mockDriver,
370
- requireAuth: true,
371
- serviceKey: SERVICE_KEY
372
- }));
373
- app.get("/test", (c) => {
374
- const user = c.get("user");
375
- return c.json({ user });
376
- });
377
-
378
- const res = await app.request("/test", {
379
- headers: { Authorization: `Bearer ${SERVICE_KEY}` }
380
- });
381
- expect(res.status).toBe(200);
382
- const body = await res.json() as any;
383
- expect(body.user.userId).toBe("service");
384
- expect(body.user.roles).toEqual(["admin"]);
385
- });
386
-
387
- it("rejects when Bearer token does not match the service key", async () => {
388
- const app = new Hono<HonoEnv>();
389
- app.use("/*", createAuthMiddleware({
390
- driver: mockDriver,
391
- requireAuth: true,
392
- serviceKey: SERVICE_KEY
393
- }));
394
- app.get("/test", (c) => c.json({ ok: true }));
395
-
396
- const res = await app.request("/test", {
397
- headers: { Authorization: "Bearer wrong-key-that-is-definitely-not-correct" }
398
- });
399
- // Wrong key is not a valid service key, and also not a valid JWT → 401
400
- expect(res.status).toBe(401);
401
- });
402
-
403
- it("still accepts valid JWT tokens when service key is configured", async () => {
404
- const token = generateAccessToken("jwt-user", ["editor"]);
405
- const app = new Hono<HonoEnv>();
406
- app.use("/*", createAuthMiddleware({
407
- driver: mockDriver,
408
- requireAuth: true,
409
- serviceKey: SERVICE_KEY
410
- }));
411
- app.get("/test", (c) => {
412
- const user = c.get("user");
413
- return c.json({ user });
414
- });
415
-
416
- const res = await app.request("/test", {
417
- headers: { Authorization: `Bearer ${token}` }
418
- });
419
- expect(res.status).toBe(200);
420
- const body = await res.json() as any;
421
- expect(body.user.userId).toBe("jwt-user");
422
- expect(body.user.roles).toEqual(["editor"]);
423
- });
424
-
425
- it("scopes driver via withAuth with service identity", async () => {
426
- const scopedDriver = { scoped: true } as unknown as DataDriver;
427
- const driverWithAuth = {
428
- withAuth: jest.fn().mockResolvedValue(scopedDriver)
429
- } as unknown as DataDriver;
430
-
431
- const app = new Hono<HonoEnv>();
432
- app.use("/*", createAuthMiddleware({
433
- driver: driverWithAuth,
434
- requireAuth: true,
435
- serviceKey: SERVICE_KEY
436
- }));
437
- app.get("/test", (c) => {
438
- const d = c.get("driver") as any;
439
- return c.json({ scoped: d.scoped });
440
- });
441
-
442
- const res = await app.request("/test", {
443
- headers: { Authorization: `Bearer ${SERVICE_KEY}` }
444
- });
445
- const body = await res.json() as any;
446
- expect(body.scoped).toBe(true);
447
- expect((driverWithAuth as any).withAuth).toHaveBeenCalledWith(
448
- expect.objectContaining({ uid: "service",
449
- roles: ["admin"] })
450
- );
451
- });
452
-
453
- it("does not activate service key path when no serviceKey is configured", async () => {
454
- const app = new Hono<HonoEnv>();
455
- app.use("/*", createAuthMiddleware({
456
- driver: mockDriver,
457
- requireAuth: true
458
- // No serviceKey configured
459
- }));
460
- app.get("/test", (c) => c.json({ ok: true }));
461
-
462
- // Sending a random string should fail as invalid JWT
463
- const res = await app.request("/test", {
464
- headers: { Authorization: `Bearer ${SERVICE_KEY}` }
465
- });
466
- expect(res.status).toBe(401);
467
- });
468
- });
469
-
470
- // ── createRequireAuth (standalone factory) ──────────────
471
- describe("createRequireAuth", () => {
472
- const SERVICE_KEY = "a-very-secure-service-key-that-is-at-least-32-characters-long";
473
- it("returns the default requireAuth when no serviceKey is provided", async () => {
474
- const middleware = createRequireAuth();
475
- // The returned function should be the original requireAuth
476
- expect(middleware).toBe(requireAuth);
477
- });
478
-
479
- it("returns the default requireAuth when serviceKey is undefined", async () => {
480
- const middleware = createRequireAuth({ serviceKey: undefined });
481
- expect(middleware).toBe(requireAuth);
482
- });
483
-
484
- it("authenticates with a valid service key", async () => {
485
- const app = new Hono<HonoEnv>();
486
- app.use("/*", createRequireAuth({ serviceKey: SERVICE_KEY }));
487
- app.get("/admin/users", (c) => {
488
- const user = c.get("user");
489
- return c.json({ userId: user?.userId,
490
- roles: user?.roles });
491
- });
492
-
493
- const res = await app.request("/admin/users", {
494
- headers: { Authorization: `Bearer ${SERVICE_KEY}` }
495
- });
496
- expect(res.status).toBe(200);
497
- const body = await res.json() as any;
498
- expect(body.userId).toBe("service");
499
- expect(body.roles).toEqual(["admin"]);
500
- });
501
-
502
- it("rejects an incorrect service key and falls back to JWT (which also fails)", async () => {
503
- const app = new Hono<HonoEnv>();
504
- app.use("/*", createRequireAuth({ serviceKey: SERVICE_KEY }));
505
- app.get("/admin/users", (c) => c.json({ ok: true }));
506
-
507
- const res = await app.request("/admin/users", {
508
- headers: { Authorization: "Bearer wrong-key" }
509
- });
510
- expect(res.status).toBe(401);
511
- });
512
-
513
- it("falls back to valid JWT when service key doesn't match", async () => {
514
- const token = generateAccessToken("user-123", ["admin"]);
515
- const app = new Hono<HonoEnv>();
516
- app.use("/*", createRequireAuth({ serviceKey: SERVICE_KEY }));
517
- app.get("/admin/users", (c) => {
518
- const user = c.get("user");
519
- return c.json({ userId: user?.userId,
520
- roles: user?.roles });
521
- });
522
-
523
- const res = await app.request("/admin/users", {
524
- headers: { Authorization: `Bearer ${token}` }
525
- });
526
- expect(res.status).toBe(200);
527
- const body = await res.json() as any;
528
- expect(body.userId).toBe("user-123");
529
- expect(body.roles).toEqual(["admin"]);
530
- });
531
-
532
- it("returns 401 when no authorization header is provided", async () => {
533
- const app = new Hono<HonoEnv>();
534
- app.use("/*", createRequireAuth({ serviceKey: SERVICE_KEY }));
535
- app.get("/admin/users", (c) => c.json({ ok: true }));
536
-
537
- const res = await app.request("/admin/users");
538
- expect(res.status).toBe(401);
539
- });
540
-
541
- it("works with requireAdmin for service key (admin role is granted)", async () => {
542
- const app = new Hono<HonoEnv>();
543
- app.use("/*", createRequireAuth({ serviceKey: SERVICE_KEY }));
544
- app.get("/admin/users", requireAdmin, (c) => {
545
- return c.json({ authorized: true });
546
- });
547
-
548
- const res = await app.request("/admin/users", {
549
- headers: { Authorization: `Bearer ${SERVICE_KEY}` }
550
- });
551
- expect(res.status).toBe(200);
552
- const body = await res.json() as any;
553
- expect(body.authorized).toBe(true);
554
- });
555
- });
556
- });