@lenne.tech/nest-server 11.41.9 → 11.42.1

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 (67) hide show
  1. package/.claude/rules/configurable-features.md +4 -3
  2. package/.claude/rules/role-system.md +31 -0
  3. package/.claude/rules/testing.md +4 -4
  4. package/CLAUDE.md +6 -0
  5. package/FRAMEWORK-API.md +4 -2
  6. package/dist/core/common/helpers/graphql-ws-context.helper.js +1 -1
  7. package/dist/core/common/helpers/graphql-ws-context.helper.js.map +1 -1
  8. package/dist/core/common/helpers/outbound-url.helper.d.ts +14 -0
  9. package/dist/core/common/helpers/outbound-url.helper.js +234 -0
  10. package/dist/core/common/helpers/outbound-url.helper.js.map +1 -0
  11. package/dist/core/common/interfaces/server-options.interface.d.ts +6 -0
  12. package/dist/core/common/middleware/request-context.middleware.js +1 -0
  13. package/dist/core/common/middleware/request-context.middleware.js.map +1 -1
  14. package/dist/core/common/services/request-context.service.d.ts +1 -0
  15. package/dist/core/common/services/request-context.service.js.map +1 -1
  16. package/dist/core/modules/ai/core-ai-mcp.controller.js +5 -2
  17. package/dist/core/modules/ai/core-ai-mcp.controller.js.map +1 -1
  18. package/dist/core/modules/ai/services/core-ai-mcp.service.d.ts +1 -1
  19. package/dist/core/modules/ai/services/core-ai-mcp.service.js +3 -2
  20. package/dist/core/modules/ai/services/core-ai-mcp.service.js.map +1 -1
  21. package/dist/core/modules/ai/tools/ai-tool.registry.js +15 -2
  22. package/dist/core/modules/ai/tools/ai-tool.registry.js.map +1 -1
  23. package/dist/core/modules/tenant/core-tenant-role.helper.d.ts +2 -0
  24. package/dist/core/modules/tenant/core-tenant-role.helper.js +36 -0
  25. package/dist/core/modules/tenant/core-tenant-role.helper.js.map +1 -0
  26. package/dist/core/modules/tenant/core-tenant.guard.d.ts +7 -1
  27. package/dist/core/modules/tenant/core-tenant.guard.js +56 -4
  28. package/dist/core/modules/tenant/core-tenant.guard.js.map +1 -1
  29. package/dist/core/modules/tus/core-tus.service.d.ts +1 -0
  30. package/dist/core/modules/tus/core-tus.service.js +29 -0
  31. package/dist/core/modules/tus/core-tus.service.js.map +1 -1
  32. package/dist/core/modules/tus/interfaces/tus-config.interface.d.ts +2 -1
  33. package/dist/core/modules/tus/interfaces/tus-config.interface.js +1 -0
  34. package/dist/core/modules/tus/interfaces/tus-config.interface.js.map +1 -1
  35. package/dist/core/modules/user/core-user.service.d.ts +1 -0
  36. package/dist/core/modules/user/core-user.service.js +24 -0
  37. package/dist/core/modules/user/core-user.service.js.map +1 -1
  38. package/dist/index.d.ts +2 -0
  39. package/dist/index.js +2 -0
  40. package/dist/index.js.map +1 -1
  41. package/dist/tsconfig.build.tsbuildinfo +1 -1
  42. package/docs/REQUEST-LIFECYCLE.md +4 -2
  43. package/migration-guides/11.41.8-to-11.41.9.md +5 -0
  44. package/migration-guides/11.41.x-to-11.42.x.md +161 -0
  45. package/migration-guides/11.42.0-to-11.42.1.md +66 -0
  46. package/migration-guides/11.9.x-to-11.10.x.md +3 -2
  47. package/package.json +8 -7
  48. package/src/core/common/helpers/graphql-ws-context.helper.ts +1 -1
  49. package/src/core/common/helpers/outbound-url.helper.ts +310 -0
  50. package/src/core/common/interfaces/server-options.interface.ts +71 -0
  51. package/src/core/common/middleware/request-context.middleware.ts +1 -0
  52. package/src/core/common/services/request-context.service.ts +9 -0
  53. package/src/core/modules/ai/README.md +25 -0
  54. package/src/core/modules/ai/core-ai-mcp.controller.ts +22 -3
  55. package/src/core/modules/ai/services/core-ai-mcp.service.ts +9 -3
  56. package/src/core/modules/ai/tools/ai-tool.registry.ts +33 -3
  57. package/src/core/modules/better-auth/INTEGRATION-CHECKLIST.md +18 -11
  58. package/src/core/modules/tenant/INTEGRATION-CHECKLIST.md +14 -0
  59. package/src/core/modules/tenant/README.md +80 -8
  60. package/src/core/modules/tenant/core-tenant-role.helper.ts +75 -0
  61. package/src/core/modules/tenant/core-tenant.guard.ts +78 -5
  62. package/src/core/modules/tus/INTEGRATION-CHECKLIST.md +3 -1
  63. package/src/core/modules/tus/README.md +20 -0
  64. package/src/core/modules/tus/core-tus.service.ts +45 -0
  65. package/src/core/modules/tus/interfaces/tus-config.interface.ts +4 -1
  66. package/src/core/modules/user/core-user.service.ts +53 -0
  67. package/src/index.ts +2 -0
