@mondaydotcomorg/monday-authorization 1.1.9-bugmoshefixhourcolumn.385 → 1.1.9-bugmoshefixlodashesm.3791
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 +15 -0
- package/README.md +57 -0
- package/dist/attributions-service.d.ts +3 -0
- package/dist/attributions-service.js +55 -0
- package/dist/authorization-attributes-service.d.ts +44 -0
- package/dist/authorization-attributes-service.js +144 -0
- package/dist/authorization-internal-service.d.ts +13 -0
- package/dist/authorization-internal-service.js +80 -0
- package/dist/authorization-middleware.d.ts +6 -0
- package/dist/authorization-middleware.js +48 -0
- package/dist/authorization-service.d.ts +29 -0
- package/dist/authorization-service.js +184 -0
- package/dist/constants/sns.d.ts +3 -0
- package/dist/constants/sns.js +9 -0
- package/dist/esm/attributions-service.d.ts +3 -0
- package/dist/esm/attributions-service.mjs +53 -0
- package/dist/esm/authorization-attributes-service.d.ts +44 -0
- package/dist/esm/authorization-attributes-service.mjs +138 -0
- package/dist/esm/authorization-internal-service.d.ts +13 -0
- package/dist/esm/authorization-internal-service.mjs +57 -0
- package/dist/esm/authorization-middleware.d.ts +6 -0
- package/dist/esm/authorization-middleware.mjs +39 -0
- package/dist/esm/authorization-service.d.ts +29 -0
- package/dist/esm/authorization-service.mjs +174 -0
- package/dist/esm/constants/sns.d.ts +3 -0
- package/dist/esm/constants/sns.mjs +5 -0
- package/dist/esm/index.d.ts +13 -0
- package/dist/esm/index.mjs +21 -0
- package/dist/esm/prometheus-service.d.ts +10 -0
- package/dist/esm/prometheus-service.mjs +49 -0
- package/dist/esm/testKit/index.d.ts +11 -0
- package/dist/esm/testKit/index.mjs +44 -0
- package/dist/esm/types/authorization-attributes-contracts.d.ts +27 -0
- package/dist/esm/types/authorization-attributes-contracts.mjs +7 -0
- package/dist/esm/types/express.d.ts +10 -0
- package/dist/esm/types/express.mjs +1 -0
- package/dist/esm/types/general.d.ts +32 -0
- package/dist/esm/types/general.mjs +1 -0
- package/dist/esm/types/scoped-actions-contracts.d.ts +38 -0
- package/dist/esm/types/scoped-actions-contracts.mjs +8 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +27 -0
- package/dist/prometheus-service.d.ts +10 -0
- package/dist/prometheus-service.js +55 -0
- package/dist/testKit/index.d.ts +11 -0
- package/dist/testKit/index.js +48 -0
- package/dist/types/authorization-attributes-contracts.d.ts +27 -0
- package/dist/types/authorization-attributes-contracts.js +7 -0
- package/dist/types/express.d.ts +10 -0
- package/dist/types/express.js +1 -0
- package/dist/types/general.d.ts +32 -0
- package/dist/types/general.js +1 -0
- package/dist/types/scoped-actions-contracts.d.ts +38 -0
- package/dist/types/scoped-actions-contracts.js +8 -0
- package/package.json +32 -11
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { performance } from 'perf_hooks';
|
|
2
|
+
import snakeCase from 'lodash/snakeCase';
|
|
3
|
+
import camelCase from 'lodash/camelCase';
|
|
4
|
+
import mapKeys from 'lodash/mapKeys';
|
|
5
|
+
import { fetch } from '@mondaydotcomorg/monday-fetch';
|
|
6
|
+
import { sendAuthorizationChecksPerRequestMetric, sendAuthorizationCheckResponseTimeMetric } from './prometheus-service.mjs';
|
|
7
|
+
import { AuthorizationInternalService, logger } from './authorization-internal-service.mjs';
|
|
8
|
+
import { getAttributionsFromApi } from './attributions-service.mjs';
|
|
9
|
+
|
|
10
|
+
const GRANTED_FEATURE_CACHE_EXPIRATION_SECONDS = 5 * 60;
|
|
11
|
+
function setRequestFetchOptions(customMondayFetchOptions) {
|
|
12
|
+
AuthorizationInternalService.setRequestFetchOptions(customMondayFetchOptions);
|
|
13
|
+
}
|
|
14
|
+
class AuthorizationService {
|
|
15
|
+
static redisClient;
|
|
16
|
+
static grantedFeatureRedisExpirationInSeconds;
|
|
17
|
+
static async isAuthorized(...args) {
|
|
18
|
+
if (args.length === 3) {
|
|
19
|
+
return this.isAuthorizedMultiple(args[0], args[1], args[2]);
|
|
20
|
+
}
|
|
21
|
+
else if (args.length == 4) {
|
|
22
|
+
return this.isAuthorizedSingular(args[0], args[1], args[2], args[3]);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
throw new Error('isAuthorized accepts either 3 or 4 arguments');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
static async isUserGrantedWithFeature(accountId, userId, featureName, options = {}) {
|
|
29
|
+
const cachedKey = this.getCachedKeyName(userId, featureName);
|
|
30
|
+
const shouldSkipCache = options.shouldSkipCache ?? false;
|
|
31
|
+
if (this.redisClient && !shouldSkipCache) {
|
|
32
|
+
const grantedFeatureValue = await this.redisClient.get(cachedKey);
|
|
33
|
+
if (!(grantedFeatureValue === undefined || grantedFeatureValue === null)) {
|
|
34
|
+
// redis returns the value as string
|
|
35
|
+
return grantedFeatureValue === 'true';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const grantedFeatureValue = await this.fetchIsUserGrantedWithFeature(featureName, accountId, userId);
|
|
39
|
+
if (this.redisClient) {
|
|
40
|
+
await this.redisClient.set(cachedKey, grantedFeatureValue, 'EX', this.grantedFeatureRedisExpirationInSeconds);
|
|
41
|
+
}
|
|
42
|
+
return grantedFeatureValue;
|
|
43
|
+
}
|
|
44
|
+
static async fetchIsUserGrantedWithFeature(featureName, accountId, userId) {
|
|
45
|
+
const authorizationObject = {
|
|
46
|
+
action: featureName,
|
|
47
|
+
resource_type: 'feature',
|
|
48
|
+
};
|
|
49
|
+
const authorizeResponsePromise = await this.isAuthorized(accountId, userId, [authorizationObject]);
|
|
50
|
+
return authorizeResponsePromise.isAuthorized;
|
|
51
|
+
}
|
|
52
|
+
static getCachedKeyName(userId, featureName) {
|
|
53
|
+
return `granted-feature-${featureName}-${userId}`;
|
|
54
|
+
}
|
|
55
|
+
static async canActionInScope(accountId, userId, action, scope) {
|
|
56
|
+
const scopedActions = [{ action, scope }];
|
|
57
|
+
const scopedActionResponseObjects = await this.canActionInScopeMultiple(accountId, userId, scopedActions);
|
|
58
|
+
return scopedActionResponseObjects[0].permit;
|
|
59
|
+
}
|
|
60
|
+
static async canActionInScopeMultiple(accountId, userId, scopedActions) {
|
|
61
|
+
const internalAuthToken = AuthorizationInternalService.generateInternalAuthToken(accountId, userId);
|
|
62
|
+
const scopedActionsPayload = scopedActions.map(scopedAction => {
|
|
63
|
+
return { ...scopedAction, scope: mapKeys(scopedAction.scope, (_, key) => snakeCase(key)) }; // for example: { workspaceId: 1 } => { workspace_id: 1 }
|
|
64
|
+
});
|
|
65
|
+
const attributionHeaders = getAttributionsFromApi();
|
|
66
|
+
const response = await fetch(getCanActionsInScopesUrl(), {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: {
|
|
69
|
+
Authorization: internalAuthToken,
|
|
70
|
+
'Content-Type': 'application/json',
|
|
71
|
+
...attributionHeaders,
|
|
72
|
+
},
|
|
73
|
+
timeout: AuthorizationInternalService.getRequestTimeout(),
|
|
74
|
+
body: JSON.stringify({
|
|
75
|
+
user_id: userId,
|
|
76
|
+
scoped_actions: scopedActionsPayload,
|
|
77
|
+
}),
|
|
78
|
+
}, AuthorizationInternalService.getRequestFetchOptions());
|
|
79
|
+
AuthorizationInternalService.throwOnHttpErrorIfNeeded(response, 'canActionInScopeMultiple');
|
|
80
|
+
const responseBody = await response.json();
|
|
81
|
+
const camelCaseKeys = obj => Object.fromEntries(Object.entries(obj).map(([key, value]) => [camelCase(key), value]));
|
|
82
|
+
const scopedActionsResponseObjects = responseBody.result.map(responseObject => {
|
|
83
|
+
const { scopedAction, permit } = responseObject;
|
|
84
|
+
const { scope } = scopedAction;
|
|
85
|
+
const transformKeys = obj => camelCaseKeys(obj);
|
|
86
|
+
return {
|
|
87
|
+
...responseObject,
|
|
88
|
+
scopedAction: { ...scopedAction, scope: transformKeys(scope) },
|
|
89
|
+
permit: transformKeys(permit),
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
return scopedActionsResponseObjects;
|
|
93
|
+
}
|
|
94
|
+
static async isAuthorizedSingular(accountId, userId, resources, action) {
|
|
95
|
+
const { authorizationObjects } = createAuthorizationParams(resources, action);
|
|
96
|
+
return this.isAuthorizedMultiple(accountId, userId, authorizationObjects);
|
|
97
|
+
}
|
|
98
|
+
static async isAuthorizedMultiple(accountId, userId, authorizationRequestObjects) {
|
|
99
|
+
const internalAuthToken = AuthorizationInternalService.generateInternalAuthToken(accountId, userId);
|
|
100
|
+
const startTime = performance.now();
|
|
101
|
+
const attributionHeaders = getAttributionsFromApi();
|
|
102
|
+
const response = await fetch(getAuthorizeUrl(), {
|
|
103
|
+
method: 'POST',
|
|
104
|
+
headers: {
|
|
105
|
+
Authorization: internalAuthToken,
|
|
106
|
+
'Content-Type': 'application/json',
|
|
107
|
+
...attributionHeaders,
|
|
108
|
+
},
|
|
109
|
+
timeout: AuthorizationInternalService.getRequestTimeout(),
|
|
110
|
+
body: JSON.stringify({
|
|
111
|
+
user_id: userId,
|
|
112
|
+
authorize_request_objects: authorizationRequestObjects,
|
|
113
|
+
}),
|
|
114
|
+
}, AuthorizationInternalService.getRequestFetchOptions());
|
|
115
|
+
const endTime = performance.now();
|
|
116
|
+
const time = endTime - startTime;
|
|
117
|
+
const responseStatus = response.status;
|
|
118
|
+
sendAuthorizationChecksPerRequestMetric(responseStatus, authorizationRequestObjects.length);
|
|
119
|
+
AuthorizationInternalService.throwOnHttpErrorIfNeeded(response, 'isAuthorizedMultiple');
|
|
120
|
+
const responseBody = await response.json();
|
|
121
|
+
const unauthorizedObjects = [];
|
|
122
|
+
responseBody.result.forEach(function (isAuthorized, index) {
|
|
123
|
+
const authorizationObject = authorizationRequestObjects[index];
|
|
124
|
+
if (!isAuthorized) {
|
|
125
|
+
unauthorizedObjects.push(authorizationObject);
|
|
126
|
+
}
|
|
127
|
+
sendAuthorizationCheckResponseTimeMetric(authorizationObject.resource_type, authorizationObject.action, isAuthorized, responseStatus, time);
|
|
128
|
+
});
|
|
129
|
+
if (unauthorizedObjects.length > 0) {
|
|
130
|
+
logger.info({
|
|
131
|
+
resources: JSON.stringify(unauthorizedObjects),
|
|
132
|
+
}, 'AuthorizationService: resource is unauthorized');
|
|
133
|
+
const unauthorizedIds = unauthorizedObjects
|
|
134
|
+
.filter(obj => !!obj.resource_id)
|
|
135
|
+
.map(obj => obj.resource_id);
|
|
136
|
+
return { isAuthorized: false, unauthorizedIds, unauthorizedObjects };
|
|
137
|
+
}
|
|
138
|
+
return { isAuthorized: true };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function setRedisClient(client, grantedFeatureRedisExpirationInSeconds = GRANTED_FEATURE_CACHE_EXPIRATION_SECONDS) {
|
|
142
|
+
AuthorizationService.redisClient = client;
|
|
143
|
+
if (grantedFeatureRedisExpirationInSeconds && grantedFeatureRedisExpirationInSeconds > 0) {
|
|
144
|
+
AuthorizationService.grantedFeatureRedisExpirationInSeconds = grantedFeatureRedisExpirationInSeconds;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
logger.warn({ grantedFeatureRedisExpirationInSeconds }, 'Invalid input for grantedFeatureRedisExpirationInSeconds, must be positive number. using default ttl.');
|
|
148
|
+
AuthorizationService.grantedFeatureRedisExpirationInSeconds = GRANTED_FEATURE_CACHE_EXPIRATION_SECONDS;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function createAuthorizationParams(resources, action) {
|
|
152
|
+
const params = {
|
|
153
|
+
authorizationObjects: resources.map((resource) => {
|
|
154
|
+
const authorizationObject = {
|
|
155
|
+
resource_id: resource.id,
|
|
156
|
+
resource_type: resource.type,
|
|
157
|
+
action,
|
|
158
|
+
};
|
|
159
|
+
if (resource.wrapperData) {
|
|
160
|
+
authorizationObject.wrapper_data = resource.wrapperData;
|
|
161
|
+
}
|
|
162
|
+
return authorizationObject;
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
return params;
|
|
166
|
+
}
|
|
167
|
+
function getAuthorizeUrl() {
|
|
168
|
+
return `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/authorize`;
|
|
169
|
+
}
|
|
170
|
+
function getCanActionsInScopesUrl() {
|
|
171
|
+
return `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/can_actions_in_scopes`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export { AuthorizationService, setRedisClient, setRequestFetchOptions };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const RESOURCE_ATTRIBUTES_SNS_ARN_SECRET_NAME = "AUTHORIZATION_RESOURCE_ATTRIBUTES_SNS_TOPIC";
|
|
2
|
+
export declare const RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND = "resourceAttributeModification";
|
|
3
|
+
export declare const ASYNC_RESOURCE_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE = 100;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
const RESOURCE_ATTRIBUTES_SNS_ARN_SECRET_NAME = 'AUTHORIZATION_RESOURCE_ATTRIBUTES_SNS_TOPIC';
|
|
2
|
+
const RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND = 'resourceAttributeModification';
|
|
3
|
+
const ASYNC_RESOURCE_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE = 100;
|
|
4
|
+
|
|
5
|
+
export { ASYNC_RESOURCE_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE, RESOURCE_ATTRIBUTES_SNS_ARN_SECRET_NAME, RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MondayFetchOptions } from '@mondaydotcomorg/monday-fetch';
|
|
2
|
+
import * as TestKit from './testKit';
|
|
3
|
+
export interface InitOptions {
|
|
4
|
+
prometheus?: any;
|
|
5
|
+
mondayFetchOptions?: MondayFetchOptions;
|
|
6
|
+
redisClient?: any;
|
|
7
|
+
grantedFeatureRedisExpirationInSeconds?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function init(options?: InitOptions): void;
|
|
10
|
+
export { authorizationCheckMiddleware, getAuthorizationMiddleware, skipAuthorizationMiddleware, } from './authorization-middleware';
|
|
11
|
+
export { AuthorizationService } from './authorization-service';
|
|
12
|
+
export { AuthorizationAttributesService } from './authorization-attributes-service';
|
|
13
|
+
export { TestKit };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { setPrometheus } from './prometheus-service.mjs';
|
|
2
|
+
import { setRequestFetchOptions, setRedisClient } from './authorization-service.mjs';
|
|
3
|
+
export { AuthorizationService } from './authorization-service.mjs';
|
|
4
|
+
import * as testKit_index from './testKit/index.mjs';
|
|
5
|
+
export { testKit_index as TestKit };
|
|
6
|
+
export { authorizationCheckMiddleware, getAuthorizationMiddleware, skipAuthorizationMiddleware } from './authorization-middleware.mjs';
|
|
7
|
+
export { AuthorizationAttributesService } from './authorization-attributes-service.mjs';
|
|
8
|
+
|
|
9
|
+
function init(options = {}) {
|
|
10
|
+
if (options.prometheus) {
|
|
11
|
+
setPrometheus(options.prometheus);
|
|
12
|
+
}
|
|
13
|
+
if (options.mondayFetchOptions) {
|
|
14
|
+
setRequestFetchOptions(options.mondayFetchOptions);
|
|
15
|
+
}
|
|
16
|
+
if (options.redisClient) {
|
|
17
|
+
setRedisClient(options.redisClient, options.grantedFeatureRedisExpirationInSeconds);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { init };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Action } from './types/general';
|
|
2
|
+
export declare const METRICS: {
|
|
3
|
+
AUTHORIZATION_CHECK: string;
|
|
4
|
+
AUTHORIZATION_CHECKS_PER_REQUEST: string;
|
|
5
|
+
AUTHORIZATION_CHECK_RESPONSE_TIME: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function setPrometheus(customPrometheus: any): void;
|
|
8
|
+
export declare function getMetricsManager(): any;
|
|
9
|
+
export declare function sendAuthorizationChecksPerRequestMetric(responseStatus: any, amountOfAuthorizationObjects: any): void;
|
|
10
|
+
export declare function sendAuthorizationCheckResponseTimeMetric(resourceType: string, action: Action, isAuthorized: boolean, responseStatus: number, time: number): void;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
let prometheus = null;
|
|
2
|
+
let authorizationChecksPerRequestMetric = null;
|
|
3
|
+
let authorizationCheckResponseTimeMetric = null;
|
|
4
|
+
const METRICS = {
|
|
5
|
+
AUTHORIZATION_CHECK: 'authorization_check',
|
|
6
|
+
AUTHORIZATION_CHECKS_PER_REQUEST: 'authorization_checks_per_request',
|
|
7
|
+
AUTHORIZATION_CHECK_RESPONSE_TIME: 'authorization_check_response_time',
|
|
8
|
+
};
|
|
9
|
+
const authorizationChecksPerRequestMetricConfig = {
|
|
10
|
+
name: METRICS.AUTHORIZATION_CHECKS_PER_REQUEST,
|
|
11
|
+
labels: ['responseStatus'],
|
|
12
|
+
description: 'Authorization checks per request summary',
|
|
13
|
+
};
|
|
14
|
+
const authorizationCheckResponseTimeMetricConfig = {
|
|
15
|
+
name: METRICS.AUTHORIZATION_CHECK_RESPONSE_TIME,
|
|
16
|
+
labels: ['resourceType', 'action', 'isAuthorized', 'responseStatus'],
|
|
17
|
+
description: 'Authorization check response time summary',
|
|
18
|
+
};
|
|
19
|
+
function setPrometheus(customPrometheus) {
|
|
20
|
+
prometheus = customPrometheus;
|
|
21
|
+
const { METRICS_TYPES } = prometheus;
|
|
22
|
+
authorizationChecksPerRequestMetric = getMetricsManager().addMetric(METRICS_TYPES.SUMMARY, authorizationChecksPerRequestMetricConfig.name, authorizationChecksPerRequestMetricConfig.labels, authorizationChecksPerRequestMetricConfig.description);
|
|
23
|
+
authorizationCheckResponseTimeMetric = getMetricsManager().addMetric(METRICS_TYPES.SUMMARY, authorizationCheckResponseTimeMetricConfig.name, authorizationCheckResponseTimeMetricConfig.labels, authorizationCheckResponseTimeMetricConfig.description);
|
|
24
|
+
}
|
|
25
|
+
function getMetricsManager() {
|
|
26
|
+
return prometheus?.metricsManager;
|
|
27
|
+
}
|
|
28
|
+
function sendAuthorizationChecksPerRequestMetric(responseStatus, amountOfAuthorizationObjects) {
|
|
29
|
+
try {
|
|
30
|
+
if (authorizationChecksPerRequestMetric) {
|
|
31
|
+
authorizationChecksPerRequestMetric.labels(responseStatus).observe(amountOfAuthorizationObjects);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
// ignore
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function sendAuthorizationCheckResponseTimeMetric(resourceType, action, isAuthorized, responseStatus, time) {
|
|
39
|
+
try {
|
|
40
|
+
if (authorizationCheckResponseTimeMetric) {
|
|
41
|
+
authorizationCheckResponseTimeMetric.labels(resourceType, action, isAuthorized, responseStatus).observe(time);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
// ignore
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { METRICS, getMetricsManager, sendAuthorizationCheckResponseTimeMetric, sendAuthorizationChecksPerRequestMetric, setPrometheus };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Action, BaseRequest, BaseResponse, ContextGetter, Resource, ResourceGetter } from '../types/general';
|
|
2
|
+
import type { NextFunction } from 'express';
|
|
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,44 @@
|
|
|
1
|
+
import { defaultContextGetter } from '../authorization-middleware.mjs';
|
|
2
|
+
import { AuthorizationInternalService } from '../authorization-internal-service.mjs';
|
|
3
|
+
|
|
4
|
+
let testPermittedActions = [];
|
|
5
|
+
const addTestPermittedAction = (accountId, userId, resources, action) => {
|
|
6
|
+
testPermittedActions.push({ accountId, userId, resources, action });
|
|
7
|
+
};
|
|
8
|
+
const clearTestPermittedActions = () => {
|
|
9
|
+
testPermittedActions = [];
|
|
10
|
+
};
|
|
11
|
+
const isActionAuthorized = (accountId, userId, resources, action) => {
|
|
12
|
+
return {
|
|
13
|
+
isAuthorized: resources.every(_ => {
|
|
14
|
+
return testPermittedActions.some(combination => {
|
|
15
|
+
return (combination.accountId === accountId &&
|
|
16
|
+
combination.userId === userId &&
|
|
17
|
+
combination.action === action &&
|
|
18
|
+
combination.resources.some(combinationResource => {
|
|
19
|
+
return resources.some(resource => {
|
|
20
|
+
return (combinationResource.id === resource.id &&
|
|
21
|
+
combinationResource.type === resource.type &&
|
|
22
|
+
JSON.stringify(combinationResource.wrapperData) === JSON.stringify(resource.wrapperData));
|
|
23
|
+
});
|
|
24
|
+
}));
|
|
25
|
+
});
|
|
26
|
+
}),
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
const getTestAuthorizationMiddleware = (action, resourceGetter, contextGetter) => {
|
|
30
|
+
return async function authorizationMiddleware(request, response, next) {
|
|
31
|
+
contextGetter ||= defaultContextGetter;
|
|
32
|
+
const { userId, accountId } = contextGetter(request);
|
|
33
|
+
const resources = resourceGetter(request);
|
|
34
|
+
const { isAuthorized } = isActionAuthorized(accountId, userId, resources, action);
|
|
35
|
+
AuthorizationInternalService.markAuthorized(request);
|
|
36
|
+
if (!isAuthorized) {
|
|
37
|
+
response.status(403).json({ message: 'Access denied' });
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
next();
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { addTestPermittedAction, clearTestPermittedActions, getTestAuthorizationMiddleware };
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
}
|
|
11
|
+
export interface ResourceAttributeDelete {
|
|
12
|
+
resourceType: Resource['type'];
|
|
13
|
+
resourceId: Resource['id'];
|
|
14
|
+
key: string;
|
|
15
|
+
}
|
|
16
|
+
export declare enum ResourceAttributeOperationEnum {
|
|
17
|
+
UPSERT = "upsert",
|
|
18
|
+
DELETE = "delete"
|
|
19
|
+
}
|
|
20
|
+
interface UpsertResourceAttributeOperation extends ResourceAttributeAssignment {
|
|
21
|
+
operationType: ResourceAttributeOperationEnum.UPSERT;
|
|
22
|
+
}
|
|
23
|
+
interface DeleteResourceAttributeOperation extends ResourceAttributeDelete {
|
|
24
|
+
operationType: ResourceAttributeOperationEnum.DELETE;
|
|
25
|
+
}
|
|
26
|
+
export type ResourceAttributesOperation = UpsertResourceAttributeOperation | DeleteResourceAttributeOperation;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
var ResourceAttributeOperationEnum;
|
|
2
|
+
(function (ResourceAttributeOperationEnum) {
|
|
3
|
+
ResourceAttributeOperationEnum["UPSERT"] = "upsert";
|
|
4
|
+
ResourceAttributeOperationEnum["DELETE"] = "delete";
|
|
5
|
+
})(ResourceAttributeOperationEnum || (ResourceAttributeOperationEnum = {}));
|
|
6
|
+
|
|
7
|
+
export { ResourceAttributeOperationEnum };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Request, Response } from 'express';
|
|
2
|
+
export interface Resource {
|
|
3
|
+
id?: number;
|
|
4
|
+
type: string;
|
|
5
|
+
wrapperData?: object;
|
|
6
|
+
}
|
|
7
|
+
export type Action = string;
|
|
8
|
+
export interface Context {
|
|
9
|
+
accountId: number;
|
|
10
|
+
userId: number;
|
|
11
|
+
}
|
|
12
|
+
export interface AuthorizationObject {
|
|
13
|
+
resource_id?: Resource['id'];
|
|
14
|
+
resource_type: Resource['type'];
|
|
15
|
+
wrapper_data?: Resource['wrapperData'];
|
|
16
|
+
action: Action;
|
|
17
|
+
}
|
|
18
|
+
export interface AuthorizationParams {
|
|
19
|
+
authorizationObjects: AuthorizationObject[];
|
|
20
|
+
}
|
|
21
|
+
type BasicObject = {
|
|
22
|
+
[key: string]: string;
|
|
23
|
+
};
|
|
24
|
+
export type BaseParameters = BasicObject;
|
|
25
|
+
export type BaseResponseBody = BasicObject;
|
|
26
|
+
export type BaseBodyParameters = BasicObject;
|
|
27
|
+
export type BaseQueryParameters = BasicObject;
|
|
28
|
+
export type BaseRequest = Request<BaseParameters, BaseResponseBody, BaseBodyParameters, BaseQueryParameters>;
|
|
29
|
+
export type BaseResponse = Response<BaseResponseBody>;
|
|
30
|
+
export type ResourceGetter = (request: BaseRequest) => Resource[];
|
|
31
|
+
export type ContextGetter = (request: BaseRequest) => Context;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export interface WorkspaceScope {
|
|
2
|
+
workspaceId: number;
|
|
3
|
+
}
|
|
4
|
+
export interface BoardScope {
|
|
5
|
+
boardId: number;
|
|
6
|
+
}
|
|
7
|
+
export interface PulseScope {
|
|
8
|
+
pulseId: number;
|
|
9
|
+
}
|
|
10
|
+
export interface AccountProductScope {
|
|
11
|
+
accountProductId: number;
|
|
12
|
+
}
|
|
13
|
+
export interface AccountScope {
|
|
14
|
+
accountId: number;
|
|
15
|
+
}
|
|
16
|
+
export type ScopeOptions = WorkspaceScope | BoardScope | PulseScope | AccountProductScope | AccountScope;
|
|
17
|
+
export interface Translation {
|
|
18
|
+
key: string;
|
|
19
|
+
[option: string]: string;
|
|
20
|
+
}
|
|
21
|
+
export declare enum PermitTechnicalReason {
|
|
22
|
+
NO_REASON = 0,
|
|
23
|
+
NOT_ELIGIBLE = 1,
|
|
24
|
+
BY_ROLE_IN_SCOPE = 2
|
|
25
|
+
}
|
|
26
|
+
export interface ScopedActionPermit {
|
|
27
|
+
can: boolean;
|
|
28
|
+
reason: Translation;
|
|
29
|
+
technicalReason: PermitTechnicalReason;
|
|
30
|
+
}
|
|
31
|
+
export interface ScopedAction {
|
|
32
|
+
action: string;
|
|
33
|
+
scope: ScopeOptions;
|
|
34
|
+
}
|
|
35
|
+
export interface ScopedActionResponseObject {
|
|
36
|
+
scopedAction: ScopedAction;
|
|
37
|
+
permit: ScopedActionPermit;
|
|
38
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
var PermitTechnicalReason;
|
|
2
|
+
(function (PermitTechnicalReason) {
|
|
3
|
+
PermitTechnicalReason[PermitTechnicalReason["NO_REASON"] = 0] = "NO_REASON";
|
|
4
|
+
PermitTechnicalReason[PermitTechnicalReason["NOT_ELIGIBLE"] = 1] = "NOT_ELIGIBLE";
|
|
5
|
+
PermitTechnicalReason[PermitTechnicalReason["BY_ROLE_IN_SCOPE"] = 2] = "BY_ROLE_IN_SCOPE";
|
|
6
|
+
})(PermitTechnicalReason || (PermitTechnicalReason = {}));
|
|
7
|
+
|
|
8
|
+
export { PermitTechnicalReason };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MondayFetchOptions } from '@mondaydotcomorg/monday-fetch';
|
|
2
|
+
import * as TestKit from './testKit';
|
|
3
|
+
export interface InitOptions {
|
|
4
|
+
prometheus?: any;
|
|
5
|
+
mondayFetchOptions?: MondayFetchOptions;
|
|
6
|
+
redisClient?: any;
|
|
7
|
+
grantedFeatureRedisExpirationInSeconds?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function init(options?: InitOptions): void;
|
|
10
|
+
export { authorizationCheckMiddleware, getAuthorizationMiddleware, skipAuthorizationMiddleware, } from './authorization-middleware';
|
|
11
|
+
export { AuthorizationService } from './authorization-service';
|
|
12
|
+
export { AuthorizationAttributesService } from './authorization-attributes-service';
|
|
13
|
+
export { TestKit };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
const prometheusService = require('./prometheus-service.js');
|
|
4
|
+
const authorizationService = require('./authorization-service.js');
|
|
5
|
+
const testKit_index = require('./testKit/index.js');
|
|
6
|
+
const authorizationMiddleware = require('./authorization-middleware.js');
|
|
7
|
+
const authorizationAttributesService = require('./authorization-attributes-service.js');
|
|
8
|
+
|
|
9
|
+
function init(options = {}) {
|
|
10
|
+
if (options.prometheus) {
|
|
11
|
+
prometheusService.setPrometheus(options.prometheus);
|
|
12
|
+
}
|
|
13
|
+
if (options.mondayFetchOptions) {
|
|
14
|
+
authorizationService.setRequestFetchOptions(options.mondayFetchOptions);
|
|
15
|
+
}
|
|
16
|
+
if (options.redisClient) {
|
|
17
|
+
authorizationService.setRedisClient(options.redisClient, options.grantedFeatureRedisExpirationInSeconds);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
exports.AuthorizationService = authorizationService.AuthorizationService;
|
|
22
|
+
exports.TestKit = testKit_index;
|
|
23
|
+
exports.authorizationCheckMiddleware = authorizationMiddleware.authorizationCheckMiddleware;
|
|
24
|
+
exports.getAuthorizationMiddleware = authorizationMiddleware.getAuthorizationMiddleware;
|
|
25
|
+
exports.skipAuthorizationMiddleware = authorizationMiddleware.skipAuthorizationMiddleware;
|
|
26
|
+
exports.AuthorizationAttributesService = authorizationAttributesService.AuthorizationAttributesService;
|
|
27
|
+
exports.init = init;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Action } from './types/general';
|
|
2
|
+
export declare const METRICS: {
|
|
3
|
+
AUTHORIZATION_CHECK: string;
|
|
4
|
+
AUTHORIZATION_CHECKS_PER_REQUEST: string;
|
|
5
|
+
AUTHORIZATION_CHECK_RESPONSE_TIME: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function setPrometheus(customPrometheus: any): void;
|
|
8
|
+
export declare function getMetricsManager(): any;
|
|
9
|
+
export declare function sendAuthorizationChecksPerRequestMetric(responseStatus: any, amountOfAuthorizationObjects: any): void;
|
|
10
|
+
export declare function sendAuthorizationCheckResponseTimeMetric(resourceType: string, action: Action, isAuthorized: boolean, responseStatus: number, time: number): void;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
let prometheus = null;
|
|
4
|
+
let authorizationChecksPerRequestMetric = null;
|
|
5
|
+
let authorizationCheckResponseTimeMetric = null;
|
|
6
|
+
const METRICS = {
|
|
7
|
+
AUTHORIZATION_CHECK: 'authorization_check',
|
|
8
|
+
AUTHORIZATION_CHECKS_PER_REQUEST: 'authorization_checks_per_request',
|
|
9
|
+
AUTHORIZATION_CHECK_RESPONSE_TIME: 'authorization_check_response_time',
|
|
10
|
+
};
|
|
11
|
+
const authorizationChecksPerRequestMetricConfig = {
|
|
12
|
+
name: METRICS.AUTHORIZATION_CHECKS_PER_REQUEST,
|
|
13
|
+
labels: ['responseStatus'],
|
|
14
|
+
description: 'Authorization checks per request summary',
|
|
15
|
+
};
|
|
16
|
+
const authorizationCheckResponseTimeMetricConfig = {
|
|
17
|
+
name: METRICS.AUTHORIZATION_CHECK_RESPONSE_TIME,
|
|
18
|
+
labels: ['resourceType', 'action', 'isAuthorized', 'responseStatus'],
|
|
19
|
+
description: 'Authorization check response time summary',
|
|
20
|
+
};
|
|
21
|
+
function setPrometheus(customPrometheus) {
|
|
22
|
+
prometheus = customPrometheus;
|
|
23
|
+
const { METRICS_TYPES } = prometheus;
|
|
24
|
+
authorizationChecksPerRequestMetric = getMetricsManager().addMetric(METRICS_TYPES.SUMMARY, authorizationChecksPerRequestMetricConfig.name, authorizationChecksPerRequestMetricConfig.labels, authorizationChecksPerRequestMetricConfig.description);
|
|
25
|
+
authorizationCheckResponseTimeMetric = getMetricsManager().addMetric(METRICS_TYPES.SUMMARY, authorizationCheckResponseTimeMetricConfig.name, authorizationCheckResponseTimeMetricConfig.labels, authorizationCheckResponseTimeMetricConfig.description);
|
|
26
|
+
}
|
|
27
|
+
function getMetricsManager() {
|
|
28
|
+
return prometheus?.metricsManager;
|
|
29
|
+
}
|
|
30
|
+
function sendAuthorizationChecksPerRequestMetric(responseStatus, amountOfAuthorizationObjects) {
|
|
31
|
+
try {
|
|
32
|
+
if (authorizationChecksPerRequestMetric) {
|
|
33
|
+
authorizationChecksPerRequestMetric.labels(responseStatus).observe(amountOfAuthorizationObjects);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
// ignore
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function sendAuthorizationCheckResponseTimeMetric(resourceType, action, isAuthorized, responseStatus, time) {
|
|
41
|
+
try {
|
|
42
|
+
if (authorizationCheckResponseTimeMetric) {
|
|
43
|
+
authorizationCheckResponseTimeMetric.labels(resourceType, action, isAuthorized, responseStatus).observe(time);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
// ignore
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
exports.METRICS = METRICS;
|
|
52
|
+
exports.getMetricsManager = getMetricsManager;
|
|
53
|
+
exports.sendAuthorizationCheckResponseTimeMetric = sendAuthorizationCheckResponseTimeMetric;
|
|
54
|
+
exports.sendAuthorizationChecksPerRequestMetric = sendAuthorizationChecksPerRequestMetric;
|
|
55
|
+
exports.setPrometheus = setPrometheus;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Action, BaseRequest, BaseResponse, ContextGetter, Resource, ResourceGetter } from '../types/general';
|
|
2
|
+
import type { NextFunction } from 'express';
|
|
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,48 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
const authorizationMiddleware = require('../authorization-middleware.js');
|
|
4
|
+
const authorizationInternalService = require('../authorization-internal-service.js');
|
|
5
|
+
|
|
6
|
+
let testPermittedActions = [];
|
|
7
|
+
const addTestPermittedAction = (accountId, userId, resources, action) => {
|
|
8
|
+
testPermittedActions.push({ accountId, userId, resources, action });
|
|
9
|
+
};
|
|
10
|
+
const clearTestPermittedActions = () => {
|
|
11
|
+
testPermittedActions = [];
|
|
12
|
+
};
|
|
13
|
+
const isActionAuthorized = (accountId, userId, resources, action) => {
|
|
14
|
+
return {
|
|
15
|
+
isAuthorized: resources.every(_ => {
|
|
16
|
+
return testPermittedActions.some(combination => {
|
|
17
|
+
return (combination.accountId === accountId &&
|
|
18
|
+
combination.userId === userId &&
|
|
19
|
+
combination.action === action &&
|
|
20
|
+
combination.resources.some(combinationResource => {
|
|
21
|
+
return resources.some(resource => {
|
|
22
|
+
return (combinationResource.id === resource.id &&
|
|
23
|
+
combinationResource.type === resource.type &&
|
|
24
|
+
JSON.stringify(combinationResource.wrapperData) === JSON.stringify(resource.wrapperData));
|
|
25
|
+
});
|
|
26
|
+
}));
|
|
27
|
+
});
|
|
28
|
+
}),
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
const getTestAuthorizationMiddleware = (action, resourceGetter, contextGetter) => {
|
|
32
|
+
return async function authorizationMiddleware$1(request, response, next) {
|
|
33
|
+
contextGetter ||= authorizationMiddleware.defaultContextGetter;
|
|
34
|
+
const { userId, accountId } = contextGetter(request);
|
|
35
|
+
const resources = resourceGetter(request);
|
|
36
|
+
const { isAuthorized } = isActionAuthorized(accountId, userId, resources, action);
|
|
37
|
+
authorizationInternalService.AuthorizationInternalService.markAuthorized(request);
|
|
38
|
+
if (!isAuthorized) {
|
|
39
|
+
response.status(403).json({ message: 'Access denied' });
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
next();
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
exports.addTestPermittedAction = addTestPermittedAction;
|
|
47
|
+
exports.clearTestPermittedActions = clearTestPermittedActions;
|
|
48
|
+
exports.getTestAuthorizationMiddleware = getTestAuthorizationMiddleware;
|