@mahmoud-auth/auth 0.0.0-stage → 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 +21 -0
- package/README.md +8 -2
- package/dist/auth-guard/authorization-context.class.d.ts +17 -0
- package/dist/auth-guard/authorization-context.class.js +51 -0
- package/dist/auth-guard/index.d.ts +18 -0
- package/dist/auth-guard/index.js +101 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +17 -0
- package/dist/core/resource.d.ts +5 -0
- package/dist/core/resource.js +15 -0
- package/dist/decorators/authorize.decorator.d.ts +8 -0
- package/dist/decorators/authorize.decorator.js +22 -0
- package/dist/decorators/index.d.ts +3 -0
- package/dist/decorators/index.js +19 -0
- package/dist/decorators/permission.decorator.d.ts +2 -0
- package/dist/decorators/permission.decorator.js +7 -0
- package/dist/decorators/policy.decorator.d.ts +3 -0
- package/dist/decorators/policy.decorator.js +7 -0
- package/dist/dsl/can.d.ts +2 -0
- package/dist/dsl/can.js +9 -0
- package/dist/dsl/condition.d.ts +2 -0
- package/dist/dsl/condition.js +9 -0
- package/dist/dsl/index.d.ts +3 -0
- package/dist/dsl/index.js +19 -0
- package/dist/dsl/trinary/and.d.ts +2 -0
- package/dist/dsl/trinary/and.js +8 -0
- package/dist/dsl/trinary/index.d.ts +3 -0
- package/dist/dsl/trinary/index.js +19 -0
- package/dist/dsl/trinary/not.d.ts +2 -0
- package/dist/dsl/trinary/not.js +8 -0
- package/dist/dsl/trinary/or.d.ts +2 -0
- package/dist/dsl/trinary/or.js +8 -0
- package/dist/errors/circular-policy.exception.d.ts +4 -0
- package/dist/errors/circular-policy.exception.js +10 -0
- package/dist/errors/duplicate-policy.exception.d.ts +4 -0
- package/dist/errors/duplicate-policy.exception.js +10 -0
- package/dist/errors/index.d.ts +4 -0
- package/dist/errors/index.js +20 -0
- package/dist/errors/missing-policy.exception.d.ts +4 -0
- package/dist/errors/missing-policy.exception.js +11 -0
- package/dist/errors/out-of-bounds-action.exception.d.ts +5 -0
- package/dist/errors/out-of-bounds-action.exception.js +13 -0
- package/dist/evaluation/index.d.ts +1 -0
- package/dist/evaluation/index.js +17 -0
- package/dist/evaluation/service.d.ts +16 -0
- package/dist/evaluation/service.js +125 -0
- package/dist/extensions/index.d.ts +1 -0
- package/dist/extensions/index.js +17 -0
- package/dist/extensions/token/index.d.ts +4 -0
- package/dist/extensions/token/index.js +10 -0
- package/dist/feature-shortcuts/index.d.ts +4 -0
- package/dist/feature-shortcuts/index.js +7 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +24 -0
- package/dist/mahmoud.module.d.ts +16 -0
- package/dist/mahmoud.module.js +46 -0
- package/dist/policy/compiler.d.ts +12 -0
- package/dist/policy/compiler.js +65 -0
- package/dist/policy/index.d.ts +4 -0
- package/dist/policy/index.js +20 -0
- package/dist/policy/module.d.ts +9 -0
- package/dist/policy/module.js +42 -0
- package/dist/policy/permission-node.type.d.ts +5 -0
- package/dist/policy/permission-node.type.js +2 -0
- package/dist/policy/registry.d.ts +17 -0
- package/dist/policy/registry.js +113 -0
- package/dist/policy/scanner.d.ts +12 -0
- package/dist/policy/scanner.js +47 -0
- package/dist/policy/validator.d.ts +8 -0
- package/dist/policy/validator.js +61 -0
- package/dist/policy/visualizer/controller.d.ts +6 -0
- package/dist/policy/visualizer/controller.js +34 -0
- package/dist/policy/visualizer/index.d.ts +1 -0
- package/dist/policy/visualizer/index.js +17 -0
- package/dist/tracing/index.d.ts +2 -0
- package/dist/tracing/index.js +18 -0
- package/dist/tracing/module.d.ts +8 -0
- package/dist/tracing/module.js +35 -0
- package/dist/tracing/service.d.ts +17 -0
- package/dist/tracing/service.js +126 -0
- package/dist/tracing/tokens/activate-trace.token.d.ts +3 -0
- package/dist/tracing/tokens/activate-trace.token.js +10 -0
- package/dist/tracing/tokens/evaluation-id.token.d.ts +4 -0
- package/dist/tracing/tokens/evaluation-id.token.js +10 -0
- package/dist/tracing/tokens/index.d.ts +2 -0
- package/dist/tracing/tokens/index.js +18 -0
- package/package.json +39 -4
- package/public/policy-ui/assets/index-BwE3sQ6i.js +149 -0
- package/public/policy-ui/assets/index-CYyoke_2.css +1 -0
- package/public/policy-ui/favicon.svg +1 -0
- package/public/policy-ui/icons.svg +24 -0
- package/public/policy-ui/index.html +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The Mahmoud Project contributors
|
|
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
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mahmoud-auth/auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Policy-based authorization for NestJS, including resource policies, route authorization, policy evaluation, and optional inspection endpoints.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @mahmoud-auth/auth
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Import `MahmoudAuthModule`, policy decorators, and authorization helpers from the package root. Configure `MahmoudAuthModule.forRoot({ resources })` in the application module. NestJS and `reflect-metadata` are peer dependencies.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Type } from "@nestjs/common";
|
|
2
|
+
import { IAuthorizationContext } from "@mahmoud-auth/common";
|
|
3
|
+
import { ResourceResolverService } from "@mahmoud-auth/model-climbers";
|
|
4
|
+
export declare class AuthorizationContext<TRequest extends Request = Request> implements IAuthorizationContext<TRequest> {
|
|
5
|
+
private readonly extensions;
|
|
6
|
+
private readonly resourceResolver;
|
|
7
|
+
private readonly rootType;
|
|
8
|
+
private readonly rootResource;
|
|
9
|
+
private readonly rootRequest;
|
|
10
|
+
readonly user: unknown;
|
|
11
|
+
readonly data: unknown;
|
|
12
|
+
private readonly resourceCache;
|
|
13
|
+
constructor(extensions: Map<Type, unknown>, resourceResolver: ResourceResolverService, rootType: Type, rootResource: unknown, rootRequest: TRequest, user: unknown, data: unknown);
|
|
14
|
+
request(): TRequest;
|
|
15
|
+
use<T>(type: Type<T>): T;
|
|
16
|
+
get<T>(resourceType: Type<T>): Promise<T | undefined>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthorizationContext = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
class AuthorizationContext {
|
|
6
|
+
extensions;
|
|
7
|
+
resourceResolver;
|
|
8
|
+
rootType;
|
|
9
|
+
rootResource;
|
|
10
|
+
rootRequest;
|
|
11
|
+
user;
|
|
12
|
+
data;
|
|
13
|
+
resourceCache = new Map();
|
|
14
|
+
constructor(extensions, resourceResolver, rootType, rootResource, rootRequest, user, data) {
|
|
15
|
+
this.extensions = extensions;
|
|
16
|
+
this.resourceResolver = resourceResolver;
|
|
17
|
+
this.rootType = rootType;
|
|
18
|
+
this.rootResource = rootResource;
|
|
19
|
+
this.rootRequest = rootRequest;
|
|
20
|
+
this.user = user;
|
|
21
|
+
this.data = data;
|
|
22
|
+
this.resourceCache.set(rootType, rootResource);
|
|
23
|
+
}
|
|
24
|
+
request() {
|
|
25
|
+
return this.rootRequest;
|
|
26
|
+
}
|
|
27
|
+
use(type) {
|
|
28
|
+
const extension = this.extensions.get(type);
|
|
29
|
+
if (!extension) {
|
|
30
|
+
throw new common_1.InternalServerErrorException(`Authorization extension ${type.name} was not registered`);
|
|
31
|
+
}
|
|
32
|
+
return extension;
|
|
33
|
+
}
|
|
34
|
+
async get(resourceType) {
|
|
35
|
+
const hasResource = this.resourceCache.has(resourceType);
|
|
36
|
+
const cachedResource = this.resourceCache.get(resourceType);
|
|
37
|
+
if (hasResource) {
|
|
38
|
+
return cachedResource;
|
|
39
|
+
}
|
|
40
|
+
const resolvedResource = await this.resourceResolver.resolve({
|
|
41
|
+
rootType: this.rootType,
|
|
42
|
+
rootResource: this.rootResource,
|
|
43
|
+
resourceCache: this.resourceCache
|
|
44
|
+
}, resourceType);
|
|
45
|
+
if (resolvedResource) {
|
|
46
|
+
this.resourceCache.set(resourceType, resolvedResource);
|
|
47
|
+
}
|
|
48
|
+
return resolvedResource;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.AuthorizationContext = AuthorizationContext;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CanActivate, ExecutionContext, Type } from '@nestjs/common';
|
|
2
|
+
import { Reflector } from '@nestjs/core';
|
|
3
|
+
import { ResourceProviderRegistry, ResourceResolverService } from '@mahmoud-auth/model-climbers';
|
|
4
|
+
import { AuthorizationMetadata } from '../decorators/authorize.decorator';
|
|
5
|
+
import { EvaluationService } from '../evaluation';
|
|
6
|
+
import { AuthorizationTraceService } from '../tracing';
|
|
7
|
+
export declare class AuthorizationGuard<T> implements CanActivate {
|
|
8
|
+
private reflector;
|
|
9
|
+
private authService;
|
|
10
|
+
private readonly providerRegistry;
|
|
11
|
+
private readonly resourceResolver;
|
|
12
|
+
private readonly trace;
|
|
13
|
+
private readonly extensions?;
|
|
14
|
+
constructor(reflector: Reflector, authService: EvaluationService, providerRegistry: ResourceProviderRegistry, resourceResolver: ResourceResolverService, trace: AuthorizationTraceService, extensions?: Map<Type, unknown> | undefined);
|
|
15
|
+
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
16
|
+
private resolveResource;
|
|
17
|
+
}
|
|
18
|
+
export declare const Authorize: <T, const TActions extends string[]>(metadata: AuthorizationMetadata<T, TActions>) => MethodDecorator;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.Authorize = exports.AuthorizationGuard = void 0;
|
|
19
|
+
const common_1 = require("@nestjs/common");
|
|
20
|
+
const core_1 = require("@nestjs/core");
|
|
21
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
22
|
+
const model_climbers_1 = require("@mahmoud-auth/model-climbers");
|
|
23
|
+
const authorize_decorator_1 = require("../decorators/authorize.decorator");
|
|
24
|
+
const evaluation_1 = require("../evaluation");
|
|
25
|
+
const extensions_1 = require("../extensions");
|
|
26
|
+
const authorization_context_class_1 = require("./authorization-context.class");
|
|
27
|
+
const tracing_1 = require("../tracing");
|
|
28
|
+
let AuthorizationGuard = class AuthorizationGuard {
|
|
29
|
+
reflector;
|
|
30
|
+
authService;
|
|
31
|
+
providerRegistry;
|
|
32
|
+
resourceResolver;
|
|
33
|
+
trace;
|
|
34
|
+
extensions;
|
|
35
|
+
constructor(reflector, authService, providerRegistry, resourceResolver, trace, extensions) {
|
|
36
|
+
this.reflector = reflector;
|
|
37
|
+
this.authService = authService;
|
|
38
|
+
this.providerRegistry = providerRegistry;
|
|
39
|
+
this.resourceResolver = resourceResolver;
|
|
40
|
+
this.trace = trace;
|
|
41
|
+
this.extensions = extensions;
|
|
42
|
+
}
|
|
43
|
+
async canActivate(context) {
|
|
44
|
+
const authorizationMetadata = this.reflector.get(authorize_decorator_1.RESOURCE_AUTHORIZE_METADATA, context.getHandler());
|
|
45
|
+
if (!authorizationMetadata) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
const request = context.switchToHttp().getRequest();
|
|
49
|
+
const resource = await this.resolveResource(authorizationMetadata, request);
|
|
50
|
+
return this.authService.can(authorizationMetadata.resourceToken, authorizationMetadata.action, new authorization_context_class_1.AuthorizationContext(this.extensions || new Map(), this.resourceResolver, authorizationMetadata.resourceToken.model, resource, request, request.user, request.body), request.url);
|
|
51
|
+
}
|
|
52
|
+
async resolveResource(metadata, request) {
|
|
53
|
+
if (!metadata.identifierPath) {
|
|
54
|
+
this.trace.resourceResolved(metadata.resourceToken.name, 'skipped');
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const id = lodash_1.default.get(request, metadata.identifierPath);
|
|
58
|
+
if (!id) {
|
|
59
|
+
this.trace.resourceResolved(metadata.resourceToken.name, 'missing_id');
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const provider = this.providerRegistry.get(metadata.resourceToken.model);
|
|
63
|
+
if (!provider) {
|
|
64
|
+
const error = new model_climbers_1.MissingResourceProviderException(metadata.resourceToken.name);
|
|
65
|
+
this.trace.resourceResolved(metadata.resourceToken.name, 'error', error);
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
const resource = await provider(id);
|
|
70
|
+
this.trace.resourceResolved(metadata.resourceToken.name, resource ? 'found' : 'not_found');
|
|
71
|
+
return resource;
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
this.trace.resourceResolved(metadata.resourceToken.name, 'error', error);
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
exports.AuthorizationGuard = AuthorizationGuard;
|
|
80
|
+
exports.AuthorizationGuard = AuthorizationGuard = __decorate([
|
|
81
|
+
(0, common_1.Injectable)(),
|
|
82
|
+
__param(5, (0, common_1.Optional)()),
|
|
83
|
+
__param(5, (0, extensions_1.InjectMahmoudExtensions)()),
|
|
84
|
+
__metadata("design:paramtypes", [core_1.Reflector,
|
|
85
|
+
evaluation_1.EvaluationService,
|
|
86
|
+
model_climbers_1.ResourceProviderRegistry,
|
|
87
|
+
model_climbers_1.ResourceResolverService,
|
|
88
|
+
tracing_1.AuthorizationTraceService,
|
|
89
|
+
Map])
|
|
90
|
+
], AuthorizationGuard);
|
|
91
|
+
const Authorize = (metadata) => {
|
|
92
|
+
const routeDecorator = (0, common_1.applyDecorators)((0, authorize_decorator_1.ResourceAuthorize)(metadata), (0, common_1.UseGuards)((AuthorizationGuard)));
|
|
93
|
+
const methodDecorator = (target, propertyKey, descriptor) => {
|
|
94
|
+
if (propertyKey === undefined) {
|
|
95
|
+
throw new TypeError('@Authorize can only be applied to route methods');
|
|
96
|
+
}
|
|
97
|
+
routeDecorator(target, propertyKey, descriptor);
|
|
98
|
+
};
|
|
99
|
+
return methodDecorator;
|
|
100
|
+
};
|
|
101
|
+
exports.Authorize = Authorize;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './resource';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./resource"), exports);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { IResourceToken } from "@mahmoud-auth/common";
|
|
2
|
+
type CreateResourcePolicyProps<T, TActions extends string[]> = Pick<IResourceToken<T, TActions>, 'model' | 'actions'>;
|
|
3
|
+
export declare const createResourcePolicy: <T, const TActions extends string[]>({ model, actions }: CreateResourcePolicyProps<T, TActions>) => IResourceToken<T, TActions>;
|
|
4
|
+
export declare const createResourceKey: <T>(resource: IResourceToken<T>, action: string) => string;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createResourceKey = exports.createResourcePolicy = void 0;
|
|
4
|
+
const utils_1 = require("@mahmoud-auth/utils");
|
|
5
|
+
const createResourcePolicy = ({ model, actions }) => ({
|
|
6
|
+
name: `${model.name}Policy`,
|
|
7
|
+
model,
|
|
8
|
+
actions,
|
|
9
|
+
mahmoudId: (0, utils_1.nanoid)(8)
|
|
10
|
+
});
|
|
11
|
+
exports.createResourcePolicy = createResourcePolicy;
|
|
12
|
+
const createResourceKey = (resource, action) => {
|
|
13
|
+
return `${resource.name}:${action}`;
|
|
14
|
+
};
|
|
15
|
+
exports.createResourceKey = createResourceKey;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IResourceToken } from '@mahmoud-auth/common';
|
|
2
|
+
export declare const RESOURCE_AUTHORIZE_METADATA: unique symbol;
|
|
3
|
+
export interface AuthorizationMetadata<T, TActions extends string[] = string[]> {
|
|
4
|
+
resourceToken: IResourceToken<T, TActions>;
|
|
5
|
+
action: TActions[number];
|
|
6
|
+
identifierPath: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const ResourceAuthorize: <T, const TActions extends string[]>(metadata: AuthorizationMetadata<T, TActions>) => import("@nestjs/common").CustomDecorator<typeof RESOURCE_AUTHORIZE_METADATA>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResourceAuthorize = exports.RESOURCE_AUTHORIZE_METADATA = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const errors_1 = require("../errors");
|
|
6
|
+
exports.RESOURCE_AUTHORIZE_METADATA = Symbol('RESOURCE_AUTHORIZE');
|
|
7
|
+
const PATH_REGEX = /^(params|query|body)(\.[a-zA-Z_$][\w$]*)+$/;
|
|
8
|
+
const validateIdPath = (identifierPath) => {
|
|
9
|
+
if (!PATH_REGEX.test(identifierPath)) {
|
|
10
|
+
throw new common_1.InternalServerErrorException(`Invalid identifierPath "${identifierPath}". Expected format examples: params.id, query.id, body.id`);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const ResourceAuthorize = (metadata) => {
|
|
14
|
+
if (!metadata.resourceToken.actions.includes(metadata.action)) {
|
|
15
|
+
throw new errors_1.OutOfBoundsActionException(metadata.resourceToken.name, metadata.action, metadata.resourceToken.actions);
|
|
16
|
+
}
|
|
17
|
+
if (metadata.identifierPath) {
|
|
18
|
+
validateIdPath(metadata.identifierPath);
|
|
19
|
+
}
|
|
20
|
+
return (0, common_1.SetMetadata)(exports.RESOURCE_AUTHORIZE_METADATA, metadata);
|
|
21
|
+
};
|
|
22
|
+
exports.ResourceAuthorize = ResourceAuthorize;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./authorize.decorator"), exports);
|
|
18
|
+
__exportStar(require("./permission.decorator"), exports);
|
|
19
|
+
__exportStar(require("./policy.decorator"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Permission = exports.PERMISSION_METADATA = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.PERMISSION_METADATA = Symbol('PERMISSION');
|
|
6
|
+
const Permission = (action) => ((0, common_1.SetMetadata)(exports.PERMISSION_METADATA, action));
|
|
7
|
+
exports.Permission = Permission;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResourcePolicy = exports.RESOURCE_POLICY_METADATA = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.RESOURCE_POLICY_METADATA = Symbol('RESOURCE_POLICY');
|
|
6
|
+
const ResourcePolicy = (resource) => (0, common_1.SetMetadata)(exports.RESOURCE_POLICY_METADATA, resource);
|
|
7
|
+
exports.ResourcePolicy = ResourcePolicy;
|
package/dist/dsl/can.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./can"), exports);
|
|
18
|
+
__exportStar(require("./condition"), exports);
|
|
19
|
+
__exportStar(require("./trinary"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./and"), exports);
|
|
18
|
+
__exportStar(require("./not"), exports);
|
|
19
|
+
__exportStar(require("./or"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CircularPolicyException = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
class CircularPolicyException extends common_1.InternalServerErrorException {
|
|
6
|
+
constructor(path) {
|
|
7
|
+
super(`Circular authorization dependency detected: ${path.join(' -> ')}`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.CircularPolicyException = CircularPolicyException;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DuplicatePolicyException = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
class DuplicatePolicyException extends common_1.InternalServerErrorException {
|
|
6
|
+
constructor(resource, action) {
|
|
7
|
+
super(`Duplicate policy found for ${resource}.${action}`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.DuplicatePolicyException = DuplicatePolicyException;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./circular-policy.exception"), exports);
|
|
18
|
+
__exportStar(require("./missing-policy.exception"), exports);
|
|
19
|
+
__exportStar(require("./duplicate-policy.exception"), exports);
|
|
20
|
+
__exportStar(require("./out-of-bounds-action.exception"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MissingPolicyException = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
class MissingPolicyException extends common_1.InternalServerErrorException {
|
|
6
|
+
constructor(resource, action, route) {
|
|
7
|
+
const routeMessage = route ? `in route ${route}` : '';
|
|
8
|
+
super(`No authorization policy found for ${resource}.${action} ${routeMessage}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.MissingPolicyException = MissingPolicyException;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { InternalServerErrorException } from "@nestjs/common";
|
|
2
|
+
export declare class OutOfBoundsActionException extends InternalServerErrorException {
|
|
3
|
+
readonly validActions?: string[] | undefined;
|
|
4
|
+
constructor(resource: string, action: string, validActions?: string[] | undefined);
|
|
5
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OutOfBoundsActionException = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
class OutOfBoundsActionException extends common_1.InternalServerErrorException {
|
|
6
|
+
validActions;
|
|
7
|
+
constructor(resource, action, validActions) {
|
|
8
|
+
const validActionsMessage = validActions ? `in actions [${validActions.map(validAction => `'${validAction}'`).join(', ')}]` : '';
|
|
9
|
+
super(`Action '${action}' is not a valid action for resource ${resource} ${validActionsMessage}`);
|
|
10
|
+
this.validActions = validActions;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.OutOfBoundsActionException = OutOfBoundsActionException;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './service';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./service"), exports);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IAuthorizationContext, IResourceToken } from "@mahmoud-auth/common";
|
|
2
|
+
import { PolicyRegistry } from "../policy";
|
|
3
|
+
import { AuthorizationTraceService } from "../tracing";
|
|
4
|
+
export declare class EvaluationService {
|
|
5
|
+
private readonly policyRegistry;
|
|
6
|
+
private readonly trace;
|
|
7
|
+
constructor(policyRegistry: PolicyRegistry, trace: AuthorizationTraceService);
|
|
8
|
+
can<T>(resource: IResourceToken<T>, action: string, context: IAuthorizationContext, route?: string): Promise<boolean>;
|
|
9
|
+
private evaluate;
|
|
10
|
+
private evaluatePermission;
|
|
11
|
+
private evaluateCondition;
|
|
12
|
+
private evaluateChildren;
|
|
13
|
+
private evaluateAnd;
|
|
14
|
+
private evaluateOr;
|
|
15
|
+
private evaluateNot;
|
|
16
|
+
}
|