@@ -10,6 +10,7 @@ import type { MongoosePingCheckSettings } from '@nestjs/terminus/dist/health-ind
10
10
  import type { DiskHealthIndicatorOptions } from '@nestjs/terminus/dist/health-indicator/disk/disk-health-options.type.js';
11
11
  import compression from 'compression';
12
12
  import { CollationOptions } from 'mongodb';
13
+ import type { Connection } from 'mongoose';
13
14
  import type * as JSONTransport from 'nodemailer/lib/json-transport';
14
15
  import type * as SendmailTransport from 'nodemailer/lib/sendmail-transport';
15
16
  import type * as SESTransport from 'nodemailer/lib/ses-transport';
@@ -1477,6 +1478,43 @@ export interface IMultiTenancy {
1477
1478
  * @since 11.21.1
1478
1479
  */
1479
1480
  cacheTtlMs?: number;
1481
+
1482
+ /**
1483
+ * Whether a tenant may be used at all — the hook that makes a DEACTIVATED tenant stop working.
1484
+ *
1485
+ * The tenant guard validates a membership and nothing else, so without this a tenant a project
1486
+ * switched off keeps working for every member, every API token and every request without a tenant
1487
+ * header. Asked after the membership check, on every path that establishes a tenant:
1488
+ *
1489
+ * - header paths (tenant roles, `S_USER` / `S_VERIFIED` with a header): **403**
1490
+ * - tenant API tokens: **403**
1491
+ * - public (`S_EVERYONE`) routes: not blocked, but no tenant context is set
1492
+ * - no header: the tenant is dropped from the user's tenant list, so its data stays out of reach
1493
+ * - GraphQL over WebSocket: no tenant is resolved for a NEW subscription; one already open keeps
1494
+ * its tenant until the client reconnects (as it does after a removed membership)
1495
+ *
1496
+ * A platform administrator under `adminBypass` still reaches the tenant — somebody has to be able to
1497
+ * look into it and switch it back on.
1498
+ *
1499
+ * Return `false` for an inactive tenant; any other value (including `undefined`) counts as active.
1500
+ * `context.connection` is the main database connection, for a lookup in the project's own tenant
1501
+ * collection. Answers are cached for {@link cacheTtlMs}; after switching a tenant on or off, call
1502
+ * `CoreTenantGuard.invalidateTenant(tenantId)` (broadcast to every replica when Redis is
1503
+ * configured). A function cannot arrive through `NSC__*` / `NEST_SERVER_CONFIG` — set it in
1504
+ * `config.env.ts`.
1505
+ *
1506
+ * @example
1507
+ * ```typescript
1508
+ * multiTenancy: {
1509
+ * isTenantActive: async (tenantId, { connection }) =>
1510
+ * (await connection.collection('tenants').findOne({ _id: new Types.ObjectId(tenantId) }))?.active !== false,
1511
+ * }
1512
+ * ```
1513
+ *
1514
+ * @default undefined (every tenant is active)
1515
+ * @since 11.42.0
1516
+ */
1517
+ isTenantActive?: (tenantId: string, context: { connection: Connection }) => boolean | Promise<boolean>;
1480
1518
  }
1481
1519
 
1482
1520
  /**
@@ -2855,6 +2893,22 @@ export interface IServerOptions {
2855
2893
  * Configuration for security pipes and interceptors
2856
2894
  */
