@mondaydotcomorg/monday-authorization 1.0.15 → 1.0.16

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
@@ -18,28 +18,45 @@ yarn add @mondaydotcomorg/monday-authorization
18
18
 
19
19
  ## Usage
20
20
 
21
+ First init the package in order for it to work properly
22
+
23
+ app.ts:
24
+
25
+ ```ts
26
+ import { startServer, getPrometheus } from '@mondaydotcomorg/monday-server-runner';
27
+ import * as MondayAuthorization from '@mondaydotcomorg/monday-authorization';
28
+
29
+ ...
30
+
31
+ MondayAuthorization.init({ prometheus: getPrometheus() });
32
+ startServer(...)
21
33
  ```
34
+
35
+ Then add this code to any route declaration:
36
+
37
+ ```ts
22
38
  import { Router } from 'express';
23
- import { getAuthorizationMiddleware, skipAuthorizationMiddleware, authorizationCheckMiddleware } from '@mondaydotcomorg/monday-authorization'
39
+ import {
40
+ getAuthorizationMiddleware,
41
+ skipAuthorizationMiddleware,
42
+ authorizationCheckMiddleware,
43
+ } from '@mondaydotcomorg/monday-authorization';
24
44
  import { like } from 'src/controllers/likes/likes-controller';
25
45
 
26
46
  const action = 'like';
27
- const resourceGetter = (request) => {
47
+ const resourceGetter = request => {
28
48
  return [
29
49
  {
30
50
  id: request.params.postId,
31
51
  type: 'post',
32
- wrapper_data: { item_id: 431234} //optional
33
- }
52
+ wrapper_data: { item_id: 431234 }, //optional
53
+ },
34
54
  ];
35
55
  };
36
56
  router = Router();
37
57
  router.use(authorizationCheckMiddleware);
38
- router.post('/posts/:postId/like', getAuthorizationMiddleware(action, resourceGetter),
39
- like
40
- );
58
+ router.post('/posts/:postId/like', getAuthorizationMiddleware(action, resourceGetter), like);
41
59
  router.get('/internal/some_unauthorized_endpoint', skipAuthorizationMiddleware, handler);
42
-
43
60
  ```
44
61
 
45
62
  `resourceGetter` is a function that gets the request and return an array of resources.
@@ -51,7 +68,7 @@ The item_id is all that needed for the authorization of posts.
51
68
  by default. If you're not using the authentication middleware, you will have to provide a contextGetter
52
69
  function, that looks like this:
53
70
 
54
- ```
71
+ ```ts
55
72
  (request) => {
56
73
  return ({
57
74
  accountId: ...,
package/dist/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
+ export interface InitOptions {
2
+ prometheus?: any;
3
+ }
4
+ export declare function init(options?: InitOptions): void;
1
5
  export { authorizationCheckMiddleware, getAuthorizationMiddleware, skipAuthorizationMiddleware, } from './lib/authorization-middleware';
2
6
  export { AuthorizationService } from './lib/authorization-service';
package/dist/index.js CHANGED
@@ -1,6 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AuthorizationService = exports.skipAuthorizationMiddleware = exports.getAuthorizationMiddleware = exports.authorizationCheckMiddleware = void 0;
3
+ exports.AuthorizationService = exports.skipAuthorizationMiddleware = exports.getAuthorizationMiddleware = exports.authorizationCheckMiddleware = exports.init = void 0;
4
+ const prometheus_service_1 = require("./lib/prometheus-service");
5
+ function init(options = {}) {
6
+ if (options.prometheus) {
7
+ prometheus_service_1.setPrometheus(options.prometheus);
8
+ }
9
+ }
10
+ exports.init = init;
4
11
  var authorization_middleware_1 = require("./lib/authorization-middleware");
5
12
  Object.defineProperty(exports, "authorizationCheckMiddleware", { enumerable: true, get: function () { return authorization_middleware_1.authorizationCheckMiddleware; } });
6
13
  Object.defineProperty(exports, "getAuthorizationMiddleware", { enumerable: true, get: function () { return authorization_middleware_1.getAuthorizationMiddleware; } });
@@ -35,6 +35,7 @@ exports.AuthorizationService = void 0;
35
35
  const monday_jwt_1 = require("@mondaydotcomorg/monday-jwt");
36
36
  const MondayLogger = __importStar(require("@mondaydotcomorg/monday-logger"));
37
37
  const monday_fetch_1 = __importDefault(require("@mondaydotcomorg/monday-fetch"));
38
+ const prometheus_service_1 = require("./prometheus-service");
38
39
  const INTERNAL_APP_NAME = 'internal_ms';
39
40
  const URL = `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/authorize`;
40
41
  const IS_DEV_ENV = process.env.NODE_ENV === 'development';
@@ -60,10 +61,12 @@ class AuthorizationService {
60
61
  }
61
62
  const responseBody = yield response.json();
62
63
  const unauthorizedObjects = [];
63
- responseBody.result.forEach(function (value, index) {
64
- if (!value) {
65
- unauthorizedObjects.push(params.authorizationObjects[index]);
64
+ responseBody.result.forEach(function (isAuthorized, index) {
65
+ const authorizationObject = params.authorizationObjects[index];
66
+ if (!isAuthorized) {
67
+ unauthorizedObjects.push(authorizationObject);
66
68
  }
69
+ sendAuthorizationCheckMetric(authorizationObject.resource_type, isAuthorized);
67
70
  });
68
71
  if (unauthorizedObjects.length > 0) {
69
72
  logger.info({
@@ -96,3 +99,12 @@ function createAuthorizationParams(resources, action) {
96
99
  function logOnFetchFail(retries, error, response) {
97
100
  logger.error({ attempt: retries, error }, 'Authorization attempt failed due to network issues');
98
101
  }
102
+ function sendAuthorizationCheckMetric(resourceType, isAuthorized) {
103
+ const metricsManager = prometheus_service_1.getMetricsManager();
104
+ if (metricsManager) {
105
+ metricsManager.increaseCounter(prometheus_service_1.METRICS.AUTHORIZATION_CHECK, {
106
+ resourceType,
107
+ isAuthorized,
108
+ });
109
+ }
110
+ }
@@ -0,0 +1,5 @@
1
+ export declare const METRICS: {
2
+ AUTHORIZATION_CHECK: string;
3
+ };
4
+ export declare function setPrometheus(customPrometheus: any): void;
5
+ export declare function getMetricsManager(): any;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMetricsManager = exports.setPrometheus = exports.METRICS = void 0;
4
+ let prometheus = null;
5
+ exports.METRICS = {
6
+ AUTHORIZATION_CHECK: 'authorization_check',
7
+ };
8
+ function setPrometheus(customPrometheus) {
9
+ prometheus = customPrometheus;
10
+ }
11
+ exports.setPrometheus = setPrometheus;
12
+ function getMetricsManager() {
13
+ return prometheus === null || prometheus === void 0 ? void 0 : prometheus.metricsManager;
14
+ }
15
+ exports.getMetricsManager = getMetricsManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mondaydotcomorg/monday-authorization",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "BSD-3-Clause",
@@ -31,5 +31,5 @@
31
31
  "files": [
32
32
  "dist/"
33
33
  ],
34
- "gitHead": "d679b58dc0c0f3c27ed0f938350a79302caa8aee"
34
+ "gitHead": "bc21495debb5e34772c3c1fd8dff26870081d103"
35
35
  }