plac-micro-common 1.2.1 → 1.2.3

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.
@@ -33,6 +33,8 @@ let JwtAuthGuard = class JwtAuthGuard {
33
33
  req.current_user = authPayload.user;
34
34
  req.current_app = authPayload.app;
35
35
  req.current_app_client = authPayload.app_client;
36
+ req.current_roles = authPayload.roles ?? [];
37
+ req.current_permissions = authPayload.permissions ?? [];
36
38
  return true;
37
39
  }
38
40
  };
@@ -13,6 +13,7 @@ exports.PermissionsGuard = void 0;
13
13
  const common_1 = require("@nestjs/common");
14
14
  const core_1 = require("@nestjs/core");
15
15
  const constants_1 = require("../constants");
16
+ const types_1 = require("../../types");
16
17
  let PermissionsGuard = class PermissionsGuard {
17
18
  constructor(reflector) {
18
19
  this.reflector = reflector;
@@ -23,17 +24,22 @@ let PermissionsGuard = class PermissionsGuard {
23
24
  if (!required || required.length === 0)
24
25
  return true;
25
26
  const req = context.switchToHttp().getRequest();
26
- // Must have req.user set by JwtAuthGuard (passport-jwt or custom)
27
- const user = req?.user;
28
- if (!user)
29
- throw new common_1.UnauthorizedException("Missing auth user");
30
- const userPerms = Array.isArray(user.permissions)
31
- ? user.permissions
27
+ // Your convention: permissions live on req.current_permissions
28
+ const perms = Array.isArray(req?.current_permissions)
29
+ ? req.current_permissions
32
30
  : [];
31
+ if (!perms.length) {
32
+ throw new common_1.UnauthorizedException("The current request does not consist of any permissions!");
33
+ }
33
34
  // require ALL permissions by default
34
- const hasAll = required.every((p) => userPerms.includes(p));
35
- if (!hasAll)
36
- throw new common_1.ForbiddenException("Insufficient permissions");
35
+ const hasAll = required.every((p) => perms.includes(p));
36
+ if (!hasAll) {
37
+ throw new common_1.ForbiddenException({
38
+ code: types_1.ErrorCode.PermissionDenied,
39
+ message: "The user does not have sufficient permissions to access this resource or perform this action!",
40
+ details: constants_1.REQUIRE_PERMISSIONS_KEY,
41
+ });
42
+ }
37
43
  return true;
38
44
  }
39
45
  };
@@ -37,4 +37,6 @@ export type AuthPayload = {
37
37
  user: CurrentUserInfo;
38
38
  app: CurrentAppInfo;
39
39
  app_client: CurrentAppClientInfo;
40
+ roles?: string[];
41
+ permissions?: string[];
40
42
  };
@@ -1,4 +1,5 @@
1
1
  export declare enum ErrorCode {
2
2
  AccessTokenExpired = "ACCESS_TOKEN_EXPIRED",
3
- InvalidToken = "INVALID_TOKEN"
3
+ InvalidToken = "INVALID_TOKEN",
4
+ PermissionDenied = "PERMISSION_DENIED"
4
5
  }
@@ -5,4 +5,5 @@ var ErrorCode;
5
5
  (function (ErrorCode) {
6
6
  ErrorCode["AccessTokenExpired"] = "ACCESS_TOKEN_EXPIRED";
7
7
  ErrorCode["InvalidToken"] = "INVALID_TOKEN";
8
+ ErrorCode["PermissionDenied"] = "PERMISSION_DENIED";
8
9
  })(ErrorCode || (exports.ErrorCode = ErrorCode = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "plac-micro-common",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {