@mondaydotcomorg/monday-authorization 1.1.9-featureyardenresource-attributes-api-support-authz-sdk.1568 → 1.1.9-fixorcoadd-on-headers-dep-to-monday-authorization.1082

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/CHANGELOG.md CHANGED
@@ -5,10 +5,6 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
7
 
8
- ## [1.2.0] - 2024-01-05
9
- ### Added
10
- - `isAuthorized` now return the unauthorized objects - regardless to the unauthorized ids (which may be missing resource ids if resource has no id, like `feature` e.g.)
11
-
12
8
  ## [1.1.0] - 2023-08-09
13
9
 
14
10
  ### ⚠ BREAKING CHANGES
package/dist/index.d.ts CHANGED
@@ -8,4 +8,3 @@ export interface InitOptions {
8
8
  export declare function init(options?: InitOptions): void;
9
9
  export { authorizationCheckMiddleware, getAuthorizationMiddleware, skipAuthorizationMiddleware, } from './lib/authorization-middleware';
10
10
  export { AuthorizationService } from './lib/authorization-service';
11
- export { AuthorizationAttributesService } from './lib/authorization-attributes-service';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AuthorizationAttributesService = exports.AuthorizationService = exports.skipAuthorizationMiddleware = exports.getAuthorizationMiddleware = exports.authorizationCheckMiddleware = exports.init = void 0;
3
+ exports.AuthorizationService = exports.skipAuthorizationMiddleware = exports.getAuthorizationMiddleware = exports.authorizationCheckMiddleware = exports.init = void 0;
4
4
  const prometheus_service_1 = require("./lib/prometheus-service");
5
5
  const authorization_service_1 = require("./lib/authorization-service");
6
6
  function init(options = {}) {
@@ -21,5 +21,3 @@ Object.defineProperty(exports, "getAuthorizationMiddleware", { enumerable: true,
21
21
  Object.defineProperty(exports, "skipAuthorizationMiddleware", { enumerable: true, get: function () { return authorization_middleware_1.skipAuthorizationMiddleware; } });
22
22
  var authorization_service_2 = require("./lib/authorization-service");
23
23
  Object.defineProperty(exports, "AuthorizationService", { enumerable: true, get: function () { return authorization_service_2.AuthorizationService; } });
24
- var authorization_attributes_service_1 = require("./lib/authorization-attributes-service");
25
- Object.defineProperty(exports, "AuthorizationAttributesService", { enumerable: true, get: function () { return authorization_attributes_service_1.AuthorizationAttributesService; } });
@@ -1,15 +1,6 @@
1
- /// <reference types="bunyan" />
2
1
  import { Request } from 'express';
3
- import { fetch, MondayFetchOptions } from '@mondaydotcomorg/monday-fetch';
4
- import * as MondayLogger from '@mondaydotcomorg/monday-logger';
5
- export declare const logger: MondayLogger.Logger;
6
2
  export declare class AuthorizationInternalService {
7
3
  static skipAuthorization(requset: Request): void;
8
4
  static markAuthorized(request: Request): void;
9
5
  static failIfNotCoveredByAuthorization(request: Request): void;
10
- static throwOnHttpErrorIfNeeded(response: Awaited<ReturnType<typeof fetch>>, placement: string): void;
11
- static generateInternalAuthToken(accountId: number, userId: number): string;
12
- static setRequestFetchOptions(customMondayFetchOptions: MondayFetchOptions): void;
13
- static getRequestFetchOptions(): MondayFetchOptions;
14
- static getRequestTimeout(): 60000 | 2000;
15
6
  }
@@ -1,46 +1,6 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.AuthorizationInternalService = exports.logger = void 0;
27
- const monday_jwt_1 = require("@mondaydotcomorg/monday-jwt");
28
- const MondayLogger = __importStar(require("@mondaydotcomorg/monday-logger"));
29
- const INTERNAL_APP_NAME = 'internal_ms';
30
- const defaultMondayFetchOptions = {
31
- retries: 3,
32
- callback: logOnFetchFail,
33
- };
34
- function logOnFetchFail(retriesLeft, error) {
35
- if (retriesLeft == 0) {
36
- exports.logger.error({ retriesLeft, error }, 'Authorization attempt failed due to network issues');
37
- }
38
- else {
39
- exports.logger.info({ retriesLeft, error }, 'Authorization attempt failed due to network issues, trying again');
40
- }
41
- }
42
- let mondayFetchOptions = defaultMondayFetchOptions;
43
- exports.logger = MondayLogger.getLogger();
3
+ exports.AuthorizationInternalService = void 0;
44
4
  class AuthorizationInternalService {
45
5
  static skipAuthorization(requset) {
46
6
  requset.authorizationSkipPerformed = true;
@@ -53,26 +13,5 @@ class AuthorizationInternalService {
53
13
  throw 'Endpoint is not covered by authorization check';
54
14
  }
55
15
  }
56
- static throwOnHttpErrorIfNeeded(response, placement) {
57
- if (response.ok) {
58
- return;
59
- }
60
- const status = response.status;
61
- exports.logger.error({ tag: 'authorization-service', placement, status }, 'AuthorizationService: authorization request failed');
62
- throw new Error(`AuthorizationService: [${placement}] authorization request failed with status ${status}`);
63
- }
64
- static generateInternalAuthToken(accountId, userId) {
65
- return (0, monday_jwt_1.signAuthorizationHeader)({ appName: INTERNAL_APP_NAME, accountId, userId });
66
- }
67
- static setRequestFetchOptions(customMondayFetchOptions) {
68
- mondayFetchOptions = Object.assign(Object.assign({}, defaultMondayFetchOptions), customMondayFetchOptions);
69
- }
70
- static getRequestFetchOptions() {
71
- return mondayFetchOptions;
72
- }
73
- static getRequestTimeout() {
74
- const isDevEnv = process.env.NODE_ENV === 'development';
75
- return isDevEnv ? 60000 : 2000;
76
- }
77
16
  }
78
17
  exports.AuthorizationInternalService = AuthorizationInternalService;
@@ -1,10 +1,9 @@
1
1
  import { MondayFetchOptions } from '@mondaydotcomorg/monday-fetch';
2
2
  import { Action, AuthorizationObject, Resource } from './types/general';
3
- import { ScopedAction, ScopedActionPermit, ScopedActionResponseObject, ScopeOptions } from './types/scoped-actions-contracts';
3
+ import { ScopedAction, ScopedActionPermit, ScopedActionResponseObject, ScopeOptions } from 'lib/types/scoped-actions-contracts';
4
4
  export interface AuthorizeResponse {
5
5
  isAuthorized: boolean;
6
6
  unauthorizedIds?: number[];
7
- unauthorizedObjects?: AuthorizationObject[];
8
7
  }
9
8
  export declare function setRequestFetchOptions(customMondayFetchOptions: MondayFetchOptions): void;
10
9
  export declare function setRedisClient(client: any, grantedFeatureRedisExpirationInSeconds?: number): void;
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -12,12 +35,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
35
  exports.AuthorizationService = exports.setRedisClient = exports.setRequestFetchOptions = void 0;
13
36
  const lodash_1 = require("lodash");
14
37
  const perf_hooks_1 = require("perf_hooks");
38
+ const monday_jwt_1 = require("@mondaydotcomorg/monday-jwt");
39
+ const MondayLogger = __importStar(require("@mondaydotcomorg/monday-logger"));
15
40
  const monday_fetch_1 = require("@mondaydotcomorg/monday-fetch");
16
41
  const prometheus_service_1 = require("./prometheus-service");
17
- const authorization_internal_service_1 = require("./authorization-internal-service");
42
+ const INTERNAL_APP_NAME = 'internal_ms';
43
+ const logger = MondayLogger.getLogger();
18
44
  const GRANTED_FEATURE_CACHE_EXPIRATION_SECONDS = 5 * 60;
45
+ const defaultMondayFetchOptions = {
46
+ retries: 3,
47
+ callback: logOnFetchFail,
48
+ };
49
+ let mondayFetchOptions = defaultMondayFetchOptions;
19
50
  function setRequestFetchOptions(customMondayFetchOptions) {
20
- authorization_internal_service_1.AuthorizationInternalService.setRequestFetchOptions(customMondayFetchOptions);
51
+ mondayFetchOptions = Object.assign(Object.assign({}, defaultMondayFetchOptions), customMondayFetchOptions);
21
52
  }
22
53
  exports.setRequestFetchOptions = setRequestFetchOptions;
23
54
  function setRedisClient(client, grantedFeatureRedisExpirationInSeconds = GRANTED_FEATURE_CACHE_EXPIRATION_SECONDS) {
@@ -26,7 +57,7 @@ function setRedisClient(client, grantedFeatureRedisExpirationInSeconds = GRANTED
26
57
  AuthorizationService.grantedFeatureRedisExpirationInSeconds = grantedFeatureRedisExpirationInSeconds;
27
58
  }
28
59
  else {
29
- authorization_internal_service_1.logger.warn({ grantedFeatureRedisExpirationInSeconds }, 'Invalid input for grantedFeatureRedisExpirationInSeconds, must be positive number. using default ttl.');
60
+ logger.warn({ grantedFeatureRedisExpirationInSeconds }, 'Invalid input for grantedFeatureRedisExpirationInSeconds, must be positive number. using default ttl.');
30
61
  AuthorizationService.grantedFeatureRedisExpirationInSeconds = GRANTED_FEATURE_CACHE_EXPIRATION_SECONDS;
31
62
  }
32
63
  }
@@ -86,20 +117,20 @@ class AuthorizationService {
86
117
  }
87
118
  static canActionInScopeMultiple(accountId, userId, scopedActions) {
88
119
  return __awaiter(this, void 0, void 0, function* () {
89
- const internalAuthToken = authorization_internal_service_1.AuthorizationInternalService.generateInternalAuthToken(accountId, userId);
120
+ const internalAuthToken = (0, monday_jwt_1.signAuthorizationHeader)({ appName: INTERNAL_APP_NAME, accountId, userId });
90
121
  const scopedActionsPayload = scopedActions.map(scopedAction => {
91
122
  return Object.assign(Object.assign({}, scopedAction), { scope: (0, lodash_1.mapKeys)(scopedAction.scope, (_, key) => (0, lodash_1.snakeCase)(key)) }); // for example: { workspaceId: 1 } => { workspace_id: 1 }
92
123
  });
93
124
  const response = yield (0, monday_fetch_1.fetch)(getCanActionsInScopesUrl(), {
94
125
  method: 'POST',
95
126
  headers: { Authorization: internalAuthToken, 'Content-Type': 'application/json' },
96
- timeout: authorization_internal_service_1.AuthorizationInternalService.getRequestTimeout(),
127
+ timeout: getRequestTimeout(),
97
128
  body: JSON.stringify({
98
129
  user_id: userId,
99
130
  scoped_actions: scopedActionsPayload,
100
131
  }),
101
- }, authorization_internal_service_1.AuthorizationInternalService.getRequestFetchOptions());
102
- authorization_internal_service_1.AuthorizationInternalService.throwOnHttpErrorIfNeeded(response, 'canActionInScopeMultiple');
132
+ }, mondayFetchOptions);
133
+ throwOnHttpErrorIfNeeded(response, 'canActionInScopeMultiple');
103
134
  const responseBody = yield response.json();
104
135
  const camelCaseKeys = (obj) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [(0, lodash_1.camelCase)(key), value]));
105
136
  const scopedActionsResponseObjects = responseBody.result.map(responseObject => {
@@ -119,22 +150,22 @@ class AuthorizationService {
119
150
  }
120
151
  static isAuthorizedMultiple(accountId, userId, authorizationRequestObjects) {
121
152
  return __awaiter(this, void 0, void 0, function* () {
122
- const internalAuthToken = authorization_internal_service_1.AuthorizationInternalService.generateInternalAuthToken(accountId, userId);
153
+ const internalAuthToken = (0, monday_jwt_1.signAuthorizationHeader)({ appName: INTERNAL_APP_NAME, accountId, userId });
123
154
  const startTime = perf_hooks_1.performance.now();
124
155
  const response = yield (0, monday_fetch_1.fetch)(getAuthorizeUrl(), {
125
156
  method: 'POST',
126
157
  headers: { Authorization: internalAuthToken, 'Content-Type': 'application/json' },
127
- timeout: authorization_internal_service_1.AuthorizationInternalService.getRequestTimeout(),
158
+ timeout: getRequestTimeout(),
128
159
  body: JSON.stringify({
129
160
  user_id: userId,
130
161
  authorize_request_objects: authorizationRequestObjects,
131
162
  }),
132
- }, authorization_internal_service_1.AuthorizationInternalService.getRequestFetchOptions());
163
+ }, mondayFetchOptions);
133
164
  const endTime = perf_hooks_1.performance.now();
134
165
  const time = endTime - startTime;
135
166
  const responseStatus = response.status;
136
167
  (0, prometheus_service_1.sendAuthorizationChecksPerRequestMetric)(responseStatus, authorizationRequestObjects.length);
137
- authorization_internal_service_1.AuthorizationInternalService.throwOnHttpErrorIfNeeded(response, 'isAuthorizedMultiple');
168
+ throwOnHttpErrorIfNeeded(response, 'isAuthorizedMultiple');
138
169
  const responseBody = yield response.json();
139
170
  const unauthorizedObjects = [];
140
171
  responseBody.result.forEach(function (isAuthorized, index) {
@@ -145,13 +176,13 @@ class AuthorizationService {
145
176
  (0, prometheus_service_1.sendAuthorizationCheckResponseTimeMetric)(authorizationObject.resource_type, authorizationObject.action, isAuthorized, responseStatus, time);
146
177
  });
147
178
  if (unauthorizedObjects.length > 0) {
148
- authorization_internal_service_1.logger.info({
179
+ logger.info({
149
180
  resources: JSON.stringify(unauthorizedObjects),
150
181
  }, 'AuthorizationService: resource is unauthorized');
151
182
  const unauthorizedIds = unauthorizedObjects
152
183
  .filter(obj => !!obj.resource_id)
153
184
  .map(obj => obj.resource_id);
154
- return { isAuthorized: false, unauthorizedIds, unauthorizedObjects };
185
+ return { isAuthorized: false, unauthorizedIds };
155
186
  }
156
187
  return { isAuthorized: true };
157
188
  });
@@ -174,9 +205,29 @@ function createAuthorizationParams(resources, action) {
174
205
  };
175
206
  return params;
176
207
  }
208
+ function logOnFetchFail(retriesLeft, error) {
209
+ if (retriesLeft == 0) {
210
+ logger.error({ retriesLeft, error }, 'Authorization attempt failed due to network issues');
211
+ }
212
+ else {
213
+ logger.info({ retriesLeft, error }, 'Authorization attempt failed due to network issues, trying again');
214
+ }
215
+ }
177
216
  function getAuthorizeUrl() {
178
217
  return `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/authorize`;
179
218
  }
180
219
  function getCanActionsInScopesUrl() {
181
220
  return `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/can_actions_in_scopes`;
182
221
  }
222
+ function getRequestTimeout() {
223
+ const isDevEnv = process.env.NODE_ENV === 'development';
224
+ return isDevEnv ? 60000 : 2000;
225
+ }
226
+ function throwOnHttpErrorIfNeeded(response, placement) {
227
+ if (response.ok) {
228
+ return;
229
+ }
230
+ const status = response.status;
231
+ logger.error({ tag: 'authorization-service', placement, status }, 'AuthorizationService: authorization request failed');
232
+ throw new Error(`AuthorizationService: [${placement}] authorization request failed with status ${status}`);
233
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mondaydotcomorg/monday-authorization",
3
- "version": "1.1.9-featureyardenresource-attributes-api-support-authz-sdk.1568+580506c24",
3
+ "version": "1.1.9-fixorcoadd-on-headers-dep-to-monday-authorization.1082+86402bc19",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "BSD-3-Clause",
@@ -32,5 +32,5 @@
32
32
  "files": [
33
33
  "dist/"
34
34
  ],
35
- "gitHead": "580506c248d4f1d0b7dc3f4aa6242b2aaa488df8"
35
+ "gitHead": "86402bc19a15e97ac5ccb4fdcdc28c30cf5e5432"
36
36
  }
@@ -1,7 +0,0 @@
1
- import { ResourceAttributeAssignment, ResourceAttributeResponse } from './types/authorization-attributes-contracts';
2
- import { Resource } from './types/general';
3
- export declare class AuthorizationAttributesService {
4
- static upsertResourceAttributesSync(accountId: number, userId: number, resourceAttributeAssignments: ResourceAttributeAssignment[]): Promise<ResourceAttributeResponse>;
5
- static deleteResourceAttributesSync(accountId: number, resource: Resource, attributeKeys: string[]): Promise<ResourceAttributeResponse>;
6
- private static getResourceAttributesUrl;
7
- }
@@ -1,49 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.AuthorizationAttributesService = void 0;
13
- const monday_fetch_1 = require("@mondaydotcomorg/monday-fetch");
14
- const authorization_internal_service_1 = require("./authorization-internal-service");
15
- class AuthorizationAttributesService {
16
- static upsertResourceAttributesSync(accountId, userId, resourceAttributeAssignments) {
17
- return __awaiter(this, void 0, void 0, function* () {
18
- const internalAuthToken = authorization_internal_service_1.AuthorizationInternalService.generateInternalAuthToken(accountId, userId);
19
- const response = yield (0, monday_fetch_1.fetch)(this.getResourceAttributesUrl(accountId), {
20
- method: 'POST',
21
- headers: { Authorization: internalAuthToken, 'Content-Type': 'application/json' },
22
- timeout: authorization_internal_service_1.AuthorizationInternalService.getRequestTimeout(),
23
- body: JSON.stringify({ resourceAttributeAssignments }),
24
- }, authorization_internal_service_1.AuthorizationInternalService.getRequestFetchOptions());
25
- const responseBody = yield response.json();
26
- authorization_internal_service_1.AuthorizationInternalService.throwOnHttpErrorIfNeeded(response, 'upsertResourceAttributesSync');
27
- return { attributes: responseBody['attributes'] };
28
- });
29
- }
30
- static deleteResourceAttributesSync(accountId, resource, attributeKeys) {
31
- return __awaiter(this, void 0, void 0, function* () {
32
- const internalAuthToken = authorization_internal_service_1.AuthorizationInternalService.generateInternalAuthToken(accountId, 0);
33
- const url = `${this.getResourceAttributesUrl(accountId)}/${resource.type}/${resource.id}`;
34
- const response = yield (0, monday_fetch_1.fetch)(url, {
35
- method: 'DELETE',
36
- headers: { Authorization: internalAuthToken, 'Content-Type': 'application/json' },
37
- timeout: authorization_internal_service_1.AuthorizationInternalService.getRequestTimeout(),
38
- body: JSON.stringify({ keys: attributeKeys }),
39
- }, authorization_internal_service_1.AuthorizationInternalService.getRequestFetchOptions());
40
- const responseBody = yield response.json();
41
- authorization_internal_service_1.AuthorizationInternalService.throwOnHttpErrorIfNeeded(response, 'deleteResourceAttributesSync');
42
- return { attributes: responseBody['attributes'] };
43
- });
44
- }
45
- static getResourceAttributesUrl(accountId) {
46
- return `${process.env.AUTHORIZATION_URL}/attributes/${accountId}/resource`;
47
- }
48
- }
49
- exports.AuthorizationAttributesService = AuthorizationAttributesService;
@@ -1,10 +0,0 @@
1
- import { Resource } from './general';
2
- export interface ResourceAttributeAssignment {
3
- resourceType: Resource['type'];
4
- resourceId: Resource['id'];
5
- key: string;
6
- value: string;
7
- }
8
- export interface ResourceAttributeResponse {
9
- attributes: ResourceAttributeAssignment[];
10
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });