@mondaydotcomorg/monday-authorization 1.0.44-featureyoniadd-granted-feature-sdk-api.558 → 1.0.44-featureyoniadd-granted-feature-sdk-api.560

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/README.md CHANGED
@@ -16,10 +16,6 @@ show is the action, post is the resource.
16
16
  yarn add @mondaydotcomorg/monday-authorization
17
17
  ```
18
18
 
19
- ## Usage
20
-
21
- First init the package in order for it to work properly
22
-
23
19
  app.ts:
24
20
 
25
21
  ```ts
@@ -28,11 +24,24 @@ import * as MondayAuthorization from '@mondaydotcomorg/monday-authorization';
28
24
 
29
25
  ...
30
26
 
31
- MondayAuthorization.init({ prometheus: getPrometheus() });
27
+ MondayAuthorization.init({
28
+ prometheus: getPrometheus(),
29
+ redisClient: redisClient,
30
+ grantedFeatureRedisExpirationInSeconds: 10 * 60
31
+ });
32
32
  startServer(...)
33
33
  ```
34
34
 
35
- Then add this code to any route declaration:
35
+ **Recommended** - optionally init authorization with redisClient so the granted feature results will be cached and reduce http calls.
36
+
37
+ - grantedFeatureRedisExpirationInSeconds - (optional), redis TTL for cached granted features, default set to 5 minutes
38
+
39
+
40
+ ## Usage
41
+
42
+ ### Use as Middleware
43
+
44
+ Add this code to any route declaration:
36
45
 
37
46
  ```ts
38
47
  import { Router } from 'express';
@@ -80,3 +89,15 @@ function, that looks like this:
80
89
  Add `authorizationCheckMiddleware` to make sure that all routes are covered by authorization check. Put this
81
90
  middleware before you define the routes.
82
91
  If you want to skip authorization, use `skipAuthorizationMiddleware`.
92
+
93
+
94
+ ### Granted Features API
95
+
96
+ Use `AuthorizationService.isUserGrantedWithFeature(accountId: number, userId: number, featureName: string): Promise<boolean>` to retrieve if a user is granted with a feature
97
+
98
+ ```ts
99
+ import {AuthorizationService, init} from '@mondaydotcomorg/monday-authorization';
100
+
101
+ let isFeatuerGranted = await AuthorizationService.isUserGrantedWithFeature(739628, 2, "unauthorized_content_change_message")
102
+ ```
103
+
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export interface InitOptions {
3
3
  prometheus?: any;
4
4
  mondayFetchOptions?: MondayFetchOptions;
5
5
  redisClient?: any;
6
+ grantedFeatureRedisExpirationInSeconds?: number;
6
7
  }
7
8
  export declare function init(options?: InitOptions): void;
8
9
  export { authorizationCheckMiddleware, getAuthorizationMiddleware, skipAuthorizationMiddleware, } from './lib/authorization-middleware';
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ function init(options = {}) {
11
11
  authorization_service_1.setRequestFetchOptions(options.mondayFetchOptions);
12
12
  }
13
13
  if (options.redisClient) {
14
- authorization_service_1.setRedisClient(options.redisClient);
14
+ authorization_service_1.setRedisClient(options.redisClient, options.grantedFeatureRedisExpirationInSeconds);
15
15
  }
16
16
  }
17
17
  exports.init = init;
@@ -5,10 +5,10 @@ export interface AuthorizeResponse {
5
5
  unauthorizedIds?: number[];
6
6
  }
7
7
  export declare function setRequestFetchOptions(customMondayFetchOptions: MondayFetchOptions): void;
