@mondaydotcomorg/monday-authorization 1.0.9 → 1.0.11

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.
@@ -22,7 +22,7 @@ function getAuthorizationMiddleware(action, resourceGetter, contextGetter) {
22
22
  contextGetter || (contextGetter = defaultContextGetter);
23
23
  const { userId, accountId } = contextGetter(request);
24
24
  const resources = resourceGetter(request);
25
- const isAuthorized = yield authorization_service_1.AuthorizationService.isAuthorized(accountId, userId, resources, action);
25
+ const { isAuthorized } = yield authorization_service_1.AuthorizationService.isAuthorized(accountId, userId, resources, action);
26
26
  authorization_internal_service_1.AuthorizationInternalService.markAuthorized(request);
27
27
  if (!isAuthorized) {
28
28
  response.status(403).json({ message: 'Access denied' });
@@ -1,4 +1,8 @@
1
1
  import { Action, Resource } from './types/general';
2
+ export interface AuthorizeResponse {
3
+ isAuthorized: boolean;
4
+ unauthorizedIds?: number[];
5
+ }
2
6
  export declare class AuthorizationService {
3
- static isAuthorized(accountId: number, userId: number, resources: Resource[], action: Action): Promise<boolean>;
7
+ static isAuthorized(accountId: number, userId: number, resources: Resource[], action: Action): Promise<AuthorizeResponse>;
4
8
  }
@@ -34,7 +34,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
34
34
  exports.AuthorizationService = void 0;
35
35
  const monday_jwt_1 = require("@mondaydotcomorg/monday-jwt");
36
36
  const MondayLogger = __importStar(require("@mondaydotcomorg/monday-logger"));
37
- const node_fetch_1 = __importDefault(require("node-fetch"));
37
+ const monday_fetch_1 = __importDefault(require("@mondaydotcomorg/monday-fetch"));
38
38
  const INTERNAL_APP_NAME = 'internal_ms';
39
39
  const URL = `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/authorize`;
40
40
  const IS_DEV_ENV = process.env.NODE_ENV === 'development';
@@ -45,7 +45,7 @@ class AuthorizationService {
45
45
  return __awaiter(this, void 0, void 0, function* () {
46
46
  const internalAuthToken = monday_jwt_1.signAuthorizationHeader(INTERNAL_APP_NAME, accountId);
47
47
  const params = createAuthorizationParams(resources, action);
48
- const response = yield node_fetch_1.default(URL, {
48
+ const response = yield monday_fetch_1.default(URL, {
49
49
  method: 'POST',
50
50
  headers: { Authorization: internalAuthToken, 'Content-Type': 'application/json' },
51
51
  timeout: REQUEST_TIMEOUT,
@@ -53,10 +53,10 @@ class AuthorizationService {
53
53
  user_id: userId,
54
54
  authorize_request_objects: params.authorizationObjects,
55
55
  }),
56
- });
56
+ }, { retries: 3, callback: logOnFetchFail });
57
57
  if (!response.ok) {
58
58
  logger.error({ status: response.status }, 'Authorization middleware: authorization request failed');
59
- return false;
59
+ return { isAuthorized: false };
60
60
  }
61
61
  const responseBody = yield response.json();
62
62
  const unauthorizedObjects = [];
@@ -69,9 +69,10 @@ class AuthorizationService {
69
69
  logger.info({
70
70
  resources: JSON.stringify(unauthorizedObjects),
71
71
  }, 'Authorization middleware: resource is unauthorized');
72
- return false;
72
+ const unauthorizedIds = unauthorizedObjects.map(obj => obj.resource_id);
73
+ return { isAuthorized: false, unauthorizedIds };
73
74
  }
74
- return true;
75
+ return { isAuthorized: true };
75
76
  });
76
77
  }
77
78
  }
@@ -92,3 +93,6 @@ function createAuthorizationParams(resources, action) {
92
93
  };
93
94
  return params;
94
95
  }
96
+ function logOnFetchFail(retries, error, response) {
97
+ logger.error({ attempt: retries, error }, 'Authorization attempt failed due to network issues');
98
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mondaydotcomorg/monday-authorization",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "BSD-3-Clause",
@@ -10,6 +10,7 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "@mondaydotcomorg/monday-authentication": "^5.0.4",
13
+ "@mondaydotcomorg/monday-fetch": "^0.0.2",
13
14
  "@mondaydotcomorg/monday-jwt": "^2.0.4",
14
15
  "@mondaydotcomorg/monday-logger": "^2.1.2",
15
16
  "@types/express": "^4.17.12",
@@ -30,5 +31,5 @@
30
31
  "files": [
31
32
  "dist/"
32
33
  ],
33
- "gitHead": "a25845d84a2c3ce7f892469c2b5621e17a13513f"
34
+ "gitHead": "5045f4bb75bdad8f142bf3d3bd65fa7a955bf39f"
34
35
  }