@dudousxd/nestjs-authz 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +109 -0
- package/README.md +279 -0
- package/dist/authz.guard.d.ts +48 -0
- package/dist/authz.guard.d.ts.map +1 -0
- package/dist/authz.guard.js +144 -0
- package/dist/authz.guard.js.map +1 -0
- package/dist/can-endpoint.controller.d.ts +0 -19
- package/dist/can-endpoint.controller.d.ts.map +1 -1
- package/dist/can-endpoint.controller.js +18 -9
- package/dist/can-endpoint.controller.js.map +1 -1
- package/dist/decision-provider.d.ts +25 -16
- package/dist/decision-provider.d.ts.map +1 -1
- package/dist/decision-provider.js +11 -1
- package/dist/decision-provider.js.map +1 -1
- package/dist/decisions.d.ts +24 -0
- package/dist/decisions.d.ts.map +1 -0
- package/dist/decisions.js +87 -0
- package/dist/decisions.js.map +1 -0
- package/dist/decorator/roles.decorator.d.ts +6 -12
- package/dist/decorator/roles.decorator.d.ts.map +1 -1
- package/dist/decorator/roles.decorator.js +13 -15
- package/dist/decorator/roles.decorator.js.map +1 -1
- package/dist/diagnostics.d.ts +15 -5
- package/dist/diagnostics.d.ts.map +1 -1
- package/dist/diagnostics.js +3 -1
- package/dist/diagnostics.js.map +1 -1
- package/dist/errors/exceptions.d.ts +0 -8
- package/dist/errors/exceptions.d.ts.map +1 -1
- package/dist/errors/exceptions.js +0 -14
- package/dist/errors/exceptions.js.map +1 -1
- package/dist/gate.d.ts +83 -218
- package/dist/gate.d.ts.map +1 -1
- package/dist/gate.js +209 -611
- package/dist/gate.js.map +1 -1
- package/dist/helpers.d.ts +46 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +89 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +28 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +30 -13
- package/dist/index.js.map +1 -1
- package/dist/links.d.ts +39 -0
- package/dist/links.d.ts.map +1 -0
- package/dist/links.js +57 -0
- package/dist/links.js.map +1 -0
- package/dist/module.d.ts +23 -22
- package/dist/module.d.ts.map +1 -1
- package/dist/module.js +129 -158
- package/dist/module.js.map +1 -1
- package/dist/official.d.ts +39 -0
- package/dist/official.d.ts.map +1 -0
- package/dist/official.js +44 -0
- package/dist/official.js.map +1 -0
- package/dist/policies/authz-policy.d.ts +42 -0
- package/dist/policies/authz-policy.d.ts.map +1 -0
- package/dist/policies/authz-policy.js +80 -0
- package/dist/policies/authz-policy.js.map +1 -0
- package/dist/policies/gate.policy.d.ts +18 -0
- package/dist/policies/gate.policy.d.ts.map +1 -0
- package/dist/policies/gate.policy.js +37 -0
- package/dist/policies/gate.policy.js.map +1 -0
- package/dist/policies/index.d.ts +5 -0
- package/dist/policies/index.d.ts.map +1 -0
- package/dist/policies/index.js +5 -0
- package/dist/policies/index.js.map +1 -0
- package/dist/policies/permission.policy.d.ts +27 -0
- package/dist/policies/permission.policy.d.ts.map +1 -0
- package/dist/policies/permission.policy.js +61 -0
- package/dist/policies/permission.policy.js.map +1 -0
- package/dist/policies/role.policy.d.ts +27 -0
- package/dist/policies/role.policy.d.ts.map +1 -0
- package/dist/policies/role.policy.js +51 -0
- package/dist/policies/role.policy.js.map +1 -0
- package/dist/policy-index.d.ts +51 -0
- package/dist/policy-index.d.ts.map +1 -0
- package/dist/policy-index.js +135 -0
- package/dist/policy-index.js.map +1 -0
- package/dist/rbac.d.ts +92 -0
- package/dist/rbac.d.ts.map +1 -0
- package/dist/rbac.js +293 -0
- package/dist/rbac.js.map +1 -0
- package/dist/resource-resolver.d.ts +15 -17
- package/dist/resource-resolver.d.ts.map +1 -1
- package/dist/resource-resolver.js +8 -8
- package/dist/resource-resolver.js.map +1 -1
- package/dist/runtime.d.ts +31 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +40 -0
- package/dist/runtime.js.map +1 -0
- package/dist/tokens.d.ts +4 -8
- package/dist/tokens.d.ts.map +1 -1
- package/dist/tokens.js +4 -8
- package/dist/tokens.js.map +1 -1
- package/dist/types.d.ts +52 -171
- package/dist/types.d.ts.map +1 -1
- package/package.json +19 -10
- package/skills/authz-enforcement/SKILL.md +67 -135
- package/skills/authz-policies/SKILL.md +43 -117
- package/skills/authz-query-scopes/SKILL.md +16 -9
- package/skills/authz-rbac-seams/SKILL.md +29 -12
- package/skills/authz-setup/SKILL.md +45 -106
- package/dist/decorator/can.decorator.d.ts +0 -29
- package/dist/decorator/can.decorator.d.ts.map +0 -1
- package/dist/decorator/can.decorator.js +0 -23
- package/dist/decorator/can.decorator.js.map +0 -1
- package/dist/decorator/policy.decorator.d.ts +0 -18
- package/dist/decorator/policy.decorator.d.ts.map +0 -1
- package/dist/decorator/policy.decorator.js +0 -28
- package/dist/decorator/policy.decorator.js.map +0 -1
- package/dist/guard/can.guard.d.ts +0 -31
- package/dist/guard/can.guard.d.ts.map +0 -1
- package/dist/guard/can.guard.js +0 -92
- package/dist/guard/can.guard.js.map +0 -1
- package/dist/guard/roles.guard.d.ts +0 -21
- package/dist/guard/roles.guard.d.ts.map +0 -1
- package/dist/guard/roles.guard.js +0 -50
- package/dist/guard/roles.guard.js.map +0 -1
- package/dist/policy-registry.d.ts +0 -46
- package/dist/policy-registry.d.ts.map +0 -1
- package/dist/policy-registry.js +0 -101
- package/dist/policy-registry.js.map +0 -1
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import { Injectable } from '@nestjs/common';
|
|
3
|
-
import { POLICY_RESOURCE_METADATA } from '../tokens.js';
|
|
4
|
-
/**
|
|
5
|
-
* Marks a class as the policy for `resource`. The class methods become abilities
|
|
6
|
-
* dispatched by name (e.g. `update(user, post)` answers `gate.authorize('update', post)`).
|
|
7
|
-
*
|
|
8
|
-
* Applying `@Policy` also makes the class an `@Injectable()` provider so it can
|
|
9
|
-
* be auto-discovered and resolved from the Nest container.
|
|
10
|
-
*/
|
|
11
|
-
export function Policy(resource) {
|
|
12
|
-
return (target) => {
|
|
13
|
-
Reflect.defineMetadata(POLICY_RESOURCE_METADATA, resource, target);
|
|
14
|
-
// Make the policy a provider without forcing the app to add @Injectable().
|
|
15
|
-
Injectable()(target);
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Returns the resource class a policy was declared for, or `undefined`.
|
|
20
|
-
*
|
|
21
|
-
* Uses `getMetadata` (not `getOwnMetadata`) so a subclass of a `@Policy`-decorated
|
|
22
|
-
* class still resolves the inherited resource via the prototype chain.
|
|
23
|
-
*/
|
|
24
|
-
export function getPolicyResource(policy) {
|
|
25
|
-
const ctor = typeof policy === 'function' ? policy : policy.constructor;
|
|
26
|
-
return Reflect.getMetadata(POLICY_RESOURCE_METADATA, ctor);
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=policy.decorator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"policy.decorator.js","sourceRoot":"","sources":["../../src/decorator/policy.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAa,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAGxD;;;;;;GAMG;AACH,MAAM,UAAU,MAAM,CAAC,QAAuB;IAC5C,OAAO,CAAC,MAAM,EAAE,EAAE;QAChB,OAAO,CAAC,cAAc,CAAC,wBAAwB,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;QACnE,2EAA2E;QAC3E,UAAU,EAAE,CAAC,MAAM,CAAC,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAA8B;IAC9D,MAAM,IAAI,GAAG,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAE,MAAyB,CAAC,WAAW,CAAC;IAC5F,OAAO,OAAO,CAAC,WAAW,CAAC,wBAAwB,EAAE,IAAI,CAA8B,CAAC;AAC1F,CAAC"}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { type CanActivate, type ExecutionContext } from '@nestjs/common';
|
|
2
|
-
import { ModuleRef, Reflector } from '@nestjs/core';
|
|
3
|
-
import { Gate } from '../gate.js';
|
|
4
|
-
import type { ResourceResolver } from '../resource-resolver.js';
|
|
5
|
-
/**
|
|
6
|
-
* Enforces `@Can(ability, Resource?)` on routes.
|
|
7
|
-
*
|
|
8
|
-
* - No `@Can` metadata → allow (the guard is inert on un-annotated routes).
|
|
9
|
-
* - Resource class + not class-level → load instance via {@link ResourceResolver}
|
|
10
|
-
* (a missing/undefined instance is a denial).
|
|
11
|
-
* - Class-level or no resource → dispatch against the class / ad-hoc gate.
|
|
12
|
-
*
|
|
13
|
-
* The decision is delegated to {@link Gate.authorize}, which throws
|
|
14
|
-
* `ForbiddenException` on denial.
|
|
15
|
-
*/
|
|
16
|
-
export declare class CanGuard implements CanActivate {
|
|
17
|
-
private readonly reflector;
|
|
18
|
-
private readonly gate;
|
|
19
|
-
private readonly moduleRef;
|
|
20
|
-
private readonly resolver?;
|
|
21
|
-
constructor(reflector: Reflector, gate: Gate, moduleRef: ModuleRef, resolver?: ResourceResolver | undefined);
|
|
22
|
-
/**
|
|
23
|
-
* Locate the resource resolver. Prefers the value injected into this module;
|
|
24
|
-
* falls back to a non-strict {@link ModuleRef} lookup so a resolver provided by
|
|
25
|
-
* ANY module (e.g. the app root) is found even though the guard lives in the
|
|
26
|
-
* global AuthzModule.
|
|
27
|
-
*/
|
|
28
|
-
private resolveResolver;
|
|
29
|
-
canActivate(ctx: ExecutionContext): Promise<boolean>;
|
|
30
|
-
}
|
|
31
|
-
//# sourceMappingURL=can.guard.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"can.guard.d.ts","sourceRoot":"","sources":["../../src/guard/can.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,gBAAgB,EAKtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGpD,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAClC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAIhE;;;;;;;;;;GAUG;AACH,qBACa,QAAS,YAAW,WAAW;IAExC,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAG1B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC;gBALT,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,IAAI,EACV,SAAS,EAAE,SAAS,EAGpB,QAAQ,CAAC,EAAE,gBAAgB,YAAA;IAG9C;;;;;OAKG;IACH,OAAO,CAAC,eAAe;IASjB,WAAW,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;CA2B3D"}
|
package/dist/guard/can.guard.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
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;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
-
};
|
|
13
|
-
import { ForbiddenException, Inject, Injectable, Optional, } from '@nestjs/common';
|
|
14
|
-
import { ModuleRef, Reflector } from '@nestjs/core';
|
|
15
|
-
import { ResourceResolverMissingException } from '../errors/exceptions.js';
|
|
16
|
-
import { Gate } from '../gate.js';
|
|
17
|
-
import { CAN_METADATA, RESOURCE_RESOLVER } from '../tokens.js';
|
|
18
|
-
/**
|
|
19
|
-
* Enforces `@Can(ability, Resource?)` on routes.
|
|
20
|
-
*
|
|
21
|
-
* - No `@Can` metadata → allow (the guard is inert on un-annotated routes).
|
|
22
|
-
* - Resource class + not class-level → load instance via {@link ResourceResolver}
|
|
23
|
-
* (a missing/undefined instance is a denial).
|
|
24
|
-
* - Class-level or no resource → dispatch against the class / ad-hoc gate.
|
|
25
|
-
*
|
|
26
|
-
* The decision is delegated to {@link Gate.authorize}, which throws
|
|
27
|
-
* `ForbiddenException` on denial.
|
|
28
|
-
*/
|
|
29
|
-
let CanGuard = class CanGuard {
|
|
30
|
-
reflector;
|
|
31
|
-
gate;
|
|
32
|
-
moduleRef;
|
|
33
|
-
resolver;
|
|
34
|
-
constructor(reflector, gate, moduleRef, resolver) {
|
|
35
|
-
this.reflector = reflector;
|
|
36
|
-
this.gate = gate;
|
|
37
|
-
this.moduleRef = moduleRef;
|
|
38
|
-
this.resolver = resolver;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Locate the resource resolver. Prefers the value injected into this module;
|
|
42
|
-
* falls back to a non-strict {@link ModuleRef} lookup so a resolver provided by
|
|
43
|
-
* ANY module (e.g. the app root) is found even though the guard lives in the
|
|
44
|
-
* global AuthzModule.
|
|
45
|
-
*/
|
|
46
|
-
resolveResolver() {
|
|
47
|
-
if (this.resolver)
|
|
48
|
-
return this.resolver;
|
|
49
|
-
try {
|
|
50
|
-
return this.moduleRef.get(RESOURCE_RESOLVER, { strict: false });
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
return undefined;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
async canActivate(ctx) {
|
|
57
|
-
const meta = this.reflector.getAllAndOverride(CAN_METADATA, [
|
|
58
|
-
ctx.getHandler(),
|
|
59
|
-
ctx.getClass(),
|
|
60
|
-
]);
|
|
61
|
-
if (!meta)
|
|
62
|
-
return true;
|
|
63
|
-
// Ad-hoc gate or class-level → no instance to load.
|
|
64
|
-
if (!meta.resource || meta.classLevel) {
|
|
65
|
-
const resource = meta.classLevel ? meta.resource : undefined;
|
|
66
|
-
await this.gate.authorize(meta.ability, resource);
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
// Instance ability → resolve the resource by route param / app resolver.
|
|
70
|
-
const resolver = this.resolveResolver();
|
|
71
|
-
if (!resolver) {
|
|
72
|
-
throw new ResourceResolverMissingException(meta.ability);
|
|
73
|
-
}
|
|
74
|
-
const instance = await resolver.resolve(meta.resource, ctx);
|
|
75
|
-
if (instance === undefined) {
|
|
76
|
-
// Not found → deny rather than dispatch a policy with a bogus resource.
|
|
77
|
-
throw new ForbiddenException(`Unauthorized: ${meta.ability}`);
|
|
78
|
-
}
|
|
79
|
-
await this.gate.authorize(meta.ability, instance);
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
CanGuard = __decorate([
|
|
84
|
-
Injectable(),
|
|
85
|
-
__param(3, Optional()),
|
|
86
|
-
__param(3, Inject(RESOURCE_RESOLVER)),
|
|
87
|
-
__metadata("design:paramtypes", [Reflector,
|
|
88
|
-
Gate,
|
|
89
|
-
ModuleRef, Object])
|
|
90
|
-
], CanGuard);
|
|
91
|
-
export { CanGuard };
|
|
92
|
-
//# sourceMappingURL=can.guard.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"can.guard.js","sourceRoot":"","sources":["../../src/guard/can.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,E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I,CAAC,UAAU,CAAC,CAAC,CAAE,IAAI,CAAC,QAAgC,CAAC,CAAC,CAAC,SAAS,CAAC;YACtF,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;YAClD,OAAO,IAAI,CAAC;QACd,CAAC;QAED,yEAAyE;QACzE,MAAM,QAAQ,GAAG,IAAI,CAAC,eAAe,EAAE,CAAC;QACxC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,MAAM,IAAI,gCAAgC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC3D,CAAC;QACD,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;QAC5D,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,wEAAwE;YACxE,MAAM,IAAI,kBAAkB,CAAC,iBAAiB,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAChE,CAAC;QACD,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QAClD,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AApDY,QAAQ;IADpB,UAAU,EAAE;IAMR,WAAA,QAAQ,EAAE,CAAA;IACV,WAAA,MAAM,CAAC,iBAAiB,CAAC,CAAA;qCAJE,SAAS;QACd,IAAI;QACC,SAAS;GAJ5B,QAAQ,CAoDpB"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { type CanActivate, type ExecutionContext } from '@nestjs/common';
|
|
2
|
-
import { Reflector } from '@nestjs/core';
|
|
3
|
-
import { Gate } from '../gate.js';
|
|
4
|
-
/**
|
|
5
|
-
* Enforces `@Roles(...roles)` on routes.
|
|
6
|
-
*
|
|
7
|
-
* - No `@Roles` metadata → allow (the guard is inert on un-annotated routes).
|
|
8
|
-
* - Otherwise allow when the current user holds ANY of the listed roles.
|
|
9
|
-
*
|
|
10
|
-
* The current user is resolved EXACTLY as the {@link Gate} does — from the optional
|
|
11
|
-
* context accessor (nestjs-context) — and an unauthenticated request is denied by
|
|
12
|
-
* default (`gate.hasAnyRole` returns `false` for a NO_USER caller). The verdict is
|
|
13
|
-
* delegated to {@link Gate.hasAnyRole}; a denial throws `ForbiddenException`.
|
|
14
|
-
*/
|
|
15
|
-
export declare class RolesGuard implements CanActivate {
|
|
16
|
-
private readonly reflector;
|
|
17
|
-
private readonly gate;
|
|
18
|
-
constructor(reflector: Reflector, gate: Gate);
|
|
19
|
-
canActivate(ctx: ExecutionContext): Promise<boolean>;
|
|
20
|
-
}
|
|
21
|
-
//# sourceMappingURL=roles.guard.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"roles.guard.d.ts","sourceRoot":"","sources":["../../src/guard/roles.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,gBAAgB,EAGtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAGlC;;;;;;;;;;GAUG;AACH,qBACa,UAAW,YAAW,WAAW;IAE1C,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,IAAI;gBADJ,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,IAAI;IAGvB,WAAW,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;CAS3D"}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
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;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
import { ForbiddenException, Injectable, } from '@nestjs/common';
|
|
11
|
-
import { Reflector } from '@nestjs/core';
|
|
12
|
-
import { Gate } from '../gate.js';
|
|
13
|
-
import { ROLES_METADATA } from '../tokens.js';
|
|
14
|
-
/**
|
|
15
|
-
* Enforces `@Roles(...roles)` on routes.
|
|
16
|
-
*
|
|
17
|
-
* - No `@Roles` metadata → allow (the guard is inert on un-annotated routes).
|
|
18
|
-
* - Otherwise allow when the current user holds ANY of the listed roles.
|
|
19
|
-
*
|
|
20
|
-
* The current user is resolved EXACTLY as the {@link Gate} does — from the optional
|
|
21
|
-
* context accessor (nestjs-context) — and an unauthenticated request is denied by
|
|
22
|
-
* default (`gate.hasAnyRole` returns `false` for a NO_USER caller). The verdict is
|
|
23
|
-
* delegated to {@link Gate.hasAnyRole}; a denial throws `ForbiddenException`.
|
|
24
|
-
*/
|
|
25
|
-
let RolesGuard = class RolesGuard {
|
|
26
|
-
reflector;
|
|
27
|
-
gate;
|
|
28
|
-
constructor(reflector, gate) {
|
|
29
|
-
this.reflector = reflector;
|
|
30
|
-
this.gate = gate;
|
|
31
|
-
}
|
|
32
|
-
async canActivate(ctx) {
|
|
33
|
-
const roles = this.reflector.getAllAndOverride(ROLES_METADATA, [
|
|
34
|
-
ctx.getHandler(),
|
|
35
|
-
ctx.getClass(),
|
|
36
|
-
]);
|
|
37
|
-
if (!roles || roles.length === 0)
|
|
38
|
-
return true;
|
|
39
|
-
if (await this.gate.hasAnyRole(roles))
|
|
40
|
-
return true;
|
|
41
|
-
throw new ForbiddenException(`Unauthorized: requires one of [${roles.join(', ')}]`);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
RolesGuard = __decorate([
|
|
45
|
-
Injectable(),
|
|
46
|
-
__metadata("design:paramtypes", [Reflector,
|
|
47
|
-
Gate])
|
|
48
|
-
], RolesGuard);
|
|
49
|
-
export { RolesGuard };
|
|
50
|
-
//# sourceMappingURL=roles.guard.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"roles.guard.js","sourceRoot":"","sources":["../../src/guard/roles.guard.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAGL,kBAAkB,EAClB,UAAU,GACX,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C;;;;;;;;;;GAUG;AAEI,IAAM,UAAU,GAAhB,MAAM,UAAU;IAEF;IACA;IAFnB,YACmB,SAAoB,EACpB,IAAU;QADV,cAAS,GAAT,SAAS,CAAW;QACpB,SAAI,GAAJ,IAAI,CAAM;IAC1B,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,GAAqB;QACrC,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAuB,cAAc,EAAE;YACnF,GAAG,CAAC,UAAU,EAAE;YAChB,GAAG,CAAC,QAAQ,EAAE;SACf,CAAC,CAAC;QACH,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QAC9C,IAAI,MAAM,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QACnD,MAAM,IAAI,kBAAkB,CAAC,kCAAkC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACtF,CAAC;CACF,CAAA;AAfY,UAAU;IADtB,UAAU,EAAE;qCAGmB,SAAS;QACd,IAAI;GAHlB,UAAU,CAetB"}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { type Type } from '@nestjs/common';
|
|
2
|
-
import type { PolicyInstance } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Maps a resource class → its policy instance. Populated at module init from
|
|
5
|
-
* explicit `policies: []` and/or auto-discovered `@Policy` providers.
|
|
6
|
-
*/
|
|
7
|
-
export declare class PolicyRegistry {
|
|
8
|
-
private readonly byResource;
|
|
9
|
-
private classAbilitiesCache;
|
|
10
|
-
/**
|
|
11
|
-
* Register a policy instance. Throws if the instance's class was not decorated
|
|
12
|
-
* with `@Policy(Resource)`.
|
|
13
|
-
*/
|
|
14
|
-
register(policy: PolicyInstance): void;
|
|
15
|
-
/** Resolve the policy instance for a resource class, or `undefined`. */
|
|
16
|
-
forResource(resource: Type<unknown>): PolicyInstance | undefined;
|
|
17
|
-
/** Resolve the policy for the class of a resource instance, or `undefined`. */
|
|
18
|
-
forInstance(instance: object): PolicyInstance | undefined;
|
|
19
|
-
/** True when a policy is registered for the resource class. */
|
|
20
|
-
has(resource: Type<unknown>): boolean;
|
|
21
|
-
/** All registered policies (for introspection/testing). */
|
|
22
|
-
all(): PolicyInstance[];
|
|
23
|
-
/** All registered resource classes (insertion order). */
|
|
24
|
-
resources(): Type<unknown>[];
|
|
25
|
-
/**
|
|
26
|
-
* Enumerate the CLASS-LEVEL ability method names declared on each registered
|
|
27
|
-
* policy, keyed by resource class. Used by integrations that pre-resolve a
|
|
28
|
-
* user's class-level abilities (e.g. to share them as Inertia props).
|
|
29
|
-
*
|
|
30
|
-
* Walks the policy prototype chain and collects own function-valued members,
|
|
31
|
-
* excluding `constructor` and the reserved `before` hook. Inherited Object
|
|
32
|
-
* members are skipped.
|
|
33
|
-
*
|
|
34
|
-
* Only methods that take NO resource instance are included — heuristically,
|
|
35
|
-
* arity `<= 1` (just `user`, e.g. `create(user)` / `viewAny(user)`). An
|
|
36
|
-
* instance method like `update(user, post)` is excluded: dispatching it
|
|
37
|
-
* against the resource CLASS would call it with the class constructor as
|
|
38
|
-
* `post` and write a bogus class-level verdict.
|
|
39
|
-
*/
|
|
40
|
-
classAbilities(): Array<{
|
|
41
|
-
resource: Type<unknown>;
|
|
42
|
-
abilities: string[];
|
|
43
|
-
}>;
|
|
44
|
-
private computeClassAbilities;
|
|
45
|
-
}
|
|
46
|
-
//# sourceMappingURL=policy-registry.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"policy-registry.d.ts","sourceRoot":"","sources":["../src/policy-registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAGvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD;;;GAGG;AACH,qBACa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA4C;IAIvE,OAAO,CAAC,mBAAmB,CAAsE;IAEjG;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI;IAUtC,wEAAwE;IACxE,WAAW,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,cAAc,GAAG,SAAS;IAIhE,+EAA+E;IAC/E,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAIzD,+DAA+D;IAC/D,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,OAAO;IAIrC,2DAA2D;IAC3D,GAAG,IAAI,cAAc,EAAE;IAIvB,yDAAyD;IACzD,SAAS,IAAI,IAAI,CAAC,OAAO,CAAC,EAAE;IAI5B;;;;;;;;;;;;;;OAcG;IACH,cAAc,IAAI,KAAK,CAAC;QAAE,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAAC,SAAS,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAOzE,OAAO,CAAC,qBAAqB;CAsB9B"}
|
package/dist/policy-registry.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
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;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
import { Injectable } from '@nestjs/common';
|
|
8
|
-
import { getPolicyResource } from './decorator/policy.decorator.js';
|
|
9
|
-
import { PolicyNotDecoratedException } from './errors/exceptions.js';
|
|
10
|
-
/**
|
|
11
|
-
* Maps a resource class → its policy instance. Populated at module init from
|
|
12
|
-
* explicit `policies: []` and/or auto-discovered `@Policy` providers.
|
|
13
|
-
*/
|
|
14
|
-
let PolicyRegistry = class PolicyRegistry {
|
|
15
|
-
byResource = new Map();
|
|
16
|
-
// Cache for classAbilities(): the result is static after registration, so it
|
|
17
|
-
// is computed lazily once and invalidated whenever a policy is registered.
|
|
18
|
-
classAbilitiesCache;
|
|
19
|
-
/**
|
|
20
|
-
* Register a policy instance. Throws if the instance's class was not decorated
|
|
21
|
-
* with `@Policy(Resource)`.
|
|
22
|
-
*/
|
|
23
|
-
register(policy) {
|
|
24
|
-
const resource = getPolicyResource(policy);
|
|
25
|
-
if (!resource) {
|
|
26
|
-
const name = policy.constructor?.name ?? 'Policy';
|
|
27
|
-
throw new PolicyNotDecoratedException(name);
|
|
28
|
-
}
|
|
29
|
-
this.byResource.set(resource, policy);
|
|
30
|
-
this.classAbilitiesCache = undefined;
|
|
31
|
-
}
|
|
32
|
-
/** Resolve the policy instance for a resource class, or `undefined`. */
|
|
33
|
-
forResource(resource) {
|
|
34
|
-
return this.byResource.get(resource);
|
|
35
|
-
}
|
|
36
|
-
/** Resolve the policy for the class of a resource instance, or `undefined`. */
|
|
37
|
-
forInstance(instance) {
|
|
38
|
-
return this.byResource.get(instance.constructor);
|
|
39
|
-
}
|
|
40
|
-
/** True when a policy is registered for the resource class. */
|
|
41
|
-
has(resource) {
|
|
42
|
-
return this.byResource.has(resource);
|
|
43
|
-
}
|
|
44
|
-
/** All registered policies (for introspection/testing). */
|
|
45
|
-
all() {
|
|
46
|
-
return [...this.byResource.values()];
|
|
47
|
-
}
|
|
48
|
-
/** All registered resource classes (insertion order). */
|
|
49
|
-
resources() {
|
|
50
|
-
return [...this.byResource.keys()];
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Enumerate the CLASS-LEVEL ability method names declared on each registered
|
|
54
|
-
* policy, keyed by resource class. Used by integrations that pre-resolve a
|
|
55
|
-
* user's class-level abilities (e.g. to share them as Inertia props).
|
|
56
|
-
*
|
|
57
|
-
* Walks the policy prototype chain and collects own function-valued members,
|
|
58
|
-
* excluding `constructor` and the reserved `before` hook. Inherited Object
|
|
59
|
-
* members are skipped.
|
|
60
|
-
*
|
|
61
|
-
* Only methods that take NO resource instance are included — heuristically,
|
|
62
|
-
* arity `<= 1` (just `user`, e.g. `create(user)` / `viewAny(user)`). An
|
|
63
|
-
* instance method like `update(user, post)` is excluded: dispatching it
|
|
64
|
-
* against the resource CLASS would call it with the class constructor as
|
|
65
|
-
* `post` and write a bogus class-level verdict.
|
|
66
|
-
*/
|
|
67
|
-
classAbilities() {
|
|
68
|
-
if (this.classAbilitiesCache === undefined) {
|
|
69
|
-
this.classAbilitiesCache = this.computeClassAbilities();
|
|
70
|
-
}
|
|
71
|
-
return this.classAbilitiesCache;
|
|
72
|
-
}
|
|
73
|
-
computeClassAbilities() {
|
|
74
|
-
const out = [];
|
|
75
|
-
for (const [resource, policy] of this.byResource) {
|
|
76
|
-
const abilities = new Set();
|
|
77
|
-
let proto = Object.getPrototypeOf(policy);
|
|
78
|
-
while (proto && proto !== Object.prototype) {
|
|
79
|
-
for (const name of Object.getOwnPropertyNames(proto)) {
|
|
80
|
-
if (name === 'constructor' || name === 'before')
|
|
81
|
-
continue;
|
|
82
|
-
const member = policy[name];
|
|
83
|
-
// Class-level abilities take only `user` (arity <= 1). A method that
|
|
84
|
-
// also declares a resource param (arity >= 2) is instance-scoped and
|
|
85
|
-
// must not be dispatched against the class.
|
|
86
|
-
if (typeof member === 'function' && member.length <= 1) {
|
|
87
|
-
abilities.add(name);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
proto = Object.getPrototypeOf(proto);
|
|
91
|
-
}
|
|
92
|
-
out.push({ resource, abilities: [...abilities] });
|
|
93
|
-
}
|
|
94
|
-
return out;
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
PolicyRegistry = __decorate([
|
|
98
|
-
Injectable()
|
|
99
|
-
], PolicyRegistry);
|
|
100
|
-
export { PolicyRegistry };
|
|
101
|
-
//# sourceMappingURL=policy-registry.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"policy-registry.js","sourceRoot":"","sources":["../src/policy-registry.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,UAAU,EAAa,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACpE,OAAO,EAAE,2BAA2B,EAAE,MAAM,wBAAwB,CAAC;AAGrE;;;GAGG;AAEI,IAAM,cAAc,GAApB,MAAM,cAAc;IACR,UAAU,GAAG,IAAI,GAAG,EAAiC,CAAC;IAEvE,6EAA6E;IAC7E,2EAA2E;IACnE,mBAAmB,CAAsE;IAEjG;;;OAGG;IACH,QAAQ,CAAC,MAAsB;QAC7B,MAAM,QAAQ,GAAG,iBAAiB,CAAC,MAAM,CAAC,CAAC;QAC3C,IAAI,CAAC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