@authhero/multi-tenancy 14.2.0 → 14.3.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 (38) hide show
  1. package/dist/multi-tenancy.cjs +1 -1
  2. package/dist/multi-tenancy.mjs +24 -25
  3. package/dist/types/hooks/access-control.d.ts +25 -0
  4. package/dist/types/hooks/access-control.d.ts.map +1 -0
  5. package/dist/types/hooks/database.d.ts +35 -0
  6. package/dist/types/hooks/database.d.ts.map +1 -0
  7. package/dist/types/hooks/index.d.ts +5 -0
  8. package/dist/types/hooks/index.d.ts.map +1 -0
  9. package/dist/types/hooks/provisioning.d.ts +15 -0
  10. package/dist/types/hooks/provisioning.d.ts.map +1 -0
  11. package/dist/types/hooks/resource-server-sync.d.ts +140 -0
  12. package/dist/types/hooks/resource-server-sync.d.ts.map +1 -0
  13. package/dist/types/hooks/role-sync.d.ts +145 -0
  14. package/dist/types/hooks/role-sync.d.ts.map +1 -0
  15. package/dist/types/hooks/sync.d.ts +79 -0
  16. package/dist/types/hooks/sync.d.ts.map +1 -0
  17. package/dist/types/index.d.ts +117 -0
  18. package/dist/types/index.d.ts.map +1 -0
  19. package/dist/types/init.d.ts +110 -0
  20. package/dist/types/init.d.ts.map +1 -0
  21. package/dist/types/middleware/index.d.ts +114 -0
  22. package/dist/types/middleware/index.d.ts.map +1 -0
  23. package/dist/types/middleware/protect-synced.d.ts +40 -0
  24. package/dist/types/middleware/protect-synced.d.ts.map +1 -0
  25. package/dist/types/middleware/settings-inheritance.d.ts +89 -0
  26. package/dist/types/middleware/settings-inheritance.d.ts.map +1 -0
  27. package/dist/types/plugin.d.ts +66 -0
  28. package/dist/types/plugin.d.ts.map +1 -0
  29. package/dist/types/routes/index.d.ts +2 -0
  30. package/dist/types/routes/index.d.ts.map +1 -0
  31. package/dist/types/routes/tenants.d.ts +18 -0
  32. package/dist/types/routes/tenants.d.ts.map +1 -0
  33. package/dist/types/types.d.ts +295 -0
  34. package/dist/types/types.d.ts.map +1 -0
  35. package/dist/types/utils/index.d.ts +3 -0
  36. package/dist/types/utils/index.d.ts.map +1 -0
  37. package/package.json +9 -9
  38. package/dist/multi-tenancy.d.ts +0 -41331
