@dudousxd/nestjs-agent-authz 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Davide Carvalho
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # `@dudousxd/nestjs-agent-authz`
2
+
3
+ > 🪺 Part of the [Aviary](https://davidecarvalho.github.io/aviary) · plugs [`@dudousxd/nestjs-authz`](https://www.npmjs.com/package/@dudousxd/nestjs-authz) into [`@dudousxd/nestjs-agent`](https://www.npmjs.com/package/@dudousxd/nestjs-agent) tool authorization.
4
+
5
+ By default the agent gates tools by **roles**. This adapter gates them by **abilities** through a
6
+ `@dudousxd/nestjs-authz` `Gate`: a tool's `ability` is checked with `gate.forUser(actor).allows(ability)`.
7
+ Tools without an `ability` fall back to the role-based policy, so apps not using authz are unaffected.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pnpm add @dudousxd/nestjs-agent-authz @dudousxd/nestjs-authz
13
+ ```
14
+
15
+ ## Use
16
+
17
+ ```ts
18
+ import { AgentAuthzModule } from '@dudousxd/nestjs-agent-authz';
19
+
20
+ @Module({
21
+ imports: [
22
+ AuthzModule.forRoot(/* … */), // the app's global Gate
23
+ AgentModule.forRoot({ model, store, modelId }),
24
+ AgentAuthzModule.forRoot(), // binds AGENT_ROLES_POLICY to the Gate-backed policy
25
+ ],
26
+ })
27
+ export class AppModule {}
28
+ ```
29
+
30
+ ```ts
31
+ @AiTool({ name: 'purgeCache', kind: 'action', description: '…', input: z.object({ key: z.string() }),
32
+ ability: 'cache.purge' }) // ← checked via gate.forUser(actor).allows('cache.purge')
33
+ export class PurgeCacheTool implements ToolHandler<{ key: string }> { /* … */ }
34
+ ```
35
+
36
+ `forRoot({ fallbackRoles })` configures the role policy used when a tool declares no `ability`.
37
+
38
+ ## License
39
+
40
+ MIT © Davide Carvalho
package/dist/index.cjs ADDED
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ AgentAuthzModule: () => AgentAuthzModule,
25
+ AuthzRolesPolicy: () => AuthzRolesPolicy,
26
+ userFromActor: () => userFromActor
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+
30
+ // src/authz-roles-policy.ts
31
+ var import_nestjs_agent_core = require("@dudousxd/nestjs-agent-core");
32
+ function userFromActor(actor) {
33
+ return {
34
+ id: actor.id,
35
+ ...actor.roles !== void 0 ? {
36
+ roles: actor.roles
37
+ } : {}
38
+ };
39
+ }
40
+ __name(userFromActor, "userFromActor");
41
+ var AuthzRolesPolicy = class {
42
+ static {
43
+ __name(this, "AuthzRolesPolicy");
44
+ }
45
+ gate;
46
+ options;
47
+ constructor(gate, options) {
48
+ this.gate = gate;
49
+ this.options = options;
50
+ }
51
+ async can(actor, tool) {
52
+ if (tool.ability !== void 0) {
53
+ return this.gate.forUser(userFromActor(actor)).allows(tool.ability);
54
+ }
55
+ return new import_nestjs_agent_core.DefaultRolesPolicy(this.options?.fallbackRoles).can(actor, tool);
56
+ }
57
+ };
58
+
59
+ // src/agent-authz.module.ts
60
+ var import_nestjs_agent_core2 = require("@dudousxd/nestjs-agent-core");
61
+ var import_nestjs_authz = require("@dudousxd/nestjs-authz");
62
+ var import_common = require("@nestjs/common");
63
+ function _ts_decorate(decorators, target, key, desc) {
64
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
65
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
66
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
67
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
68
+ }
69
+ __name(_ts_decorate, "_ts_decorate");
70
+ var AgentAuthzModule = class _AgentAuthzModule {
71
+ static {
72
+ __name(this, "AgentAuthzModule");
73
+ }
74
+ static forRoot(options) {
75
+ return {
76
+ module: _AgentAuthzModule,
77
+ providers: [
78
+ {
79
+ provide: import_nestjs_agent_core2.AGENT_ROLES_POLICY,
80
+ useFactory: /* @__PURE__ */ __name((gate) => new AuthzRolesPolicy(gate, options), "useFactory"),
81
+ inject: [
82
+ import_nestjs_authz.Gate
83
+ ]
84
+ }
85
+ ],
86
+ exports: [
87
+ import_nestjs_agent_core2.AGENT_ROLES_POLICY
88
+ ]
89
+ };
90
+ }
91
+ };
92
+ AgentAuthzModule = _ts_decorate([
93
+ (0, import_common.Global)(),
94
+ (0, import_common.Module)({})
95
+ ], AgentAuthzModule);
96
+ // Annotate the CommonJS export names for ESM import in node:
97
+ 0 && (module.exports = {
98
+ AgentAuthzModule,
99
+ AuthzRolesPolicy,
100
+ userFromActor
101
+ });
102
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/authz-roles-policy.ts","../src/agent-authz.module.ts"],"sourcesContent":["export {\n AuthzRolesPolicy,\n userFromActor,\n type AuthzRolesPolicyOptions,\n type AuthzUser,\n} from './authz-roles-policy.js';\nexport { AgentAuthzModule } from './agent-authz.module.js';\n","import {\n type Actor,\n DefaultRolesPolicy,\n type RolesPolicy,\n type ToolSpec,\n} from '@dudousxd/nestjs-agent-core';\nimport type { Gate } from '@dudousxd/nestjs-authz';\n\n/** Options for {@link AuthzRolesPolicy}. */\nexport interface AuthzRolesPolicyOptions {\n /**\n * Roles used by the role-based fallback ({@link DefaultRolesPolicy}) when a tool\n * declares neither an `ability` nor `roles`. Defaults to ADMIN-only (the\n * DefaultRolesPolicy default).\n */\n fallbackRoles?: string[];\n}\n\n/**\n * The user object handed to `gate.forUser(...)`. The agent {@link Actor} only carries\n * `{ id, roles? }`, which is exactly what authz's default {@link defaultRoleResolver}\n * (and any ad-hoc gate reading `user.roles`) needs to resolve a role-based ability —\n * so no host user entity is required.\n */\nexport interface AuthzUser {\n id: string;\n roles?: string[];\n}\n\n/**\n * Map an agent {@link Actor} onto the minimal user shape authz expects. authz's\n * `User` is `unknown`, and its default role resolver reads `user.roles`; passing\n * `{ id, roles }` is enough for a role-based ability to resolve. `roles` is only\n * attached when present (honors `exactOptionalPropertyTypes`).\n */\nexport function userFromActor(actor: Actor): AuthzUser {\n return { id: actor.id, ...(actor.roles !== undefined ? { roles: actor.roles } : {}) };\n}\n\n/**\n * A {@link RolesPolicy} backed by a `@dudousxd/nestjs-authz` {@link Gate}.\n *\n * - When a tool declares an `ability`, the decision is delegated to the Gate\n * (`gate.forUser(actor).allows(ability)`), so apps get policies, abilities, and the\n * RBAC seams for free.\n * - Otherwise it falls back to the role-based {@link DefaultRolesPolicy} (matching the\n * actor's role against `tool.roles`, defaulting to `fallbackRoles`).\n */\nexport class AuthzRolesPolicy implements RolesPolicy {\n constructor(\n private readonly gate: Gate,\n private readonly options?: AuthzRolesPolicyOptions,\n ) {}\n\n async can(actor: Actor, tool: ToolSpec): Promise<boolean> {\n if (tool.ability !== undefined) {\n return this.gate.forUser(userFromActor(actor)).allows(tool.ability);\n }\n return new DefaultRolesPolicy(this.options?.fallbackRoles).can(actor, tool);\n }\n}\n","import { AGENT_ROLES_POLICY } from '@dudousxd/nestjs-agent-core';\nimport { Gate } from '@dudousxd/nestjs-authz';\nimport { type DynamicModule, Global, Module } from '@nestjs/common';\nimport { AuthzRolesPolicy, type AuthzRolesPolicyOptions } from './authz-roles-policy.js';\n\n/**\n * Wires an authz-backed {@link AuthzRolesPolicy} as nestjs-agent's `RolesPolicy`,\n * binding it under the shared {@link AGENT_ROLES_POLICY} token so the agent loop's\n * tool gate consults the {@link Gate}.\n *\n * The {@link Gate} is injected from the app's global `AuthzModule` — this module does\n * NOT import `AuthzModule`; the app is expected to register it once at the root.\n */\n@Global()\n@Module({})\nexport class AgentAuthzModule {\n static forRoot(options?: AuthzRolesPolicyOptions): DynamicModule {\n return {\n module: AgentAuthzModule,\n providers: [\n {\n provide: AGENT_ROLES_POLICY,\n useFactory: (gate: Gate) => new AuthzRolesPolicy(gate, options),\n inject: [Gate],\n },\n ],\n exports: [AGENT_ROLES_POLICY],\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACAA,+BAKO;AA8BA,SAASA,cAAcC,OAAY;AACxC,SAAO;IAAEC,IAAID,MAAMC;IAAI,GAAID,MAAME,UAAUC,SAAY;MAAED,OAAOF,MAAME;IAAM,IAAI,CAAC;EAAG;AACtF;AAFgBH;AAaT,IAAMK,mBAAN,MAAMA;EAhDb,OAgDaA;;;;;EACX,YACmBC,MACAC,SACjB;SAFiBD,OAAAA;SACAC,UAAAA;EAChB;EAEH,MAAMC,IAAIP,OAAcQ,MAAkC;AACxD,QAAIA,KAAKC,YAAYN,QAAW;AAC9B,aAAO,KAAKE,KAAKK,QAAQX,cAAcC,KAAAA,CAAAA,EAAQW,OAAOH,KAAKC,OAAO;IACpE;AACA,WAAO,IAAIG,4CAAmB,KAAKN,SAASO,aAAAA,EAAeN,IAAIP,OAAOQ,IAAAA;EACxE;AACF;;;AC5DA,IAAAM,4BAAmC;AACnC,0BAAqB;AACrB,oBAAmD;;;;;;;;AAa5C,IAAMC,mBAAN,MAAMA,kBAAAA;SAAAA;;;EACX,OAAOC,QAAQC,SAAkD;AAC/D,WAAO;MACLC,QAAQH;MACRI,WAAW;QACT;UACEC,SAASC;UACTC,YAAY,wBAACC,SAAe,IAAIC,iBAAiBD,MAAMN,OAAAA,GAA3C;UACZQ,QAAQ;YAACC;;QACX;;MAEFC,SAAS;QAACN;;IACZ;EACF;AACF;;;;;","names":["userFromActor","actor","id","roles","undefined","AuthzRolesPolicy","gate","options","can","tool","ability","forUser","allows","DefaultRolesPolicy","fallbackRoles","import_nestjs_agent_core","AgentAuthzModule","forRoot","options","module","providers","provide","AGENT_ROLES_POLICY","useFactory","gate","AuthzRolesPolicy","inject","Gate","exports"]}
@@ -0,0 +1,59 @@
1
+ import { RolesPolicy, Actor, ToolSpec } from '@dudousxd/nestjs-agent-core';
2
+ import { Gate } from '@dudousxd/nestjs-authz';
3
+ import { DynamicModule } from '@nestjs/common';
4
+
5
+ /** Options for {@link AuthzRolesPolicy}. */
6
+ interface AuthzRolesPolicyOptions {
7
+ /**
8
+ * Roles used by the role-based fallback ({@link DefaultRolesPolicy}) when a tool
9
+ * declares neither an `ability` nor `roles`. Defaults to ADMIN-only (the
10
+ * DefaultRolesPolicy default).
11
+ */
12
+ fallbackRoles?: string[];
13
+ }
14
+ /**
15
+ * The user object handed to `gate.forUser(...)`. The agent {@link Actor} only carries
16
+ * `{ id, roles? }`, which is exactly what authz's default {@link defaultRoleResolver}
17
+ * (and any ad-hoc gate reading `user.roles`) needs to resolve a role-based ability —
18
+ * so no host user entity is required.
19
+ */
20
+ interface AuthzUser {
21
+ id: string;
22
+ roles?: string[];
23
+ }
24
+ /**
25
+ * Map an agent {@link Actor} onto the minimal user shape authz expects. authz's
26
+ * `User` is `unknown`, and its default role resolver reads `user.roles`; passing
27
+ * `{ id, roles }` is enough for a role-based ability to resolve. `roles` is only
28
+ * attached when present (honors `exactOptionalPropertyTypes`).
29
+ */
30
+ declare function userFromActor(actor: Actor): AuthzUser;
31
+ /**
32
+ * A {@link RolesPolicy} backed by a `@dudousxd/nestjs-authz` {@link Gate}.
33
+ *
34
+ * - When a tool declares an `ability`, the decision is delegated to the Gate
35
+ * (`gate.forUser(actor).allows(ability)`), so apps get policies, abilities, and the
36
+ * RBAC seams for free.
37
+ * - Otherwise it falls back to the role-based {@link DefaultRolesPolicy} (matching the
38
+ * actor's role against `tool.roles`, defaulting to `fallbackRoles`).
39
+ */
40
+ declare class AuthzRolesPolicy implements RolesPolicy {
41
+ private readonly gate;
42
+ private readonly options?;
43
+ constructor(gate: Gate, options?: AuthzRolesPolicyOptions | undefined);
44
+ can(actor: Actor, tool: ToolSpec): Promise<boolean>;
45
+ }
46
+
47
+ /**
48
+ * Wires an authz-backed {@link AuthzRolesPolicy} as nestjs-agent's `RolesPolicy`,
49
+ * binding it under the shared {@link AGENT_ROLES_POLICY} token so the agent loop's
50
+ * tool gate consults the {@link Gate}.
51
+ *
52
+ * The {@link Gate} is injected from the app's global `AuthzModule` — this module does
53
+ * NOT import `AuthzModule`; the app is expected to register it once at the root.
54
+ */
55
+ declare class AgentAuthzModule {
56
+ static forRoot(options?: AuthzRolesPolicyOptions): DynamicModule;
57
+ }
58
+
59
+ export { AgentAuthzModule, AuthzRolesPolicy, type AuthzRolesPolicyOptions, type AuthzUser, userFromActor };
@@ -0,0 +1,59 @@
1
+ import { RolesPolicy, Actor, ToolSpec } from '@dudousxd/nestjs-agent-core';
2
+ import { Gate } from '@dudousxd/nestjs-authz';
3
+ import { DynamicModule } from '@nestjs/common';
4
+
5
+ /** Options for {@link AuthzRolesPolicy}. */
6
+ interface AuthzRolesPolicyOptions {
7
+ /**
8
+ * Roles used by the role-based fallback ({@link DefaultRolesPolicy}) when a tool
9
+ * declares neither an `ability` nor `roles`. Defaults to ADMIN-only (the
10
+ * DefaultRolesPolicy default).
11
+ */
12
+ fallbackRoles?: string[];
13
+ }
14
+ /**
15
+ * The user object handed to `gate.forUser(...)`. The agent {@link Actor} only carries
16
+ * `{ id, roles? }`, which is exactly what authz's default {@link defaultRoleResolver}
17
+ * (and any ad-hoc gate reading `user.roles`) needs to resolve a role-based ability —
18
+ * so no host user entity is required.
19
+ */
20
+ interface AuthzUser {
21
+ id: string;
22
+ roles?: string[];
23
+ }
24
+ /**
25
+ * Map an agent {@link Actor} onto the minimal user shape authz expects. authz's
26
+ * `User` is `unknown`, and its default role resolver reads `user.roles`; passing
27
+ * `{ id, roles }` is enough for a role-based ability to resolve. `roles` is only
28
+ * attached when present (honors `exactOptionalPropertyTypes`).
29
+ */
30
+ declare function userFromActor(actor: Actor): AuthzUser;
31
+ /**
32
+ * A {@link RolesPolicy} backed by a `@dudousxd/nestjs-authz` {@link Gate}.
33
+ *
34
+ * - When a tool declares an `ability`, the decision is delegated to the Gate
35
+ * (`gate.forUser(actor).allows(ability)`), so apps get policies, abilities, and the
36
+ * RBAC seams for free.
37
+ * - Otherwise it falls back to the role-based {@link DefaultRolesPolicy} (matching the
38
+ * actor's role against `tool.roles`, defaulting to `fallbackRoles`).
39
+ */
40
+ declare class AuthzRolesPolicy implements RolesPolicy {
41
+ private readonly gate;
42
+ private readonly options?;
43
+ constructor(gate: Gate, options?: AuthzRolesPolicyOptions | undefined);
44
+ can(actor: Actor, tool: ToolSpec): Promise<boolean>;
45
+ }
46
+
47
+ /**
48
+ * Wires an authz-backed {@link AuthzRolesPolicy} as nestjs-agent's `RolesPolicy`,
49
+ * binding it under the shared {@link AGENT_ROLES_POLICY} token so the agent loop's
50
+ * tool gate consults the {@link Gate}.
51
+ *
52
+ * The {@link Gate} is injected from the app's global `AuthzModule` — this module does
53
+ * NOT import `AuthzModule`; the app is expected to register it once at the root.
54
+ */
55
+ declare class AgentAuthzModule {
56
+ static forRoot(options?: AuthzRolesPolicyOptions): DynamicModule;
57
+ }
58
+
59
+ export { AgentAuthzModule, AuthzRolesPolicy, type AuthzRolesPolicyOptions, type AuthzUser, userFromActor };
package/dist/index.js ADDED
@@ -0,0 +1,75 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/authz-roles-policy.ts
5
+ import { DefaultRolesPolicy } from "@dudousxd/nestjs-agent-core";
6
+ function userFromActor(actor) {
7
+ return {
8
+ id: actor.id,
9
+ ...actor.roles !== void 0 ? {
10
+ roles: actor.roles
11
+ } : {}
12
+ };
13
+ }
14
+ __name(userFromActor, "userFromActor");
15
+ var AuthzRolesPolicy = class {
16
+ static {
17
+ __name(this, "AuthzRolesPolicy");
18
+ }
19
+ gate;
20
+ options;
21
+ constructor(gate, options) {
22
+ this.gate = gate;
23
+ this.options = options;
24
+ }
25
+ async can(actor, tool) {
26
+ if (tool.ability !== void 0) {
27
+ return this.gate.forUser(userFromActor(actor)).allows(tool.ability);
28
+ }
29
+ return new DefaultRolesPolicy(this.options?.fallbackRoles).can(actor, tool);
30
+ }
31
+ };
32
+
33
+ // src/agent-authz.module.ts
34
+ import { AGENT_ROLES_POLICY } from "@dudousxd/nestjs-agent-core";
35
+ import { Gate } from "@dudousxd/nestjs-authz";
36
+ import { Global, Module } from "@nestjs/common";
37
+ function _ts_decorate(decorators, target, key, desc) {
38
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
39
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
40
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
41
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
42
+ }
43
+ __name(_ts_decorate, "_ts_decorate");
44
+ var AgentAuthzModule = class _AgentAuthzModule {
45
+ static {
46
+ __name(this, "AgentAuthzModule");
47
+ }
48
+ static forRoot(options) {
49
+ return {
50
+ module: _AgentAuthzModule,
51
+ providers: [
52
+ {
53
+ provide: AGENT_ROLES_POLICY,
54
+ useFactory: /* @__PURE__ */ __name((gate) => new AuthzRolesPolicy(gate, options), "useFactory"),
55
+ inject: [
56
+ Gate
57
+ ]
58
+ }
59
+ ],
60
+ exports: [
61
+ AGENT_ROLES_POLICY
62
+ ]
63
+ };
64
+ }
65
+ };
66
+ AgentAuthzModule = _ts_decorate([
67
+ Global(),
68
+ Module({})
69
+ ], AgentAuthzModule);
70
+ export {
71
+ AgentAuthzModule,
72
+ AuthzRolesPolicy,
73
+ userFromActor
74
+ };
75
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/authz-roles-policy.ts","../src/agent-authz.module.ts"],"sourcesContent":["import {\n type Actor,\n DefaultRolesPolicy,\n type RolesPolicy,\n type ToolSpec,\n} from '@dudousxd/nestjs-agent-core';\nimport type { Gate } from '@dudousxd/nestjs-authz';\n\n/** Options for {@link AuthzRolesPolicy}. */\nexport interface AuthzRolesPolicyOptions {\n /**\n * Roles used by the role-based fallback ({@link DefaultRolesPolicy}) when a tool\n * declares neither an `ability` nor `roles`. Defaults to ADMIN-only (the\n * DefaultRolesPolicy default).\n */\n fallbackRoles?: string[];\n}\n\n/**\n * The user object handed to `gate.forUser(...)`. The agent {@link Actor} only carries\n * `{ id, roles? }`, which is exactly what authz's default {@link defaultRoleResolver}\n * (and any ad-hoc gate reading `user.roles`) needs to resolve a role-based ability —\n * so no host user entity is required.\n */\nexport interface AuthzUser {\n id: string;\n roles?: string[];\n}\n\n/**\n * Map an agent {@link Actor} onto the minimal user shape authz expects. authz's\n * `User` is `unknown`, and its default role resolver reads `user.roles`; passing\n * `{ id, roles }` is enough for a role-based ability to resolve. `roles` is only\n * attached when present (honors `exactOptionalPropertyTypes`).\n */\nexport function userFromActor(actor: Actor): AuthzUser {\n return { id: actor.id, ...(actor.roles !== undefined ? { roles: actor.roles } : {}) };\n}\n\n/**\n * A {@link RolesPolicy} backed by a `@dudousxd/nestjs-authz` {@link Gate}.\n *\n * - When a tool declares an `ability`, the decision is delegated to the Gate\n * (`gate.forUser(actor).allows(ability)`), so apps get policies, abilities, and the\n * RBAC seams for free.\n * - Otherwise it falls back to the role-based {@link DefaultRolesPolicy} (matching the\n * actor's role against `tool.roles`, defaulting to `fallbackRoles`).\n */\nexport class AuthzRolesPolicy implements RolesPolicy {\n constructor(\n private readonly gate: Gate,\n private readonly options?: AuthzRolesPolicyOptions,\n ) {}\n\n async can(actor: Actor, tool: ToolSpec): Promise<boolean> {\n if (tool.ability !== undefined) {\n return this.gate.forUser(userFromActor(actor)).allows(tool.ability);\n }\n return new DefaultRolesPolicy(this.options?.fallbackRoles).can(actor, tool);\n }\n}\n","import { AGENT_ROLES_POLICY } from '@dudousxd/nestjs-agent-core';\nimport { Gate } from '@dudousxd/nestjs-authz';\nimport { type DynamicModule, Global, Module } from '@nestjs/common';\nimport { AuthzRolesPolicy, type AuthzRolesPolicyOptions } from './authz-roles-policy.js';\n\n/**\n * Wires an authz-backed {@link AuthzRolesPolicy} as nestjs-agent's `RolesPolicy`,\n * binding it under the shared {@link AGENT_ROLES_POLICY} token so the agent loop's\n * tool gate consults the {@link Gate}.\n *\n * The {@link Gate} is injected from the app's global `AuthzModule` — this module does\n * NOT import `AuthzModule`; the app is expected to register it once at the root.\n */\n@Global()\n@Module({})\nexport class AgentAuthzModule {\n static forRoot(options?: AuthzRolesPolicyOptions): DynamicModule {\n return {\n module: AgentAuthzModule,\n providers: [\n {\n provide: AGENT_ROLES_POLICY,\n useFactory: (gate: Gate) => new AuthzRolesPolicy(gate, options),\n inject: [Gate],\n },\n ],\n exports: [AGENT_ROLES_POLICY],\n };\n }\n}\n"],"mappings":";;;;AAAA,SAEEA,0BAGK;AA8BA,SAASC,cAAcC,OAAY;AACxC,SAAO;IAAEC,IAAID,MAAMC;IAAI,GAAID,MAAME,UAAUC,SAAY;MAAED,OAAOF,MAAME;IAAM,IAAI,CAAC;EAAG;AACtF;AAFgBH;AAaT,IAAMK,mBAAN,MAAMA;EAhDb,OAgDaA;;;;;EACX,YACmBC,MACAC,SACjB;SAFiBD,OAAAA;SACAC,UAAAA;EAChB;EAEH,MAAMC,IAAIP,OAAcQ,MAAkC;AACxD,QAAIA,KAAKC,YAAYN,QAAW;AAC9B,aAAO,KAAKE,KAAKK,QAAQX,cAAcC,KAAAA,CAAAA,EAAQW,OAAOH,KAAKC,OAAO;IACpE;AACA,WAAO,IAAIG,mBAAmB,KAAKN,SAASO,aAAAA,EAAeN,IAAIP,OAAOQ,IAAAA;EACxE;AACF;;;AC5DA,SAASM,0BAA0B;AACnC,SAASC,YAAY;AACrB,SAA6BC,QAAQC,cAAc;;;;;;;;AAa5C,IAAMC,mBAAN,MAAMA,kBAAAA;SAAAA;;;EACX,OAAOC,QAAQC,SAAkD;AAC/D,WAAO;MACLC,QAAQH;MACRI,WAAW;QACT;UACEC,SAASC;UACTC,YAAY,wBAACC,SAAe,IAAIC,iBAAiBD,MAAMN,OAAAA,GAA3C;UACZQ,QAAQ;YAACC;;QACX;;MAEFC,SAAS;QAACN;;IACZ;EACF;AACF;;;;;","names":["DefaultRolesPolicy","userFromActor","actor","id","roles","undefined","AuthzRolesPolicy","gate","options","can","tool","ability","forUser","allows","DefaultRolesPolicy","fallbackRoles","AGENT_ROLES_POLICY","Gate","Global","Module","AgentAuthzModule","forRoot","options","module","providers","provide","AGENT_ROLES_POLICY","useFactory","gate","AuthzRolesPolicy","inject","Gate","exports"]}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@dudousxd/nestjs-agent-authz",
3
+ "version": "0.1.0",
4
+ "description": "nestjs-agent authz adapter — plug @dudousxd/nestjs-authz into tool authorization",
5
+ "license": "MIT",
6
+ "author": "Davide Carvalho",
7
+ "type": "module",
8
+ "main": "./dist/index.cjs",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ },
17
+ "require": {
18
+ "types": "./dist/index.d.cts",
19
+ "default": "./dist/index.cjs"
20
+ }
21
+ }
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "dependencies": {
27
+ "@dudousxd/nestjs-agent-core": "0.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "@dudousxd/nestjs-authz": "~0.6.3",
31
+ "@nestjs/common": "^10.0.0 || ^11.0.0"
32
+ },
33
+ "devDependencies": {
34
+ "@dudousxd/nestjs-authz": "0.6.3",
35
+ "@nestjs/common": "11.1.6",
36
+ "reflect-metadata": "0.2.2",
37
+ "tsup": "8.3.5",
38
+ "typescript": "5.9.3",
39
+ "zod": "3.25.76",
40
+ "@dudousxd/nestjs-agent-core": "0.1.0"
41
+ },
42
+ "scripts": {
43
+ "build": "tsup",
44
+ "typecheck": "tsc -p tsconfig.json --noEmit"
45
+ }
46
+ }