2857
2895
  security?: {
2896
+ /**
2897
+ * Whether the outbound URL guard (`outboundFetch`, `assertOutboundUrlAllowed`,
2898
+ * `createOutboundDispatcher`) may reach INTERNAL addresses — loopback, private networks,
2899
+ * link-local incl. the cloud metadata service.
2900
+ *
2901
+ * The guard is for URLs a USER entered (webhooks, export targets); configured targets do not use
2902
+ * it. Unset, internal targets are allowed in the `local`, `e2e` and `ci` environments and
2903
+ * refused everywhere else. `true` / `false` decide it explicitly
2904
+ * (`NSC__SECURITY__ALLOW_PRIVATE_OUTBOUND_TARGETS`). To free a single internal host, pass
2905
+ * `allowedHosts` to the call instead of opening all of them.
2906
+ *
2907
+ * @default undefined (allowed only in local, e2e and ci — not in `test`, a deployed stage in starter-based projects)
2908
+ * @since 11.42.0
2909
+ */
2910
+ allowPrivateOutboundTargets?: boolean;
2911
+
2858
2912
  /**
2859
2913
  * Check restrictions for output (models and output objects)
2860
2914
  * See @lenne.tech/nest-server/src/core/common/interceptors/check-response.interceptor.ts
@@ -3552,6 +3606,23 @@ export interface ITusConfig {
3552
3606
  */
3553
3607
  allowedHeaders?: string[];
3554
3608
 
3609
+ /**
3610
+ * Origins the tus handler may answer a cross-origin request with (`Access-Control-Allow-Origin`).
3611
+ *
3612
+ * Unset, they come from the API's own CORS configuration — `buildCorsConfig()`, the function REST,
3613
+ * GraphQL and Better-Auth share (`appUrl`, `baseUrl`, `cors.allowedOrigins`, `cors.allowAll`).
3614
+ * Without credentialed CORS configured (cookies off, or no origin resolvable) the tus server keeps
3615
+ * its own default, `*`.
3616
+ *
3617
+ * The wildcard is what broke browser uploads with cookie authentication and separate app/API
3618
+ * origins before 11.42.1: a credentialed response with `Access-Control-Allow-Origin: *` is refused
3619
+ * by every browser, although the server answered 201.
3620
+ *
3621
+ * @default undefined (derived from the API CORS configuration)
3622
+ * @since 11.42.1
3623
+ */
3624
+ allowedOrigins?: string[];
3625
+
3555
3626
  /**
3556
3627
  * Allowed MIME types for uploads.
3557
3628
  * If undefined, all types are allowed.
@@ -33,6 +33,7 @@ export class RequestContextMiddleware implements NestMiddleware {
33
33
  get isAdminBypass() {
34
34
  return (req as any).isAdminBypass ?? false;
35
35
  },
36
+ fromRequest: true,
36
37
  };
37
38
  RequestContext.run(context, () => next());
38
39
  }
@@ -19,6 +19,15 @@ export interface IRequestContext {
19
19
  tenantRole?: string;
20
20
  /** When true, indicates admin bypass is active (admin without header sees all data) */
21
21
  isAdminBypass?: boolean;
22
+ /**
23
+ * Set by `RequestContextMiddleware` (HTTP) and the GraphQL WebSocket context builder: this context
24
+ * belongs to a CLIENT operation, signed in or not. Absent for work the server does on its own —
25
+ * cron jobs, migrations, seeds, a `runWithBypass…()` outside a request.
26
+ *
27
+ * It is what tells an anonymous request apart from system work: both have no current user, and a
28
+ * check that exempts system work must not exempt the anonymous caller of a public route with it.
29
+ */
30
+ fromRequest?: boolean;
22
31
  /**
23
32
  * Tracks the nesting depth of process() calls.
24
33
  * 0 = outermost call (full pipeline), > 0 = nested call (reduced pipeline).
@@ -600,6 +600,31 @@ ai: {
600
600
 
601
601
  - tokens, limits, reset) is available via the `aiUsage` query / `GET /ai/usage`.
602
602
 
603
+ ## Tool access is the user's access
604
+
605
+ An AI client — the chat's agent loop or an MCP client — acts **on behalf of the signed-in user and
606
+ never beyond that user's rights**. Three things make that hold, and a project's tools must not undo
607
+ them:
608
+
609
+ - **Visibility and execution** are decided by `AiToolRegistry.userCanAccess()`, evaluated per request —
610
+ over MCP with the user as of THAT request, not as of the session's `initialize` (11.42.0+), so a
611
+ revoked role stops working without a reconnect.
612
+ Under multi-tenancy (11.42.0+) it resolves a tool's `roles` like `CoreTenantGuard` resolves
613
+ `@Roles()`: a global role (`RoleEnum.ADMIN`, `multiTenancy.globalOnlyRoles`) against `user.roles`, a
614
+ tenant role against the **membership role in the request's tenant** (by hierarchy), and the ADMIN
615
+ bypass only where `multiTenancy.adminBypass` grants it. Before, every role was compared with the
616
+ global `user.roles` — a tool requiring the tenant role `owner` was offered to nobody who held it as
617
+ a membership, and to anyone holding a global role of that name.
618
+ - **Data** is scoped by the request context: a tool runs inside the HTTP request (chat or `/ai/mcp`),
619
+ so `mongooseTenantPlugin` filters by the tenant the guard validated. Without a tenant header there
620
+ is no tenant scope on these routes and tenant-scoped reads are refused (403) — an AI client is
621
+ never broader than its user, at worst narrower.
622
+ - **Service-level rules** apply on every entry point when the service enforces them itself —
623
+ `assertTenantRole()` (see the tenant module README) — instead of relying on a controller's
624
+ `@Roles()`, which a tool calling the service never passes through.
625
+
626
+ Without multi-tenancy, tool roles are compared with `user.roles` exactly as before.
627
+
603
628
  ## MCP server (`ai.mcp: true`)
604
629
 
605
630
  The `AiToolRegistry` also feeds a real **MCP server** at `POST/GET/DELETE /ai/mcp`
@@ -11,6 +11,7 @@ import { CoreBetterAuthModule } from '../better-auth/core-better-auth.module';
11
11
  import { ErrorCode } from '../error-code/error-codes';
12
12
  import { CoreAiMcpOAuthService } from './services/core-ai-mcp-oauth.service';
13
13
  import { CoreAiMcpService } from './services/core-ai-mcp.service';
14
+ import { AiToolUser } from './tools/ai-tool.registry';
14
15
 
15
16
  /**
16
17
  * MCP Streamable-HTTP endpoint at `/ai/mcp`.
@@ -49,6 +50,18 @@ import { CoreAiMcpService } from './services/core-ai-mcp.service';
49
50
  * ids would learn both that a session id is valid and the internal hostname/PID holding it,
50
51
  * where the same probe against the local map deliberately answers 404.
51
52
  */
53
+ /**
54
+ * One MCP session: its transport, its owner, and the owner AS OF THE LATEST REQUEST. `user` is
55
+ * refreshed on every request so the server — which reads it through a getter — always acts with the
56
+ * user's current rights rather than those from `initialize`.
57
+ */
58
+ interface McpSessionEntry {
59
+ lastUsed: number;
60
+ ownerId: string;
61
+ transport: any;
62
+ user: AiToolUser;
63
+ }
64
+
52
65
  @ApiExcludeController()
53
66
  @Controller('ai/mcp')
54
67
  @Roles(RoleEnum.S_EVERYONE)
@@ -65,7 +78,7 @@ export class CoreAiMcpController implements OnModuleDestroy {
65
78
  * anyone holding another user's session id drives that user's server — the id travels in a
66
79
  * response header, through proxies and client logs.
67
80
  */
68
- private readonly transports = new Map<string, { lastUsed: number; ownerId: string; transport: any }>();
81
+ private readonly transports = new Map<string, McpSessionEntry>();
69
82
 
70
83
  /**
71
84
  * Cap on concurrent MCP sessions held by ONE user.
@@ -160,7 +173,10 @@ export class CoreAiMcpController implements OnModuleDestroy {
160
173
  }
161
174
  const { randomUUID } = await import('node:crypto');
162
175
  const transport: any = new StreamableHTTPServerTransport({ sessionIdGenerator: () => randomUUID() });
163
- const server = await this.mcpService.createServer(user);
176
+ // The server reads the user through a getter on every request; `user` below is refreshed by
177
+ // each request of the session, so a role revoked mid-session takes effect at once.
178
+ const created: McpSessionEntry = { lastUsed: Date.now(), ownerId: user.id, transport, user };
179
+ const server = await this.mcpService.createServer(() => created.user);
164
180
  await server.connect(transport);
165
181
  // The MCP SDK transport exposes `onclose` as a callback property (not a DOM
166
182
  // EventTarget), so addEventListener does not apply here.
@@ -171,10 +187,12 @@ export class CoreAiMcpController implements OnModuleDestroy {
171
187
  this.releaseSession(transport.sessionId);
172
188
  }
173
189
  };
174
- entry = { lastUsed: Date.now(), ownerId: user.id, transport };
190
+ entry = created;
175
191
  }
176
192
 
177
193
  entry.lastUsed = Date.now();
194
+ // Same owner (checked above), possibly different rights than at `initialize`.
195
+ entry.user = user;
178
196
  await entry.transport.handleRequest(req, res, req.body);
179
197
 
180
198
  // The sessionId is assigned during handleRequest (initialize); register after.
@@ -280,6 +298,7 @@ export class CoreAiMcpController implements OnModuleDestroy {
280
298
  return;
281
299
  }
282
300
  entry.lastUsed = Date.now();
301
+ entry.user = user;
283
302
  this.registerSession(sessionId as string, user.id);
284
303
  await entry.transport.handleRequest(req, res, (req as any).body);
285
304
  }
@@ -76,17 +76,23 @@ export class CoreAiMcpService {
76
76
  /**
77
77
  * Create a low-level MCP server bound to a user. Uses the JSON-schema tool
78
78
  * definitions directly (no zod conversion needed). The SDK is imported lazily.
79
+ *
80
+ * Pass a GETTER rather than a user object: an MCP session stays open for hours, and every
81
+ * `tools/list` / `tools/call` must use the user's CURRENT rights — a role revoked after
82
+ * `initialize` must not survive in the session. `CoreAiMcpController` passes a getter it refreshes
83
+ * on every request (11.42.0+). A plain object is still accepted and is then fixed for the session.
79
84
  */
80
- async createServer(user: AiToolUser): Promise<any> {
85
+ async createServer(user: AiToolUser | (() => AiToolUser)): Promise<any> {
86
+ const currentUser = (): AiToolUser => (typeof user === 'function' ? user() : user);
81
87
  const { Server } = await import('@modelcontextprotocol/sdk/server/index.js');
82
88
  const { CallToolRequestSchema, ListToolsRequestSchema } = await import('@modelcontextprotocol/sdk/types.js');
83
89
 
84
90
  // Loosely typed to decouple from the SDK's strict request/result types.
85
91
  const server: any = new Server({ name: 'lt-nest-server-ai', version: '1.0.0' }, { capabilities: { tools: {} } });
86
92
 
87
- server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: this.mcpListTools(user) }));
93
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: this.mcpListTools(currentUser()) }));
88
94
  server.setRequestHandler(CallToolRequestSchema, async (request: any) =>
89
- this.mcpCallTool(user, request.params.name, request.params.arguments ?? {}),
95
+ this.mcpCallTool(currentUser(), request.params.name, request.params.arguments ?? {}),
90
96
  );
