@mondaydotcomorg/monday-authorization 1.1.9-featuremosheauthorizationesm.3695 → 1.1.9-featureorcomonday-jwt-ts.472
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 +0 -15
- package/README.md +0 -57
- package/dist/index.d.ts +2 -5
- package/dist/index.js +14 -18
- package/dist/lib/authorization-internal-service.d.ts +6 -0
- package/dist/lib/authorization-internal-service.js +17 -0
- package/dist/{authorization-middleware.d.ts → lib/authorization-middleware.d.ts} +1 -2
- package/dist/lib/authorization-middleware.js +54 -0
- package/dist/{authorization-service.d.ts → lib/authorization-service.d.ts} +1 -2
- package/dist/lib/authorization-service.js +233 -0
- package/dist/{prometheus-service.js → lib/prometheus-service.js} +10 -11
- package/dist/lib/types/express.js +1 -0
- package/dist/lib/types/general.js +2 -0
- package/dist/{types → lib/types}/scoped-actions-contracts.js +4 -3
- package/package.json +7 -30
- package/dist/attributions-service.d.ts +0 -3
- package/dist/attributions-service.js +0 -55
- package/dist/authorization-attributes-service.d.ts +0 -44
- package/dist/authorization-attributes-service.js +0 -144
- package/dist/authorization-internal-service.d.ts +0 -13
- package/dist/authorization-internal-service.js +0 -80
- package/dist/authorization-middleware.js +0 -48
- package/dist/authorization-service.js +0 -176
- package/dist/constants/sns.d.ts +0 -3
- package/dist/constants/sns.js +0 -9
- package/dist/esm/attributions-service.d.ts +0 -3
- package/dist/esm/attributions-service.mjs +0 -53
- package/dist/esm/authorization-attributes-service.d.ts +0 -44
- package/dist/esm/authorization-attributes-service.mjs +0 -138
- package/dist/esm/authorization-internal-service.d.ts +0 -13
- package/dist/esm/authorization-internal-service.mjs +0 -57
- package/dist/esm/authorization-middleware.d.ts +0 -6
- package/dist/esm/authorization-middleware.mjs +0 -39
- package/dist/esm/authorization-service.d.ts +0 -29
- package/dist/esm/authorization-service.mjs +0 -172
- package/dist/esm/constants/sns.d.ts +0 -3
- package/dist/esm/constants/sns.mjs +0 -5
- package/dist/esm/index.d.ts +0 -13
- package/dist/esm/index.mjs +0 -21
- package/dist/esm/prometheus-service.mjs +0 -45
- package/dist/esm/testKit/index.d.ts +0 -11
- package/dist/esm/testKit/index.mjs +0 -44
- package/dist/esm/types/authorization-attributes-contracts.d.ts +0 -27
- package/dist/esm/types/authorization-attributes-contracts.mjs +0 -7
- package/dist/esm/types/express.mjs +0 -1
- package/dist/esm/types/general.mjs +0 -1
- package/dist/esm/types/scoped-actions-contracts.mjs +0 -8
- package/dist/prometheus-service.d.ts +0 -10
- package/dist/testKit/index.d.ts +0 -11
- package/dist/testKit/index.js +0 -48
- package/dist/types/authorization-attributes-contracts.d.ts +0 -27
- package/dist/types/authorization-attributes-contracts.js +0 -7
- package/dist/types/express.d.ts +0 -10
- package/dist/types/express.js +0 -1
- package/dist/types/general.d.ts +0 -30
- package/dist/types/general.js +0 -1
- package/dist/types/scoped-actions-contracts.d.ts +0 -38
- /package/dist/{esm → lib}/prometheus-service.d.ts +0 -0
- /package/dist/{esm → lib}/types/express.d.ts +0 -0
- /package/dist/{esm → lib}/types/general.d.ts +0 -0
- /package/dist/{esm → lib}/types/scoped-actions-contracts.d.ts +0 -0
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { ResourceAttributeAssignment, ResourceAttributeResponse, ResourceAttributesOperation } from './types/authorization-attributes-contracts';
|
|
2
|
-
import { Resource } from './types/general';
|
|
3
|
-
export declare class AuthorizationAttributesService {
|
|
4
|
-
private static LOG_TAG;
|
|
5
|
-
/**
|
|
6
|
-
* Upsert resource attributes synchronously, performing http call to the authorization MS to assign the given attributes to the given resource.
|
|
7
|
-
* @param accountId
|
|
8
|
-
* @param userId
|
|
9
|
-
* @param resourceAttributeAssignments - Array of resource (resourceType, resourceId) and attribute (key, value) pairs to upsert in the authorization MS.
|
|
10
|
-
* e.g. [{ resourceType: 'board', resourceId: 123, key: 'board_kind', value: 'private' }]
|
|
11
|
-
* @returns ResourceAttributeResponse - The affected (created and updated_ resource attributes assignments in the `attributes` field.
|
|
12
|
-
*/
|
|
13
|
-
static upsertResourceAttributes(accountId: number, userId: number, resourceAttributeAssignments: ResourceAttributeAssignment[]): Promise<ResourceAttributeResponse>;
|
|
14
|
-
/**
|
|
15
|
-
* Delete resource attributes assignments synchronously, performing http call to the authorization MS to delete the given attributes from the given singular resource.
|
|
16
|
-
* @param accountId
|
|
17
|
-
* @param userId
|
|
18
|
-
* @param resource - The resource (resourceType, resourceId) to delete the attributes for.
|
|
19
|
-
* @param attributeKeys - Array of attribute keys to delete for the resource.
|
|
20
|
-
* @returns ResourceAttributeResponse - The affected (deleted) resource attributes assignments in the `attributes` field.
|
|
21
|
-
*/
|
|
22
|
-
static deleteResourceAttributes(accountId: number, userId: number, resource: Resource, attributeKeys: string[]): Promise<ResourceAttributeResponse>;
|
|
23
|
-
/**
|
|
24
|
-
* Async function, this function only send the updates request to SNS and return before the change actually took place
|
|
25
|
-
* @param accountId
|
|
26
|
-
* @param appName - App name of the calling app
|
|
27
|
-
* @param callerActionIdentifier - action identifier
|
|
28
|
-
* @param resourceAttributeOperations - Array of operations to do on resource attributes.
|
|
29
|
-
* @return {Promise<ResourceAttributesOperation[]>} Array of sent operations
|
|
30
|
-
* */
|
|
31
|
-
static updateResourceAttributesAsync(accountId: number, appName: string, callerActionIdentifier: string, resourceAttributeOperations: ResourceAttributesOperation[]): Promise<ResourceAttributesOperation[]>;
|
|
32
|
-
private static sendSingleSnsMessage;
|
|
33
|
-
private static getSnsTopicArn;
|
|
34
|
-
private static getResourceAttributesUrl;
|
|
35
|
-
/**
|
|
36
|
-
* Checks we can contact the required SNS topic that used to send attribute updates to Authorization MS.
|
|
37
|
-
* This function can be used as health check for services that updating resource attributes in async is crucial.
|
|
38
|
-
* Note this function only verify the POD can contact AWS SDK and the topic exists, but the user still might get
|
|
39
|
-
* errors when pushing for the SNS (e.g: in case the AWS role of the POD don't have permissions to push messages).
|
|
40
|
-
* However, this is the best we can do without actually push dummy messages to the SNS.
|
|
41
|
-
* @return {Promise<boolean>} - true if succeeded
|
|
42
|
-
*/
|
|
43
|
-
static asyncResourceAttributesHealthCheck(): Promise<boolean>;
|
|
44
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import chunk from 'lodash/chunk';
|
|
2
|
-
import { fetch } from '@mondaydotcomorg/monday-fetch';
|
|
3
|
-
import { AuthorizationInternalService, logger } from './authorization-internal-service.mjs';
|
|
4
|
-
import { getAttributionsFromApi } from './attributions-service.mjs';
|
|
5
|
-
import { Api } from '@mondaydotcomorg/trident-backend-api';
|
|
6
|
-
import { sendToSns, getTopicAttributes } from '@mondaydotcomorg/monday-sns';
|
|
7
|
-
import { ASYNC_RESOURCE_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE, RESOURCE_ATTRIBUTES_SNS_ARN_SECRET_NAME, RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND } from './constants/sns.mjs';
|
|
8
|
-
|
|
9
|
-
class AuthorizationAttributesService {
|
|
10
|
-
static LOG_TAG = "authorization_attributes";
|
|
11
|
-
/**
|
|
12
|
-
* Upsert resource attributes synchronously, performing http call to the authorization MS to assign the given attributes to the given resource.
|
|
13
|
-
* @param accountId
|
|
14
|
-
* @param userId
|
|
15
|
-
* @param resourceAttributeAssignments - Array of resource (resourceType, resourceId) and attribute (key, value) pairs to upsert in the authorization MS.
|
|
16
|
-
* e.g. [{ resourceType: 'board', resourceId: 123, key: 'board_kind', value: 'private' }]
|
|
17
|
-
* @returns ResourceAttributeResponse - The affected (created and updated_ resource attributes assignments in the `attributes` field.
|
|
18
|
-
*/
|
|
19
|
-
static async upsertResourceAttributes(accountId, userId, resourceAttributeAssignments) {
|
|
20
|
-
const internalAuthToken = AuthorizationInternalService.generateInternalAuthToken(accountId, userId);
|
|
21
|
-
const attributionHeaders = getAttributionsFromApi();
|
|
22
|
-
const response = await fetch(this.getResourceAttributesUrl(accountId), {
|
|
23
|
-
method: 'POST',
|
|
24
|
-
headers: {
|
|
25
|
-
Authorization: internalAuthToken,
|
|
26
|
-
'Content-Type': 'application/json',
|
|
27
|
-
...attributionHeaders,
|
|
28
|
-
},
|
|
29
|
-
timeout: AuthorizationInternalService.getRequestTimeout(),
|
|
30
|
-
body: JSON.stringify({ resourceAttributeAssignments }),
|
|
31
|
-
}, AuthorizationInternalService.getRequestFetchOptions());
|
|
32
|
-
const responseBody = await response.json();
|
|
33
|
-
AuthorizationInternalService.throwOnHttpErrorIfNeeded(response, 'upsertResourceAttributesSync');
|
|
34
|
-
return { attributes: responseBody['attributes'] };
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Delete resource attributes assignments synchronously, performing http call to the authorization MS to delete the given attributes from the given singular resource.
|
|
38
|
-
* @param accountId
|
|
39
|
-
* @param userId
|
|
40
|
-
* @param resource - The resource (resourceType, resourceId) to delete the attributes for.
|
|
41
|
-
* @param attributeKeys - Array of attribute keys to delete for the resource.
|
|
42
|
-
* @returns ResourceAttributeResponse - The affected (deleted) resource attributes assignments in the `attributes` field.
|
|
43
|
-
*/
|
|
44
|
-
static async deleteResourceAttributes(accountId, userId, resource, attributeKeys) {
|
|
45
|
-
const internalAuthToken = AuthorizationInternalService.generateInternalAuthToken(accountId, userId);
|
|
46
|
-
const url = `${this.getResourceAttributesUrl(accountId)}/${resource.type}/${resource.id}`;
|
|
47
|
-
const attributionHeaders = getAttributionsFromApi();
|
|
48
|
-
const response = await fetch(url, {
|
|
49
|
-
method: 'DELETE',
|
|
50
|
-
headers: {
|
|
51
|
-
Authorization: internalAuthToken,
|
|
52
|
-
'Content-Type': 'application/json',
|
|
53
|
-
...attributionHeaders,
|
|
54
|
-
},
|
|
55
|
-
timeout: AuthorizationInternalService.getRequestTimeout(),
|
|
56
|
-
body: JSON.stringify({ keys: attributeKeys }),
|
|
57
|
-
}, AuthorizationInternalService.getRequestFetchOptions());
|
|
58
|
-
const responseBody = await response.json();
|
|
59
|
-
AuthorizationInternalService.throwOnHttpErrorIfNeeded(response, 'deleteResourceAttributesSync');
|
|
60
|
-
return { attributes: responseBody['attributes'] };
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Async function, this function only send the updates request to SNS and return before the change actually took place
|
|
64
|
-
* @param accountId
|
|
65
|
-
* @param appName - App name of the calling app
|
|
66
|
-
* @param callerActionIdentifier - action identifier
|
|
67
|
-
* @param resourceAttributeOperations - Array of operations to do on resource attributes.
|
|
68
|
-
* @return {Promise<ResourceAttributesOperation[]>} Array of sent operations
|
|
69
|
-
* */
|
|
70
|
-
static async updateResourceAttributesAsync(accountId, appName, callerActionIdentifier, resourceAttributeOperations) {
|
|
71
|
-
const topicArn = this.getSnsTopicArn();
|
|
72
|
-
const sendToSnsPromises = [];
|
|
73
|
-
const operationChucks = chunk(resourceAttributeOperations, ASYNC_RESOURCE_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE);
|
|
74
|
-
for (const operationsChunk of operationChucks) {
|
|
75
|
-
sendToSnsPromises.push(this.sendSingleSnsMessage(topicArn, accountId, appName, callerActionIdentifier, operationsChunk));
|
|
76
|
-
}
|
|
77
|
-
return (await Promise.all(sendToSnsPromises)).flat();
|
|
78
|
-
}
|
|
79
|
-
static async sendSingleSnsMessage(topicArn, accountId, appName, callerActionIdentifier, operations) {
|
|
80
|
-
const payload = {
|
|
81
|
-
kind: RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND,
|
|
82
|
-
payload: {
|
|
83
|
-
accountId: accountId,
|
|
84
|
-
callerAppName: appName,
|
|
85
|
-
callerActionIdentifier: callerActionIdentifier,
|
|
86
|
-
operations: operations,
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
try {
|
|
90
|
-
await sendToSns(payload, topicArn);
|
|
91
|
-
return operations;
|
|
92
|
-
}
|
|
93
|
-
catch (error) {
|
|
94
|
-
logger.error({ error, tag: this.LOG_TAG }, "Authorization resource attributes async update: failed to send operations to SNS");
|
|
95
|
-
return [];
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
static getSnsTopicArn() {
|
|
99
|
-
const arnFromApi = Api.getPart('configurationVariables')?.get(RESOURCE_ATTRIBUTES_SNS_ARN_SECRET_NAME).arn;
|
|
100
|
-
if (arnFromApi) {
|
|
101
|
-
return arnFromApi;
|
|
102
|
-
}
|
|
103
|
-
const jsonArnFromEnv = process.env[RESOURCE_ATTRIBUTES_SNS_ARN_SECRET_NAME];
|
|
104
|
-
const arnFromEnv = JSON.parse(jsonArnFromEnv).arn;
|
|
105
|
-
if (arnFromEnv) {
|
|
106
|
-
return arnFromEnv;
|
|
107
|
-
}
|
|
108
|
-
throw new Error('Unable to get sns topic arn from env variable');
|
|
109
|
-
}
|
|
110
|
-
static getResourceAttributesUrl(accountId) {
|
|
111
|
-
return `${process.env.AUTHORIZATION_URL}/attributes/${accountId}/resource`;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Checks we can contact the required SNS topic that used to send attribute updates to Authorization MS.
|
|
115
|
-
* This function can be used as health check for services that updating resource attributes in async is crucial.
|
|
116
|
-
* Note this function only verify the POD can contact AWS SDK and the topic exists, but the user still might get
|
|
117
|
-
* errors when pushing for the SNS (e.g: in case the AWS role of the POD don't have permissions to push messages).
|
|
118
|
-
* However, this is the best we can do without actually push dummy messages to the SNS.
|
|
119
|
-
* @return {Promise<boolean>} - true if succeeded
|
|
120
|
-
*/
|
|
121
|
-
static async asyncResourceAttributesHealthCheck() {
|
|
122
|
-
try {
|
|
123
|
-
const requestedTopicArn = this.getSnsTopicArn();
|
|
124
|
-
const attributes = await getTopicAttributes(requestedTopicArn);
|
|
125
|
-
const isHealthy = !(!attributes || !("TopicArn" in attributes) || attributes.TopicArn !== requestedTopicArn);
|
|
126
|
-
if (!isHealthy) {
|
|
127
|
-
logger.error({ requestedTopicArn, snsReturnedAttributes: attributes, tag: this.LOG_TAG }, "authorization-attributes-service failed in health check");
|
|
128
|
-
}
|
|
129
|
-
return isHealthy;
|
|
130
|
-
}
|
|
131
|
-
catch (error) {
|
|
132
|
-
logger.error({ error, tag: this.LOG_TAG }, "authorization-attributes-service got error during health check");
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export { AuthorizationAttributesService };
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Request } from 'express';
|
|
2
|
-
import { fetch, MondayFetchOptions } from '@mondaydotcomorg/monday-fetch';
|
|
3
|
-
export declare const logger: import("bunyan");
|
|
4
|
-
export declare class AuthorizationInternalService {
|
|
5
|
-
static skipAuthorization(requset: Request): void;
|
|
6
|
-
static markAuthorized(request: Request): void;
|
|
7
|
-
static failIfNotCoveredByAuthorization(request: Request): void;
|
|
8
|
-
static throwOnHttpErrorIfNeeded(response: Awaited<ReturnType<typeof fetch>>, placement: string): void;
|
|
9
|
-
static generateInternalAuthToken(accountId: number, userId: number): string;
|
|
10
|
-
static setRequestFetchOptions(customMondayFetchOptions: MondayFetchOptions): void;
|
|
11
|
-
static getRequestFetchOptions(): MondayFetchOptions;
|
|
12
|
-
static getRequestTimeout(): 60000 | 2000;
|
|
13
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { signAuthorizationHeader } from '@mondaydotcomorg/monday-jwt';
|
|
2
|
-
import * as MondayLogger from '@mondaydotcomorg/monday-logger';
|
|
3
|
-
|
|
4
|
-
const INTERNAL_APP_NAME = 'internal_ms';
|
|
5
|
-
const defaultMondayFetchOptions = {
|
|
6
|
-
retries: 3,
|
|
7
|
-
callback: logOnFetchFail,
|
|
8
|
-
};
|
|
9
|
-
function logOnFetchFail(retriesLeft, error) {
|
|
10
|
-
if (retriesLeft == 0) {
|
|
11
|
-
logger.error({ retriesLeft, error }, 'Authorization attempt failed due to network issues');
|
|
12
|
-
}
|
|
13
|
-
else {
|
|
14
|
-
logger.info({ retriesLeft, error }, 'Authorization attempt failed due to network issues, trying again');
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
let mondayFetchOptions = defaultMondayFetchOptions;
|
|
18
|
-
const logger = MondayLogger.getLogger();
|
|
19
|
-
class AuthorizationInternalService {
|
|
20
|
-
static skipAuthorization(requset) {
|
|
21
|
-
requset.authorizationSkipPerformed = true;
|
|
22
|
-
}
|
|
23
|
-
static markAuthorized(request) {
|
|
24
|
-
request.authorizationCheckPerformed = true;
|
|
25
|
-
}
|
|
26
|
-
static failIfNotCoveredByAuthorization(request) {
|
|
27
|
-
if (!request.authorizationCheckPerformed && !request.authorizationSkipPerformed) {
|
|
28
|
-
throw 'Endpoint is not covered by authorization check';
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
static throwOnHttpErrorIfNeeded(response, placement) {
|
|
32
|
-
if (response.ok) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
const status = response.status;
|
|
36
|
-
logger.error({ tag: 'authorization-service', placement, status }, 'AuthorizationService: authorization request failed');
|
|
37
|
-
throw new Error(`AuthorizationService: [${placement}] authorization request failed with status ${status}`);
|
|
38
|
-
}
|
|
39
|
-
static generateInternalAuthToken(accountId, userId) {
|
|
40
|
-
return signAuthorizationHeader({ appName: INTERNAL_APP_NAME, accountId, userId });
|
|
41
|
-
}
|
|
42
|
-
static setRequestFetchOptions(customMondayFetchOptions) {
|
|
43
|
-
mondayFetchOptions = {
|
|
44
|
-
...defaultMondayFetchOptions,
|
|
45
|
-
...customMondayFetchOptions,
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
static getRequestFetchOptions() {
|
|
49
|
-
return mondayFetchOptions;
|
|
50
|
-
}
|
|
51
|
-
static getRequestTimeout() {
|
|
52
|
-
const isDevEnv = process.env.NODE_ENV === 'development';
|
|
53
|
-
return isDevEnv ? 60000 : 2000;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export { AuthorizationInternalService, logger };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { NextFunction } from 'express';
|
|
2
|
-
import { Action, BaseRequest, BaseResponse, Context, ContextGetter, ResourceGetter } from './types/general';
|
|
3
|
-
export declare function getAuthorizationMiddleware(action: Action, resourceGetter: ResourceGetter, contextGetter?: ContextGetter): (request: BaseRequest, response: BaseResponse, next: NextFunction) => Promise<void>;
|
|
4
|
-
export declare function skipAuthorizationMiddleware(request: BaseRequest, response: BaseResponse, next: NextFunction): void;
|
|
5
|
-
export declare function authorizationCheckMiddleware(request: BaseRequest, response: BaseResponse, next: NextFunction): void;
|
|
6
|
-
export declare function defaultContextGetter(request: BaseRequest): Context;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import onHeaders from 'on-headers';
|
|
2
|
-
import { AuthorizationInternalService } from './authorization-internal-service.mjs';
|
|
3
|
-
import { AuthorizationService } from './authorization-service.mjs';
|
|
4
|
-
|
|
5
|
-
// getAuthorizationMiddleware is duplicated in testKit/index.ts
|
|
6
|
-
// If you are making changes to this function, please make sure to update the other file as well
|
|
7
|
-
function getAuthorizationMiddleware(action, resourceGetter, contextGetter) {
|
|
8
|
-
return async function authorizationMiddleware(request, response, next) {
|
|
9
|
-
contextGetter ||= defaultContextGetter;
|
|
10
|
-
const { userId, accountId } = contextGetter(request);
|
|
11
|
-
const resources = resourceGetter(request);
|
|
12
|
-
const { isAuthorized } = await AuthorizationService.isAuthorized(accountId, userId, resources, action);
|
|
13
|
-
AuthorizationInternalService.markAuthorized(request);
|
|
14
|
-
if (!isAuthorized) {
|
|
15
|
-
response.status(403).json({ message: 'Access denied' });
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
next();
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
function skipAuthorizationMiddleware(request, response, next) {
|
|
22
|
-
AuthorizationInternalService.skipAuthorization(request);
|
|
23
|
-
next();
|
|
24
|
-
}
|
|
25
|
-
function authorizationCheckMiddleware(request, response, next) {
|
|
26
|
-
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
27
|
-
onHeaders(response, function () {
|
|
28
|
-
if (response.statusCode < 400) {
|
|
29
|
-
AuthorizationInternalService.failIfNotCoveredByAuthorization(request);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
next();
|
|
34
|
-
}
|
|
35
|
-
function defaultContextGetter(request) {
|
|
36
|
-
return request.payload;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export { authorizationCheckMiddleware, defaultContextGetter, getAuthorizationMiddleware, skipAuthorizationMiddleware };
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { MondayFetchOptions } from '@mondaydotcomorg/monday-fetch';
|
|
2
|
-
import { Action, AuthorizationObject, Resource } from './types/general';
|
|
3
|
-
import { ScopedAction, ScopedActionPermit, ScopedActionResponseObject, ScopeOptions } from './types/scoped-actions-contracts';
|
|
4
|
-
export interface AuthorizeResponse {
|
|
5
|
-
isAuthorized: boolean;
|
|
6
|
-
unauthorizedIds?: number[];
|
|
7
|
-
unauthorizedObjects?: AuthorizationObject[];
|
|
8
|
-
}
|
|
9
|
-
export declare function setRequestFetchOptions(customMondayFetchOptions: MondayFetchOptions): void;
|
|
10
|
-
export declare function setRedisClient(client: any, grantedFeatureRedisExpirationInSeconds?: number): void;
|
|
11
|
-
export declare class AuthorizationService {
|
|
12
|
-
static redisClient?: any;
|
|
13
|
-
static grantedFeatureRedisExpirationInSeconds?: number;
|
|
14
|
-
/**
|
|
15
|
-
* @deprecated use the second form with authorizationRequestObjects instead,
|
|
16
|
-
* support of this function will be dropped gradually
|
|
17
|
-
*/
|
|
18
|
-
static isAuthorized(accountId: number, userId: number, resources: Resource[], action: Action): Promise<AuthorizeResponse>;
|
|
19
|
-
static isAuthorized(accountId: number, userId: number, authorizationRequestObjects: AuthorizationObject[]): Promise<AuthorizeResponse>;
|
|
20
|
-
static isUserGrantedWithFeature(accountId: number, userId: number, featureName: string, options?: {
|
|
21
|
-
shouldSkipCache?: boolean;
|
|
22
|
-
}): Promise<boolean>;
|
|
23
|
-
private static fetchIsUserGrantedWithFeature;
|
|
24
|
-
private static getCachedKeyName;
|
|
25
|
-
static canActionInScope(accountId: number, userId: number, action: string, scope: ScopeOptions): Promise<ScopedActionPermit>;
|
|
26
|
-
static canActionInScopeMultiple(accountId: number, userId: number, scopedActions: ScopedAction[]): Promise<ScopedActionResponseObject[]>;
|
|
27
|
-
private static isAuthorizedSingular;
|
|
28
|
-
private static isAuthorizedMultiple;
|
|
29
|
-
}
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import { mapKeys, snakeCase, camelCase } from 'lodash';
|
|
2
|
-
import { performance } from 'perf_hooks';
|
|
3
|
-
import { fetch } from '@mondaydotcomorg/monday-fetch';
|
|
4
|
-
import { sendAuthorizationChecksPerRequestMetric, sendAuthorizationCheckResponseTimeMetric } from './prometheus-service.mjs';
|
|
5
|
-
import { AuthorizationInternalService, logger } from './authorization-internal-service.mjs';
|
|
6
|
-
import { getAttributionsFromApi } from './attributions-service.mjs';
|
|
7
|
-
|
|
8
|
-
const GRANTED_FEATURE_CACHE_EXPIRATION_SECONDS = 5 * 60;
|
|
9
|
-
function setRequestFetchOptions(customMondayFetchOptions) {
|
|
10
|
-
AuthorizationInternalService.setRequestFetchOptions(customMondayFetchOptions);
|
|
11
|
-
}
|
|
12
|
-
function setRedisClient(client, grantedFeatureRedisExpirationInSeconds = GRANTED_FEATURE_CACHE_EXPIRATION_SECONDS) {
|
|
13
|
-
AuthorizationService.redisClient = client;
|
|
14
|
-
if (grantedFeatureRedisExpirationInSeconds && grantedFeatureRedisExpirationInSeconds > 0) {
|
|
15
|
-
AuthorizationService.grantedFeatureRedisExpirationInSeconds = grantedFeatureRedisExpirationInSeconds;
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
logger.warn({ grantedFeatureRedisExpirationInSeconds }, 'Invalid input for grantedFeatureRedisExpirationInSeconds, must be positive number. using default ttl.');
|
|
19
|
-
AuthorizationService.grantedFeatureRedisExpirationInSeconds = GRANTED_FEATURE_CACHE_EXPIRATION_SECONDS;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
class AuthorizationService {
|
|
23
|
-
static redisClient;
|
|
24
|
-
static grantedFeatureRedisExpirationInSeconds;
|
|
25
|
-
static async isAuthorized(...args) {
|
|
26
|
-
if (args.length === 3) {
|
|
27
|
-
return this.isAuthorizedMultiple(args[0], args[1], args[2]);
|
|
28
|
-
}
|
|
29
|
-
else if (args.length == 4) {
|
|
30
|
-
return this.isAuthorizedSingular(args[0], args[1], args[2], args[3]);
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
throw new Error('isAuthorized accepts either 3 or 4 arguments');
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
static async isUserGrantedWithFeature(accountId, userId, featureName, options = {}) {
|
|
37
|
-
let cachedKey = this.getCachedKeyName(userId, featureName);
|
|
38
|
-
const shouldSkipCache = options.shouldSkipCache ?? false;
|
|
39
|
-
if (this.redisClient && !shouldSkipCache) {
|
|
40
|
-
let grantedFeatureValue = await this.redisClient.get(cachedKey);
|
|
41
|
-
if (!(grantedFeatureValue === undefined || grantedFeatureValue === null)) {
|
|
42
|
-
// redis returns the value as string
|
|
43
|
-
return grantedFeatureValue === 'true';
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
let grantedFeatureValue = await this.fetchIsUserGrantedWithFeature(featureName, accountId, userId);
|
|
47
|
-
if (this.redisClient) {
|
|
48
|
-
await this.redisClient.set(cachedKey, grantedFeatureValue, 'EX', this.grantedFeatureRedisExpirationInSeconds);
|
|
49
|
-
}
|
|
50
|
-
return grantedFeatureValue;
|
|
51
|
-
}
|
|
52
|
-
static async fetchIsUserGrantedWithFeature(featureName, accountId, userId) {
|
|
53
|
-
let authorizationObject = {
|
|
54
|
-
action: featureName,
|
|
55
|
-
resource_type: 'feature',
|
|
56
|
-
};
|
|
57
|
-
let authorizeResponsePromise = await this.isAuthorized(accountId, userId, [authorizationObject]);
|
|
58
|
-
return authorizeResponsePromise.isAuthorized;
|
|
59
|
-
}
|
|
60
|
-
static getCachedKeyName(userId, featureName) {
|
|
61
|
-
return `granted-feature-${featureName}-${userId}`;
|
|
62
|
-
}
|
|
63
|
-
static async canActionInScope(accountId, userId, action, scope) {
|
|
64
|
-
const scopedActions = [{ action, scope }];
|
|
65
|
-
const scopedActionResponseObjects = await this.canActionInScopeMultiple(accountId, userId, scopedActions);
|
|
66
|
-
return scopedActionResponseObjects[0].permit;
|
|
67
|
-
}
|
|
68
|
-
static async canActionInScopeMultiple(accountId, userId, scopedActions) {
|
|
69
|
-
const internalAuthToken = AuthorizationInternalService.generateInternalAuthToken(accountId, userId);
|
|
70
|
-
const scopedActionsPayload = scopedActions.map(scopedAction => {
|
|
71
|
-
return { ...scopedAction, scope: mapKeys(scopedAction.scope, (_, key) => snakeCase(key)) }; // for example: { workspaceId: 1 } => { workspace_id: 1 }
|
|
72
|
-
});
|
|
73
|
-
const attributionHeaders = getAttributionsFromApi();
|
|
74
|
-
const response = await fetch(getCanActionsInScopesUrl(), {
|
|
75
|
-
method: 'POST',
|
|
76
|
-
headers: {
|
|
77
|
-
Authorization: internalAuthToken,
|
|
78
|
-
'Content-Type': 'application/json',
|
|
79
|
-
...attributionHeaders,
|
|
80
|
-
},
|
|
81
|
-
timeout: AuthorizationInternalService.getRequestTimeout(),
|
|
82
|
-
body: JSON.stringify({
|
|
83
|
-
user_id: userId,
|
|
84
|
-
scoped_actions: scopedActionsPayload,
|
|
85
|
-
}),
|
|
86
|
-
}, AuthorizationInternalService.getRequestFetchOptions());
|
|
87
|
-
AuthorizationInternalService.throwOnHttpErrorIfNeeded(response, 'canActionInScopeMultiple');
|
|
88
|
-
const responseBody = await response.json();
|
|
89
|
-
const camelCaseKeys = obj => Object.fromEntries(Object.entries(obj).map(([key, value]) => [camelCase(key), value]));
|
|
90
|
-
const scopedActionsResponseObjects = responseBody.result.map(responseObject => {
|
|
91
|
-
const { scopedAction, permit } = responseObject;
|
|
92
|
-
const { scope } = scopedAction;
|
|
93
|
-
const transformKeys = obj => camelCaseKeys(obj);
|
|
94
|
-
return {
|
|
95
|
-
...responseObject,
|
|
96
|
-
scopedAction: { ...scopedAction, scope: transformKeys(scope) },
|
|
97
|
-
permit: transformKeys(permit),
|
|
98
|
-
};
|
|
99
|
-
});
|
|
100
|
-
return scopedActionsResponseObjects;
|
|
101
|
-
}
|
|
102
|
-
static async isAuthorizedSingular(accountId, userId, resources, action) {
|
|
103
|
-
const { authorizationObjects } = createAuthorizationParams(resources, action);
|
|
104
|
-
return this.isAuthorizedMultiple(accountId, userId, authorizationObjects);
|
|
105
|
-
}
|
|
106
|
-
static async isAuthorizedMultiple(accountId, userId, authorizationRequestObjects) {
|
|
107
|
-
const internalAuthToken = AuthorizationInternalService.generateInternalAuthToken(accountId, userId);
|
|
108
|
-
const startTime = performance.now();
|
|
109
|
-
const attributionHeaders = getAttributionsFromApi();
|
|
110
|
-
const response = await fetch(getAuthorizeUrl(), {
|
|
111
|
-
method: 'POST',
|
|
112
|
-
headers: {
|
|
113
|
-
Authorization: internalAuthToken,
|
|
114
|
-
'Content-Type': 'application/json',
|
|
115
|
-
...attributionHeaders,
|
|
116
|
-
},
|
|
117
|
-
timeout: AuthorizationInternalService.getRequestTimeout(),
|
|
118
|
-
body: JSON.stringify({
|
|
119
|
-
user_id: userId,
|
|
120
|
-
authorize_request_objects: authorizationRequestObjects,
|
|
121
|
-
}),
|
|
122
|
-
}, AuthorizationInternalService.getRequestFetchOptions());
|
|
123
|
-
const endTime = performance.now();
|
|
124
|
-
const time = endTime - startTime;
|
|
125
|
-
const responseStatus = response.status;
|
|
126
|
-
sendAuthorizationChecksPerRequestMetric(responseStatus, authorizationRequestObjects.length);
|
|
127
|
-
AuthorizationInternalService.throwOnHttpErrorIfNeeded(response, 'isAuthorizedMultiple');
|
|
128
|
-
const responseBody = await response.json();
|
|
129
|
-
const unauthorizedObjects = [];
|
|
130
|
-
responseBody.result.forEach(function (isAuthorized, index) {
|
|
131
|
-
const authorizationObject = authorizationRequestObjects[index];
|
|
132
|
-
if (!isAuthorized) {
|
|
133
|
-
unauthorizedObjects.push(authorizationObject);
|
|
134
|
-
}
|
|
135
|
-
sendAuthorizationCheckResponseTimeMetric(authorizationObject.resource_type, authorizationObject.action, isAuthorized, responseStatus, time);
|
|
136
|
-
});
|
|
137
|
-
if (unauthorizedObjects.length > 0) {
|
|
138
|
-
logger.info({
|
|
139
|
-
resources: JSON.stringify(unauthorizedObjects),
|
|
140
|
-
}, 'AuthorizationService: resource is unauthorized');
|
|
141
|
-
const unauthorizedIds = unauthorizedObjects
|
|
142
|
-
.filter(obj => !!obj.resource_id)
|
|
143
|
-
.map(obj => obj.resource_id);
|
|
144
|
-
return { isAuthorized: false, unauthorizedIds, unauthorizedObjects };
|
|
145
|
-
}
|
|
146
|
-
return { isAuthorized: true };
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
function createAuthorizationParams(resources, action) {
|
|
150
|
-
const params = {
|
|
151
|
-
authorizationObjects: resources.map((resource) => {
|
|
152
|
-
const authorizationObject = {
|
|
153
|
-
resource_id: resource.id,
|
|
154
|
-
resource_type: resource.type,
|
|
155
|
-
action,
|
|
156
|
-
};
|
|
157
|
-
if (resource.wrapperData) {
|
|
158
|
-
authorizationObject.wrapper_data = resource.wrapperData;
|
|
159
|
-
}
|
|
160
|
-
return authorizationObject;
|
|
161
|
-
}),
|
|
162
|
-
};
|
|
163
|
-
return params;
|
|
164
|
-
}
|
|
165
|
-
function getAuthorizeUrl() {
|
|
166
|
-
return `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/authorize`;
|
|
167
|
-
}
|
|
168
|
-
function getCanActionsInScopesUrl() {
|
|
169
|
-
return `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/can_actions_in_scopes`;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export { AuthorizationService, setRedisClient, setRequestFetchOptions };
|
|
@@ -1,3 +0,0 @@
|
|
|
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;
|
|
@@ -1,5 +0,0 @@
|
|
|
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 };
|
package/dist/esm/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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/esm/index.mjs
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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 };
|
|
@@ -1,45 +0,0 @@
|
|
|
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
|
-
}
|
|
36
|
-
function sendAuthorizationCheckResponseTimeMetric(resourceType, action, isAuthorized, responseStatus, time) {
|
|
37
|
-
try {
|
|
38
|
-
if (authorizationCheckResponseTimeMetric) {
|
|
39
|
-
authorizationCheckResponseTimeMetric.labels(resourceType, action, isAuthorized, responseStatus).observe(time);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
catch (e) { }
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export { METRICS, getMetricsManager, sendAuthorizationCheckResponseTimeMetric, sendAuthorizationChecksPerRequestMetric, setPrometheus };
|
|
@@ -1,11 +0,0 @@
|
|
|
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>;
|
|
@@ -1,44 +0,0 @@
|
|
|
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(resource => {
|
|
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 };
|