@nathapp/nestjs-tenant 1.0.0 → 1.2.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/package.json +5 -5
- package/AGENTS.md +0 -142
- package/CLAUDE.md +0 -140
- package/GEMINI.md +0 -140
- package/codex.md +0 -140
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nathapp/nestjs-tenant",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Multi-tenant context management for NestJS",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "rimraf dist && tsc -p tsconfig.json",
|
|
@@ -24,16 +24,16 @@
|
|
|
24
24
|
"lib": "lib"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@nathapp/nestjs-common": "^3.3.0 || ^4.0.
|
|
28
|
-
"@nathapp/nestjs-data": "^3.3.0 || ^4.0.
|
|
27
|
+
"@nathapp/nestjs-common": "^3.3.0 || ^4.0.3",
|
|
28
|
+
"@nathapp/nestjs-data": "^3.3.0 || ^4.0.3"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@nathapp/nestjs-cache": "^4.0.
|
|
31
|
+
"@nathapp/nestjs-cache": "^4.0.3",
|
|
32
32
|
"eslint": "^8.57.0",
|
|
33
33
|
"typescript": "^5"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@nathapp/nestjs-cache": "^3.3.0 || ^4.0.
|
|
36
|
+
"@nathapp/nestjs-cache": "^3.3.0 || ^4.0.3",
|
|
37
37
|
"@nestjs/common": "^11.0.0 || ^12.1.0",
|
|
38
38
|
"@nestjs/config": "^4.0.0 || ^12.0.0",
|
|
39
39
|
"@nestjs/core": "^11.0.0 || ^12.1.0"
|
package/AGENTS.md
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
# Agent Instructions
|
|
2
|
-
|
|
3
|
-
This file is auto-generated from `.nax/context.md`.
|
|
4
|
-
DO NOT EDIT MANUALLY — run `nax generate` to regenerate.
|
|
5
|
-
|
|
6
|
-
These instructions apply to all AI coding agents in this project.
|
|
7
|
-
|
|
8
|
-
---
|
|
9
|
-
|
|
10
|
-
## Project Metadata
|
|
11
|
-
|
|
12
|
-
> Auto-injected by `nax generate`
|
|
13
|
-
|
|
14
|
-
**Project:** `@nathapp/nestjs-tenant`
|
|
15
|
-
|
|
16
|
-
**Language:** TypeScript
|
|
17
|
-
|
|
18
|
-
**Key dependencies:** typescript
|
|
19
|
-
|
|
20
|
-
**Commands:** test: `npm run test` | lint: `npm run lint` | typecheck: `npm run type-check`
|
|
21
|
-
|
|
22
|
-
---
|
|
23
|
-
# CLAUDE.md — @nathapp/nestjs-tenant
|
|
24
|
-
|
|
25
|
-
Multi-tenant context management for NestJS. Column-based tenancy with automatic request-scoped context.
|
|
26
|
-
|
|
27
|
-
## Status: ✅ Complete
|
|
28
|
-
|
|
29
|
-
## Key Files
|
|
30
|
-
|
|
31
|
-
- `lib/interface/` — `ITenant`, `ITenantRepository`, `ITenantService`, `ITenantResolver`, `ITenantContext`, `IMultiTenantPrincipal`, `ITenantPrincipalBinder`
|
|
32
|
-
- `lib/service/default-tenant.service.ts` — Default tenant CRUD with single-tenant mode support
|
|
33
|
-
- `lib/service/tenant-context.service.ts` — AsyncLocalStorage-based request-scoped tenant context
|
|
34
|
-
- `lib/resolver/` — 5 resolvers: `HeaderTenantResolver`, `JwtClaimTenantResolver`, `SubdomainTenantResolver`, `PathTenantResolver`, `DtoFieldTenantResolver`
|
|
35
|
-
- `lib/guard/tenant.guard.ts` — `TenantGuard` resolves + validates tenant per request, sets context
|
|
36
|
-
- `lib/decorator/` — `@CurrentTenant()`, `@SkipTenant()`, `@TenantId()` (DTO param)
|
|
37
|
-
- `lib/module/tenant.module.ts` — `TenantModule.register()` / `registerAsync()`
|
|
38
|
-
- `lib/constant/tokens.ts` — DI tokens: `TENANT_REPOSITORY`, `TENANT_SERVICE`, `TENANT_RESOLVER`, `TENANT_CONTEXT`, `TENANT_MODULE_OPTIONS`, `TENANT_CACHE`, `TENANT_PRINCIPAL_BINDER`
|
|
39
|
-
|
|
40
|
-
## DI Tokens
|
|
41
|
-
|
|
42
|
-
| Token | Interface | Default |
|
|
43
|
-
|:---|:---|:---|
|
|
44
|
-
| `TENANT_REPOSITORY` | `ITenantRepository` | None (consumer must provide) |
|
|
45
|
-
| `TENANT_SERVICE` | `ITenantService` | `DefaultTenantService` |
|
|
46
|
-
| `TENANT_RESOLVER` | `ITenantResolver` | Configured via module options |
|
|
47
|
-
| `TENANT_CONTEXT` | `ITenantContext` | `TenantContextService` |
|
|
48
|
-
| `TENANT_CACHE` | `ITenantCache` | `InMemoryTenantCache` |
|
|
49
|
-
| `TENANT_PRINCIPAL_BINDER` | `ITenantPrincipalBinder` | None (consumer must provide) |
|
|
50
|
-
|
|
51
|
-
## Module Registration
|
|
52
|
-
|
|
53
|
-
```typescript
|
|
54
|
-
TenantModule.register({
|
|
55
|
-
repository: MyTenantRepository, // REQUIRED — consumer's ITenantRepository class
|
|
56
|
-
resolution: { strategy: TenantResolutionStrategy.HEADER, headerName: 'x-tenant-id' },
|
|
57
|
-
// Single-tenant mode (optional, off by default — set enabled: true to arm it).
|
|
58
|
-
// Armed, TenantGuard falls back to defaultTenantId when the configured resolver
|
|
59
|
-
// finds no tenant identifier on the request. headerName is only read by the
|
|
60
|
-
// HEADER strategy; JWT_CLAIM reads claimKey, SUBDOMAIN reads the host, PATH reads
|
|
61
|
-
// pathPrefix, DTO_FIELD reads dtoField, and CUSTOM takes a resolver class.
|
|
62
|
-
//
|
|
63
|
-
// defaultTenantId MUST be a real, existing tenant id. It is not validated at
|
|
64
|
-
// registration: a falsy value is returned like any other resolution result and
|
|
65
|
-
// fails the guard's `if (!resolved)` check, so outside a guard.skipPaths match
|
|
66
|
-
// it rejects with the normal 400 (RESOLUTION_FAILED) — 403 under
|
|
67
|
-
// onTenantMissing: 'forbidden' — while a skip path is allowed unbound instead.
|
|
68
|
-
// The fallback is only "fail-open" when the id is real. Header/subdomain
|
|
69
|
-
// resolution still wins when present, so multi-tenant behaviour is unaffected.
|
|
70
|
-
// Enable only for genuinely single-tenant deployments.
|
|
71
|
-
singleTenant: { enabled: false, defaultTenantId: 'tenant-1' },
|
|
72
|
-
})
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
## Supported Wiring
|
|
76
|
-
|
|
77
|
-
Supported production order — `TenantGuard` reads the principal off `request.user`, so register the real guards explicitly in authentication → tenant → authorization order:
|
|
78
|
-
|
|
79
|
-
```
|
|
80
|
-
JwtAuthGuard (@nathapp/nestjs-auth)
|
|
81
|
-
→ TenantGuard (configured resolver + optional principal binding)
|
|
82
|
-
→ PermissionAuthGuard (@nathapp/nestjs-auth) + AccountCaslAbilityFactory (@nathapp/nestjs-account)
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
The resolver strategy controls where the request's tenant ID comes from; principal binding then checks membership in that resolved tenant. These are separate decisions:
|
|
86
|
-
|
|
87
|
-
- `HEADER` resolves `x-tenant-id` (or the configured header name). `require-match` rejects an authenticated principal that is not a member of that header-selected tenant.
|
|
88
|
-
- `JWT_CLAIM` resolves a claim on the authenticated principal: first `user.tenantId`, then `user.extra.tenantId` for claims nested by the core auth provider. It does not read the tenant header.
|
|
89
|
-
|
|
90
|
-
The real guard order, Fastify request path, and tenant AsyncLocalStorage are exercised by `packages/integration-e2e/test/tenant-principal-isolation.spec.ts` for both strategies.
|
|
91
|
-
|
|
92
|
-
### `guard.principalBinding`
|
|
93
|
-
|
|
94
|
-
| Mode | Behaviour |
|
|
95
|
-
|:---|:---|
|
|
96
|
-
| `off` (default) | No check; the binder is never called |
|
|
97
|
-
| `require-match` | 403 (`TENANT_PRINCIPAL_MISMATCH`) when the principal is not a member of the resolved tenant |
|
|
98
|
-
| `assert` | Dry run — warns on mismatch (and on binder error) and continues; never rejects |
|
|
99
|
-
|
|
100
|
-
There is deliberately **no default binder**. `TenantGuard` injects `TENANT_PRINCIPAL_BINDER` with `@Optional()`, so `require-match` without one would 403 every authenticated request — `TenantModule.register()` / `registerAsync()` throw instead. Supply one:
|
|
101
|
-
|
|
102
|
-
```typescript
|
|
103
|
-
TenantModule.register({
|
|
104
|
-
repository: MyTenantRepository, // REQUIRED — consumer's ITenantRepository class
|
|
105
|
-
resolution: { strategy: TenantResolutionStrategy.JWT_CLAIM },
|
|
106
|
-
principalBinder: MyTenantPrincipalBinder, // registered as TENANT_PRINCIPAL_BINDER (recommended)
|
|
107
|
-
guard: { principalBinding: { mode: 'require-match' } },
|
|
108
|
-
})
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
A binder reaching the token from another module needs `guard.principalBinding.binderProvided: true` — the module cannot verify it, and under `register()` (no `imports`) the providing module must be `@Global()` **and export** the token; merely providing it stays invisible. Prefer `principalBinder`: it is the only route the module can resolve for you, and the only such route under `register()`. Under `registerAsync` set `principalBinder` on `TenantModuleAsyncOptions`, not on the options the factory returns.
|
|
112
|
-
|
|
113
|
-
`ITenantPrincipalBinder` is declared here, deliberately not a re-export of `nestjs-iam`'s `ITenantMembershipResolver` — `nestjs-tenant` is the dependency floor and cannot depend on `nestjs-iam`, so the consumer supplies a bridging binder. Implement it as:
|
|
114
|
-
|
|
115
|
-
```typescript
|
|
116
|
-
interface ITenantPrincipalBinder {
|
|
117
|
-
assertMembership(principal: { id?: string }, tenantId: string): Promise<boolean>;
|
|
118
|
-
}
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
`principal` is typed `{ id?: string }` and not a broader record: a binder normally bridges `nestjs-iam`'s `IPrincipal`, and an index signature here would reject that (TypeScript grants implicit index signatures to object literal types, not to interfaces). The binder reads `principal.id`; look up anything else off the concrete principal type inside your own implementation.
|
|
122
|
-
|
|
123
|
-
The check runs after `setTenant` and is never cached, so a revoked membership takes effect immediately rather than after `validation.cacheTtl`. It is skipped when the request carries no principal, and skipped for `@SkipTenant()` handlers. Note the asymmetry on `guard.skipPaths`: a matching path is **not** an unbound request — resolution and lookup still run and a found tenant **is** still set on the context and `request.__tenant`; only this assertion is skipped.
|
|
124
|
-
|
|
125
|
-
### JWT tenant claim
|
|
126
|
-
|
|
127
|
-
`JwtClaimTenantResolver` reads `user[claimKey]` (default `'tenantId'`), then `user.extra[claimKey]`; a dotted `claimKey` such as `'extra.tenantId'` is walked against `user` instead. A function may be passed in place of the key (selector mode). `extra` is where core `SimpleAuthProvider` puts arbitrary claims — it maps a payload to `{ id, username, name, authorities, extra }`, so a top-level-only read resolved nothing for a core-issued JWT. A non-primitive claim resolves to `null`, not `"[object Object]"`.
|
|
128
|
-
|
|
129
|
-
The cross-tenant HTTP acceptance coverage lives in `packages/integration-e2e/test/tenant-principal-isolation.spec.ts`; the OAuth flow tests separately keep their existing `TenantContextService.run()` coverage.
|
|
130
|
-
|
|
131
|
-
## Testing
|
|
132
|
-
|
|
133
|
-
```bash
|
|
134
|
-
npm test # run tests
|
|
135
|
-
npm run test:cov # with coverage
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
## Gotchas
|
|
139
|
-
|
|
140
|
-
- `TenantContextService` uses `AsyncLocalStorage` — context is lost if you break the async chain (e.g., `setTimeout` without `AsyncResource`)
|
|
141
|
-
- `@SkipTenant()` on a handler skips `TenantGuard` resolution entirely
|
|
142
|
-
- `IMultiTenantPrincipal` extends `IPrincipal` from `@nathapp/nestjs-auth` — adds `tenantId` and `roles`
|
package/CLAUDE.md
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
# Project Context
|
|
2
|
-
|
|
3
|
-
This file is auto-generated from `.nax/context.md`.
|
|
4
|
-
DO NOT EDIT MANUALLY — run `nax generate` to regenerate.
|
|
5
|
-
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Project Metadata
|
|
9
|
-
|
|
10
|
-
> Auto-injected by `nax generate`
|
|
11
|
-
|
|
12
|
-
**Project:** `@nathapp/nestjs-tenant`
|
|
13
|
-
|
|
14
|
-
**Language:** TypeScript
|
|
15
|
-
|
|
16
|
-
**Key dependencies:** typescript
|
|
17
|
-
|
|
18
|
-
**Commands:** test: `npm run test` | lint: `npm run lint` | typecheck: `npm run type-check`
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
# CLAUDE.md — @nathapp/nestjs-tenant
|
|
22
|
-
|
|
23
|
-
Multi-tenant context management for NestJS. Column-based tenancy with automatic request-scoped context.
|
|
24
|
-
|
|
25
|
-
## Status: ✅ Complete
|
|
26
|
-
|
|
27
|
-
## Key Files
|
|
28
|
-
|
|
29
|
-
- `lib/interface/` — `ITenant`, `ITenantRepository`, `ITenantService`, `ITenantResolver`, `ITenantContext`, `IMultiTenantPrincipal`, `ITenantPrincipalBinder`
|
|
30
|
-
- `lib/service/default-tenant.service.ts` — Default tenant CRUD with single-tenant mode support
|
|
31
|
-
- `lib/service/tenant-context.service.ts` — AsyncLocalStorage-based request-scoped tenant context
|
|
32
|
-
- `lib/resolver/` — 5 resolvers: `HeaderTenantResolver`, `JwtClaimTenantResolver`, `SubdomainTenantResolver`, `PathTenantResolver`, `DtoFieldTenantResolver`
|
|
33
|
-
- `lib/guard/tenant.guard.ts` — `TenantGuard` resolves + validates tenant per request, sets context
|
|
34
|
-
- `lib/decorator/` — `@CurrentTenant()`, `@SkipTenant()`, `@TenantId()` (DTO param)
|
|
35
|
-
- `lib/module/tenant.module.ts` — `TenantModule.register()` / `registerAsync()`
|
|
36
|
-
- `lib/constant/tokens.ts` — DI tokens: `TENANT_REPOSITORY`, `TENANT_SERVICE`, `TENANT_RESOLVER`, `TENANT_CONTEXT`, `TENANT_MODULE_OPTIONS`, `TENANT_CACHE`, `TENANT_PRINCIPAL_BINDER`
|
|
37
|
-
|
|
38
|
-
## DI Tokens
|
|
39
|
-
|
|
40
|
-
| Token | Interface | Default |
|
|
41
|
-
|:---|:---|:---|
|
|
42
|
-
| `TENANT_REPOSITORY` | `ITenantRepository` | None (consumer must provide) |
|
|
43
|
-
| `TENANT_SERVICE` | `ITenantService` | `DefaultTenantService` |
|
|
44
|
-
| `TENANT_RESOLVER` | `ITenantResolver` | Configured via module options |
|
|
45
|
-
| `TENANT_CONTEXT` | `ITenantContext` | `TenantContextService` |
|
|
46
|
-
| `TENANT_CACHE` | `ITenantCache` | `InMemoryTenantCache` |
|
|
47
|
-
| `TENANT_PRINCIPAL_BINDER` | `ITenantPrincipalBinder` | None (consumer must provide) |
|
|
48
|
-
|
|
49
|
-
## Module Registration
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
TenantModule.register({
|
|
53
|
-
repository: MyTenantRepository, // REQUIRED — consumer's ITenantRepository class
|
|
54
|
-
resolution: { strategy: TenantResolutionStrategy.HEADER, headerName: 'x-tenant-id' },
|
|
55
|
-
// Single-tenant mode (optional, off by default — set enabled: true to arm it).
|
|
56
|
-
// Armed, TenantGuard falls back to defaultTenantId when the configured resolver
|
|
57
|
-
// finds no tenant identifier on the request. headerName is only read by the
|
|
58
|
-
// HEADER strategy; JWT_CLAIM reads claimKey, SUBDOMAIN reads the host, PATH reads
|
|
59
|
-
// pathPrefix, DTO_FIELD reads dtoField, and CUSTOM takes a resolver class.
|
|
60
|
-
//
|
|
61
|
-
// defaultTenantId MUST be a real, existing tenant id. It is not validated at
|
|
62
|
-
// registration: a falsy value is returned like any other resolution result and
|
|
63
|
-
// fails the guard's `if (!resolved)` check, so outside a guard.skipPaths match
|
|
64
|
-
// it rejects with the normal 400 (RESOLUTION_FAILED) — 403 under
|
|
65
|
-
// onTenantMissing: 'forbidden' — while a skip path is allowed unbound instead.
|
|
66
|
-
// The fallback is only "fail-open" when the id is real. Header/subdomain
|
|
67
|
-
// resolution still wins when present, so multi-tenant behaviour is unaffected.
|
|
68
|
-
// Enable only for genuinely single-tenant deployments.
|
|
69
|
-
singleTenant: { enabled: false, defaultTenantId: 'tenant-1' },
|
|
70
|
-
})
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
## Supported Wiring
|
|
74
|
-
|
|
75
|
-
Supported production order — `TenantGuard` reads the principal off `request.user`, so register the real guards explicitly in authentication → tenant → authorization order:
|
|
76
|
-
|
|
77
|
-
```
|
|
78
|
-
JwtAuthGuard (@nathapp/nestjs-auth)
|
|
79
|
-
→ TenantGuard (configured resolver + optional principal binding)
|
|
80
|
-
→ PermissionAuthGuard (@nathapp/nestjs-auth) + AccountCaslAbilityFactory (@nathapp/nestjs-account)
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
The resolver strategy controls where the request's tenant ID comes from; principal binding then checks membership in that resolved tenant. These are separate decisions:
|
|
84
|
-
|
|
85
|
-
- `HEADER` resolves `x-tenant-id` (or the configured header name). `require-match` rejects an authenticated principal that is not a member of that header-selected tenant.
|
|
86
|
-
- `JWT_CLAIM` resolves a claim on the authenticated principal: first `user.tenantId`, then `user.extra.tenantId` for claims nested by the core auth provider. It does not read the tenant header.
|
|
87
|
-
|
|
88
|
-
The real guard order, Fastify request path, and tenant AsyncLocalStorage are exercised by `packages/integration-e2e/test/tenant-principal-isolation.spec.ts` for both strategies.
|
|
89
|
-
|
|
90
|
-
### `guard.principalBinding`
|
|
91
|
-
|
|
92
|
-
| Mode | Behaviour |
|
|
93
|
-
|:---|:---|
|
|
94
|
-
| `off` (default) | No check; the binder is never called |
|
|
95
|
-
| `require-match` | 403 (`TENANT_PRINCIPAL_MISMATCH`) when the principal is not a member of the resolved tenant |
|
|
96
|
-
| `assert` | Dry run — warns on mismatch (and on binder error) and continues; never rejects |
|
|
97
|
-
|
|
98
|
-
There is deliberately **no default binder**. `TenantGuard` injects `TENANT_PRINCIPAL_BINDER` with `@Optional()`, so `require-match` without one would 403 every authenticated request — `TenantModule.register()` / `registerAsync()` throw instead. Supply one:
|
|
99
|
-
|
|
100
|
-
```typescript
|
|
101
|
-
TenantModule.register({
|
|
102
|
-
repository: MyTenantRepository, // REQUIRED — consumer's ITenantRepository class
|
|
103
|
-
resolution: { strategy: TenantResolutionStrategy.JWT_CLAIM },
|
|
104
|
-
principalBinder: MyTenantPrincipalBinder, // registered as TENANT_PRINCIPAL_BINDER (recommended)
|
|
105
|
-
guard: { principalBinding: { mode: 'require-match' } },
|
|
106
|
-
})
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
A binder reaching the token from another module needs `guard.principalBinding.binderProvided: true` — the module cannot verify it, and under `register()` (no `imports`) the providing module must be `@Global()` **and export** the token; merely providing it stays invisible. Prefer `principalBinder`: it is the only route the module can resolve for you, and the only such route under `register()`. Under `registerAsync` set `principalBinder` on `TenantModuleAsyncOptions`, not on the options the factory returns.
|
|
110
|
-
|
|
111
|
-
`ITenantPrincipalBinder` is declared here, deliberately not a re-export of `nestjs-iam`'s `ITenantMembershipResolver` — `nestjs-tenant` is the dependency floor and cannot depend on `nestjs-iam`, so the consumer supplies a bridging binder. Implement it as:
|
|
112
|
-
|
|
113
|
-
```typescript
|
|
114
|
-
interface ITenantPrincipalBinder {
|
|
115
|
-
assertMembership(principal: { id?: string }, tenantId: string): Promise<boolean>;
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
`principal` is typed `{ id?: string }` and not a broader record: a binder normally bridges `nestjs-iam`'s `IPrincipal`, and an index signature here would reject that (TypeScript grants implicit index signatures to object literal types, not to interfaces). The binder reads `principal.id`; look up anything else off the concrete principal type inside your own implementation.
|
|
120
|
-
|
|
121
|
-
The check runs after `setTenant` and is never cached, so a revoked membership takes effect immediately rather than after `validation.cacheTtl`. It is skipped when the request carries no principal, and skipped for `@SkipTenant()` handlers. Note the asymmetry on `guard.skipPaths`: a matching path is **not** an unbound request — resolution and lookup still run and a found tenant **is** still set on the context and `request.__tenant`; only this assertion is skipped.
|
|
122
|
-
|
|
123
|
-
### JWT tenant claim
|
|
124
|
-
|
|
125
|
-
`JwtClaimTenantResolver` reads `user[claimKey]` (default `'tenantId'`), then `user.extra[claimKey]`; a dotted `claimKey` such as `'extra.tenantId'` is walked against `user` instead. A function may be passed in place of the key (selector mode). `extra` is where core `SimpleAuthProvider` puts arbitrary claims — it maps a payload to `{ id, username, name, authorities, extra }`, so a top-level-only read resolved nothing for a core-issued JWT. A non-primitive claim resolves to `null`, not `"[object Object]"`.
|
|
126
|
-
|
|
127
|
-
The cross-tenant HTTP acceptance coverage lives in `packages/integration-e2e/test/tenant-principal-isolation.spec.ts`; the OAuth flow tests separately keep their existing `TenantContextService.run()` coverage.
|
|
128
|
-
|
|
129
|
-
## Testing
|
|
130
|
-
|
|
131
|
-
```bash
|
|
132
|
-
npm test # run tests
|
|
133
|
-
npm run test:cov # with coverage
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
## Gotchas
|
|
137
|
-
|
|
138
|
-
- `TenantContextService` uses `AsyncLocalStorage` — context is lost if you break the async chain (e.g., `setTimeout` without `AsyncResource`)
|
|
139
|
-
- `@SkipTenant()` on a handler skips `TenantGuard` resolution entirely
|
|
140
|
-
- `IMultiTenantPrincipal` extends `IPrincipal` from `@nathapp/nestjs-auth` — adds `tenantId` and `roles`
|
package/GEMINI.md
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
# Gemini CLI Context
|
|
2
|
-
|
|
3
|
-
This file is auto-generated from `.nax/context.md`.
|
|
4
|
-
DO NOT EDIT MANUALLY — run `nax generate` to regenerate.
|
|
5
|
-
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Project Metadata
|
|
9
|
-
|
|
10
|
-
> Auto-injected by `nax generate`
|
|
11
|
-
|
|
12
|
-
**Project:** `@nathapp/nestjs-tenant`
|
|
13
|
-
|
|
14
|
-
**Language:** TypeScript
|
|
15
|
-
|
|
16
|
-
**Key dependencies:** typescript
|
|
17
|
-
|
|
18
|
-
**Commands:** test: `npm run test` | lint: `npm run lint` | typecheck: `npm run type-check`
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
# CLAUDE.md — @nathapp/nestjs-tenant
|
|
22
|
-
|
|
23
|
-
Multi-tenant context management for NestJS. Column-based tenancy with automatic request-scoped context.
|
|
24
|
-
|
|
25
|
-
## Status: ✅ Complete
|
|
26
|
-
|
|
27
|
-
## Key Files
|
|
28
|
-
|
|
29
|
-
- `lib/interface/` — `ITenant`, `ITenantRepository`, `ITenantService`, `ITenantResolver`, `ITenantContext`, `IMultiTenantPrincipal`, `ITenantPrincipalBinder`
|
|
30
|
-
- `lib/service/default-tenant.service.ts` — Default tenant CRUD with single-tenant mode support
|
|
31
|
-
- `lib/service/tenant-context.service.ts` — AsyncLocalStorage-based request-scoped tenant context
|
|
32
|
-
- `lib/resolver/` — 5 resolvers: `HeaderTenantResolver`, `JwtClaimTenantResolver`, `SubdomainTenantResolver`, `PathTenantResolver`, `DtoFieldTenantResolver`
|
|
33
|
-
- `lib/guard/tenant.guard.ts` — `TenantGuard` resolves + validates tenant per request, sets context
|
|
34
|
-
- `lib/decorator/` — `@CurrentTenant()`, `@SkipTenant()`, `@TenantId()` (DTO param)
|
|
35
|
-
- `lib/module/tenant.module.ts` — `TenantModule.register()` / `registerAsync()`
|
|
36
|
-
- `lib/constant/tokens.ts` — DI tokens: `TENANT_REPOSITORY`, `TENANT_SERVICE`, `TENANT_RESOLVER`, `TENANT_CONTEXT`, `TENANT_MODULE_OPTIONS`, `TENANT_CACHE`, `TENANT_PRINCIPAL_BINDER`
|
|
37
|
-
|
|
38
|
-
## DI Tokens
|
|
39
|
-
|
|
40
|
-
| Token | Interface | Default |
|
|
41
|
-
|:---|:---|:---|
|
|
42
|
-
| `TENANT_REPOSITORY` | `ITenantRepository` | None (consumer must provide) |
|
|
43
|
-
| `TENANT_SERVICE` | `ITenantService` | `DefaultTenantService` |
|
|
44
|
-
| `TENANT_RESOLVER` | `ITenantResolver` | Configured via module options |
|
|
45
|
-
| `TENANT_CONTEXT` | `ITenantContext` | `TenantContextService` |
|
|
46
|
-
| `TENANT_CACHE` | `ITenantCache` | `InMemoryTenantCache` |
|
|
47
|
-
| `TENANT_PRINCIPAL_BINDER` | `ITenantPrincipalBinder` | None (consumer must provide) |
|
|
48
|
-
|
|
49
|
-
## Module Registration
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
TenantModule.register({
|
|
53
|
-
repository: MyTenantRepository, // REQUIRED — consumer's ITenantRepository class
|
|
54
|
-
resolution: { strategy: TenantResolutionStrategy.HEADER, headerName: 'x-tenant-id' },
|
|
55
|
-
// Single-tenant mode (optional, off by default — set enabled: true to arm it).
|
|
56
|
-
// Armed, TenantGuard falls back to defaultTenantId when the configured resolver
|
|
57
|
-
// finds no tenant identifier on the request. headerName is only read by the
|
|
58
|
-
// HEADER strategy; JWT_CLAIM reads claimKey, SUBDOMAIN reads the host, PATH reads
|
|
59
|
-
// pathPrefix, DTO_FIELD reads dtoField, and CUSTOM takes a resolver class.
|
|
60
|
-
//
|
|
61
|
-
// defaultTenantId MUST be a real, existing tenant id. It is not validated at
|
|
62
|
-
// registration: a falsy value is returned like any other resolution result and
|
|
63
|
-
// fails the guard's `if (!resolved)` check, so outside a guard.skipPaths match
|
|
64
|
-
// it rejects with the normal 400 (RESOLUTION_FAILED) — 403 under
|
|
65
|
-
// onTenantMissing: 'forbidden' — while a skip path is allowed unbound instead.
|
|
66
|
-
// The fallback is only "fail-open" when the id is real. Header/subdomain
|
|
67
|
-
// resolution still wins when present, so multi-tenant behaviour is unaffected.
|
|
68
|
-
// Enable only for genuinely single-tenant deployments.
|
|
69
|
-
singleTenant: { enabled: false, defaultTenantId: 'tenant-1' },
|
|
70
|
-
})
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
## Supported Wiring
|
|
74
|
-
|
|
75
|
-
Supported production order — `TenantGuard` reads the principal off `request.user`, so register the real guards explicitly in authentication → tenant → authorization order:
|
|
76
|
-
|
|
77
|
-
```
|
|
78
|
-
JwtAuthGuard (@nathapp/nestjs-auth)
|
|
79
|
-
→ TenantGuard (configured resolver + optional principal binding)
|
|
80
|
-
→ PermissionAuthGuard (@nathapp/nestjs-auth) + AccountCaslAbilityFactory (@nathapp/nestjs-account)
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
The resolver strategy controls where the request's tenant ID comes from; principal binding then checks membership in that resolved tenant. These are separate decisions:
|
|
84
|
-
|
|
85
|
-
- `HEADER` resolves `x-tenant-id` (or the configured header name). `require-match` rejects an authenticated principal that is not a member of that header-selected tenant.
|
|
86
|
-
- `JWT_CLAIM` resolves a claim on the authenticated principal: first `user.tenantId`, then `user.extra.tenantId` for claims nested by the core auth provider. It does not read the tenant header.
|
|
87
|
-
|
|
88
|
-
The real guard order, Fastify request path, and tenant AsyncLocalStorage are exercised by `packages/integration-e2e/test/tenant-principal-isolation.spec.ts` for both strategies.
|
|
89
|
-
|
|
90
|
-
### `guard.principalBinding`
|
|
91
|
-
|
|
92
|
-
| Mode | Behaviour |
|
|
93
|
-
|:---|:---|
|
|
94
|
-
| `off` (default) | No check; the binder is never called |
|
|
95
|
-
| `require-match` | 403 (`TENANT_PRINCIPAL_MISMATCH`) when the principal is not a member of the resolved tenant |
|
|
96
|
-
| `assert` | Dry run — warns on mismatch (and on binder error) and continues; never rejects |
|
|
97
|
-
|
|
98
|
-
There is deliberately **no default binder**. `TenantGuard` injects `TENANT_PRINCIPAL_BINDER` with `@Optional()`, so `require-match` without one would 403 every authenticated request — `TenantModule.register()` / `registerAsync()` throw instead. Supply one:
|
|
99
|
-
|
|
100
|
-
```typescript
|
|
101
|
-
TenantModule.register({
|
|
102
|
-
repository: MyTenantRepository, // REQUIRED — consumer's ITenantRepository class
|
|
103
|
-
resolution: { strategy: TenantResolutionStrategy.JWT_CLAIM },
|
|
104
|
-
principalBinder: MyTenantPrincipalBinder, // registered as TENANT_PRINCIPAL_BINDER (recommended)
|
|
105
|
-
guard: { principalBinding: { mode: 'require-match' } },
|
|
106
|
-
})
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
A binder reaching the token from another module needs `guard.principalBinding.binderProvided: true` — the module cannot verify it, and under `register()` (no `imports`) the providing module must be `@Global()` **and export** the token; merely providing it stays invisible. Prefer `principalBinder`: it is the only route the module can resolve for you, and the only such route under `register()`. Under `registerAsync` set `principalBinder` on `TenantModuleAsyncOptions`, not on the options the factory returns.
|
|
110
|
-
|
|
111
|
-
`ITenantPrincipalBinder` is declared here, deliberately not a re-export of `nestjs-iam`'s `ITenantMembershipResolver` — `nestjs-tenant` is the dependency floor and cannot depend on `nestjs-iam`, so the consumer supplies a bridging binder. Implement it as:
|
|
112
|
-
|
|
113
|
-
```typescript
|
|
114
|
-
interface ITenantPrincipalBinder {
|
|
115
|
-
assertMembership(principal: { id?: string }, tenantId: string): Promise<boolean>;
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
`principal` is typed `{ id?: string }` and not a broader record: a binder normally bridges `nestjs-iam`'s `IPrincipal`, and an index signature here would reject that (TypeScript grants implicit index signatures to object literal types, not to interfaces). The binder reads `principal.id`; look up anything else off the concrete principal type inside your own implementation.
|
|
120
|
-
|
|
121
|
-
The check runs after `setTenant` and is never cached, so a revoked membership takes effect immediately rather than after `validation.cacheTtl`. It is skipped when the request carries no principal, and skipped for `@SkipTenant()` handlers. Note the asymmetry on `guard.skipPaths`: a matching path is **not** an unbound request — resolution and lookup still run and a found tenant **is** still set on the context and `request.__tenant`; only this assertion is skipped.
|
|
122
|
-
|
|
123
|
-
### JWT tenant claim
|
|
124
|
-
|
|
125
|
-
`JwtClaimTenantResolver` reads `user[claimKey]` (default `'tenantId'`), then `user.extra[claimKey]`; a dotted `claimKey` such as `'extra.tenantId'` is walked against `user` instead. A function may be passed in place of the key (selector mode). `extra` is where core `SimpleAuthProvider` puts arbitrary claims — it maps a payload to `{ id, username, name, authorities, extra }`, so a top-level-only read resolved nothing for a core-issued JWT. A non-primitive claim resolves to `null`, not `"[object Object]"`.
|
|
126
|
-
|
|
127
|
-
The cross-tenant HTTP acceptance coverage lives in `packages/integration-e2e/test/tenant-principal-isolation.spec.ts`; the OAuth flow tests separately keep their existing `TenantContextService.run()` coverage.
|
|
128
|
-
|
|
129
|
-
## Testing
|
|
130
|
-
|
|
131
|
-
```bash
|
|
132
|
-
npm test # run tests
|
|
133
|
-
npm run test:cov # with coverage
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
## Gotchas
|
|
137
|
-
|
|
138
|
-
- `TenantContextService` uses `AsyncLocalStorage` — context is lost if you break the async chain (e.g., `setTimeout` without `AsyncResource`)
|
|
139
|
-
- `@SkipTenant()` on a handler skips `TenantGuard` resolution entirely
|
|
140
|
-
- `IMultiTenantPrincipal` extends `IPrincipal` from `@nathapp/nestjs-auth` — adds `tenantId` and `roles`
|
package/codex.md
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
# Codex Instructions
|
|
2
|
-
|
|
3
|
-
This file is auto-generated from `.nax/context.md`.
|
|
4
|
-
DO NOT EDIT MANUALLY — run `nax generate` to regenerate.
|
|
5
|
-
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Project Metadata
|
|
9
|
-
|
|
10
|
-
> Auto-injected by `nax generate`
|
|
11
|
-
|
|
12
|
-
**Project:** `@nathapp/nestjs-tenant`
|
|
13
|
-
|
|
14
|
-
**Language:** TypeScript
|
|
15
|
-
|
|
16
|
-
**Key dependencies:** typescript
|
|
17
|
-
|
|
18
|
-
**Commands:** test: `npm run test` | lint: `npm run lint` | typecheck: `npm run type-check`
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
# CLAUDE.md — @nathapp/nestjs-tenant
|
|
22
|
-
|
|
23
|
-
Multi-tenant context management for NestJS. Column-based tenancy with automatic request-scoped context.
|
|
24
|
-
|
|
25
|
-
## Status: ✅ Complete
|
|
26
|
-
|
|
27
|
-
## Key Files
|
|
28
|
-
|
|
29
|
-
- `lib/interface/` — `ITenant`, `ITenantRepository`, `ITenantService`, `ITenantResolver`, `ITenantContext`, `IMultiTenantPrincipal`, `ITenantPrincipalBinder`
|
|
30
|
-
- `lib/service/default-tenant.service.ts` — Default tenant CRUD with single-tenant mode support
|
|
31
|
-
- `lib/service/tenant-context.service.ts` — AsyncLocalStorage-based request-scoped tenant context
|
|
32
|
-
- `lib/resolver/` — 5 resolvers: `HeaderTenantResolver`, `JwtClaimTenantResolver`, `SubdomainTenantResolver`, `PathTenantResolver`, `DtoFieldTenantResolver`
|
|
33
|
-
- `lib/guard/tenant.guard.ts` — `TenantGuard` resolves + validates tenant per request, sets context
|
|
34
|
-
- `lib/decorator/` — `@CurrentTenant()`, `@SkipTenant()`, `@TenantId()` (DTO param)
|
|
35
|
-
- `lib/module/tenant.module.ts` — `TenantModule.register()` / `registerAsync()`
|
|
36
|
-
- `lib/constant/tokens.ts` — DI tokens: `TENANT_REPOSITORY`, `TENANT_SERVICE`, `TENANT_RESOLVER`, `TENANT_CONTEXT`, `TENANT_MODULE_OPTIONS`, `TENANT_CACHE`, `TENANT_PRINCIPAL_BINDER`
|
|
37
|
-
|
|
38
|
-
## DI Tokens
|
|
39
|
-
|
|
40
|
-
| Token | Interface | Default |
|
|
41
|
-
|:---|:---|:---|
|
|
42
|
-
| `TENANT_REPOSITORY` | `ITenantRepository` | None (consumer must provide) |
|
|
43
|
-
| `TENANT_SERVICE` | `ITenantService` | `DefaultTenantService` |
|
|
44
|
-
| `TENANT_RESOLVER` | `ITenantResolver` | Configured via module options |
|
|
45
|
-
| `TENANT_CONTEXT` | `ITenantContext` | `TenantContextService` |
|
|
46
|
-
| `TENANT_CACHE` | `ITenantCache` | `InMemoryTenantCache` |
|
|
47
|
-
| `TENANT_PRINCIPAL_BINDER` | `ITenantPrincipalBinder` | None (consumer must provide) |
|
|
48
|
-
|
|
49
|
-
## Module Registration
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
TenantModule.register({
|
|
53
|
-
repository: MyTenantRepository, // REQUIRED — consumer's ITenantRepository class
|
|
54
|
-
resolution: { strategy: TenantResolutionStrategy.HEADER, headerName: 'x-tenant-id' },
|
|
55
|
-
// Single-tenant mode (optional, off by default — set enabled: true to arm it).
|
|
56
|
-
// Armed, TenantGuard falls back to defaultTenantId when the configured resolver
|
|
57
|
-
// finds no tenant identifier on the request. headerName is only read by the
|
|
58
|
-
// HEADER strategy; JWT_CLAIM reads claimKey, SUBDOMAIN reads the host, PATH reads
|
|
59
|
-
// pathPrefix, DTO_FIELD reads dtoField, and CUSTOM takes a resolver class.
|
|
60
|
-
//
|
|
61
|
-
// defaultTenantId MUST be a real, existing tenant id. It is not validated at
|
|
62
|
-
// registration: a falsy value is returned like any other resolution result and
|
|
63
|
-
// fails the guard's `if (!resolved)` check, so outside a guard.skipPaths match
|
|
64
|
-
// it rejects with the normal 400 (RESOLUTION_FAILED) — 403 under
|
|
65
|
-
// onTenantMissing: 'forbidden' — while a skip path is allowed unbound instead.
|
|
66
|
-
// The fallback is only "fail-open" when the id is real. Header/subdomain
|
|
67
|
-
// resolution still wins when present, so multi-tenant behaviour is unaffected.
|
|
68
|
-
// Enable only for genuinely single-tenant deployments.
|
|
69
|
-
singleTenant: { enabled: false, defaultTenantId: 'tenant-1' },
|
|
70
|
-
})
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
## Supported Wiring
|
|
74
|
-
|
|
75
|
-
Supported production order — `TenantGuard` reads the principal off `request.user`, so register the real guards explicitly in authentication → tenant → authorization order:
|
|
76
|
-
|
|
77
|
-
```
|
|
78
|
-
JwtAuthGuard (@nathapp/nestjs-auth)
|
|
79
|
-
→ TenantGuard (configured resolver + optional principal binding)
|
|
80
|
-
→ PermissionAuthGuard (@nathapp/nestjs-auth) + AccountCaslAbilityFactory (@nathapp/nestjs-account)
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
The resolver strategy controls where the request's tenant ID comes from; principal binding then checks membership in that resolved tenant. These are separate decisions:
|
|
84
|
-
|
|
85
|
-
- `HEADER` resolves `x-tenant-id` (or the configured header name). `require-match` rejects an authenticated principal that is not a member of that header-selected tenant.
|
|
86
|
-
- `JWT_CLAIM` resolves a claim on the authenticated principal: first `user.tenantId`, then `user.extra.tenantId` for claims nested by the core auth provider. It does not read the tenant header.
|
|
87
|
-
|
|
88
|
-
The real guard order, Fastify request path, and tenant AsyncLocalStorage are exercised by `packages/integration-e2e/test/tenant-principal-isolation.spec.ts` for both strategies.
|
|
89
|
-
|
|
90
|
-
### `guard.principalBinding`
|
|
91
|
-
|
|
92
|
-
| Mode | Behaviour |
|
|
93
|
-
|:---|:---|
|
|
94
|
-
| `off` (default) | No check; the binder is never called |
|
|
95
|
-
| `require-match` | 403 (`TENANT_PRINCIPAL_MISMATCH`) when the principal is not a member of the resolved tenant |
|
|
96
|
-
| `assert` | Dry run — warns on mismatch (and on binder error) and continues; never rejects |
|
|
97
|
-
|
|
98
|
-
There is deliberately **no default binder**. `TenantGuard` injects `TENANT_PRINCIPAL_BINDER` with `@Optional()`, so `require-match` without one would 403 every authenticated request — `TenantModule.register()` / `registerAsync()` throw instead. Supply one:
|
|
99
|
-
|
|
100
|
-
```typescript
|
|
101
|
-
TenantModule.register({
|
|
102
|
-
repository: MyTenantRepository, // REQUIRED — consumer's ITenantRepository class
|
|
103
|
-
resolution: { strategy: TenantResolutionStrategy.JWT_CLAIM },
|
|
104
|
-
principalBinder: MyTenantPrincipalBinder, // registered as TENANT_PRINCIPAL_BINDER (recommended)
|
|
105
|
-
guard: { principalBinding: { mode: 'require-match' } },
|
|
106
|
-
})
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
A binder reaching the token from another module needs `guard.principalBinding.binderProvided: true` — the module cannot verify it, and under `register()` (no `imports`) the providing module must be `@Global()` **and export** the token; merely providing it stays invisible. Prefer `principalBinder`: it is the only route the module can resolve for you, and the only such route under `register()`. Under `registerAsync` set `principalBinder` on `TenantModuleAsyncOptions`, not on the options the factory returns.
|
|
110
|
-
|
|
111
|
-
`ITenantPrincipalBinder` is declared here, deliberately not a re-export of `nestjs-iam`'s `ITenantMembershipResolver` — `nestjs-tenant` is the dependency floor and cannot depend on `nestjs-iam`, so the consumer supplies a bridging binder. Implement it as:
|
|
112
|
-
|
|
113
|
-
```typescript
|
|
114
|
-
interface ITenantPrincipalBinder {
|
|
115
|
-
assertMembership(principal: { id?: string }, tenantId: string): Promise<boolean>;
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
`principal` is typed `{ id?: string }` and not a broader record: a binder normally bridges `nestjs-iam`'s `IPrincipal`, and an index signature here would reject that (TypeScript grants implicit index signatures to object literal types, not to interfaces). The binder reads `principal.id`; look up anything else off the concrete principal type inside your own implementation.
|
|
120
|
-
|
|
121
|
-
The check runs after `setTenant` and is never cached, so a revoked membership takes effect immediately rather than after `validation.cacheTtl`. It is skipped when the request carries no principal, and skipped for `@SkipTenant()` handlers. Note the asymmetry on `guard.skipPaths`: a matching path is **not** an unbound request — resolution and lookup still run and a found tenant **is** still set on the context and `request.__tenant`; only this assertion is skipped.
|
|
122
|
-
|
|
123
|
-
### JWT tenant claim
|
|
124
|
-
|
|
125
|
-
`JwtClaimTenantResolver` reads `user[claimKey]` (default `'tenantId'`), then `user.extra[claimKey]`; a dotted `claimKey` such as `'extra.tenantId'` is walked against `user` instead. A function may be passed in place of the key (selector mode). `extra` is where core `SimpleAuthProvider` puts arbitrary claims — it maps a payload to `{ id, username, name, authorities, extra }`, so a top-level-only read resolved nothing for a core-issued JWT. A non-primitive claim resolves to `null`, not `"[object Object]"`.
|
|
126
|
-
|
|
127
|
-
The cross-tenant HTTP acceptance coverage lives in `packages/integration-e2e/test/tenant-principal-isolation.spec.ts`; the OAuth flow tests separately keep their existing `TenantContextService.run()` coverage.
|
|
128
|
-
|
|
129
|
-
## Testing
|
|
130
|
-
|
|
131
|
-
```bash
|
|
132
|
-
npm test # run tests
|
|
133
|
-
npm run test:cov # with coverage
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
## Gotchas
|
|
137
|
-
|
|
138
|
-
- `TenantContextService` uses `AsyncLocalStorage` — context is lost if you break the async chain (e.g., `setTimeout` without `AsyncResource`)
|
|
139
|
-
- `@SkipTenant()` on a handler skips `TenantGuard` resolution entirely
|
|
140
|
-
- `IMultiTenantPrincipal` extends `IPrincipal` from `@nathapp/nestjs-auth` — adds `tenantId` and `roles`
|