@@ -0,0 +1,79 @@
1
+ import { DataAdapters, Role, RoleInsert, ResourceServer, ResourceServerInsert, EntityHooks } from "authhero";
2
+ import { TenantEntityHooks } from "../types";
3
+ /**
4
+ * Configuration for entity synchronization
5
+ */
6
+ export interface EntitySyncConfig {
7
+ /**
8
+ * The control plane tenant ID from which entities are synced
9
+ */
10
+ controlPlaneTenantId: string;
11
+ /**
12
+ * Function to get the list of all tenant IDs to sync to.
13
+ */
14
+ getChildTenantIds: () => Promise<string[]>;
15
+ /**
16
+ * Function to get adapters for a specific tenant.
17
+ */
18
+ getAdapters: (tenantId: string) => Promise<DataAdapters>;
19
+ /**
20
+ * Function to get adapters for the control plane (used for tenant creation sync).
21
+ */
22
+ getControlPlaneAdapters: () => Promise<DataAdapters>;
23
+ /**
24
+ * Which entities to sync. All default to true.
25
+ * Note: Connections are NOT synced - they use runtime fallback by strategy instead.
26
+ */
27
+ sync?: {
28
+ resourceServers?: boolean;
29
+ roles?: boolean;
30
+ };
31
+ /**
32
+ * Optional filters for each entity type
33
+ */
34
+ filters?: {
35
+ resourceServers?: (entity: ResourceServer) => boolean;
36
+ roles?: (entity: Role) => boolean;
37
+ };
38
+ }
39
+ /**
40
+ * Result from createSyncHooks containing all entity and tenant hooks
41
+ *
42
+ * Note: Connections are NOT synced - they use runtime fallback by strategy instead.
43
+ * Tenants can define a connection with a strategy (e.g., "google") and leave keys blank,
44
+ * and the system will fallback to the control plane's connection with the same strategy.
45
+ */
46
+ export interface SyncHooksResult {
47
+ entityHooks: {
48
+ resourceServers?: EntityHooks<ResourceServer, ResourceServerInsert>;
49
+ roles?: EntityHooks<Role, RoleInsert>;
50
+ };
51
+ tenantHooks: TenantEntityHooks;
52
+ }
53
+ /**
54
+ * Creates all sync hooks for resource servers and roles.
55
+ *
56
+ * This is the main entry point for entity synchronization. It creates hooks that:
57
+ * - Sync changes from control plane to all child tenants (create, update, delete)
58
+ * - Copy entities to newly created tenants
59
+ *
60
+ * Note: Connections are NOT synced. Instead, they use runtime fallback by strategy.
61
+ * Tenants can define a connection with a strategy (e.g., "google") and leave keys blank,
62
+ * and the system will fallback to the control plane's connection with the same strategy.
63
+ *
64
+ * @param config - Sync configuration
65
+ * @returns Object with entityHooks and tenantHooks ready to use
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * const { entityHooks, tenantHooks } = createSyncHooks({
70
+ * controlPlaneTenantId: "main",
71
+ * getChildTenantIds: async () => ["tenant1", "tenant2"],
72
+ * getAdapters: async () => dataAdapter,
73
+ * getControlPlaneAdapters: async () => dataAdapter,
74
+ * sync: { resourceServers: true, roles: true },
75
+ * });
76
+ * ```
77
+ */
78
+ export declare function createSyncHooks(config: EntitySyncConfig): SyncHooksResult;
79
+ //# sourceMappingURL=sync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../../../src/hooks/sync.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,IAAI,EACJ,UAAU,EACV,cAAc,EACd,oBAAoB,EAGpB,WAAW,EAEZ,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,iBAAiB,EAAqB,MAAM,UAAU,CAAC;AAEhE;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,oBAAoB,EAAE,MAAM,CAAC;IAE7B;;OAEG;IACH,iBAAiB,EAAE,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAE3C;;OAEG;IACH,WAAW,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,YAAY,CAAC,CAAC;IAEzD;;OAEG;IACH,uBAAuB,EAAE,MAAM,OAAO,CAAC,YAAY,CAAC,CAAC;IAErD;;;OAGG;IACH,IAAI,CAAC,EAAE;QACL,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB,CAAC;IAEF;;OAEG;IACH,OAAO,CAAC,EAAE;QACR,eAAe,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,OAAO,CAAC;QACtD,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,KAAK,OAAO,CAAC;KACnC,CAAC;CACH;AAoQD;;;;;;GAMG;AACH,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE;QACX,eAAe,CAAC,EAAE,WAAW,CAAC,cAAc,EAAE,oBAAoB,CAAC,CAAC;QACpE,KAAK,CAAC,EAAE,WAAW,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;KACvC,CAAC;IACF,WAAW,EAAE,iBAAiB,CAAC;CAChC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,gBAAgB,GAAG,eAAe,CA6KzE"}
@@ -0,0 +1,117 @@
1
+ import { Hono } from "hono";
2
+ import { MultiTenancyConfig, MultiTenancyHooks, MultiTenancyBindings, MultiTenancyVariables } from "./types";
3
+ export * from "./types";
4
+ export { initMultiTenant } from "./init";
5
+ export type { MultiTenantConfig, MultiTenantResult } from "./init";
6
+ export { createSyncHooks } from "./hooks/sync";
7
+ export type { EntitySyncConfig, SyncHooksResult } from "./hooks/sync";
8
+ export { createTenantsOpenAPIRouter } from "./routes";
9
+ export { createMultiTenancyMiddleware, createAccessControlMiddleware, createControlPlaneTenantMiddleware, createSubdomainMiddleware, createDatabaseMiddleware, createProtectSyncedMiddleware, createRuntimeFallbackAdapter, withRuntimeFallback, createSettingsInheritanceAdapter, withSettingsInheritance, } from "./middleware";
10
+ export type { RuntimeFallbackConfig, SettingsInheritanceConfig, } from "./middleware";
11
+ export { createMultiTenancyPlugin } from "./plugin";
12
+ export type { AuthHeroPlugin } from "./plugin";
13
+ export { createAccessControlHooks, createDatabaseHooks, createProvisioningHooks, } from "./hooks";
14
+ export { validateTenantAccess } from "./hooks/access-control";
15
+ export type { DatabaseFactory } from "./hooks/database";
16
+ /**
17
+ * Creates multi-tenancy hooks from configuration.
18
+ *
19
+ * This combines access control, database resolution, and provisioning hooks
20
+ * into a single hooks object that can be passed to AuthHero.
21
+ *
22
+ * @param config - Multi-tenancy configuration
23
+ * @returns Combined hooks for multi-tenancy support
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * import { createMultiTenancyHooks } from "@authhero/multi-tenancy";
28
+ *
29
+ * const hooks = createMultiTenancyHooks({
30
+ * accessControl: {
31
+ * controlPlaneTenantId: "control_plane",
32
+ * defaultPermissions: ["tenant:admin"],
33
+ * },
34
+ * databaseIsolation: {
35
+ * getAdapters: async (tenantId) => getDatabase(tenantId),
36
+ * },
37
+ * });
38
+ * ```
39
+ */
40
+ export declare function createMultiTenancyHooks(config: MultiTenancyConfig): MultiTenancyHooks;
41
+ /**
42
+ * Creates a complete multi-tenancy Hono app with routes and middleware.
43
+ *
44
+ * This creates a Hono app with:
45
+ * - Tenant management routes (CRUD for tenants)
46
+ * - Access control middleware
47
+ * - Subdomain routing (optional)
48
+ * - Database resolution (optional)
49
+ *
50
+ * @param config - Multi-tenancy configuration
51
+ * @returns Hono app with multi-tenancy routes
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * import { createMultiTenancy } from "@authhero/multi-tenancy";
56
+ *
57
+ * const multiTenancyApp = createMultiTenancy({
58
+ * accessControl: {
59
+ * controlPlaneTenantId: "main",
60
+ * },
61
+ * });
62
+ *
63
+ * // Mount on your main app
64
+ * app.route("/management", multiTenancyApp);
65
+ * ```
66
+ */
67
+ export declare function createMultiTenancy(config: MultiTenancyConfig): Hono<{
68
+ Bindings: MultiTenancyBindings;
69
+ Variables: MultiTenancyVariables;
70
+ }, import("hono/types").BlankSchema, "/">;
71
+ /**
72
+ * Creates a multi-tenancy setup with both hooks and middleware.
73
+ *
74
+ * This is a convenience function that returns everything needed to
75
+ * integrate multi-tenancy into an AuthHero application.
76
+ *
77
+ * @param config - Multi-tenancy configuration
78
+ * @returns Object with hooks, middleware, and routes
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import { setupMultiTenancy } from "@authhero/multi-tenancy";
83
+ *
84
+ * const multiTenancy = setupMultiTenancy({
85
+ * accessControl: {
86
+ * controlPlaneTenantId: "main",
87
+ * },
88
+ * subdomainRouting: {
89
+ * baseDomain: "auth.example.com",
90
+ * },
91
+ * });
92
+ *
93
+ * // Use the middleware
94
+ * app.use("*", multiTenancy.middleware);
95
+ *
96
+ * // Mount the routes
97
+ * app.route("/management", multiTenancy.app);
98
+ *
99
+ * // Pass hooks to AuthHero
100
+ * const authhero = createAuthhero({
101
+ * hooks: multiTenancy.hooks,
102
+ * });
103
+ * ```
104
+ */
105
+ export declare function setupMultiTenancy(config: MultiTenancyConfig): {
106
+ hooks: MultiTenancyHooks;
107
+ middleware: import("hono").MiddlewareHandler<{
108
+ Bindings: MultiTenancyBindings;
109
+ Variables: MultiTenancyVariables;
110
+ }>;
111
+ app: Hono<{
112
+ Bindings: MultiTenancyBindings;
113
+ Variables: MultiTenancyVariables;
114
+ }, import("hono/types").BlankSchema, "/">;
115
+ config: MultiTenancyConfig;
116
+ };
117
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACtB,MAAM,SAAS,CAAC;AAUjB,cAAc,SAAS,CAAC;AAGxB,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACzC,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAGnE,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,YAAY,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEtE,OAAO,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AAEtD,OAAO,EACL,4BAA4B,EAC5B,6BAA6B,EAC7B,kCAAkC,EAClC,yBAAyB,EACzB,wBAAwB,EACxB,6BAA6B,EAC7B,4BAA4B,EAC5B,mBAAmB,EAEnB,gCAAgC,EAChC,uBAAuB,GACxB,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,qBAAqB,EACrB,yBAAyB,GAC1B,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,wBAAwB,EAAE,MAAM,UAAU,CAAC;AACpD,YAAY,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAG/C,OAAO,EACL,wBAAwB,EACxB,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,YAAY,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,kBAAkB,GACzB,iBAAiB,CAgBnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,kBAAkB;cAE/C,oBAAoB;eACnB,qBAAqB;0CASnC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,kBAAkB;;;;;;;kBA9C9C,oBAAoB;mBACnB,qBAAqB;;;EAoDnC"}
@@ -0,0 +1,110 @@
1
+ import { init, AuthHeroConfig, DataAdapters } from "authhero";
2
+ /**
3
+ * Configuration for multi-tenant AuthHero initialization.
4
+ */
5
+ export interface MultiTenantConfig extends Omit<AuthHeroConfig, "entityHooks" | "managementApiExtensions"> {
6
+ /**
7
+ * The control plane tenant ID - the tenant that manages all other tenants.
8
+ * @default "control_plane"
9
+ */
10
+ controlPlaneTenantId?: string;
11
+ /**
12
+ * Control which entities to sync from control plane to child tenants.
13
+ * Set to `false` to disable all syncing.
14
+ * @default { resourceServers: true, roles: true }
15
+ */
16
+ sync?: {
17
+ resourceServers?: boolean;
18
+ roles?: boolean;
19
+ } | false;
20
+ /**
21
+ * Default permissions to grant when creating a tenant organization.
22
+ * @default ["tenant:admin"]
23
+ */
24
+ defaultPermissions?: string[];
25
+ /**
26
+ * Whether to require organization match for tenant access.
27
+ * @default false
28
+ */
29
+ requireOrganizationMatch?: boolean;
30
+ /**
31
+ * Additional management API extensions to mount.
32
+ * The tenants router is automatically added.
33
+ */
34
+ managementApiExtensions?: AuthHeroConfig["managementApiExtensions"];
35
+ /**
36
+ * Additional entity hooks to merge with sync hooks.
37
+ * Sync hooks will be called first, then your custom hooks.
38
+ */
39
+ entityHooks?: AuthHeroConfig["entityHooks"];
40
+ /**
41
+ * Custom function to get child tenant IDs.
42
+ * By default, queries all tenants except the control plane.
43
+ */
44
+ getChildTenantIds?: () => Promise<string[]>;
45
+ /**
46
+ * Custom function to get adapters for a specific tenant.
47
+ * By default, returns the main dataAdapter for all tenants.
48
+ * Override this for per-tenant database isolation.
49
+ */
50
+ getAdapters?: (tenantId: string) => Promise<DataAdapters>;
51
+ }
52
+ /**
53
+ * Result from initMultiTenant
54
+ */
55
+ export interface MultiTenantResult {
56
+ /** The configured Hono app */
57
+ app: ReturnType<typeof init>["app"];
58
+ /** The control plane tenant ID */
59
+ controlPlaneTenantId: string;
60
+ }
61
+ /**
62
+ * Initialize a multi-tenant AuthHero application with sensible defaults.
63
+ *
64
+ * This is the easiest way to set up multi-tenancy. It automatically:
65
+ * - Creates sync hooks for resource servers and roles
66
+ * - Mounts the tenants management API at `/tenants`
67
+ * - Adds middleware to protect synced entities on child tenants
68
+ * - Sets up organization-based access control
69
+ *
70
+ * @param config - Multi-tenant configuration
71
+ * @returns The configured app and control plane tenant ID
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * import { initMultiTenant } from "@authhero/multi-tenancy";
76
+ * import createAdapters from "@authhero/kysely-adapter";
77
+ *
78
+ * const dataAdapter = createAdapters(db);
79
+ *
80
+ * const { app } = initMultiTenant({
81
+ * dataAdapter,
82
+ * // That's it! Everything else has sensible defaults.
83
+ * });
84
+ *
85
+ * export default app;
86
+ * ```
87
+ *
88
+ * @example With customization
89
+ * ```typescript
90
+ * const { app } = initMultiTenant({
91
+ * dataAdapter,
92
+ * controlPlaneTenantId: "main",
93
+ * sync: {
94
+ * resourceServers: true,
95
+ * roles: false, // Don't sync roles
96
+ * },
97
+ * defaultPermissions: ["tenant:admin", "tenant:read"],
98
+ * });
99
+ * ```
100
+ *
101
+ * @example Disable syncing entirely
102
+ * ```typescript
103
+ * const { app } = initMultiTenant({
104
+ * dataAdapter,
105
+ * sync: false, // Each tenant manages their own entities
106
+ * });
107
+ * ```
108
+ */
109
+ export declare function initMultiTenant(config: MultiTenantConfig): MultiTenantResult;
110
+ //# sourceMappingURL=init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/init.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAY,YAAY,EAAU,MAAM,UAAU,CAAC;AAQhF;;GAEG;AACH,MAAM,WAAW,iBAAkB,SAAQ,IAAI,CAC7C,cAAc,EACd,aAAa,GAAG,yBAAyB,CAC1C;IACC;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAE9B;;;;OAIG;IACH,IAAI,CAAC,EACD;QACE,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB,GACD,KAAK,CAAC;IAEV;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,EAAE,CAAC;IAE9B;;;OAGG;IACH,wBAAwB,CAAC,EAAE,OAAO,CAAC;IAEnC;;;OAGG;IACH,uBAAuB,CAAC,EAAE,cAAc,CAAC,yBAAyB,CAAC,CAAC;IAEpE;;;OAGG;IACH,WAAW,CAAC,EAAE,cAAc,CAAC,aAAa,CAAC,CAAC;IAE5C;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAE5C;;;;OAIG;IACH,WAAW,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,YAAY,CAAC,CAAC;CAC3D;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,8BAA8B;IAC9B,GAAG,EAAE,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC;IACpC,kCAAkC;IAClC,oBAAoB,EAAE,MAAM,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,iBAAiB,GAAG,iBAAiB,CAkG5E"}
@@ -0,0 +1,114 @@
1
+ import { MiddlewareHandler } from "hono";
2
+ import { MultiTenancyBindings, MultiTenancyVariables, MultiTenancyConfig } from "../types";
3
+ /**
4
+ * Creates middleware that resolves tenant_id from org_name for control plane users.
5
+ *
6
+ * When a user authenticates to the control plane tenant and has an org_name claim,
7
+ * this middleware sets the tenant_id to the org_name, allowing them to access
8
+ * that child tenant's resources.
9
+ *
10
+ * @param controlPlaneTenantId - The ID of the control plane tenant
11
+ * @returns Hono middleware handler
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { createControlPlaneTenantMiddleware } from "@authhero/multi-tenancy";
16
+ *
17
+ * const middleware = createControlPlaneTenantMiddleware("control_plane");
18
+ *
19
+ * app.use("/api/*", middleware);
20
+ * ```
21
+ */
22
+ export declare function createControlPlaneTenantMiddleware(controlPlaneTenantId: string): MiddlewareHandler<{
23
+ Bindings: MultiTenancyBindings;
24
+ Variables: MultiTenancyVariables;
25
+ }>;
26
+ /**
27
+ * Creates middleware for validating organization-based tenant access.
28
+ *
29
+ * This middleware checks that the token's organization claim matches
30
+ * the target tenant ID, implementing the access control model where:
31
+ *
32
+ * - Control plane: Accessible without an organization claim
33
+ * - Child tenants: Require an organization claim matching the tenant ID
34
+ *
35
+ * @param config - Multi-tenancy configuration
36
+ * @returns Hono middleware handler
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * import { createAccessControlMiddleware } from "@authhero/multi-tenancy";
41
+ *
42
+ * const middleware = createAccessControlMiddleware({
43
+ * accessControl: {
44
+ * controlPlaneTenantId: "main",
45
+ * },
46
+ * });
47
+ *
48
+ * app.use("/api/*", middleware);
49
+ * ```
50
+ */
51
+ export declare function createAccessControlMiddleware(config: MultiTenancyConfig): MiddlewareHandler<{
52
+ Bindings: MultiTenancyBindings;
53
+ Variables: MultiTenancyVariables;
54
+ }>;
55
+ /**
56
+ * Creates middleware for resolving tenants from subdomains.
57
+ *
58
+ * This middleware extracts the subdomain from the request host and
59
+ * resolves it to a tenant ID using organizations on the control plane.
60
+ *
61
+ * @param config - Multi-tenancy configuration
62
+ * @returns Hono middleware handler
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * import { createSubdomainMiddleware } from "@authhero/multi-tenancy";
67
+ *
68
+ * const middleware = createSubdomainMiddleware({
69
+ * subdomainRouting: {
70
+ * baseDomain: "auth.example.com",
71
+ * reservedSubdomains: ["www", "api", "admin"],
72
+ * },
73
+ * accessControl: {
74
+ * controlPlaneTenantId: "main",
75
+ * },
76
+ * });
77
+ *
78
+ * app.use("*", middleware);
79
+ * ```
80
+ */
81
+ export declare function createSubdomainMiddleware(config: MultiTenancyConfig): MiddlewareHandler<{
82
+ Bindings: MultiTenancyBindings;
83
+ Variables: MultiTenancyVariables;
84
+ }>;
85
+ /**
86
+ * Creates middleware for resolving data adapters per tenant.
87
+ *
88
+ * This middleware resolves the data adapters for the target tenant,
89
+ * enabling per-tenant database isolation.
90
+ *
91
+ * @param config - Multi-tenancy configuration
92
+ * @returns Hono middleware handler
93
+ */
94
+ export declare function createDatabaseMiddleware(config: MultiTenancyConfig): MiddlewareHandler<{
95
+ Bindings: MultiTenancyBindings;
96
+ Variables: MultiTenancyVariables;
97
+ }>;
98
+ /**
99
+ * Creates a combined middleware stack for multi-tenancy.
100
+ *
101
+ * This combines subdomain routing, access control, and database resolution
102
+ * into a single middleware chain.
103
+ *
104
+ * @param config - Multi-tenancy configuration
105
+ * @returns Hono middleware handler
106
+ */
107
+ export declare function createMultiTenancyMiddleware(config: MultiTenancyConfig): MiddlewareHandler<{
108
+ Bindings: MultiTenancyBindings;
109
+ Variables: MultiTenancyVariables;
110
+ }>;
111
+ export { createProtectSyncedMiddleware } from "./protect-synced";
112
+ export { createRuntimeFallbackAdapter, withRuntimeFallback, createSettingsInheritanceAdapter, withSettingsInheritance, } from "./settings-inheritance";
113
+ export type { RuntimeFallbackConfig, SettingsInheritanceConfig, } from "./settings-inheritance";
114
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/middleware/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAGzC,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,UAAU,CAAC;AAGlB;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kCAAkC,CAChD,oBAAoB,EAAE,MAAM,GAC3B,iBAAiB,CAAC;IACnB,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,SAAS,EAAE,qBAAqB,CAAC;CAClC,CAAC,CAUD;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,6BAA6B,CAC3C,MAAM,EAAE,kBAAkB,GACzB,iBAAiB,CAAC;IACnB,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,SAAS,EAAE,qBAAqB,CAAC;CAClC,CAAC,CAsDD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,kBAAkB,GACzB,iBAAiB,CAAC;IACnB,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,SAAS,EAAE,qBAAqB,CAAC;CAClC,CAAC,CAqED;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,kBAAkB,GACzB,iBAAiB,CAAC;IACnB,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,SAAS,EAAE,qBAAqB,CAAC;CAClC,CAAC,CA6BD;AAED;;;;;;;;GAQG;AACH,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,kBAAkB,GACzB,iBAAiB,CAAC;IACnB,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,SAAS,EAAE,qBAAqB,CAAC;CAClC,CAAC,CAiBD;AAGD,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAC;AAGjE,OAAO,EACL,4BAA4B,EAC5B,mBAAmB,EAEnB,gCAAgC,EAChC,uBAAuB,GACxB,MAAM,wBAAwB,CAAC;AAChC,YAAY,EACV,qBAAqB,EAErB,yBAAyB,GAC1B,MAAM,wBAAwB,CAAC"}
@@ -0,0 +1,40 @@
1
+ import { MiddlewareHandler } from "hono";
2
+ import type { DataAdapters } from "authhero";
3
+ /**
4
+ * Bindings for the protect system middleware
5
+ */
6
+ interface ProtectSystemBindings {
7
+ data: DataAdapters;
8
+ }
9
+ /**
10
+ * Variables expected to be set by earlier middleware
11
+ */
12
+ interface ProtectSystemVariables {
13
+ tenant_id?: string;
14
+ }
15
+ /**
16
+ * Creates middleware to protect system resources from modification.
17
+ *
18
+ * This middleware intercepts write operations (PATCH, PUT, DELETE) on
19
+ * entities that are marked as system entities from the control plane and returns a 403
20
+ * error if modification is attempted.
21
+ *
22
+ * System resources can only be modified in the control plane, and changes
23
+ * will be propagated to child tenants automatically.
24
+ *
25
+ * @returns Hono middleware handler
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * import { createProtectSyncedMiddleware } from "@authhero/multi-tenancy";
30
+ *
31
+ * // Apply to management API routes
32
+ * app.use("/api/v2/*", createProtectSyncedMiddleware());
33
+ * ```
34
+ */
35
+ export declare function createProtectSyncedMiddleware(): MiddlewareHandler<{
36
+ Bindings: ProtectSystemBindings;
37
+ Variables: ProtectSystemVariables;
38
+ }>;
39
+ export {};
40
+ //# sourceMappingURL=protect-synced.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protect-synced.d.ts","sourceRoot":"","sources":["../../../src/middleware/protect-synced.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAEzC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C;;GAEG;AACH,UAAU,qBAAqB;IAC7B,IAAI,EAAE,YAAY,CAAC;CACpB;AAED;;GAEG;AACH,UAAU,sBAAsB;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AA4FD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,6BAA6B,IAAI,iBAAiB,CAAC;IACjE,QAAQ,EAAE,qBAAqB,CAAC;IAChC,SAAS,EAAE,sBAAsB,CAAC;CACnC,CAAC,CA+BD"}
@@ -0,0 +1,89 @@
1
+ import { DataAdapters } from "authhero";
2
+ /**
3
+ * Configuration for runtime settings fallback from a control plane tenant.
4
+ *
5
+ * Runtime fallback provides default values at query time without copying sensitive data.
6
+ */
7
+ export interface RuntimeFallbackConfig {
8
+ /**
9
+ * The control plane tenant ID from which settings are inherited at runtime.
10
+ * Child tenants will fall back to this tenant's configuration for
11
+ * missing or default values (e.g., connection secrets, SMTP settings).
12
+ */
13
+ controlPlaneTenantId?: string;
14
+ /**
15
+ * The control plane client ID used for fallback client settings.
16
+ * This client's configuration (web_origins, callbacks, etc.) will
17
+ * be merged with child tenant clients at runtime.
18
+ */
19
+ controlPlaneClientId?: string;
20
+ }
21
+ /**
22
+ * Creates a data adapter wrapper that provides runtime settings fallback from a control plane tenant.
23
+ *
24
+ * This adapter wraps the base adapters to provide fallback functionality where child tenants
25
+ * can inherit default configurations from a control plane (main) tenant **at runtime**.
26
+ * This is useful for:
27
+ *
28
+ * - **Default connection settings**: Inherit OAuth secrets, SMTP API keys, etc. without copying them
29
+ * - **Client URL fallbacks**: Add control plane URLs to allowed origins, callbacks, etc.
30
+ * - **Tenant property defaults**: Fall back to control plane values for missing tenant settings
31
+ *
32
+ * **Connection fallback by strategy:**
33
+ * Connections are matched by **strategy** (e.g., "google", "facebook", "email") rather than by name.
34
+ * This allows tenants to create a connection with a strategy like "google" and leave keys blank,
35
+ * and the system will automatically merge in the OAuth credentials from the control plane's
36
+ * google connection. This is the recommended approach for sharing social auth across tenants.
37
+ *
38
+ * @param baseAdapters - The base data adapters to wrap
39
+ * @param config - Configuration for runtime settings fallback
40
+ * @returns Wrapped data adapters with runtime fallback functionality
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * import { createRuntimeFallbackAdapter } from "@authhero/multi-tenancy";
45
+ * import createAdapters from "@authhero/kysely";
46
+ *
47
+ * const db = // ... your database connection
48
+ * const baseAdapters = createAdapters(db);
49
+ *
50
+ * const adapters = createRuntimeFallbackAdapter(baseAdapters, {
51
+ * controlPlaneTenantId: "main",
52
+ * controlPlaneClientId: "main-client"
53
+ * });
54
+ * ```
55
+ */
56
+ export declare function createRuntimeFallbackAdapter(baseAdapters: DataAdapters, config: RuntimeFallbackConfig): DataAdapters;
57
+ /**
58
+ * Helper function to wrap data adapters with runtime fallback from control plane.
59
+ *
60
+ * This is a convenience wrapper around `createRuntimeFallbackAdapter`.
61
+ *
62
+ * @param baseAdapters - The base data adapters to wrap
63
+ * @param config - Configuration for runtime fallback
64
+ * @returns Wrapped data adapters with runtime fallback functionality
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * import { withRuntimeFallback } from "@authhero/multi-tenancy";
69
+ *
70
+ * const adapters = withRuntimeFallback(baseAdapters, {
71
+ * controlPlaneTenantId: "main",
72
+ * controlPlaneClientId: "main-client"
73
+ * });
74
+ * ```
75
+ */
76
+ export declare function withRuntimeFallback(baseAdapters: DataAdapters, config: RuntimeFallbackConfig): DataAdapters;
77
+ /**
78
+ * @deprecated Use `RuntimeFallbackConfig` instead
79
+ */
80
+ export type SettingsInheritanceConfig = RuntimeFallbackConfig;
81
+ /**
82
+ * @deprecated Use `createRuntimeFallbackAdapter` instead
83
+ */
84
+ export declare const createSettingsInheritanceAdapter: typeof createRuntimeFallbackAdapter;
85
+ /**
86
+ * @deprecated Use `withRuntimeFallback` instead
87
+ */
88
+ export declare const withSettingsInheritance: typeof withRuntimeFallback;
89
+ //# sourceMappingURL=settings-inheritance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"settings-inheritance.d.ts","sourceRoot":"","sources":["../../../src/middleware/settings-inheritance.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EAKb,MAAM,UAAU,CAAC;AAElB;;;;GAIG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAE9B;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,4BAA4B,CAC1C,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,qBAAqB,GAC5B,YAAY,CA6Ld;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,qBAAqB,GAC5B,YAAY,CAEd;AAGD;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,qBAAqB,CAAC;AAE9D;;GAEG;AACH,eAAO,MAAM,gCAAgC,qCAA+B,CAAC;AAE7E;;GAEG;AACH,eAAO,MAAM,uBAAuB,4BAAsB,CAAC"}
@@ -0,0 +1,66 @@
1
+ import { Hono, MiddlewareHandler } from "hono";
2
+ import { MultiTenancyConfig, MultiTenancyHooks, MultiTenancyBindings, MultiTenancyVariables } from "./types";
3
+ type MultiTenancyEnv = {
4
+ Bindings: MultiTenancyBindings;
5
+ Variables: MultiTenancyVariables;
6
+ };
7
+ /**
8
+ * Plugin interface for AuthHero extensions
9
+ */
10
+ export interface AuthHeroPlugin {
11
+ /**
12
+ * Plugin name for identification
13
+ */
14
+ name: string;
15
+ /**
16
+ * Middleware to run before AuthHero routes
17
+ */
18
+ middleware?: MiddlewareHandler<MultiTenancyEnv>;
19
+ /**
20
+ * Lifecycle hooks
21
+ */
22
+ hooks?: MultiTenancyHooks;
23
+ /**
24
+ * Additional routes to mount
25
+ */
26
+ routes?: Array<{
27
+ path: string;
28
+ handler: Hono<MultiTenancyEnv>;
29
+ }>;
30
+ /**
31
+ * Called when plugin is registered
32
+ */
33
+ onRegister?: (app: Hono) => void | Promise<void>;
34
+ }
35
+ /**
36
+ * Creates a multi-tenancy plugin for AuthHero.
37
+ *
38
+ * This packages all multi-tenancy functionality (middleware, hooks, and routes)
39
+ * into a single plugin that can be registered with AuthHero.
40
+ *
41
+ * @param config - Multi-tenancy configuration
42
+ * @returns AuthHero plugin for multi-tenancy support
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * import { createAuthhero } from "@authhero/authhero";
47
+ * import { createMultiTenancyPlugin } from "@authhero/multi-tenancy";
48
+ *
49
+ * const app = createAuthhero({
50
+ * plugins: [
51
+ * createMultiTenancyPlugin({
52
+ * accessControl: {
53
+ * controlPlaneTenantId: "main",
54
+ * defaultPermissions: ["tenant:admin"],
55
+ * },
56
+ * subdomainRouting: {
57
+ * baseDomain: "auth.example.com",
58
+ * },
59
+ * }),
60
+ * ],
61
+ * });
62
+ * ```
63
+ */
64
+ export declare function createMultiTenancyPlugin(config: MultiTenancyConfig): AuthHeroPlugin;
65
+ export {};
66
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC/C,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACtB,MAAM,SAAS,CAAC;AAKjB,KAAK,eAAe,GAAG;IACrB,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,SAAS,EAAE,qBAAqB,CAAC;CAClC,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,UAAU,CAAC,EAAE,iBAAiB,CAAC,eAAe,CAAC,CAAC;IAEhD;;OAEG;IACH,KAAK,CAAC,EAAE,iBAAiB,CAAC;IAE1B;;OAEG;IACH,MAAM,CAAC,EAAE,KAAK,CAAC;QACb,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;KAChC,CAAC,CAAC;IAEH;;OAEG;IACH,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,IAAI,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAClD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,kBAAkB,GACzB,cAAc,CAsChB"}
@@ -0,0 +1,2 @@
1
+ export { createTenantsOpenAPIRouter } from "./tenants";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/routes/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,WAAW,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { OpenAPIHono } from "@hono/zod-openapi";
2
+ import { MultiTenancyBindings, MultiTenancyVariables, MultiTenancyConfig, MultiTenancyHooks } from "../types";
3
+ /**
4
+ * Creates OpenAPI-based tenant management routes.
5
+ *
6
+ * These routes handle CRUD operations for tenants and are designed to be
7
+ * mounted on authhero's management API so they get the same authentication
8
+ * middleware.
9
+ *
10
+ * @param config - Multi-tenancy configuration
11
+ * @param hooks - Multi-tenancy hooks for lifecycle events
12
+ * @returns OpenAPIHono router with tenant routes
13
+ */
14
+ export declare function createTenantsOpenAPIRouter(config: MultiTenancyConfig, hooks: MultiTenancyHooks): OpenAPIHono<{
15
+ Bindings: MultiTenancyBindings;
16
+ Variables: MultiTenancyVariables;
17
+ }, {}, "/">;
18
+ //# sourceMappingURL=tenants.d.ts.map