@mondaydotcomorg/monday-authorization 1.2.3 → 1.2.5
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/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MondayFetchOptions } from '@mondaydotcomorg/monday-fetch';
|
|
2
|
+
import * as TestKit from './lib/testKit';
|
|
2
3
|
export interface InitOptions {
|
|
3
4
|
prometheus?: any;
|
|
4
5
|
mondayFetchOptions?: MondayFetchOptions;
|
|
@@ -9,3 +10,4 @@ export declare function init(options?: InitOptions): void;
|
|
|
9
10
|
export { authorizationCheckMiddleware, getAuthorizationMiddleware, skipAuthorizationMiddleware, } from './lib/authorization-middleware';
|
|
10
11
|
export { AuthorizationService } from './lib/authorization-service';
|
|
11
12
|
export { AuthorizationAttributesService } from './lib/authorization-attributes-service';
|
|
13
|
+
export { TestKit };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,33 @@
|
|
|
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
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AuthorizationAttributesService = exports.AuthorizationService = exports.skipAuthorizationMiddleware = exports.getAuthorizationMiddleware = exports.authorizationCheckMiddleware = exports.init = void 0;
|
|
26
|
+
exports.TestKit = exports.AuthorizationAttributesService = exports.AuthorizationService = exports.skipAuthorizationMiddleware = exports.getAuthorizationMiddleware = exports.authorizationCheckMiddleware = exports.init = void 0;
|
|
4
27
|
const prometheus_service_1 = require("./lib/prometheus-service");
|
|
5
28
|
const authorization_service_1 = require("./lib/authorization-service");
|
|
29
|
+
const TestKit = __importStar(require("./lib/testKit"));
|
|
30
|
+
exports.TestKit = TestKit;
|
|
6
31
|
function init(options = {}) {
|
|
7
32
|
if (options.prometheus) {
|
|
8
33
|
(0, prometheus_service_1.setPrometheus)(options.prometheus);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NextFunction } from 'express';
|
|
2
|
-
import { Action, BaseRequest, BaseResponse, ContextGetter, ResourceGetter } from './types/general';
|
|
2
|
+
import { Action, BaseRequest, BaseResponse, Context, ContextGetter, ResourceGetter } from './types/general';
|
|
3
3
|
export declare function getAuthorizationMiddleware(action: Action, resourceGetter: ResourceGetter, contextGetter?: ContextGetter): (request: BaseRequest, response: BaseResponse, next: NextFunction) => Promise<void>;
|
|
4
4
|
export declare function skipAuthorizationMiddleware(request: BaseRequest, response: BaseResponse, next: NextFunction): void;
|
|
5
5
|
export declare function authorizationCheckMiddleware(request: BaseRequest, response: BaseResponse, next: NextFunction): void;
|
|
6
|
+
export declare function defaultContextGetter(request: BaseRequest): Context;
|
|
@@ -12,10 +12,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.authorizationCheckMiddleware = exports.skipAuthorizationMiddleware = exports.getAuthorizationMiddleware = void 0;
|
|
15
|
+
exports.defaultContextGetter = exports.authorizationCheckMiddleware = exports.skipAuthorizationMiddleware = exports.getAuthorizationMiddleware = void 0;
|
|
16
16
|
const on_headers_1 = __importDefault(require("on-headers"));
|
|
17
17
|
const authorization_internal_service_1 = require("./authorization-internal-service");
|
|
18
18
|
const authorization_service_1 = require("./authorization-service");
|
|
19
|
+
// getAuthorizationMiddleware is duplicated in testKit/index.ts
|
|
20
|
+
// If you are making changes to this function, please make sure to update the other file as well
|
|
19
21
|
function getAuthorizationMiddleware(action, resourceGetter, contextGetter) {
|
|
20
22
|
return function authorizationMiddleware(request, response, next) {
|
|
21
23
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -52,3 +54,4 @@ exports.authorizationCheckMiddleware = authorizationCheckMiddleware;
|
|
|
52
54
|
function defaultContextGetter(request) {
|
|
53
55
|
return request.payload;
|
|
54
56
|
}
|
|
57
|
+
exports.defaultContextGetter = defaultContextGetter;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { NextFunction } from "express";
|
|
2
|
+
import { Action, BaseRequest, BaseResponse, ContextGetter, Resource, ResourceGetter } from "../types/general";
|
|
3
|
+
export type TestPermittedAction = {
|
|
4
|
+
accountId: number;
|
|
5
|
+
userId: number;
|
|
6
|
+
resources: Resource[];
|
|
7
|
+
action: Action;
|
|
8
|
+
};
|
|
9
|
+
export declare const addTestPermittedAction: (accountId: number, userId: number, resources: Resource[], action: Action) => void;
|
|
10
|
+
export declare const clearTestPermittedActions: () => void;
|
|
11
|
+
export declare const getTestAuthorizationMiddleware: (action: Action, resourceGetter: ResourceGetter, contextGetter?: ContextGetter) => (request: BaseRequest, response: BaseResponse, next: NextFunction) => Promise<void>;
|
|
@@ -0,0 +1,58 @@
|
|
|
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.getTestAuthorizationMiddleware = exports.clearTestPermittedActions = exports.addTestPermittedAction = void 0;
|
|
13
|
+
const authorization_middleware_1 = require("../authorization-middleware");
|
|
14
|
+
const authorization_internal_service_1 = require("../authorization-internal-service");
|
|
15
|
+
let testPermittedActions = [];
|
|
16
|
+
const addTestPermittedAction = (accountId, userId, resources, action) => {
|
|
17
|
+
testPermittedActions.push({ accountId, userId, resources, action });
|
|
18
|
+
};
|
|
19
|
+
exports.addTestPermittedAction = addTestPermittedAction;
|
|
20
|
+
const clearTestPermittedActions = () => {
|
|
21
|
+
testPermittedActions = [];
|
|
22
|
+
};
|
|
23
|
+
exports.clearTestPermittedActions = clearTestPermittedActions;
|
|
24
|
+
const isActionAuthorized = (accountId, userId, resources, action) => {
|
|
25
|
+
return {
|
|
26
|
+
isAuthorized: resources.every(resource => {
|
|
27
|
+
return testPermittedActions.some(combination => {
|
|
28
|
+
return combination.accountId === accountId &&
|
|
29
|
+
combination.userId === userId &&
|
|
30
|
+
combination.action === action &&
|
|
31
|
+
combination.resources.some(combinationResource => {
|
|
32
|
+
return resources.some(resource => {
|
|
33
|
+
return combinationResource.id === resource.id &&
|
|
34
|
+
combinationResource.type === resource.type &&
|
|
35
|
+
JSON.stringify(combinationResource.wrapperData) === JSON.stringify(resource.wrapperData);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
})
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
const getTestAuthorizationMiddleware = (action, resourceGetter, contextGetter) => {
|
|
43
|
+
return function authorizationMiddleware(request, response, next) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
contextGetter || (contextGetter = authorization_middleware_1.defaultContextGetter);
|
|
46
|
+
const { userId, accountId } = contextGetter(request);
|
|
47
|
+
const resources = resourceGetter(request);
|
|
48
|
+
const { isAuthorized } = isActionAuthorized(accountId, userId, resources, action);
|
|
49
|
+
authorization_internal_service_1.AuthorizationInternalService.markAuthorized(request);
|
|
50
|
+
if (!isAuthorized) {
|
|
51
|
+
response.status(403).json({ message: 'Access denied' });
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
next();
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
exports.getTestAuthorizationMiddleware = getTestAuthorizationMiddleware;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mondaydotcomorg/monday-authorization",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
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": "
|
|
35
|
+
"gitHead": "c244e400fe97b5535aab3bb061679ea398c2d9df"
|
|
36
36
|
}
|