91
97
 
92
98
  return server;
@@ -1,6 +1,14 @@
1
1
  import { Injectable, Logger } from '@nestjs/common';
2
2
 
3
3
  import { RoleEnum } from '../../../common/enums/role.enum';
4
+ import { ConfigService } from '../../../common/services/config.service';
5
+ import { RequestContext } from '../../../common/services/request-context.service';
6
+ import {
7
+ checkRoleAccess,
8
+ isMultiTenancyActive,
9
+ resolveGlobalAndTenantRoles,
10
+ tenantSatisfiableRoles,
11
+ } from '../../tenant/core-tenant.helpers';
4
12
  import { IAiTool } from '../interfaces/ai-tool.interface';
5
13
 
6
14
  /**
@@ -75,6 +83,13 @@ export class AiToolRegistry {
75
83
 
76
84
  /**
77
85
  * Whether a user satisfies a tool's role requirements.
86
+ *
87
+ * The answer is what an AI client — the MCP server or the chat's agent loop — may do on the user's
88
+ * behalf, so it must not exceed what the user may do through the API. Under multi-tenancy each role
89
+ * is therefore resolved like `CoreTenantGuard` resolves a `@Roles()`: a global role against
90
+ * `user.roles`, a tenant role against the membership role of the current request's tenant (by
91
+ * hierarchy), and the ADMIN bypass only where `multiTenancy.adminBypass` grants it. Without
92
+ * multi-tenancy, roles are compared with `user.roles` as they always were.
78
93
  */
