@mondaydotcomorg/monday-authorization 1.0.53-featuregallisupportprivatekeysigning.163 → 1.0.53-featureyardenauthorization-sdk-can-action-in-scope-support.34

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.
@@ -4,6 +4,27 @@ export interface AuthorizeResponse {
4
4
  isAuthorized: boolean;
5
5
  unauthorizedIds?: number[];
6
6
  }
7
+ export interface ScopeOptions {
8
+ workspaceId?: number;
9
+ accountProductId?: number;
10
+ boardId?: number;
11
+ pulseId?: number;
12
+ }
13
+ export interface Translation {
14
+ key: string;
15
+ [option: string]: string;
16
+ }
17
+ export interface SubjectRequestObject {
18
+ action: string;
19
+ scope: ScopeOptions;
20
+ }
21
+ export interface SubjectResponseObject {
22
+ subject: SubjectRequestObject;
23
+ permit: {
24
+ isAllowed: boolean;
25
+ reason: Translation;
26
+ };
27
+ }
7
28
  export declare function setRequestFetchOptions(customMondayFetchOptions: MondayFetchOptions): void;
8
29
  export declare function setRedisClient(client: any, grantedFeatureRedisExpirationInSeconds?: number): void;
9
30
  export declare class AuthorizationService {
@@ -16,6 +37,8 @@ export declare class AuthorizationService {
16
37
  static isAuthorized(accountId: number, userId: number, resources: Resource[], action: Action): Promise<AuthorizeResponse>;
17
38
  static isAuthorized(accountId: number, userId: number, authorizationRequestObjects: AuthorizationObject[]): Promise<AuthorizeResponse>;
18
39
  static isUserGrantedWithFeature(accountId: number, userId: number, featureName: string): Promise<boolean>;
40
+ static canActionInScope(accountId: number, userId: number, action: string, scope: ScopeOptions): Promise<boolean>;
41
+ static canActionInScopeMultiple(accountId: number, userId: number, subjects: SubjectRequestObject[]): Promise<SubjectResponseObject[]>;
19
42
  private static fetchIsUserGrantedWithFeature;
20
43
  private static getCachedKeyName;
21
44
  private static isAuthorizedSingular;
@@ -30,6 +30,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
31
  exports.AuthorizationService = exports.setRedisClient = exports.setRequestFetchOptions = void 0;
32
32
  const perf_hooks_1 = require("perf_hooks");
33
+ const lodash_1 = require("lodash");
33
34
  const monday_jwt_1 = require("@mondaydotcomorg/monday-jwt");
34
35
  const MondayLogger = __importStar(require("@mondaydotcomorg/monday-logger"));
35
36
  const monday_fetch_1 = require("@mondaydotcomorg/monday-fetch");
@@ -88,6 +89,37 @@ class AuthorizationService {
88
89
  return grantedFeatureValue;
89
90
  });
90
91
  }
92
+ static canActionInScope(accountId, userId, action, scope) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ const subjects = [{ action, scope }];
95
+ const subjectsResponse = yield this.canActionInScopeMultiple(accountId, userId, subjects);
96
+ return subjectsResponse[0].permit.isAllowed;
97
+ });
98
+ }
99
+ ;
100
+ static canActionInScopeMultiple(accountId, userId, subjects) {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ const internalAuthToken = monday_jwt_1.signAuthorizationHeader({ appName: INTERNAL_APP_NAME, accountId, userId });
103
+ const subjectsPayload = subjects.map((subject) => {
104
+ return Object.assign(Object.assign({}, subject), { scope: lodash_1.mapKeys(subject.scope, (_, key) => lodash_1.snakeCase(key)) }); // for example: { workspaceId: 1 } => { workspace_id: 1 }
105
+ });
106
+ const response = yield monday_fetch_1.fetch(getCanActionsInScopesUrl(), {
107
+ method: 'POST',
108
+ headers: { Authorization: internalAuthToken, 'Content-Type': 'application/json' },
109
+ timeout: getRequestTimeout(),
110
+ body: JSON.stringify({
111
+ user_id: userId,
112
+ subjects: subjectsPayload,
113
+ }),
114
+ }, mondayFetchOptions);
115
+ if (!response.ok) {
116
+ logger.error({ status: response.status }, 'AuthorizationService: authorization request failed - canActionInScopeMultiple');
117
+ return subjects.map((subject) => ({ subject, permit: { isAllowed: false, reason: { key: 'internal error' } } }));
118
+ }
119
+ const responseBody = yield response.json();
120
+ return responseBody.result;
121
+ });
122
+ }
91
123
  static fetchIsUserGrantedWithFeature(featureName, accountId, userId) {
92
124
  return __awaiter(this, void 0, void 0, function* () {
93
125
  let authorizationObject = {
@@ -111,7 +143,7 @@ class AuthorizationService {
111
143
  return __awaiter(this, void 0, void 0, function* () {
112
144
  const internalAuthToken = monday_jwt_1.signAuthorizationHeader({ appName: INTERNAL_APP_NAME, accountId, userId });
113
145
  const startTime = perf_hooks_1.performance.now();
114
- const response = yield monday_fetch_1.fetch(getUrl(), {
146
+ const response = yield monday_fetch_1.fetch(getAuthorizeUrl(), {
115
147
  method: 'POST',
116
148
  headers: { Authorization: internalAuthToken, 'Content-Type': 'application/json' },
117
149
  timeout: getRequestTimeout(),
@@ -179,9 +211,12 @@ function logOnFetchFail(retriesLeft, error) {
179
211
  logger.info({ retriesLeft, error }, 'Authorization attempt failed due to network issues, trying again');
180
212
  }
181
213
  }
182
- function getUrl() {
214
+ function getAuthorizeUrl() {
183
215
  return `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/authorize`;
184
216
  }
217
+ function getCanActionsInScopesUrl() {
218
+ return `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/can_actions_in_scopes`;
219
+ }
185
220
  function getRequestTimeout() {
186
221
  const isDevEnv = process.env.NODE_ENV === 'development';
187
222
  return isDevEnv ? 60000 : 2000;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mondaydotcomorg/monday-authorization",
3
- "version": "1.0.53-featuregallisupportprivatekeysigning.163+78259a8b4",
3
+ "version": "1.0.53-featureyardenauthorization-sdk-can-action-in-scope-support.34+3c772f361",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "BSD-3-Clause",
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "@mondaydotcomorg/monday-fetch": "^0.0.7",
13
- "@mondaydotcomorg/monday-jwt": "^3.0.6-featuregallisupportprivatekeysigning.163+78259a8b4",
13
+ "@mondaydotcomorg/monday-jwt": "^3.0.5",
14
14
  "@mondaydotcomorg/monday-logger": "^3.0.10",
15
15
  "@types/express": "^4.17.12",
16
16
  "node-fetch": "^2.6.7",
@@ -32,5 +32,5 @@
32
32
  "files": [
33
33
  "dist/"
34
34
  ],
35
- "gitHead": "78259a8b42531e299acc8dba733fd46bbf6875f9"
35
+ "gitHead": "3c772f361ac623b6da03988d05af464886b0ff48"
36
36
  }