@open-kingdom/shared-backend-util-rbac 0.0.2-17

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/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # @open-kingdom/shared-backend-util-rbac
2
+
3
+ Decorators, guards, and tokens for role-based access control in NestJS.
4
+
5
+ ## Setup
6
+
7
+ Register both guards globally in `AppModule`. `JwtAuthGuard` must run first to populate `req.user`.
8
+
9
+ ```typescript
10
+ import { APP_GUARD } from '@nestjs/core';
11
+ import { JwtAuthGuard } from '@open-kingdom/shared-backend-feature-authentication';
12
+ import { PermissionGuard, ROLE_RESOLVER } from '@open-kingdom/shared-backend-util-rbac';
13
+ import { UserRolesService } from '@open-kingdom/shared-backend-feature-user-management';
14
+
15
+ providers: [
16
+ { provide: APP_GUARD, useClass: JwtAuthGuard },
17
+ { provide: APP_GUARD, useClass: PermissionGuard },
18
+ { provide: ROLE_RESOLVER, useExisting: UserRolesService },
19
+ ];
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```typescript
25
+ import { Public, RequirePermission } from '@open-kingdom/shared-backend-util-rbac';
26
+
27
+ @Controller('users')
28
+ export class UsersController {
29
+ @Public()
30
+ @Post('login')
31
+ login() { ... }
32
+
33
+ @RequirePermission('users', 'read')
34
+ @Get()
35
+ findAll() { ... }
36
+ }
37
+ ```
38
+
39
+ `@Public()` bypasses both JWT auth and permission checks. It takes precedence over `@RequirePermission()`.
40
+
41
+ Permissions are checked against the database on every request via `ROLE_RESOLVER.findPermissions(userId)` — not from the JWT.
@@ -0,0 +1,6 @@
1
+ export { RequirePermission, REQUIRED_PERMISSION_KEY, } from './lib/decorators/require-permission.decorator.js';
2
+ export { Public, IS_PUBLIC_KEY } from './lib/decorators/public.decorator.js';
3
+ export { PermissionGuard } from './lib/guards/permission.guard.js';
4
+ export { ROLE_RESOLVER } from './lib/role-resolver.js';
5
+ export type { RoleResolver } from './lib/role-resolver.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,kDAAkD,CAAC;AAC1D,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,YAAY,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { RequirePermission, REQUIRED_PERMISSION_KEY, } from './lib/decorators/require-permission.decorator.js';
2
+ export { Public, IS_PUBLIC_KEY } from './lib/decorators/public.decorator.js';
3
+ export { PermissionGuard } from './lib/guards/permission.guard.js';
4
+ export { ROLE_RESOLVER } from './lib/role-resolver.js';
@@ -0,0 +1,3 @@
1
+ export declare const IS_PUBLIC_KEY = "isPublic";
2
+ export declare const Public: () => import("@nestjs/common").CustomDecorator<string>;
3
+ //# sourceMappingURL=public.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public.decorator.d.ts","sourceRoot":"","sources":["../../../src/lib/decorators/public.decorator.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,aAAa,CAAC;AACxC,eAAO,MAAM,MAAM,wDAAyC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { SetMetadata } from '@nestjs/common';
2
+ export const IS_PUBLIC_KEY = 'isPublic';
3
+ export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);
@@ -0,0 +1,3 @@
1
+ export declare const REQUIRED_PERMISSION_KEY = "requiredPermission";
2
+ export declare const RequirePermission: (resource: string, action: string) => import("@nestjs/common").CustomDecorator<string>;
3
+ //# sourceMappingURL=require-permission.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"require-permission.decorator.d.ts","sourceRoot":"","sources":["../../../src/lib/decorators/require-permission.decorator.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,uBAAuB,uBAAuB,CAAC;AAE5D,eAAO,MAAM,iBAAiB,GAAI,UAAU,MAAM,EAAE,QAAQ,MAAM,qDACH,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { SetMetadata } from '@nestjs/common';
2
+ export const REQUIRED_PERMISSION_KEY = 'requiredPermission';
3
+ export const RequirePermission = (resource, action) => SetMetadata(REQUIRED_PERMISSION_KEY, `${resource}:${action}`);
@@ -0,0 +1,12 @@
1
+ import { CanActivate, ExecutionContext, OnModuleInit } from '@nestjs/common';
2
+ import { Reflector } from '@nestjs/core';
3
+ import type { RoleResolver } from '../role-resolver.js';
4
+ export declare class PermissionGuard implements CanActivate, OnModuleInit {
5
+ private readonly reflector;
6
+ private readonly roleResolver?;
7
+ private resolvedRoleResolver;
8
+ constructor(reflector: Reflector, roleResolver?: RoleResolver | undefined);
9
+ onModuleInit(): void;
10
+ canActivate(context: ExecutionContext): Promise<boolean>;
11
+ }
12
+ //# sourceMappingURL=permission.guard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"permission.guard.d.ts","sourceRoot":"","sources":["../../../src/lib/guards/permission.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,gBAAgB,EAIhB,YAAY,EAGb,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAKzC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,qBACa,eAAgB,YAAW,WAAW,EAAE,YAAY;IAI7D,OAAO,CAAC,QAAQ,CAAC,SAAS;IAG1B,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC;IANhC,OAAO,CAAC,oBAAoB,CAAgB;gBAGzB,SAAS,EAAE,SAAS,EAGpB,YAAY,CAAC,EAAE,YAAY,YAAA;IAG9C,YAAY;IAUN,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;CAgC/D"}
@@ -0,0 +1,49 @@
1
+ import { __decorate, __metadata, __param } from "tslib";
2
+ import { ForbiddenException, Inject, Injectable, Optional, UnauthorizedException, } from '@nestjs/common';
3
+ import { Reflector } from '@nestjs/core';
4
+ import { IS_PUBLIC_KEY } from '../decorators/public.decorator.js';
5
+ import { REQUIRED_PERMISSION_KEY } from '../decorators/require-permission.decorator.js';
6
+ import { ROLE_RESOLVER } from '../role-resolver.js';
7
+ let PermissionGuard = class PermissionGuard {
8
+ constructor(reflector, roleResolver) {
9
+ this.reflector = reflector;
10
+ this.roleResolver = roleResolver;
11
+ }
12
+ onModuleInit() {
13
+ if (!this.roleResolver) {
14
+ throw new Error('PermissionGuard requires ROLE_RESOLVER to be provided. ' +
15
+ 'Register a provider: { provide: ROLE_RESOLVER, useExisting: YourRoleResolverService }');
16
+ }
17
+ this.resolvedRoleResolver = this.roleResolver;
18
+ }
19
+ async canActivate(context) {
20
+ const isPublic = this.reflector.getAllAndOverride(IS_PUBLIC_KEY, [
21
+ context.getHandler(),
22
+ context.getClass(),
23
+ ]);
24
+ if (isPublic)
25
+ return true;
26
+ const { user } = context.switchToHttp().getRequest();
27
+ if (!user?.id) {
28
+ throw new UnauthorizedException();
29
+ }
30
+ const [role, permissions] = await Promise.all([
31
+ this.resolvedRoleResolver.findPrimaryRole(user.id),
32
+ this.resolvedRoleResolver.findPermissions(user.id),
33
+ ]);
34
+ user.role = role;
35
+ user.permissions = permissions;
36
+ const requiredPermission = this.reflector.getAllAndOverride(REQUIRED_PERMISSION_KEY, [context.getHandler(), context.getClass()]);
37
+ if (requiredPermission && !permissions.includes(requiredPermission)) {
38
+ throw new ForbiddenException('Insufficient permissions');
39
+ }
40
+ return true;
41
+ }
42
+ };
43
+ PermissionGuard = __decorate([
44
+ Injectable(),
45
+ __param(1, Optional()),
46
+ __param(1, Inject(ROLE_RESOLVER)),
47
+ __metadata("design:paramtypes", [Reflector, Object])
48
+ ], PermissionGuard);
49
+ export { PermissionGuard };
@@ -0,0 +1,6 @@
1
+ export declare const ROLE_RESOLVER = "ROLE_RESOLVER";
2
+ export interface RoleResolver {
3
+ findPrimaryRole(userId: number): Promise<string | null>;
4
+ findPermissions(userId: number): Promise<string[]>;
5
+ }
6
+ //# sourceMappingURL=role-resolver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"role-resolver.d.ts","sourceRoot":"","sources":["../../src/lib/role-resolver.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,kBAAkB,CAAC;AAE7C,MAAM,WAAW,YAAY;IAC3B,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACxD,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;CACpD"}
@@ -0,0 +1 @@
1
+ export const ROLE_RESOLVER = 'ROLE_RESOLVER';
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@open-kingdom/shared-backend-util-rbac",
3
+ "version": "0.0.2-17",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ "./package.json": "./package.json",
13
+ ".": {
14
+ "@open-kingdom/source": "./src/index.ts",
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "!**/*.tsbuildinfo"
23
+ ],
24
+ "nx": {
25
+ "tags": [
26
+ "scope:shared",
27
+ "type:util",
28
+ "environment:backend"
29
+ ]
30
+ },
31
+ "dependencies": {
32
+ "tslib": "^2.3.0",
33
+ "@nestjs/common": "^11.0.0",
34
+ "@nestjs/core": "^11.0.0"
35
+ }
36
+ }