79
94
  userCanAccess(tool: IAiTool, user: AiToolUser | null | undefined): boolean {
80
95
  const roles = tool.roles ?? [];
@@ -94,8 +109,11 @@ export class AiToolRegistry {
94
109
  return false;
95
110
  }
96
111
 
97
- // Admin bypass.
98
- if (user.roles?.includes(RoleEnum.ADMIN)) {
112
+ const multiTenancy = isMultiTenancyActive();
113
+
114
+ // Admin bypass — unless multi-tenancy withholds it (adminBypass: false).
115
+ const adminBypass = !multiTenancy || ConfigService.configFastButReadOnly?.multiTenancy?.adminBypass !== false;
116
+ if (adminBypass && user.roles?.includes(RoleEnum.ADMIN)) {
99
117
  return true;
100
118
  }
101
119
 
@@ -119,6 +137,18 @@ export class AiToolRegistry {
119
137
  RoleEnum.S_VERIFIED,
120
138
  ];
121
139
  const requiredRealRoles = roles.filter((r) => !systemRoles.includes(r));
122
- return requiredRealRoles.some((r) => user.roles?.includes(r));
140
+ if (!multiTenancy) {
141
+ return requiredRealRoles.some((r) => user.roles?.includes(r));
142
+ }
143
+
144
+ // Multi-tenancy: each role answered by the source entitled to answer it.
145
+ const { global } = resolveGlobalAndTenantRoles(requiredRealRoles);
146
+ if (global.some((r) => user.roles?.includes(r))) {
147
+ return true;
148
+ }
149
+ // In a tenant, only the membership role counts; outside one, user.roles — as in the guard.
150
+ // The length guard matters: checkRoleAccess() answers TRUE for an empty required list.
151
+ const tenantRoles = tenantSatisfiableRoles(requiredRealRoles);
152
+ return tenantRoles.length > 0 && checkRoleAccess(tenantRoles, user.roles, RequestContext.get()?.tenantRole);
123
153
  }
124
154
  }
@@ -494,7 +494,12 @@ See: `.claude/rules/module-inheritance.md` for the full pattern.
494
494
 
495
495
  ## Client-Side Configuration
496
496
 
497
- Clients must be configured to use the correct base path and hash passwords:
497
+ Clients must be configured to use the correct base path and hash passwords.
498
+
499
+ > **Nuxt projects on `@lenne.tech/nuxt-extensions`** get all of this preconfigured: `useLtAuthClient()`
500
+ > is the Better-Auth client (base path, password hashing, plugins), and `useLtAuth()` holds the auth
501
+ > state plus the JWT-safe passkey helpers used below. The hand-written client that follows is for
502
+ > clients outside that stack.
498
503
 
499
504
  ```typescript
500
505
  // auth-client.ts (e.g., for Nuxt/Vue)
@@ -622,14 +627,14 @@ async function useBackupCode(code: string) {
622
627
 
623
628
  Handle passkey authentication with session validation fallback.
624
629
 
625
- **IMPORTANT:** For JWT mode (`cookies: false`), you MUST use the `authenticateWithPasskey()` function from the composable instead of `authClient.signIn.passkey()` directly. This is because JWT mode requires sending a `challengeId` to the server for challenge verification.
630
+ **IMPORTANT:** For JWT mode (`cookies: false`), you MUST use the `authenticateWithPasskey()` function from `useLtAuth()` instead of `authClient.signIn.passkey()` directly. This is because JWT mode requires sending a `challengeId` to the server for challenge verification.
626
631
 
627
632
  **Note on Passkey Login Response (v11.13.0+):** The server enriches the passkey verify-authentication response with user data. Better Auth's passkey plugin only returns `{ session }`, but the server fetches the user from the database and adds it to the response. The composable handles both scenarios (user in response vs. fallback to get-session).
628
633
 
629
634
  ```typescript