8
- export declare function setRedisClient(client: any): void;
8
+ export declare function setRedisClient(client: any, grantedFeatureRedisExpirationInSeconds?: number): void;
9
9
  export declare class AuthorizationService {
10
10
  static redisClient: any;
11
- static cacheExpirationInSeconds: number;
11
+ static grantedFeatureRedisExpirationInSeconds: number;
12
12
  /**
13
13
  * @deprecated use the second form with authorizationRequestObjects instead,
14
14
  * support of this function will be dropped gradually
@@ -36,15 +36,6 @@ const monday_fetch_1 = require("@mondaydotcomorg/monday-fetch");
36
36
  const prometheus_service_1 = require("./prometheus-service");
37
37
  const INTERNAL_APP_NAME = 'internal_ms';
38
38
  const logger = MondayLogger.getLogger();
39
- //
40
- // const RedisClient = require('@mondaydotcomorg/monday-redis');
41
- // const redis_options = {
42
- // host: "https://top-secret-redis.com",
43
- // port: 1234,
44
- // password: "password" // Optional
45
- // };
46
- //
47
- // const redisClient = new RedisClient(redis_options);
48
39
  const defaultMondayFetchOptions = {
49
40
  retries: 3,
50
41
  callback: logOnFetchFail,
@@ -54,8 +45,11 @@ function setRequestFetchOptions(customMondayFetchOptions) {
54
45
  mondayFetchOptions = Object.assign(Object.assign({}, defaultMondayFetchOptions), customMondayFetchOptions);
55
46
  }
56
47
  exports.setRequestFetchOptions = setRequestFetchOptions;
57
- function setRedisClient(client) {
48
+ function setRedisClient(client, grantedFeatureRedisExpirationInSeconds = 5 * 60) {
58
49
  AuthorizationService.redisClient = client;
50
+ if (grantedFeatureRedisExpirationInSeconds && grantedFeatureRedisExpirationInSeconds > 0) {
51
+ AuthorizationService.grantedFeatureRedisExpirationInSeconds = grantedFeatureRedisExpirationInSeconds;
52
+ }
59
53
  }
60
54
  exports.setRedisClient = setRedisClient;
61
55
  class AuthorizationService {
@@ -74,15 +68,17 @@ class AuthorizationService {
74
68
  }
75
69
  static isUserGrantedWithFeature(accountId, userId, featureName) {
76
70
  return __awaiter(this, void 0, void 0, function* () {
71
+ let cachedKey = this.getCachedKey(userId, featureName);
77
72
  if (this.redisClient) {
78
- let grantedFeatureValue = yield this.redisClient.get(this.getCachedKey(userId, featureName));
73
+ let grantedFeatureValue = yield this.redisClient.get(cachedKey);
79
74
  if (!(grantedFeatureValue === undefined || grantedFeatureValue === null)) {
80
- return grantedFeatureValue;
75
+ // redis returns the value as string
76
+ return grantedFeatureValue === 'true';
81
77
  }
82
78
  }
83
79
  let grantedFeatureValue = yield this.fetchIsUserGrantedWithFeature(featureName, accountId, userId);
84
80
  if (this.redisClient) {
85
- yield this.redisClient.set(this.getCachedKey(userId, featureName), grantedFeatureValue, 'EX', this.cacheExpirationInSeconds);
81
+ yield this.redisClient.set(cachedKey, grantedFeatureValue, 'EX', this.grantedFeatureRedisExpirationInSeconds);
86
82
  }
87
83
  return grantedFeatureValue;
88
84
  });
@@ -98,7 +94,7 @@ class AuthorizationService {
98
94
  });
99
95
  }
100
96
  static getCachedKey(userId, featureName) {
101
- return `${userId}-granted-feature-${featureName}`;
97
+ return `granted-feature-${featureName}-${userId}`;
102
98
  }
103
99
  static isAuthorizedSingular(accountId, userId, resources, action) {
104
100
  return __awaiter(this, void 0, void 0, function* () {
@@ -154,7 +150,6 @@ class AuthorizationService {
154
150
  }
155
151
  }
156
152
  exports.AuthorizationService = AuthorizationService;
157
- AuthorizationService.cacheExpirationInSeconds = 5 * 60;
158
153
  function createAuthorizationParams(resources, action) {
159
154
  const params = {
160
155
  authorizationObjects: resources.map((resource) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mondaydotcomorg/monday-authorization",
3
- "version": "1.0.44-featureyoniadd-granted-feature-sdk-api.558+2db7dbe4",
3
+ "version": "1.0.44-featureyoniadd-granted-feature-sdk-api.560+aa13dbf4",
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": "2db7dbe4642516131dfd137c959eef8abf05db96"
35
+ "gitHead": "aa13dbf433d5ef55c0b59c19fedab103d4d707b4"
36
36
  }