@clairejs/server 3.25.1 → 3.25.2

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
@@ -1,7 +1,8 @@
1
1
  ## Change Log
2
2
 
3
- #### 3.25.1
3
+ #### 3.25.2
4
4
 
5
+ - add AbstractRbacAuthorizer
5
6
  - add other security access condition classes
6
7
 
7
8
  #### 3.24.0
@@ -0,0 +1,22 @@
1
+ import { HttpRequest } from "../common/HttpRequest";
2
+ import { EndpointMetadata } from "../../common/request/endpoint-metadata";
3
+ import { AbstractRequestAuthorizer } from "./AbstractHttpAuthorizer";
4
+ interface UserPrincipal {
5
+ principalId: string;
6
+ tfa?: boolean;
7
+ }
8
+ interface PermissionDetailResponse {
9
+ isSuperUser?: boolean;
10
+ permissions?: {
11
+ conditions?: {
12
+ conditionName: string;
13
+ conditionValue: string;
14
+ }[];
15
+ }[];
16
+ }
17
+ export declare abstract class AbstractRbacAuthorizer extends AbstractRequestAuthorizer {
18
+ abstract getPermissionDetail(principal: string, endpoint: string): Promise<PermissionDetailResponse>;
19
+ abstract getUserFromRequest(req: HttpRequest): Promise<UserPrincipal>;
20
+ authorize(req: HttpRequest, endpoint: EndpointMetadata): Promise<void>;
21
+ }
22
+ export {};
@@ -0,0 +1,59 @@
1
+ import { Errors } from "@clairejs/core";
2
+ import { AbstractRequestAuthorizer } from "./AbstractHttpAuthorizer";
3
+ export class AbstractRbacAuthorizer extends AbstractRequestAuthorizer {
4
+ async authorize(req, endpoint) {
5
+ if (endpoint.publicAccess) {
6
+ return;
7
+ }
8
+ const user = await this.getUserFromRequest(req);
9
+ if (endpoint.tfaRequired && !user.tfa) {
10
+ throw Errors.TFA_REQUIRED();
11
+ }
12
+ const detail = await this.getPermissionDetail(user.principalId, endpoint.id);
13
+ if (detail.isSuperUser) {
14
+ return;
15
+ }
16
+ if (!detail.permissions?.length) {
17
+ throw Errors.ACCESS_DENIED();
18
+ }
19
+ let passed = false;
20
+ let failedCondition = "";
21
+ for (const permission of detail.permissions) {
22
+ if (!permission.conditions?.length) {
23
+ passed = true;
24
+ }
25
+ else {
26
+ let allConditionCheckPassed = true;
27
+ for (const condition of permission.conditions) {
28
+ const matchedCondition = endpoint.accessConditionInstances?.find((c) => c.getConditionMetadata().name === condition.conditionName);
29
+ if (!matchedCondition) {
30
+ //-- condition not found, skip
31
+ continue;
32
+ }
33
+ try {
34
+ const requestedConditionValue = await matchedCondition.resolveConditionValue(req);
35
+ const permittedConditionValue = JSON.parse(condition.conditionValue);
36
+ allConditionCheckPassed = await matchedCondition.validate(requestedConditionValue, permittedConditionValue);
37
+ }
38
+ catch (err) {
39
+ //-- this condition does not passed, skip
40
+ allConditionCheckPassed = false;
41
+ }
42
+ if (!allConditionCheckPassed) {
43
+ //-- at least one condition not satisfied, stop condition checking
44
+ failedCondition = condition.conditionName;
45
+ break;
46
+ }
47
+ }
48
+ passed = allConditionCheckPassed;
49
+ }
50
+ if (passed) {
51
+ //-- at least one permission and conditions suit checked passed
52
+ break;
53
+ }
54
+ }
55
+ if (!passed) {
56
+ throw Errors.ACCESS_DENIED(`Condition check failed: ${failedCondition}`);
57
+ }
58
+ }
59
+ }
package/dist/index.d.ts CHANGED
@@ -23,6 +23,7 @@ export * from "./http/security/abstract-access-condition";
23
23
  export * from "./http/security/access-condition-metadata";
24
24
  export * from "./http/security/access-condition-value-type";
25
25
  export * from "./http/auth/AbstractHttpAuthorizer";
26
+ export * from "./http/auth/AbstractRbacAuthorizer";
26
27
  export * from "./http/repository/ModelRepository";
27
28
  export * from "./http/repository/DtoRepository";
28
29
  export * from "./http/repository/ICrudRepository";
package/dist/index.js CHANGED
@@ -25,6 +25,7 @@ export * from "./http/security/abstract-access-condition";
25
25
  export * from "./http/security/access-condition-metadata";
26
26
  export * from "./http/security/access-condition-value-type";
27
27
  export * from "./http/auth/AbstractHttpAuthorizer";
28
+ export * from "./http/auth/AbstractRbacAuthorizer";
28
29
  export * from "./http/repository/ModelRepository";
29
30
  export * from "./http/repository/DtoRepository";
30
31
  export * from "./http/repository/ICrudRepository";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clairejs/server",
3
- "version": "3.25.1",
3
+ "version": "3.25.2",
4
4
  "description": "Claire server NodeJs framework written in Typescript.",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",