630
635
  // login.vue - Passkey login (JWT-compatible)
631
- // Use authenticateWithPasskey from useBetterAuth composable
632
- const { authenticateWithPasskey, setUser, validateSession } = useBetterAuth();
636
+ // authenticateWithPasskey from useLtAuth() (@lenne.tech/nuxt-extensions)
637
+ const { authenticateWithPasskey, setUser, validateSession } = useLtAuth();
633
638
 
634
639
  async function onPasskeyLogin() {
635
640
  try {
@@ -677,11 +682,13 @@ In JWT mode (`cookies: false`), the server stores WebAuthn challenges in the dat
677
682
 
678
683
  Register a new passkey for an authenticated user.
679
684
 
680
- **IMPORTANT:** For JWT mode (`cookies: false`), you MUST use the `registerPasskey()` function from the composable. This ensures the `challengeId` is correctly sent to the server.
685
+ **IMPORTANT:** For JWT mode (`cookies: false`), you MUST use the `registerPasskey()` function from `useLtAuth()`. This ensures the `challengeId` is correctly sent to the server.
681
686
 
682
687
  ```typescript
683
688
  // settings.vue - Register new passkey (JWT-compatible)
684
- const { registerPasskey, listPasskeys, deletePasskey } = useBetterAuth();
689
+ // registerPasskey from useLtAuth(); listing and deleting go through the Better-Auth client
690
+ const { registerPasskey } = useLtAuth();
691
+ const authClient = useLtAuthClient();
685
692
 
686
693
  async function onRegisterPasskey() {
687
694
  try {
@@ -707,16 +714,16 @@ async function onRegisterPasskey() {
707
714
 
708
715
  // List user's passkeys (works in both modes)
709
716
  async function loadPasskeys() {
710
- const result = await listPasskeys();
711
- if (result.success) {
712
- passkeys.value = result.passkeys || [];
717
+ const { data, error } = await authClient.passkey.listUserPasskeys();
718
+ if (!error) {
719
+ passkeys.value = data ?? [];
713
720
  }
714
721
  }
715
722
 
716
723
  // Delete a passkey (works in both modes)
717
724
  async function onDeletePasskey(passkeyId: string) {
718
- const result = await deletePasskey(passkeyId);
719
- if (result.success) {
725
+ const { error } = await authClient.passkey.deletePasskey({ id: passkeyId });
726
+ if (!error) {
720
727
  await loadPasskeys();
721
728
  }
722
729
  }
@@ -161,6 +161,20 @@ betterAuth: {
161
161
 
162
162
  See [Tenant README — BetterAuth Integration](./README.md#betterauth-iam-integration) for details.
163
163
 
164
+ ### 11. Deactivated Tenants and Service-Level Roles (since 11.42.0)
165
+
166
+ - **Can a tenant be switched off in your project?** Set `multiTenancy.isTenantActive` in
167
+ `config.env.ts` and call `tenantGuard.invalidateTenant(tenantId)` after switching. WHY: the guard
168
+ checks the membership only; without the hook a deactivated tenant keeps working for its members,
169
+ its API tokens and every request without a tenant header.
170
+ - **Does a service enforce a tenant role that a controller also demands?** Call
171
+ `assertTenantRole(...)` in the service. WHY: MCP and AI tools call the service directly and never
172
+ pass the controller's `@Roles()`.
173
+ - **Running with `adminBypass: false`?** Platform administrators can no longer set a tenant account's
174
+ e-mail address or password through `updateUser`; support goes through the password-reset flow.
175
+
176
+ See the module README: "Deactivated Tenants" and "Service-Level Role Checks".
177
+
164
178
  ## Verification Checklist
165
179
 
166
180
  - [ ] `pnpm run build` succeeds
@@ -211,15 +211,87 @@ It also suppresses tenant membership validation for `S_USER`/`S_VERIFIED` system
211
211
  System admins (`RoleEnum.ADMIN`) bypass the membership check by default.
212
212
  Disable with `multiTenancy: { adminBypass: false }`.
213
213
 
214
+ `adminBypass: false` promises that a platform administrator has no way into a tenant. Since 11.42.0
215
+ two paths that went around that promise follow it too:
216
+
217
+ - **Credentials:** `CoreUserService.update()` refuses a change of another account's e-mail address or
218
+ password (403) — for any caller, including the account's creator and other administrators. Exempt:
219
+ the account itself, profile fields, an unchanged address sent along, and system work without a
220
+ current user (invitation, password reset). Administrator TARGETS are deliberately not exempt: an
221
+ administrator may write `roles`, so promote-then-change would open every account. Override
222
+ `assertCredentialChangeAllowed()` to adjust.
223
+ - **AI tools** (chat and MCP): no ADMIN bypass in the tool registry; tenant-role tools need a
224
+ membership role like anyone's.
225
+
226
+ ### Deactivated Tenants (`isTenantActive`, since 11.42.0)
227
+
228
+ The guard validates a membership and nothing else, so a tenant a project switched off keeps working —
229
+ for members, API tokens and the header-less tenant list — unless the project tells the guard:
230
+
231
+ ```typescript
232
+ // config.env.ts
233
+ multiTenancy: {
234
+ isTenantActive: async (tenantId, { connection }) =>
235
+ (await connection.collection('tenants').findOne({ _id: new Types.ObjectId(tenantId) }))?.active !== false,
236
+ }
237
+ ```
238
+
239
+ | Path | Deactivated tenant |
240
+ | -------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
241
+ | Header + tenant role, or `S_USER` / `S_VERIFIED` with a header | **403** `Tenant is inactive` |
242
+ | Tenant API token | **403** |
243
+ | `S_EVERYONE` with a header | not blocked, but no tenant context is set |
244
+ | No header | dropped from the user's tenant list — its data stays out of reach |
245
+ | GraphQL over WebSocket | no tenant resolved for a new subscription — an open one keeps its tenant until it reconnects (as after a removed membership) |
246
+ | Admin under `adminBypass` | still allowed — somebody has to be able to switch it back on |
247
+
248
+ The hook is asked **after** the membership check, so a non-member learns nothing about a tenant's
249
+ state. Return `false` for inactive; anything else counts as active. Answers are cached for
250
+ `cacheTtlMs`; call `tenantGuard.invalidateTenant(tenantId)` right after switching a tenant on or off
251
+ (broadcast to every replica with Redis). A function cannot come through `NSC__*` — set it in
252
+ `config.env.ts`. A custom guard can override the protected `isTenantActive(tenantId)` instead.
253
+
254
+ ### Service-Level Role Checks (`assertTenantRole`, since 11.42.0)
255
+
256
+ `@Roles()` protects a controller or resolver method — the route, not the operation. MCP tools, AI tools,
257
+ queue processors and other services call the service directly and never pass that route. Enforce a
258
+ tenant role in the service itself to close every entry point at once:
259
+
260
+ ```typescript
261
+ import { assertTenantRole, DefaultHR } from '@lenne.tech/nest-server';
262
+
263
+ async updateAiModels(input: AiModelsInput, serviceOptions?: ServiceOptions) {
264
+ assertTenantRole(DefaultHR.OWNER, 'Only owners may change the AI models');
265
+ // …
266
+ }
267
+ ```
268
+
269
+ Decided like the guard decides `@Roles()` on the header path: `adminBypass` passes, a global role
270
+ answers from `user.roles`, a tenant role from the request's membership role (by hierarchy; without
271
+ tenant context it fails). Several roles are alternatives. `hasTenantRole(...roles)` answers the same
272
+ question as a boolean.
273
+
274
+ | Caller | Result |
275
+ | ------------------------------------------------------------------------------------------ | ------- |
276
+ | Signed in, role held | passes |
277
+ | Signed in, role not held | **403** |
278
+ | Anonymous client request (public route) | **401** |
279
+ | System work — no request context, or one no client request created (cron, migration, seed) | passes |
280
+
281
+ The last two rows are told apart by `RequestContext.fromRequest`, which `RequestContextMiddleware` and
282
+ the GraphQL WebSocket context set. Treating "no current user" alone as system work would wave the
283
+ anonymous caller of a public route through.
284
+
214
285
  ### Filtering Without Header
215
286
 
216
- | User State | Filter Applied |
217
- | ------------------------------ | ------------------------------------------------------------------- |
218
- | Not authenticated, no context | Safety Net: `ForbiddenException` on tenantId-schemas |
219
- | Authenticated, no memberships | Safety Net: `ForbiddenException` on tenantId-schemas |
220
- | Authenticated, has memberships | `{ tenantId: { $in: [user's tenant IDs] } }` |
221
- | Authenticated + hierarchy role | `{ tenantId: { $in: [qualified tenant IDs] } }` (filtered by level) |
222
- | Admin without header | No filter (sees all data via `isAdminBypass`) |
287
+ | User State | Filter Applied |
288
+ | ----------------------------------------------------- | ------------------------------------------------------------------- |
289
+ | Not authenticated, no context | Safety Net: `ForbiddenException` on tenantId-schemas |
290
+ | Authenticated, no memberships | Safety Net: `ForbiddenException` on tenantId-schemas |
291
+ | Authenticated, has memberships | `{ tenantId: { $in: [user's tenant IDs] } }` |
292
+ | Authenticated + hierarchy role | `{ tenantId: { $in: [qualified tenant IDs] } }` (filtered by level) |
293
+ | Admin without header | No filter (sees all data via `isAdminBypass`) |
294
+ | Tenant deactivated (`isTenantActive` returns `false`) | Dropped from the user's tenant IDs (11.42.0+) |
223
295
 
224
296
  ## Extending via Module Inheritance
225
297
 
@@ -290,7 +362,7 @@ Both are **instance** methods on the singleton `CoreTenantGuard` — inject it a
290
362
  Mechanism, for anyone extending the guard (`core-tenant.guard.ts`):
291
363
 
292
364
  - The channel is namespaced with the framework's Redis key prefix, so two applications sharing one Redis instance do not clear each other's caches. Two **stages** of the same application do, unless you set `redis.keyPrefix` or `redis.db`.
293
- - The message is `{ scope: 'user', userId }` or `{ scope: 'all' }`, JSON-encoded.
365
+ - The message is `{ scope: 'user', userId }`, `{ scope: 'tenant', tenantId }` (from `invalidateTenant()`, 11.42.0+) or `{ scope: 'all' }`, JSON-encoded.
294
366
  - Subscription uses the shared subscriber connection from `CoreRedisService.getSubscriber()`; the listener is held in a field and detached on destroy, because that connection is shared.
295
367
  - **A received broadcast clears locally and does NOT re-publish** (`applyInvalidation()` → `clearUser()` / `clearAll()`, the no-broadcast variants). Re-publishing would bounce the message around the cluster forever.
296
368
  - **Publishing is fire-and-forget and failures are debug-level only.** A missed broadcast is not an error: it merely leaves the other replicas' caches stale until their TTL — exactly the no-Redis behaviour. Nothing about a request path depends on the broadcast succeeding.
@@ -0,0 +1,75 @@
1
+ import { looksLikeSystemRole } from '../../common/enums/role.enum';
2
+ import { accessDeniedException } from '../../common/exceptions/access-denied.exception';
3
+ import { RequestContext } from '../../common/services/request-context.service';
4
+ import { checkRoleAccess, resolveGlobalAndTenantRoles, tenantSatisfiableRoles } from './core-tenant.helpers';
5
+
6
+ /**
7
+ * Service-level tenant role checks.
8
+ *
9
+ * `@Roles()` protects a controller or resolver method — the route, not the operation. A service is
10
+ * also reached from MCP tools, AI tools, queue processors and other services, and on those paths a
11
+ * tenant role the controller demands is demanded by nobody. Checking in the service closes every
12
+ * entry point at once.
13
+ *
14
+ * The decision mirrors `CoreTenantGuard` on the tenant-header path:
15
+ * - an administrator under `adminBypass` passes,
16
+ * - a GLOBAL role (`RoleEnum.ADMIN`, `multiTenancy.globalOnlyRoles`) is answered by `user.roles`,
17
+ * never by a membership role,
18
+ * - a tenant role is answered by the validated `tenantRole` of the current request (hierarchy roles
19
+ * by level, others by exact match) — without tenant context there is nothing to compare, and it
20
+ * fails,
21
+ * - system roles (`S_*`) are runtime checks of their own and never satisfy anything here.
22
+ *
23
+ * Several roles read as alternatives, as in `@Roles()`.
24
+ */
25
+
26
+ /**
27
+ * Whether the current request holds one of `roles` in its tenant. `false` without a signed-in user.
28
+ */
29
+ export function hasTenantRole(...roles: string[]): boolean {
30
+ const context = RequestContext.get();
31
+ const user = context?.currentUser;
32
+ if (!user) {
33
+ return false;
34
+ }
35
+ if (context?.isAdminBypass) {
36
+ return true;
37
+ }
38
+
39
+ const required = roles.filter((role) => typeof role === 'string' && !looksLikeSystemRole(role));
40
+ const { global } = resolveGlobalAndTenantRoles(required);
41
+ if (global.some((role) => user.roles?.includes(role))) {
42
+ return true;
43
+ }
44
+
45
+ // The length guard matters: checkRoleAccess() answers TRUE for an empty required list.
46
+ const tenantRoles = tenantSatisfiableRoles(required);
47
+ return tenantRoles.length > 0 && !!context?.tenantRole && checkRoleAccess(tenantRoles, undefined, context.tenantRole);
48
+ }
49
+
50
+ /**
51
+ * Refuses the current operation unless it holds one of `roles` in its tenant.
52
+ *
53
+ * Work the server does on its own — no request context at all, or one no client request created
54
+ * (cron, migration, seed, `runWithBypass…()` outside a request) — passes: there is no caller whose
55
+ * role could be wrong. An ANONYMOUS client request does not pass: it gets 401, and a signed-in caller
56
+ * without the role gets 403.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * async updateAiModels(input: AiModelsInput, serviceOptions?: ServiceOptions) {
61
+ * assertTenantRole(DefaultHR.OWNER, 'Only owners may change the AI models');
62
+ * …
63
+ * }
64
+ * ```
65
+ */
66
+ export function assertTenantRole(roles: string | string[], message?: string): void {
67
+ const context = RequestContext.get();
68
+ if (!context?.currentUser && !context?.fromRequest) {
69
+ return;
70
+ }
71
+ const list = Array.isArray(roles) ? roles : [roles];
72
+ if (!hasTenantRole(...list)) {
73
+ throw accessDeniedException(context?.currentUser, message);
74
+ }
75
+ }