@riocrypto/common-server 1.0.2380 → 1.0.2382

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.
@@ -1,3 +1,4 @@
1
1
  import { AuthRole } from "@riocrypto/common";
2
+ import { AuthPermission } from "@riocrypto/common/build/types/auth-permission";
2
3
  import { NextFunction, Request, Response } from "express";
3
- export declare const requireMinRole: (req: Request, res: Response, next: NextFunction, minRole: AuthRole) => Promise<void>;
4
+ export declare const requireMinRoleOrPermissions: (req: Request, res: Response, next: NextFunction, minRole: AuthRole, requiredPermissions: AuthPermission[]) => Promise<void>;
@@ -9,20 +9,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.requireMinRole = void 0;
12
+ exports.requireMinRoleOrPermissions = void 0;
13
13
  const common_1 = require("@riocrypto/common");
14
14
  const authRoleOrder = [
15
15
  common_1.AuthRole.ViewOnly,
16
16
  common_1.AuthRole.CreateAndModify,
17
17
  common_1.AuthRole.Owner,
18
18
  ];
19
- const requireMinRole = (req, res, next, minRole) => __awaiter(void 0, void 0, void 0, function* () {
19
+ const requireMinRoleOrPermissions = (req, res, next, minRole, requiredPermissions) => __awaiter(void 0, void 0, void 0, function* () {
20
20
  var _a;
21
21
  if (!req.adminAuth && !req.validClusterApiKey) {
22
- if (authRoleOrder.indexOf((_a = req.auth) === null || _a === void 0 ? void 0 : _a.role) < authRoleOrder.indexOf(minRole)) {
23
- throw new common_1.GenericInputError(`Insufficient permissions. This endpoint requires at least the ${minRole} role. Contact your account owner to change your access role.`);
22
+ const auth = req.auth;
23
+ if (!auth) {
24
+ throw new common_1.NotAuthorizedError();
25
+ }
26
+ if (auth.permissions) {
27
+ // Permission based requirements
28
+ if (!auth.permissions.some((permission) => requiredPermissions.includes(permission))) {
29
+ throw new common_1.GenericInputError(`Insufficient permissions. Contact your account owner to change your access permissions.`);
30
+ }
31
+ }
32
+ else {
33
+ // Role based requirements
34
+ if (authRoleOrder.indexOf((_a = req.auth) === null || _a === void 0 ? void 0 : _a.role) < authRoleOrder.indexOf(minRole)) {
35
+ throw new common_1.GenericInputError(`Incompatible role. Contact your account owner to change your access role.`);
36
+ }
24
37
  }
25
38
  }
26
39
  next();
27
40
  });
28
- exports.requireMinRole = requireMinRole;
41
+ exports.requireMinRoleOrPermissions = requireMinRoleOrPermissions;
@@ -1,5 +1,6 @@
1
1
  import { APIKey, AuthRole, SecurityQuestion } from "@riocrypto/common";
2
2
  import { Mongoose, Model, Document } from "mongoose";
3
+ import { AuthPermission } from "@riocrypto/common/build/types/auth-permission";
3
4
  interface AuthAttrs {
4
5
  phoneNumber: string;
5
6
  password?: string;
@@ -22,7 +23,8 @@ interface AuthAttrs {
22
23
  middleName?: string;
23
24
  lastName?: string;
24
25
  secondLastName?: string;
25
- role: AuthRole;
26
+ role?: AuthRole;
27
+ permissions?: AuthPermission[];
26
28
  }
27
29
  interface AuthDoc extends Document {
28
30
  phoneNumber: string;
@@ -46,7 +48,8 @@ interface AuthDoc extends Document {
46
48
  middleName?: string;
47
49
  lastName?: string;
48
50
  secondLastName?: string;
49
- role: AuthRole;
51
+ role?: AuthRole;
52
+ permissions?: AuthPermission[];
50
53
  }
51
54
  interface AuthModel extends Model<AuthDoc> {
52
55
  build(attrs: AuthAttrs): AuthDoc;
@@ -76,6 +76,11 @@ const buildAuth = (mongoose) => {
76
76
  role: {
77
77
  type: String,
78
78
  },
79
+ permissions: [
80
+ {
81
+ type: String,
82
+ },
83
+ ],
79
84
  apiKeys: [
80
85
  {
81
86
  value: {
@@ -58,7 +58,7 @@ class LoggerService {
58
58
  "routingNumber",
59
59
  ];
60
60
  requestBodyFieldsToMaskWithLastFour.forEach((field) => {
61
- if (requestBody.hasOwnProperty(field) &&
61
+ if (Object.prototype.hasOwnProperty.call(requestBody, field) &&
62
62
  typeof requestBody[field] === "string") {
63
63
  requestBody[field] = this.maskValue(requestBody[field], true);
64
64
  }
@@ -71,7 +71,7 @@ class LoggerService {
71
71
  "authenticatorCode",
72
72
  ];
73
73
  requestBodyFieldsToMaskWithoutLastFour.forEach((field) => {
74
- if (requestBody.hasOwnProperty(field) &&
74
+ if (Object.prototype.hasOwnProperty.call(requestBody, field) &&
75
75
  typeof requestBody[field] === "string") {
76
76
  requestBody[field] = this.maskValue(requestBody[field]);
77
77
  }
@@ -85,7 +85,7 @@ class LoggerService {
85
85
  const responseBody = resAsAny.body; // Keep this assertion for object access
86
86
  const responseBodyFieldsToMask = ["secret", "value"];
87
87
  responseBodyFieldsToMask.forEach((field) => {
88
- if (responseBody.hasOwnProperty(field) &&
88
+ if (Object.prototype.hasOwnProperty.call(responseBody, field) &&
89
89
  typeof responseBody[field] === "string") {
90
90
  responseBody[field] = this.maskValue(responseBody[field]);
91
91
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.2380",
3
+ "version": "1.0.2382",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -28,7 +28,7 @@
28
28
  "@google-cloud/secret-manager": "^5.3.0",
29
29
  "@google-cloud/storage": "^6.9.5",
30
30
  "@hyperdx/node-opentelemetry": "^0.7.0",
31
- "@riocrypto/common": "^1.0.2106",
31
+ "@riocrypto/common": "^1.0.2107",
32
32
  "@types/express": "^4.17.13",
33
33
  "axios": "^1.7.4",
34
34
  "crypto-js": "^4.2.0",