@attraccess/plugins-backend-sdk 1.8.1 → 1.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@attraccess/plugins-backend-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"types": "./src/index.d.ts",
|
|
@@ -26,9 +26,10 @@
|
|
|
26
26
|
"@nestjs/event-emitter": "^3.1.0",
|
|
27
27
|
"rxjs": "^7.8.2",
|
|
28
28
|
"@nestjs/core": "^11.1.0",
|
|
29
|
-
"typeorm": "0.3.
|
|
29
|
+
"typeorm": "0.3.31",
|
|
30
30
|
"@nestjs/swagger": "^11.2.0",
|
|
31
31
|
"express": "4.22.1",
|
|
32
|
-
"@nestjs/passport": "^11.0.5"
|
|
32
|
+
"@nestjs/passport": "^11.0.5",
|
|
33
|
+
"@attraccess/shared": "*"
|
|
33
34
|
}
|
|
34
35
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { ExecutionContext, CanActivate } from '@nestjs/common';
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
3
|
import { Reflector } from '@nestjs/core';
|
|
4
|
-
import { SystemPermission } from '@attraccess/
|
|
4
|
+
import { SystemPermission } from '@attraccess/shared';
|
|
5
|
+
export type { SystemPermission };
|
|
5
6
|
export declare function Auth(...permissions: SystemPermission[]): <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
6
|
-
export declare
|
|
7
|
+
export declare function AuthAny(...permissions: SystemPermission[]): <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
8
|
+
export declare class EffectivePermissionsGuard implements CanActivate {
|
|
7
9
|
private reflector;
|
|
8
10
|
private readonly logger;
|
|
9
11
|
constructor(reflector: Reflector);
|
|
10
12
|
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean>;
|
|
11
|
-
private matchPermissions;
|
|
12
13
|
}
|
|
14
|
+
export declare const SystemPermissionsGuard: typeof EffectivePermissionsGuard;
|
|
@@ -1,25 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
2
|
+
var EffectivePermissionsGuard_1;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.SystemPermissionsGuard = void 0;
|
|
4
|
+
exports.SystemPermissionsGuard = exports.EffectivePermissionsGuard = void 0;
|
|
5
5
|
exports.Auth = Auth;
|
|
6
|
+
exports.AuthAny = AuthAny;
|
|
6
7
|
const tslib_1 = require("tslib");
|
|
7
8
|
const common_1 = require("@nestjs/common");
|
|
8
9
|
const core_1 = require("@nestjs/core");
|
|
9
10
|
const swagger_1 = require("@nestjs/swagger");
|
|
10
11
|
const dual_auth_guard_1 = require("./dual-auth.guard");
|
|
11
|
-
const
|
|
12
|
+
const NeedsPermissions = core_1.Reflector.createDecorator();
|
|
13
|
+
const NeedsAnyPermission = core_1.Reflector.createDecorator();
|
|
12
14
|
function Auth(...permissions) {
|
|
13
|
-
return (0, common_1.applyDecorators)(
|
|
15
|
+
return (0, common_1.applyDecorators)(NeedsPermissions(permissions), (0, common_1.UseGuards)(dual_auth_guard_1.DualAuthGuard, EffectivePermissionsGuard), (0, swagger_1.ApiBearerAuth)(), (0, swagger_1.ApiUnauthorizedResponse)({ description: 'Unauthorized' }));
|
|
14
16
|
}
|
|
15
|
-
|
|
17
|
+
function AuthAny(...permissions) {
|
|
18
|
+
return (0, common_1.applyDecorators)(NeedsAnyPermission(permissions), (0, common_1.UseGuards)(dual_auth_guard_1.DualAuthGuard, EffectivePermissionsGuard), (0, swagger_1.ApiBearerAuth)(), (0, swagger_1.ApiUnauthorizedResponse)({ description: 'Unauthorized' }));
|
|
19
|
+
}
|
|
20
|
+
let EffectivePermissionsGuard = EffectivePermissionsGuard_1 = class EffectivePermissionsGuard {
|
|
16
21
|
constructor(reflector) {
|
|
17
22
|
this.reflector = reflector;
|
|
18
|
-
this.logger = new common_1.Logger(
|
|
23
|
+
this.logger = new common_1.Logger(EffectivePermissionsGuard_1.name);
|
|
19
24
|
}
|
|
20
25
|
canActivate(context) {
|
|
21
|
-
const
|
|
22
|
-
|
|
26
|
+
const requiredAll = this.reflector.getAllAndOverride(NeedsPermissions, [
|
|
27
|
+
context.getHandler(),
|
|
28
|
+
context.getClass(),
|
|
29
|
+
]);
|
|
30
|
+
const requiredAny = this.reflector.getAllAndOverride(NeedsAnyPermission, [
|
|
31
|
+
context.getHandler(),
|
|
32
|
+
context.getClass(),
|
|
33
|
+
]);
|
|
34
|
+
if ((!requiredAll || requiredAll.length === 0) && (!requiredAny || requiredAny.length === 0)) {
|
|
23
35
|
return true;
|
|
24
36
|
}
|
|
25
37
|
const request = context.switchToHttp().getRequest();
|
|
@@ -28,22 +40,27 @@ let SystemPermissionsGuard = SystemPermissionsGuard_1 = class SystemPermissionsG
|
|
|
28
40
|
this.logger.warn('Auth check - No user found in request');
|
|
29
41
|
throw new common_1.UnauthorizedException();
|
|
30
42
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
if (requiredAll && requiredAll.length > 0) {
|
|
44
|
+
const missing = requiredAll.filter((p) => { var _a; return !((_a = user.effectivePermissions) === null || _a === void 0 ? void 0 : _a.has(p)); });
|
|
45
|
+
if (missing.length > 0) {
|
|
46
|
+
this.logger.debug(`User ${user.id} missing permissions: ${missing.join(', ')}`);
|
|
47
|
+
throw new common_1.ForbiddenException('Insufficient permissions');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (requiredAny && requiredAny.length > 0) {
|
|
51
|
+
const hasAny = requiredAny.some((p) => { var _a; return (_a = user.effectivePermissions) === null || _a === void 0 ? void 0 : _a.has(p); });
|
|
52
|
+
if (!hasAny) {
|
|
53
|
+
this.logger.debug(`User ${user.id} has none of the required permissions: ${requiredAny.join(', ')}`);
|
|
54
|
+
throw new common_1.ForbiddenException('Insufficient permissions');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
42
58
|
}
|
|
43
59
|
};
|
|
44
|
-
exports.
|
|
45
|
-
exports.
|
|
60
|
+
exports.EffectivePermissionsGuard = EffectivePermissionsGuard;
|
|
61
|
+
exports.EffectivePermissionsGuard = EffectivePermissionsGuard = EffectivePermissionsGuard_1 = tslib_1.__decorate([
|
|
46
62
|
(0, common_1.Injectable)(),
|
|
47
63
|
tslib_1.__metadata("design:paramtypes", [core_1.Reflector])
|
|
48
|
-
],
|
|
64
|
+
], EffectivePermissionsGuard);
|
|
65
|
+
exports.SystemPermissionsGuard = EffectivePermissionsGuard;
|
|
49
66
|
//# sourceMappingURL=auth-decorator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-decorator.js","sourceRoot":"","sources":["../../../../../libs/plugins-backend-sdk/src/lib/auth-decorator.ts"],"names":[],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"auth-decorator.js","sourceRoot":"","sources":["../../../../../libs/plugins-backend-sdk/src/lib/auth-decorator.ts"],"names":[],"mappings":";;;;AAuBA,oBAOC;AAED,0BAOC;;AAvCD,2CASwB;AAExB,uCAAyC;AAEzC,6CAAyE;AACzE,uDAAkD;AAMlD,MAAM,gBAAgB,GAAG,gBAAS,CAAC,eAAe,EAAsB,CAAC;AACzE,MAAM,kBAAkB,GAAG,gBAAS,CAAC,eAAe,EAAsB,CAAC;AAE3E,SAAgB,IAAI,CAAC,GAAG,WAA+B;IACrD,OAAO,IAAA,wBAAe,EACpB,gBAAgB,CAAC,WAAW,CAAC,EAC7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IADrC,IAAA,mBAAU,GAAE;6CAIoB,gBAAS;GAH7B,yBAAyB,CA6CrC;AAEY,QAAA,sBAAsB,GAAG,yBAAyB,CAAC"}
|
package/src/lib/auth.types.d.ts
CHANGED
|
@@ -2,6 +2,13 @@ import { User } from '@attraccess/database-entities';
|
|
|
2
2
|
import { Request as BaseRequest } from 'express';
|
|
3
3
|
export interface AuthenticatedUser extends User {
|
|
4
4
|
jwtTokenId: string;
|
|
5
|
+
/**
|
|
6
|
+
* Effective permissions for this user. Stored as a Set for O(1) has() lookups.
|
|
7
|
+
* Note: A plain Set serializes as {} in JSON — spread to an array before including in any HTTP response.
|
|
8
|
+
* In practice the runtime value is a SerializableSet whose toJSON() emits an array,
|
|
9
|
+
* but callers should not rely on that implementation detail.
|
|
10
|
+
*/
|
|
11
|
+
effectivePermissions?: Set<string>;
|
|
5
12
|
}
|
|
6
13
|
export interface AuthenticatedRequest extends Omit<BaseRequest, 'logout'> {
|
|
7
14
|
user: AuthenticatedUser;
|