@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.
- package/README.md +2 -2
- package/dist/api/ast-schema-editor.d.ts +21 -0
- package/dist/api/collections_for_test/callbacks_test_collection.d.ts +2 -0
- package/dist/api/errors.d.ts +53 -0
- package/dist/api/index.d.ts +7 -0
- package/dist/api/logs-routes.d.ts +37 -0
- package/dist/api/openapi-generator.d.ts +16 -0
- package/dist/api/rest/api-generator.d.ts +68 -0
- package/{src/api/rest/index.ts → dist/api/rest/index.d.ts} +0 -1
- package/dist/api/rest/query-parser.d.ts +6 -0
- package/dist/api/schema-editor-routes.d.ts +3 -0
- package/dist/api/types.d.ts +56 -0
- package/dist/auth/adapter-middleware.d.ts +43 -0
- package/dist/auth/admin-roles-route.d.ts +18 -0
- package/dist/auth/admin-user-ops.d.ts +79 -0
- package/dist/auth/admin-users-route.d.ts +28 -0
- package/dist/auth/api-keys/api-key-middleware.d.ts +39 -0
- package/{src/auth/api-keys/api-key-permission-guard.ts → dist/auth/api-keys/api-key-permission-guard.d.ts} +2 -34
- package/dist/auth/api-keys/api-key-routes.d.ts +20 -0
- package/dist/auth/api-keys/api-key-store.d.ts +35 -0
- package/{src/auth/api-keys/api-key-types.ts → dist/auth/api-keys/api-key-types.d.ts} +0 -6
- package/{src/auth/api-keys/index.ts → dist/auth/api-keys/index.d.ts} +2 -22
- package/dist/auth/apple-oauth.d.ts +30 -0
- package/{src/auth/auth-hooks.ts → dist/auth/auth-hooks.d.ts} +15 -73
- package/dist/auth/bitbucket-oauth.d.ts +11 -0
- package/dist/auth/builtin-auth-adapter.d.ts +55 -0
- package/dist/auth/cookie-utils.d.ts +32 -0
- package/{src/auth/crypto-utils.ts → dist/auth/crypto-utils.d.ts} +1 -16
- package/dist/auth/custom-auth-adapter.d.ts +39 -0
- package/dist/auth/discord-oauth.d.ts +14 -0
- package/dist/auth/facebook-oauth.d.ts +14 -0
- package/dist/auth/github-oauth.d.ts +15 -0
- package/dist/auth/gitlab-oauth.d.ts +13 -0
- package/dist/auth/google-oauth.d.ts +47 -0
- package/{src/auth/index.ts → dist/auth/index.d.ts} +4 -22
- package/{src/auth/interfaces.ts → dist/auth/interfaces.d.ts} +20 -89
- package/dist/auth/jwt.d.ts +67 -0
- package/dist/auth/linkedin-oauth.d.ts +18 -0
- package/dist/auth/magic-link-routes.d.ts +32 -0
- package/dist/auth/mfa-crypto.d.ts +23 -0
- package/dist/auth/mfa-routes.d.ts +7 -0
- package/dist/auth/mfa.d.ts +49 -0
- package/dist/auth/microsoft-oauth.d.ts +16 -0
- package/dist/auth/middleware.d.ts +158 -0
- package/dist/auth/password.d.ts +22 -0
- package/dist/auth/rate-limiter.d.ts +50 -0
- package/dist/auth/reset-password-admin.d.ts +29 -0
- package/dist/auth/rls-scope.d.ts +31 -0
- package/dist/auth/routes.d.ts +66 -0
- package/dist/auth/session-routes.d.ts +29 -0
- package/dist/auth/slack-oauth.d.ts +12 -0
- package/dist/auth/spotify-oauth.d.ts +12 -0
- package/dist/auth/twitter-oauth.d.ts +18 -0
- package/dist/collections/BackendCollectionRegistry.d.ts +13 -0
- package/dist/collections/loader.d.ts +5 -0
- package/dist/cron/cron-loader.d.ts +18 -0
- package/dist/cron/cron-routes.d.ts +14 -0
- package/dist/cron/cron-scheduler.d.ts +106 -0
- package/dist/cron/cron-store.d.ts +32 -0
- package/dist/cron/define-cron.d.ts +28 -0
- package/{src/cron/index.ts → dist/cron/index.d.ts} +1 -0
- package/dist/db/interfaces.d.ts +18 -0
- package/dist/dynamic-import-Dvh-K5fl.js +22 -0
- package/dist/dynamic-import-Dvh-K5fl.js.map +1 -0
- package/dist/email/index.d.ts +6 -0
- package/dist/email/smtp-email-service.d.ts +30 -0
- package/dist/email/templates.d.ts +50 -0
- package/{src/email/types.ts → dist/email/types.d.ts} +40 -36
- package/dist/env.d.ts +102 -0
- package/dist/functions/define-function.d.ts +55 -0
- package/dist/functions/function-loader.d.ts +18 -0
- package/dist/functions/function-routes.d.ts +10 -0
- package/{src/functions/index.ts → dist/functions/index.d.ts} +2 -0
- package/dist/history/history-routes.d.ts +23 -0
- package/{src/history/index.ts → dist/history/index.d.ts} +0 -1
- package/dist/index.d.ts +35 -0
- package/dist/index.es.js +18184 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +48629 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/init/docs.d.ts +4 -0
- package/dist/init/health.d.ts +2 -0
- package/dist/init/middlewares.d.ts +10 -0
- package/dist/init/shutdown.d.ts +65 -0
- package/dist/init/storage.d.ts +5 -0
- package/dist/init.d.ts +344 -0
- package/dist/jwt-BwIn8xmk.js +4200 -0
- package/dist/jwt-BwIn8xmk.js.map +1 -0
- package/dist/logger-BYU66ENZ.js +94 -0
- package/dist/logger-BYU66ENZ.js.map +1 -0
- package/dist/ms-DnYXB-Wd.js +162 -0
- package/dist/ms-DnYXB-Wd.js.map +1 -0
- package/dist/openapi-generator-Z9oYWLf_.js +586 -0
- package/dist/openapi-generator-Z9oYWLf_.js.map +1 -0
- package/dist/schema-editor-routes-D3ef8zu1.js +219 -0
- package/dist/schema-editor-routes-D3ef8zu1.js.map +1 -0
- package/dist/serve-spa.d.ts +36 -0
- package/dist/services/driver-registry.d.ts +78 -0
- package/dist/services/routed-realtime-service.d.ts +43 -0
- package/dist/services/webhook-service.d.ts +29 -0
- package/dist/singleton.d.ts +51 -0
- package/dist/src-B4OLmNVa.js +437 -0
- package/dist/src-B4OLmNVa.js.map +1 -0
- package/dist/src-B5WkP1Rv.js +24778 -0
- package/dist/src-B5WkP1Rv.js.map +1 -0
- package/dist/storage/GCSStorageController.d.ts +43 -0
- package/dist/storage/LocalStorageController.d.ts +46 -0
- package/dist/storage/S3StorageController.d.ts +40 -0
- package/dist/storage/image-transform.d.ts +50 -0
- package/{src/storage/index.ts → dist/storage/index.d.ts} +6 -21
- package/dist/storage/routes.d.ts +70 -0
- package/dist/storage/storage-registry.d.ts +78 -0
- package/dist/storage/tus-handler.d.ts +53 -0
- package/{src/storage/types.ts → dist/storage/types.d.ts} +32 -38
- package/dist/types/index.d.ts +11 -0
- package/dist/utils/dev-port.d.ts +45 -0
- package/dist/utils/dynamic-import.d.ts +25 -0
- package/dist/utils/logger.d.ts +31 -0
- package/dist/utils/logging.d.ts +9 -0
- package/dist/utils/request-id.d.ts +4 -0
- package/dist/utils/request-logger.d.ts +19 -0
- package/{src/utils/sql.ts → dist/utils/sql.d.ts} +5 -16
- package/package.json +49 -22
- package/coverage/clover.xml +0 -3739
- package/coverage/coverage-final.json +0 -31
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -87
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -266
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -210
- package/coverage/lcov-report/src/api/ast-schema-editor.ts.html +0 -952
- package/coverage/lcov-report/src/api/errors.ts.html +0 -472
- package/coverage/lcov-report/src/api/graphql/graphql-schema-generator.ts.html +0 -1069
- package/coverage/lcov-report/src/api/graphql/index.html +0 -116
- package/coverage/lcov-report/src/api/index.html +0 -176
- package/coverage/lcov-report/src/api/openapi-generator.ts.html +0 -565
- package/coverage/lcov-report/src/api/rest/api-generator.ts.html +0 -994
- package/coverage/lcov-report/src/api/rest/index.html +0 -131
- package/coverage/lcov-report/src/api/rest/query-parser.ts.html +0 -550
- package/coverage/lcov-report/src/api/schema-editor-routes.ts.html +0 -202
- package/coverage/lcov-report/src/api/server.ts.html +0 -823
- package/coverage/lcov-report/src/auth/admin-routes.ts.html +0 -973
- package/coverage/lcov-report/src/auth/index.html +0 -176
- package/coverage/lcov-report/src/auth/jwt.ts.html +0 -574
- package/coverage/lcov-report/src/auth/middleware.ts.html +0 -745
- package/coverage/lcov-report/src/auth/password.ts.html +0 -310
- package/coverage/lcov-report/src/auth/services.ts.html +0 -2074
- package/coverage/lcov-report/src/collections/index.html +0 -116
- package/coverage/lcov-report/src/collections/loader.ts.html +0 -232
- package/coverage/lcov-report/src/db/auth-schema.ts.html +0 -523
- package/coverage/lcov-report/src/db/data-transformer.ts.html +0 -1753
- package/coverage/lcov-report/src/db/entityService.ts.html +0 -700
- package/coverage/lcov-report/src/db/index.html +0 -146
- package/coverage/lcov-report/src/db/services/EntityFetchService.ts.html +0 -4048
- package/coverage/lcov-report/src/db/services/EntityPersistService.ts.html +0 -883
- package/coverage/lcov-report/src/db/services/RelationService.ts.html +0 -3121
- package/coverage/lcov-report/src/db/services/entity-helpers.ts.html +0 -442
- package/coverage/lcov-report/src/db/services/index.html +0 -176
- package/coverage/lcov-report/src/db/services/index.ts.html +0 -124
- package/coverage/lcov-report/src/generate-drizzle-schema-logic.ts.html +0 -1960
- package/coverage/lcov-report/src/index.html +0 -116
- package/coverage/lcov-report/src/services/driver-registry.ts.html +0 -631
- package/coverage/lcov-report/src/services/index.html +0 -131
- package/coverage/lcov-report/src/services/postgresDataDriver.ts.html +0 -3025
- package/coverage/lcov-report/src/storage/LocalStorageController.ts.html +0 -1189
- package/coverage/lcov-report/src/storage/S3StorageController.ts.html +0 -970
- package/coverage/lcov-report/src/storage/index.html +0 -161
- package/coverage/lcov-report/src/storage/storage-registry.ts.html +0 -646
- package/coverage/lcov-report/src/storage/types.ts.html +0 -451
- package/coverage/lcov-report/src/utils/drizzle-conditions.ts.html +0 -3082
- package/coverage/lcov-report/src/utils/index.html +0 -116
- package/coverage/lcov.info +0 -7179
- package/jest.config.cjs +0 -19
- package/src/api/ast-schema-editor.ts +0 -289
- package/src/api/collections_for_test/callbacks_test_collection.ts +0 -61
- package/src/api/errors.ts +0 -278
- package/src/api/graphql/graphql-schema-generator.ts +0 -383
- package/src/api/graphql/index.ts +0 -2
- package/src/api/index.ts +0 -11
- package/src/api/logs-routes.ts +0 -132
- package/src/api/openapi-generator.ts +0 -727
- package/src/api/rest/api-generator-count.test.ts +0 -126
- package/src/api/rest/api-generator.ts +0 -747
- package/src/api/rest/query-parser.ts +0 -300
- package/src/api/schema-editor-routes.ts +0 -41
- package/src/api/server.ts +0 -271
- package/src/api/types.ts +0 -104
- package/src/auth/adapter-middleware.ts +0 -114
- package/src/auth/admin-roles-route.ts +0 -36
- package/src/auth/admin-user-ops.ts +0 -236
- package/src/auth/admin-users-route.ts +0 -302
- package/src/auth/api-keys/api-key-middleware.ts +0 -133
- package/src/auth/api-keys/api-key-routes.ts +0 -193
- package/src/auth/api-keys/api-key-store.ts +0 -340
- package/src/auth/apple-oauth.ts +0 -122
- package/src/auth/bitbucket-oauth.ts +0 -84
- package/src/auth/builtin-auth-adapter.ts +0 -380
- package/src/auth/custom-auth-adapter.ts +0 -86
- package/src/auth/discord-oauth.ts +0 -85
- package/src/auth/facebook-oauth.ts +0 -74
- package/src/auth/github-oauth.ts +0 -112
- package/src/auth/gitlab-oauth.ts +0 -72
- package/src/auth/google-oauth.ts +0 -223
- package/src/auth/jwt.ts +0 -208
- package/src/auth/linkedin-oauth.ts +0 -83
- package/src/auth/magic-link-routes.ts +0 -167
- package/src/auth/mfa-crypto.ts +0 -91
- package/src/auth/mfa-routes.ts +0 -323
- package/src/auth/mfa.ts +0 -161
- package/src/auth/microsoft-oauth.ts +0 -90
- package/src/auth/middleware.ts +0 -369
- package/src/auth/password.ts +0 -77
- package/src/auth/rate-limiter.ts +0 -225
- package/src/auth/reset-password-admin.ts +0 -160
- package/src/auth/rls-scope.ts +0 -58
- package/src/auth/routes.ts +0 -802
- package/src/auth/session-routes.ts +0 -353
- package/src/auth/slack-oauth.ts +0 -73
- package/src/auth/spotify-oauth.ts +0 -69
- package/src/auth/twitter-oauth.ts +0 -128
- package/src/collections/BackendCollectionRegistry.ts +0 -20
- package/src/collections/loader.ts +0 -50
- package/src/cron/cron-loader.ts +0 -84
- package/src/cron/cron-routes.test.ts +0 -265
- package/src/cron/cron-routes.ts +0 -85
- package/src/cron/cron-scheduler.test.ts +0 -559
- package/src/cron/cron-scheduler.ts +0 -581
- package/src/cron/cron-store.ts +0 -166
- package/src/db/interfaces.ts +0 -60
- package/src/email/index.ts +0 -19
- package/src/email/smtp-email-service.ts +0 -123
- package/src/email/templates.ts +0 -470
- package/src/env.ts +0 -227
- package/src/functions/function-loader.ts +0 -116
- package/src/functions/function-routes.ts +0 -31
- package/src/history/history-routes.ts +0 -129
- package/src/index.ts +0 -70
- package/src/init/docs.ts +0 -47
- package/src/init/health.ts +0 -37
- package/src/init/middlewares.ts +0 -61
- package/src/init/shutdown.ts +0 -56
- package/src/init/storage.ts +0 -57
- package/src/init.ts +0 -1041
- package/src/serve-spa.ts +0 -89
- package/src/services/driver-registry.ts +0 -183
- package/src/services/routed-realtime-service.ts +0 -113
- package/src/services/webhook-service.ts +0 -155
- package/src/singleton.ts +0 -70
- package/src/storage/LocalStorageController.ts +0 -389
- package/src/storage/S3StorageController.ts +0 -300
- package/src/storage/image-transform.ts +0 -218
- package/src/storage/routes.ts +0 -368
- package/src/storage/storage-registry.ts +0 -188
- package/src/storage/tus-handler.ts +0 -315
- package/src/types/index.ts +0 -27
- package/src/utils/dev-port.ts +0 -196
- package/src/utils/logger.ts +0 -143
- package/src/utils/logging.ts +0 -40
- package/src/utils/request-id.ts +0 -40
- package/src/utils/request-logger.ts +0 -72
- package/test/api-generator.test.ts +0 -588
- package/test/api-key-permission-guard.test.ts +0 -144
- package/test/ast-schema-editor.test.ts +0 -89
- package/test/auth-middleware-hono.test.ts +0 -556
- package/test/auth-routes.test.ts +0 -1098
- package/test/backend-hooks-data.test.ts +0 -477
- package/test/custom-auth-adapter.test.ts +0 -189
- package/test/driver-registry.test.ts +0 -282
- package/test/email-templates.test.ts +0 -174
- package/test/env.test.ts +0 -128
- package/test/error-propagation.test.ts +0 -226
- package/test/errors-hono.test.ts +0 -133
- package/test/errors.test.ts +0 -155
- package/test/function-loader.test.ts +0 -127
- package/test/graphql-schema-generator.test.ts +0 -554
- package/test/jwt-security.test.ts +0 -182
- package/test/jwt.test.ts +0 -326
- package/test/mfa.test.ts +0 -197
- package/test/middleware.test.ts +0 -305
- package/test/multi-datasource-routing.test.ts +0 -113
- package/test/password.test.ts +0 -165
- package/test/query-parser.test.ts +0 -233
- package/test/rate-limiter.test.ts +0 -102
- package/test/reset-password-admin.test.ts +0 -113
- package/test/routed-realtime-service.test.ts +0 -86
- package/test/safe-compare.test.ts +0 -66
- package/test/singleton.test.ts +0 -59
- package/test/smtp-email-service.test.ts +0 -169
- package/test/storage-local.test.ts +0 -271
- package/test/storage-registry.test.ts +0 -282
- package/test/storage-routes.test.ts +0 -222
- package/test/storage-s3.test.ts +0 -304
- package/test/transform-auth-response.test.ts +0 -305
- package/test/webhook-service.test.ts +0 -260
- package/tsconfig.json +0 -54
- package/tsconfig.prod.json +0 -23
- package/vite.config.ts +0 -86
package/test/mfa.test.ts
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
base32Encode,
|
|
3
|
-
base32Decode,
|
|
4
|
-
generateTotp,
|
|
5
|
-
verifyTotp,
|
|
6
|
-
generateTotpSecret,
|
|
7
|
-
generateRecoveryCodes,
|
|
8
|
-
hashRecoveryCode
|
|
9
|
-
} from "../src/auth/mfa";
|
|
10
|
-
|
|
11
|
-
describe("base32Encode / base32Decode", () => {
|
|
12
|
-
it("round-trips a known buffer", () => {
|
|
13
|
-
const original = Buffer.from("Hello, World!");
|
|
14
|
-
const encoded = base32Encode(original);
|
|
15
|
-
const decoded = base32Decode(encoded);
|
|
16
|
-
expect(decoded.toString()).toBe("Hello, World!");
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("encodes known test vectors (RFC 4648)", () => {
|
|
20
|
-
// RFC 4648 test vectors
|
|
21
|
-
expect(base32Encode(Buffer.from(""))).toBe("");
|
|
22
|
-
expect(base32Encode(Buffer.from("f"))).toBe("MY");
|
|
23
|
-
expect(base32Encode(Buffer.from("fo"))).toBe("MZXQ");
|
|
24
|
-
expect(base32Encode(Buffer.from("foo"))).toBe("MZXW6");
|
|
25
|
-
expect(base32Encode(Buffer.from("foob"))).toBe("MZXW6YQ");
|
|
26
|
-
expect(base32Encode(Buffer.from("fooba"))).toBe("MZXW6YTB");
|
|
27
|
-
expect(base32Encode(Buffer.from("foobar"))).toBe("MZXW6YTBOI");
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it("decodes base32 with trailing padding characters", () => {
|
|
31
|
-
const decoded = base32Decode("MZXW6===");
|
|
32
|
-
expect(decoded.toString()).toBe("foo");
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it("is case-insensitive when decoding", () => {
|
|
36
|
-
const upper = base32Decode("MZXW6YTBOI");
|
|
37
|
-
const lower = base32Decode("mzxw6ytboi");
|
|
38
|
-
expect(upper.equals(lower)).toBe(true);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("handles single byte", () => {
|
|
42
|
-
const buf = Buffer.from([0xff]);
|
|
43
|
-
const encoded = base32Encode(buf);
|
|
44
|
-
const decoded = base32Decode(encoded);
|
|
45
|
-
expect(decoded[0]).toBe(0xff);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("round-trips random 20-byte secret", () => {
|
|
49
|
-
const original = Buffer.from("abcdefghij1234567890");
|
|
50
|
-
const encoded = base32Encode(original);
|
|
51
|
-
const decoded = base32Decode(encoded);
|
|
52
|
-
expect(decoded.toString()).toBe(original.toString());
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
describe("generateTotp", () => {
|
|
57
|
-
it("returns a 6-digit string", () => {
|
|
58
|
-
const secret = Buffer.from("12345678901234567890");
|
|
59
|
-
const code = generateTotp(secret);
|
|
60
|
-
expect(code).toMatch(/^\d{6}$/);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("returns consistent results for the same time step", () => {
|
|
64
|
-
const secret = Buffer.from("test-secret-1234567");
|
|
65
|
-
const code1 = generateTotp(secret);
|
|
66
|
-
const code2 = generateTotp(secret);
|
|
67
|
-
expect(code1).toBe(code2);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it("pads short codes with leading zeros", () => {
|
|
71
|
-
// Generate many codes to increase chance of seeing leading zeros behavior
|
|
72
|
-
const secret = Buffer.from("12345678901234567890");
|
|
73
|
-
const code = generateTotp(secret);
|
|
74
|
-
expect(code.length).toBe(6);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
describe("verifyTotp", () => {
|
|
79
|
-
it("verifies a freshly generated token", () => {
|
|
80
|
-
const secret = Buffer.from("12345678901234567890");
|
|
81
|
-
const token = generateTotp(secret);
|
|
82
|
-
expect(verifyTotp(secret, token)).toBe(true);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("rejects an incorrect token", () => {
|
|
86
|
-
const secret = Buffer.from("12345678901234567890");
|
|
87
|
-
expect(verifyTotp(secret, "000000")).toBe(false);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("rejects token from different secret", () => {
|
|
91
|
-
const secret1 = Buffer.from("secret-one-12345678");
|
|
92
|
-
const secret2 = Buffer.from("secret-two-12345678");
|
|
93
|
-
const token = generateTotp(secret1);
|
|
94
|
-
expect(verifyTotp(secret2, token)).toBe(false);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it("uses window parameter to check adjacent time steps", () => {
|
|
98
|
-
const secret = Buffer.from("12345678901234567890");
|
|
99
|
-
const token = generateTotp(secret);
|
|
100
|
-
// Window of 0 should still pass for current step
|
|
101
|
-
expect(verifyTotp(secret, token, 0)).toBe(true);
|
|
102
|
-
// Window of 2 should also pass for current step
|
|
103
|
-
expect(verifyTotp(secret, token, 2)).toBe(true);
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
describe("generateTotpSecret", () => {
|
|
108
|
-
it("returns a base32 secret and otpauth URI", () => {
|
|
109
|
-
const result = generateTotpSecret("MyApp", "user@example.com");
|
|
110
|
-
expect(result.secret).toBeTruthy();
|
|
111
|
-
expect(result.uri).toBeTruthy();
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it("generates a valid otpauth URI format", () => {
|
|
115
|
-
const result = generateTotpSecret("MyApp", "user@example.com");
|
|
116
|
-
expect(result.uri).toMatch(/^otpauth:\/\/totp\//);
|
|
117
|
-
expect(result.uri).toContain("secret=");
|
|
118
|
-
expect(result.uri).toContain("issuer=MyApp");
|
|
119
|
-
expect(result.uri).toContain("algorithm=SHA1");
|
|
120
|
-
expect(result.uri).toContain("digits=6");
|
|
121
|
-
expect(result.uri).toContain("period=30");
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it("encodes special characters in issuer and account", () => {
|
|
125
|
-
const result = generateTotpSecret("My App & Co.", "user+test@example.com");
|
|
126
|
-
expect(result.uri).toContain(encodeURIComponent("My App & Co."));
|
|
127
|
-
expect(result.uri).toContain(encodeURIComponent("user+test@example.com"));
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it("generates unique secrets on each call", () => {
|
|
131
|
-
const result1 = generateTotpSecret("App", "user@test.com");
|
|
132
|
-
const result2 = generateTotpSecret("App", "user@test.com");
|
|
133
|
-
expect(result1.secret).not.toBe(result2.secret);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it("secret can be decoded back to a 20-byte buffer", () => {
|
|
137
|
-
const result = generateTotpSecret("App", "user@test.com");
|
|
138
|
-
const decoded = base32Decode(result.secret);
|
|
139
|
-
expect(decoded.length).toBe(20);
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
describe("generateRecoveryCodes", () => {
|
|
144
|
-
it("generates 10 codes by default", () => {
|
|
145
|
-
const codes = generateRecoveryCodes();
|
|
146
|
-
expect(codes).toHaveLength(10);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("generates the specified number of codes", () => {
|
|
150
|
-
const codes = generateRecoveryCodes(5);
|
|
151
|
-
expect(codes).toHaveLength(5);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it("generates codes in XXXXX-XXXXX format", () => {
|
|
155
|
-
const codes = generateRecoveryCodes();
|
|
156
|
-
for (const code of codes) {
|
|
157
|
-
expect(code).toMatch(/^[A-F0-9]{5}-[A-F0-9]{5}$/);
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it("generates unique codes", () => {
|
|
162
|
-
const codes = generateRecoveryCodes(20);
|
|
163
|
-
const unique = new Set(codes);
|
|
164
|
-
expect(unique.size).toBe(codes.length);
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
describe("hashRecoveryCode", () => {
|
|
169
|
-
it("returns a hex string", () => {
|
|
170
|
-
const hash = hashRecoveryCode("ABC12-DEF34");
|
|
171
|
-
expect(hash).toMatch(/^[a-f0-9]{64}$/);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it("is deterministic", () => {
|
|
175
|
-
const hash1 = hashRecoveryCode("ABC12-DEF34");
|
|
176
|
-
const hash2 = hashRecoveryCode("ABC12-DEF34");
|
|
177
|
-
expect(hash1).toBe(hash2);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it("strips dashes before hashing", () => {
|
|
181
|
-
const withDashes = hashRecoveryCode("ABC12-DEF34");
|
|
182
|
-
const withoutDashes = hashRecoveryCode("ABC12DEF34");
|
|
183
|
-
expect(withDashes).toBe(withoutDashes);
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
it("normalizes to uppercase before hashing", () => {
|
|
187
|
-
const upper = hashRecoveryCode("ABC12-DEF34");
|
|
188
|
-
const lower = hashRecoveryCode("abc12-def34");
|
|
189
|
-
expect(upper).toBe(lower);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
it("different codes produce different hashes", () => {
|
|
193
|
-
const hash1 = hashRecoveryCode("ABC12-DEF34");
|
|
194
|
-
const hash2 = hashRecoveryCode("XYZ98-UVW76");
|
|
195
|
-
expect(hash1).not.toBe(hash2);
|
|
196
|
-
});
|
|
197
|
-
});
|
package/test/middleware.test.ts
DELETED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
import { requireAuth, optionalAuth, extractUserFromToken, requireAdmin } from "../src/auth/middleware";
|
|
2
|
-
import { configureJwt, generateAccessToken } from "../src/auth/jwt";
|
|
3
|
-
|
|
4
|
-
// ── Minimal Hono-context mock ────────────────────────────────────────────
|
|
5
|
-
function createMockContext(opts: {
|
|
6
|
-
authHeader?: string;
|
|
7
|
-
queryToken?: string;
|
|
8
|
-
user?: any;
|
|
9
|
-
} = {}) {
|
|
10
|
-
let capturedStatus: number | undefined;
|
|
11
|
-
let capturedBody: any;
|
|
12
|
-
const variables = new Map<string, any>();
|
|
13
|
-
|
|
14
|
-
if (opts.user !== undefined) variables.set("user", opts.user);
|
|
15
|
-
|
|
16
|
-
const c = {
|
|
17
|
-
req: {
|
|
18
|
-
header: (name: string) => {
|
|
19
|
-
if (name === "authorization") return opts.authHeader;
|
|
20
|
-
return undefined;
|
|
21
|
-
},
|
|
22
|
-
query: (name: string) => {
|
|
23
|
-
if (name === "token") return opts.queryToken;
|
|
24
|
-
return undefined;
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
json: (body: any, status?: number) => {
|
|
28
|
-
capturedBody = body;
|
|
29
|
-
capturedStatus = status ?? 200;
|
|
30
|
-
return new Response(JSON.stringify(body), { status: capturedStatus });
|
|
31
|
-
},
|
|
32
|
-
set: (key: string, value: any) => variables.set(key, value),
|
|
33
|
-
get: (key: string) => variables.get(key)
|
|
34
|
-
} as any;
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
c,
|
|
38
|
-
getStatus: () => capturedStatus,
|
|
39
|
-
getBody: () => capturedBody,
|
|
40
|
-
getUser: () => variables.get("user")
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
describe("Auth Middleware", () => {
|
|
45
|
-
const testSecret = "test-secret-key-for-middleware-testing";
|
|
46
|
-
const nextFn = jest.fn();
|
|
47
|
-
|
|
48
|
-
beforeAll(() => {
|
|
49
|
-
configureJwt({
|
|
50
|
-
secret: testSecret,
|
|
51
|
-
accessExpiresIn: "1h",
|
|
52
|
-
refreshExpiresIn: "30d"
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
beforeEach(() => {
|
|
57
|
-
nextFn.mockClear();
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
describe("requireAuth", () => {
|
|
61
|
-
it("should call next() and set user for valid token", async () => {
|
|
62
|
-
const token = generateAccessToken("user-123", ["admin", "editor"]);
|
|
63
|
-
const { c, getUser } = createMockContext({ authHeader: `Bearer ${token}` });
|
|
64
|
-
|
|
65
|
-
await requireAuth(c, nextFn);
|
|
66
|
-
|
|
67
|
-
expect(nextFn).toHaveBeenCalled();
|
|
68
|
-
expect(getUser()).toEqual({
|
|
69
|
-
userId: "user-123",
|
|
70
|
-
roles: ["admin", "editor"],
|
|
71
|
-
aal: "aal1"
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it("should return 401 for missing Authorization header", async () => {
|
|
76
|
-
const { c, getStatus, getBody } = createMockContext();
|
|
77
|
-
|
|
78
|
-
await requireAuth(c, nextFn);
|
|
79
|
-
|
|
80
|
-
expect(getStatus()).toBe(401);
|
|
81
|
-
expect(getBody()).toEqual({
|
|
82
|
-
error: {
|
|
83
|
-
message: "Authorization header or token query parameter missing or invalid",
|
|
84
|
-
code: "UNAUTHORIZED"
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
expect(nextFn).not.toHaveBeenCalled();
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("should return 401 for Authorization header without Bearer prefix", async () => {
|
|
91
|
-
const { c, getStatus } = createMockContext({ authHeader: "token-without-bearer" });
|
|
92
|
-
|
|
93
|
-
await requireAuth(c, nextFn);
|
|
94
|
-
|
|
95
|
-
expect(getStatus()).toBe(401);
|
|
96
|
-
expect(nextFn).not.toHaveBeenCalled();
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it("should return 401 for invalid token", async () => {
|
|
100
|
-
const { c, getStatus, getBody } = createMockContext({ authHeader: "Bearer invalid-token" });
|
|
101
|
-
|
|
102
|
-
await requireAuth(c, nextFn);
|
|
103
|
-
|
|
104
|
-
expect(getStatus()).toBe(401);
|
|
105
|
-
expect(getBody()).toEqual({
|
|
106
|
-
error: {
|
|
107
|
-
message: "Invalid or expired token",
|
|
108
|
-
code: "UNAUTHORIZED"
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
expect(nextFn).not.toHaveBeenCalled();
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it("should return 401 for token signed with different secret", async () => {
|
|
115
|
-
const token = generateAccessToken("user-123", ["admin"]);
|
|
116
|
-
configureJwt({ secret: "different-secret-that-is-at-least-32-chars-long" });
|
|
117
|
-
const { c, getStatus } = createMockContext({ authHeader: `Bearer ${token}` });
|
|
118
|
-
|
|
119
|
-
await requireAuth(c, nextFn);
|
|
120
|
-
|
|
121
|
-
expect(getStatus()).toBe(401);
|
|
122
|
-
expect(nextFn).not.toHaveBeenCalled();
|
|
123
|
-
|
|
124
|
-
// Reset to original secret
|
|
125
|
-
configureJwt({ secret: testSecret });
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("should handle lowercase bearer prefix", async () => {
|
|
129
|
-
const token = generateAccessToken("user-123", ["admin"]);
|
|
130
|
-
const { c, getStatus } = createMockContext({ authHeader: `bearer ${token}` });
|
|
131
|
-
|
|
132
|
-
await requireAuth(c, nextFn);
|
|
133
|
-
|
|
134
|
-
// The implementation requires "Bearer " with capital B
|
|
135
|
-
expect(getStatus()).toBe(401);
|
|
136
|
-
expect(nextFn).not.toHaveBeenCalled();
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it("should accept token from query parameter", async () => {
|
|
140
|
-
const token = generateAccessToken("user-123", ["viewer"]);
|
|
141
|
-
const { c, getUser } = createMockContext({ queryToken: token });
|
|
142
|
-
|
|
143
|
-
await requireAuth(c, nextFn);
|
|
144
|
-
|
|
145
|
-
expect(nextFn).toHaveBeenCalled();
|
|
146
|
-
expect(getUser()).toEqual({
|
|
147
|
-
userId: "user-123",
|
|
148
|
-
roles: ["viewer"],
|
|
149
|
-
aal: "aal1"
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
describe("optionalAuth", () => {
|
|
155
|
-
it("should set user for valid token", async () => {
|
|
156
|
-
const token = generateAccessToken("user-456", ["viewer"]);
|
|
157
|
-
const { c, getUser } = createMockContext({ authHeader: `Bearer ${token}` });
|
|
158
|
-
|
|
159
|
-
await optionalAuth(c, nextFn);
|
|
160
|
-
|
|
161
|
-
expect(nextFn).toHaveBeenCalled();
|
|
162
|
-
expect(getUser()).toEqual({
|
|
163
|
-
userId: "user-456",
|
|
164
|
-
roles: ["viewer"],
|
|
165
|
-
aal: "aal1"
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it("should call next() without setting user when no token provided", async () => {
|
|
170
|
-
const { c, getUser } = createMockContext();
|
|
171
|
-
|
|
172
|
-
await optionalAuth(c, nextFn);
|
|
173
|
-
|
|
174
|
-
expect(nextFn).toHaveBeenCalled();
|
|
175
|
-
expect(getUser()).toBeUndefined();
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it("should call next() without setting user for invalid token", async () => {
|
|
179
|
-
const { c, getUser } = createMockContext({ authHeader: "Bearer invalid-token" });
|
|
180
|
-
|
|
181
|
-
await optionalAuth(c, nextFn);
|
|
182
|
-
|
|
183
|
-
expect(nextFn).toHaveBeenCalled();
|
|
184
|
-
expect(getUser()).toBeUndefined();
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it("should call next() without setting user for non-Bearer auth", async () => {
|
|
188
|
-
const { c, getUser } = createMockContext({ authHeader: "Basic dXNlcjpwYXNz" });
|
|
189
|
-
|
|
190
|
-
await optionalAuth(c, nextFn);
|
|
191
|
-
|
|
192
|
-
expect(nextFn).toHaveBeenCalled();
|
|
193
|
-
expect(getUser()).toBeUndefined();
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
describe("requireAdmin", () => {
|
|
198
|
-
it("should return 401 if user is not authenticated", async () => {
|
|
199
|
-
const { c, getStatus, getBody } = createMockContext();
|
|
200
|
-
|
|
201
|
-
await requireAdmin(c, nextFn);
|
|
202
|
-
|
|
203
|
-
expect(getStatus()).toBe(401);
|
|
204
|
-
expect(getBody()).toEqual({
|
|
205
|
-
error: {
|
|
206
|
-
message: "User not authenticated. requireAuth middleware is missing?",
|
|
207
|
-
code: "UNAUTHORIZED"
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
expect(nextFn).not.toHaveBeenCalled();
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it("should return 403 if user has no roles array", async () => {
|
|
214
|
-
const { c, getStatus } = createMockContext({ user: { userId: "user-123" } });
|
|
215
|
-
|
|
216
|
-
await requireAdmin(c, nextFn);
|
|
217
|
-
|
|
218
|
-
expect(getStatus()).toBe(403);
|
|
219
|
-
expect(nextFn).not.toHaveBeenCalled();
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
it("should return 403 if user has empty roles array", async () => {
|
|
223
|
-
const { c, getStatus } = createMockContext({ user: { userId: "user-123",
|
|
224
|
-
roles: [] } });
|
|
225
|
-
|
|
226
|
-
await requireAdmin(c, nextFn);
|
|
227
|
-
|
|
228
|
-
expect(getStatus()).toBe(403);
|
|
229
|
-
expect(nextFn).not.toHaveBeenCalled();
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
it("should return 403 if user has standard roles (forbidden)", async () => {
|
|
233
|
-
const { c, getStatus } = createMockContext({ user: { userId: "user-123",
|
|
234
|
-
roles: ["editor", "viewer"] } });
|
|
235
|
-
|
|
236
|
-
await requireAdmin(c, nextFn);
|
|
237
|
-
|
|
238
|
-
expect(getStatus()).toBe(403);
|
|
239
|
-
expect(nextFn).not.toHaveBeenCalled();
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
it("should allow access if user has 'admin' role", async () => {
|
|
243
|
-
const { c, getStatus } = createMockContext({ user: { userId: "user-123",
|
|
244
|
-
roles: ["editor", "admin"] } });
|
|
245
|
-
|
|
246
|
-
await requireAdmin(c, nextFn);
|
|
247
|
-
|
|
248
|
-
expect(nextFn).toHaveBeenCalled();
|
|
249
|
-
expect(getStatus()).toBeUndefined(); // No error response
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
it("should allow access if user has 'schema-admin' role", async () => {
|
|
253
|
-
const { c } = createMockContext({ user: { userId: "user-123",
|
|
254
|
-
roles: ["schema-admin"] } });
|
|
255
|
-
|
|
256
|
-
await requireAdmin(c, nextFn);
|
|
257
|
-
|
|
258
|
-
expect(nextFn).toHaveBeenCalled();
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it("should block access for malformed spoofed string roles", async () => {
|
|
262
|
-
const { c, getStatus } = createMockContext({ user: { userId: "user-123",
|
|
263
|
-
roles: ["schema-adminstration", "admins", "admin "] } });
|
|
264
|
-
|
|
265
|
-
await requireAdmin(c, nextFn);
|
|
266
|
-
|
|
267
|
-
expect(getStatus()).toBe(403);
|
|
268
|
-
expect(nextFn).not.toHaveBeenCalled();
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
describe("extractUserFromToken", () => {
|
|
273
|
-
it("should extract user from valid token", () => {
|
|
274
|
-
const token = generateAccessToken("ws-user-123", ["admin"]);
|
|
275
|
-
const payload = extractUserFromToken(token);
|
|
276
|
-
|
|
277
|
-
expect(payload).toEqual({
|
|
278
|
-
userId: "ws-user-123",
|
|
279
|
-
roles: ["admin"],
|
|
280
|
-
aal: "aal1"
|
|
281
|
-
});
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
it("should return null for invalid token", () => {
|
|
285
|
-
const payload = extractUserFromToken("invalid-token");
|
|
286
|
-
expect(payload).toBeNull();
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
it("should return null for empty token", () => {
|
|
290
|
-
const payload = extractUserFromToken("");
|
|
291
|
-
expect(payload).toBeNull();
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
it("should work with tokens having empty roles", () => {
|
|
295
|
-
const token = generateAccessToken("user-no-roles", []);
|
|
296
|
-
const payload = extractUserFromToken(token);
|
|
297
|
-
|
|
298
|
-
expect(payload).toEqual({
|
|
299
|
-
userId: "user-no-roles",
|
|
300
|
-
roles: [],
|
|
301
|
-
aal: "aal1"
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
});
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeAll } from "@jest/globals";
|
|
2
|
-
import { Hono, Context } from "hono";
|
|
3
|
-
import { configureJwt, generateAccessToken } from "../src/auth/jwt";
|
|
4
|
-
import { createAuthMiddleware } from "../src/auth/middleware";
|
|
5
|
-
import { createAdapterAuthMiddleware } from "../src/auth/adapter-middleware";
|
|
6
|
-
import type { HonoEnv } from "../src/api/types";
|
|
7
|
-
import type { DataDriver, AuthAdapter } from "@rebasepro/types";
|
|
8
|
-
|
|
9
|
-
const TEST_SECRET = "test-secret-key-for-multi-datasource-routing-1234567890";
|
|
10
|
-
|
|
11
|
-
// A mock driver that tags itself so we can assert which one was scoped into
|
|
12
|
-
// the request context. The Postgres-like one implements withAuth (RLS), the
|
|
13
|
-
// Mongo-like one does not (scopeDataDriver must no-op for it).
|
|
14
|
-
function mockDriver(key: string, withRls: boolean): DataDriver {
|
|
15
|
-
const base: Record<string, unknown> = {
|
|
16
|
-
key,
|
|
17
|
-
fetchCollection: async () => [],
|
|
18
|
-
fetchEntity: async () => undefined,
|
|
19
|
-
saveEntity: async () => ({ id: "x", path: key, values: {} }),
|
|
20
|
-
deleteEntity: async () => undefined
|
|
21
|
-
};
|
|
22
|
-
if (withRls) {
|
|
23
|
-
// Returns a scoped clone that keeps the same key tag.
|
|
24
|
-
base.withAuth = async () => ({ ...base, scoped: true });
|
|
25
|
-
}
|
|
26
|
-
return base as unknown as DataDriver;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
describe("Multi-data-source request routing (middleware)", () => {
|
|
30
|
-
|
|
31
|
-
beforeAll(() => {
|
|
32
|
-
configureJwt({ secret: TEST_SECRET, accessExpiresIn: "1h" });
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
const pg = mockDriver("postgres", true);
|
|
36
|
-
const mongo = mockDriver("mongodb", false);
|
|
37
|
-
|
|
38
|
-
// Route by first path segment: /products → pg, /events → mongo.
|
|
39
|
-
const resolveDriver = (c: Context<HonoEnv>): DataDriver => {
|
|
40
|
-
const slug = c.req.path.replace(/^\/+/, "").split("/")[0];
|
|
41
|
-
return slug === "events" ? mongo : pg;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
function createApp() {
|
|
45
|
-
const app = new Hono<HonoEnv>();
|
|
46
|
-
app.use("/*", createAuthMiddleware({ driver: pg, resolveDriver, requireAuth: false }));
|
|
47
|
-
app.get("/:slug", (c: Context<HonoEnv>) => {
|
|
48
|
-
const driver = c.get("driver") as DataDriver | undefined;
|
|
49
|
-
return c.json({ key: driver?.key });
|
|
50
|
-
});
|
|
51
|
-
return app;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
it("scopes the Postgres delegate for a Postgres collection path", async () => {
|
|
55
|
-
const token = generateAccessToken("u1", ["admin"]);
|
|
56
|
-
const res = await createApp().request("/products", { headers: { Authorization: `Bearer ${token}` } });
|
|
57
|
-
const body = await res.json() as { key: string };
|
|
58
|
-
expect(body.key).toBe("postgres");
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("scopes the Mongo delegate for a Mongo collection path", async () => {
|
|
62
|
-
const token = generateAccessToken("u1", ["admin"]);
|
|
63
|
-
const res = await createApp().request("/events", { headers: { Authorization: `Bearer ${token}` } });
|
|
64
|
-
const body = await res.json() as { key: string };
|
|
65
|
-
expect(body.key).toBe("mongodb");
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it("routes per request based on the resolver, not a fixed driver", async () => {
|
|
69
|
-
const token = generateAccessToken("u1", ["admin"]);
|
|
70
|
-
const app = createApp();
|
|
71
|
-
const a = await (await app.request("/events", { headers: { Authorization: `Bearer ${token}` } })).json() as { key: string };
|
|
72
|
-
const b = await (await app.request("/products", { headers: { Authorization: `Bearer ${token}` } })).json() as { key: string };
|
|
73
|
-
expect(a.key).toBe("mongodb");
|
|
74
|
-
expect(b.key).toBe("postgres");
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("falls back to the base driver when no resolver is provided", async () => {
|
|
78
|
-
const app = new Hono<HonoEnv>();
|
|
79
|
-
app.use("/*", createAuthMiddleware({ driver: mongo, requireAuth: false }));
|
|
80
|
-
app.get("/:slug", (c: Context<HonoEnv>) => c.json({ key: (c.get("driver") as DataDriver | undefined)?.key }));
|
|
81
|
-
const res = await app.request("/anything");
|
|
82
|
-
const body = await res.json() as { key: string };
|
|
83
|
-
expect(body.key).toBe("mongodb");
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it("routes a nested/subcollection path by its top-level segment", async () => {
|
|
87
|
-
const token = generateAccessToken("u1", ["admin"]);
|
|
88
|
-
const app = new Hono<HonoEnv>();
|
|
89
|
-
app.use("/*", createAuthMiddleware({ driver: pg, resolveDriver, requireAuth: false }));
|
|
90
|
-
app.get("/events/:id/attendees", (c: Context<HonoEnv>) =>
|
|
91
|
-
c.json({ key: (c.get("driver") as DataDriver | undefined)?.key }));
|
|
92
|
-
const res = await app.request("/events/e1/attendees", { headers: { Authorization: `Bearer ${token}` } });
|
|
93
|
-
const body = await res.json() as { key: string };
|
|
94
|
-
expect(body.key).toBe("mongodb");
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it("routes per request through the adapter auth middleware too", async () => {
|
|
98
|
-
const adapter: AuthAdapter = {
|
|
99
|
-
id: "test-adapter",
|
|
100
|
-
serviceKey: undefined,
|
|
101
|
-
verifyRequest: async () => ({ uid: "u1", roles: ["admin"] })
|
|
102
|
-
} as unknown as AuthAdapter;
|
|
103
|
-
|
|
104
|
-
const app = new Hono<HonoEnv>();
|
|
105
|
-
app.use("/*", createAdapterAuthMiddleware({ adapter, driver: pg, resolveDriver, requireAuth: false }));
|
|
106
|
-
app.get("/:slug", (c: Context<HonoEnv>) => c.json({ key: (c.get("driver") as DataDriver | undefined)?.key }));
|
|
107
|
-
|
|
108
|
-
const a = await (await app.request("/events")).json() as { key: string };
|
|
109
|
-
const b = await (await app.request("/products")).json() as { key: string };
|
|
110
|
-
expect(a.key).toBe("mongodb");
|
|
111
|
-
expect(b.key).toBe("postgres");
|
|
112
|
-
});
|
|
113
|
-
});
|