@moostjs/arbac 0.5.28 → 0.5.30
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.cjs +49 -19
- package/dist/index.d.ts +94 -1
- package/dist/index.mjs +41 -10
- package/package.json +7 -5
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
//#region rolldown:runtime
|
|
3
2
|
var __create = Object.create;
|
|
4
3
|
var __defProp = Object.defineProperty;
|
|
@@ -51,7 +50,7 @@ var ArbacUserProvider = class {
|
|
|
51
50
|
* @returns {string | Promise<string>} The user ID, or a rejected promise if not implemented.
|
|
52
51
|
* @throws {Error} If the method is not overridden by a subclass.
|
|
53
52
|
*/ getUserId() {
|
|
54
|
-
return Promise.reject(new Error("ArbacUserProvider class must be extended"));
|
|
53
|
+
return Promise.reject(/* @__PURE__ */ new Error("ArbacUserProvider class must be extended"));
|
|
55
54
|
}
|
|
56
55
|
/**
|
|
57
56
|
* Retrieves the roles assigned to a user based on their ID.
|
|
@@ -60,7 +59,7 @@ var ArbacUserProvider = class {
|
|
|
60
59
|
* @returns {string[] | Promise<string[]>} An array of role identifiers, or a rejected promise if not implemented.
|
|
61
60
|
* @throws {Error} If the method is not overridden by a subclass.
|
|
62
61
|
*/ getRoles(id) {
|
|
63
|
-
return Promise.reject(new Error("ArbacUserProvider class must be extended"));
|
|
62
|
+
return Promise.reject(/* @__PURE__ */ new Error("ArbacUserProvider class must be extended"));
|
|
64
63
|
}
|
|
65
64
|
/**
|
|
66
65
|
* Retrieves the attributes associated with a user based on their ID.
|
|
@@ -69,14 +68,18 @@ var ArbacUserProvider = class {
|
|
|
69
68
|
* @returns {TUserAttrs | Promise<TUserAttrs>} The user attributes, or a rejected promise if not implemented.
|
|
70
69
|
* @throws {Error} If the method is not overridden by a subclass.
|
|
71
70
|
*/ getAttrs(id) {
|
|
72
|
-
return Promise.reject(new Error("ArbacUserProvider class must be extended"));
|
|
71
|
+
return Promise.reject(/* @__PURE__ */ new Error("ArbacUserProvider class must be extended"));
|
|
73
72
|
}
|
|
74
73
|
};
|
|
75
74
|
ArbacUserProvider = _ts_decorate([(0, moost.Injectable)()], ArbacUserProvider);
|
|
76
75
|
|
|
77
76
|
//#endregion
|
|
78
77
|
//#region packages/arbac/src/arbac.composables.ts
|
|
79
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Composable for ARBAC (Advanced Role-Based Access Control) utilities within MoostJS.
|
|
80
|
+
*
|
|
81
|
+
* @template TScope - Type representing the scope of access control.
|
|
82
|
+
*/ function useArbac() {
|
|
80
83
|
const store = (0, moost.useAsyncEventContext)().store("arbac");
|
|
81
84
|
const cc = (0, moost.useControllerContext)();
|
|
82
85
|
const getScopes = () => store.get("scopes");
|
|
@@ -116,7 +119,12 @@ function getArbacMate() {
|
|
|
116
119
|
|
|
117
120
|
//#endregion
|
|
118
121
|
//#region packages/arbac/src/arbac.decorator.ts
|
|
119
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Interceptor function that enforces authorization checks based on ARBAC rules.
|
|
124
|
+
* It evaluates the user's permissions against the requested resource and action.
|
|
125
|
+
*
|
|
126
|
+
* @constant
|
|
127
|
+
*/ const arbackAuthorizeInterceptor = (0, moost.defineInterceptorFn)(async (before, after, onError) => {
|
|
120
128
|
const logger = (0, moost.useEventLogger)("arbac");
|
|
121
129
|
const { setScopes, evaluate, resource, action, isPublic } = useArbac();
|
|
122
130
|
if (!action || !resource || isPublic) return;
|
|
@@ -134,18 +142,40 @@ const arbackAuthorizeInterceptor = (0, moost.defineInterceptorFn)(async (before,
|
|
|
134
142
|
throw new __wooksjs_event_http.HttpError(401, `Authorization error`);
|
|
135
143
|
}
|
|
136
144
|
}, moost.TInterceptorPriority.GUARD);
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const
|
|
145
|
+
/**
|
|
146
|
+
* Decorator that applies the `arbackAuthorizeInterceptor` to enforce authorization.
|
|
147
|
+
*
|
|
148
|
+
* @returns {MethodDecorator} A method decorator that enforces ARBAC.
|
|
149
|
+
*/ const ArbacAuthorize = () => (0, moost.Intercept)(arbackAuthorizeInterceptor);
|
|
150
|
+
/**
|
|
151
|
+
* Resolves and retrieves the current ARBAC scopes in the request context.
|
|
152
|
+
*
|
|
153
|
+
* @returns {Function} A resolver function that returns user access scopes.
|
|
154
|
+
*/ const ArbacScopes = () => (0, moost.Resolve)(() => useArbac().getScopes());
|
|
155
|
+
/**
|
|
156
|
+
* Decorator to specify a resource for ARBAC evaluation.
|
|
157
|
+
*
|
|
158
|
+
* @param {string} name - The name of the resource.
|
|
159
|
+
* @returns {PropertyDecorator} A property decorator for ARBAC resource identification.
|
|
160
|
+
*/ const ArbacResource = (name) => getArbacMate().decorate("arbacResourceId", name);
|
|
161
|
+
/**
|
|
162
|
+
* Decorator to specify an action for ARBAC evaluation.
|
|
163
|
+
*
|
|
164
|
+
* @param {string} name - The name of the action.
|
|
165
|
+
* @returns {PropertyDecorator} A property decorator for ARBAC action identification.
|
|
166
|
+
*/ const ArbacAction = (name) => getArbacMate().decorate("arbacActionId", name);
|
|
167
|
+
/**
|
|
168
|
+
* Marks a resource or action as publicly accessible, bypassing authorization checks.
|
|
169
|
+
*
|
|
170
|
+
* @returns {PropertyDecorator} A property decorator that marks an entity as public.
|
|
171
|
+
*/ const ArbacPublic = () => getArbacMate().decorate("arbacPublic", true);
|
|
142
172
|
|
|
143
173
|
//#endregion
|
|
144
|
-
exports.ArbacAction = ArbacAction
|
|
145
|
-
exports.ArbacAuthorize = ArbacAuthorize
|
|
146
|
-
exports.ArbacPublic = ArbacPublic
|
|
147
|
-
exports.ArbacResource = ArbacResource
|
|
148
|
-
exports.ArbacScopes = ArbacScopes
|
|
174
|
+
exports.ArbacAction = ArbacAction;
|
|
175
|
+
exports.ArbacAuthorize = ArbacAuthorize;
|
|
176
|
+
exports.ArbacPublic = ArbacPublic;
|
|
177
|
+
exports.ArbacResource = ArbacResource;
|
|
178
|
+
exports.ArbacScopes = ArbacScopes;
|
|
149
179
|
Object.defineProperty(exports, 'ArbacUserProvider', {
|
|
150
180
|
enumerable: true,
|
|
151
181
|
get: function () {
|
|
@@ -158,6 +188,6 @@ Object.defineProperty(exports, 'MoostArbac', {
|
|
|
158
188
|
return MoostArbac;
|
|
159
189
|
}
|
|
160
190
|
});
|
|
161
|
-
exports.arbackAuthorizeInterceptor = arbackAuthorizeInterceptor
|
|
162
|
-
exports.getArbacMate = getArbacMate
|
|
163
|
-
exports.useArbac = useArbac
|
|
191
|
+
exports.arbackAuthorizeInterceptor = arbackAuthorizeInterceptor;
|
|
192
|
+
exports.getArbacMate = getArbacMate;
|
|
193
|
+
exports.useArbac = useArbac;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,25 +3,81 @@ import { Arbac } from '@prostojs/arbac';
|
|
|
3
3
|
import * as moost from 'moost';
|
|
4
4
|
import { Mate, TMoostMetadata, TMateParamMeta } from 'moost';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Composable for ARBAC (Advanced Role-Based Access Control) utilities within MoostJS.
|
|
8
|
+
*
|
|
9
|
+
* @template TScope - Type representing the scope of access control.
|
|
10
|
+
*/
|
|
6
11
|
declare function useArbac<TScope extends object>(): {
|
|
12
|
+
/**
|
|
13
|
+
* Get evaluated scopes
|
|
14
|
+
*/
|
|
7
15
|
getScopes: () => TScope[] | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Set evaluated scopes
|
|
18
|
+
*/
|
|
8
19
|
setScopes: (scope: TScope[] | undefined) => TScope[] | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Evaluate access control for the given resource and action.
|
|
22
|
+
*/
|
|
9
23
|
evaluate: (opts: {
|
|
10
24
|
resource: string;
|
|
11
25
|
action: string;
|
|
12
26
|
}) => Promise<_prostojs_arbac.TArbacEvalResult<TScope> & {
|
|
13
27
|
userId: string;
|
|
14
28
|
}>;
|
|
29
|
+
/**
|
|
30
|
+
* Current resource
|
|
31
|
+
*/
|
|
15
32
|
resource: string;
|
|
33
|
+
/**
|
|
34
|
+
* Current action
|
|
35
|
+
*/
|
|
16
36
|
action: string;
|
|
37
|
+
/**
|
|
38
|
+
* Public flag (if true, access must be granted without evaluation)
|
|
39
|
+
*/
|
|
17
40
|
isPublic: boolean;
|
|
18
41
|
};
|
|
19
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Interceptor function that enforces authorization checks based on ARBAC rules.
|
|
45
|
+
* It evaluates the user's permissions against the requested resource and action.
|
|
46
|
+
*
|
|
47
|
+
* @constant
|
|
48
|
+
*/
|
|
20
49
|
declare const arbackAuthorizeInterceptor: moost.TInterceptorFn;
|
|
50
|
+
/**
|
|
51
|
+
* Decorator that applies the `arbackAuthorizeInterceptor` to enforce authorization.
|
|
52
|
+
*
|
|
53
|
+
* @returns {MethodDecorator} A method decorator that enforces ARBAC.
|
|
54
|
+
*/
|
|
21
55
|
declare const ArbacAuthorize: () => ClassDecorator & MethodDecorator;
|
|
56
|
+
/**
|
|
57
|
+
* Resolves and retrieves the current ARBAC scopes in the request context.
|
|
58
|
+
*
|
|
59
|
+
* @returns {Function} A resolver function that returns user access scopes.
|
|
60
|
+
*/
|
|
22
61
|
declare const ArbacScopes: () => ParameterDecorator & PropertyDecorator;
|
|
62
|
+
/**
|
|
63
|
+
* Decorator to specify a resource for ARBAC evaluation.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} name - The name of the resource.
|
|
66
|
+
* @returns {PropertyDecorator} A property decorator for ARBAC resource identification.
|
|
67
|
+
*/
|
|
23
68
|
declare const ArbacResource: (name: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
69
|
+
/**
|
|
70
|
+
* Decorator to specify an action for ARBAC evaluation.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} name - The name of the action.
|
|
73
|
+
* @returns {PropertyDecorator} A property decorator for ARBAC action identification.
|
|
74
|
+
*/
|
|
24
75
|
declare const ArbacAction: (name: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
76
|
+
/**
|
|
77
|
+
* Marks a resource or action as publicly accessible, bypassing authorization checks.
|
|
78
|
+
*
|
|
79
|
+
* @returns {PropertyDecorator} A property decorator that marks an entity as public.
|
|
80
|
+
*/
|
|
25
81
|
declare const ArbacPublic: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
26
82
|
|
|
27
83
|
interface TArbacMeta {
|
|
@@ -35,13 +91,50 @@ declare function getArbacMate(): Mate<TMoostMetadata & TArbacMeta & {
|
|
|
35
91
|
params: Array<TMateParamMeta>;
|
|
36
92
|
}>;
|
|
37
93
|
|
|
94
|
+
/**
|
|
95
|
+
* A DI-enabled extension of the `Arbac` class for use within MoostJS.
|
|
96
|
+
*
|
|
97
|
+
* This class allows ARBAC (Advanced Role-Based Access Control) to be easily injected
|
|
98
|
+
* into MoostJS services and controllers using its dependency injection system.
|
|
99
|
+
*
|
|
100
|
+
* @template TUserAttrs - The type representing user attributes relevant to access control.
|
|
101
|
+
* @template TScope - The type representing access control scopes.
|
|
102
|
+
*/
|
|
38
103
|
declare class MoostArbac<TUserAttrs extends object, TScope extends object> extends Arbac<TUserAttrs, TScope> {
|
|
39
104
|
}
|
|
40
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Base class for providing user data required for ARBAC (Advanced Role-Based Access Control) evaluations.
|
|
108
|
+
*
|
|
109
|
+
* This class must be extended to define how user data is retrieved in the application.
|
|
110
|
+
*
|
|
111
|
+
* @template TUserAttrs - The type representing user attributes relevant to access control.
|
|
112
|
+
*/
|
|
41
113
|
declare class ArbacUserProvider<TUserAttrs extends object> {
|
|
114
|
+
/**
|
|
115
|
+
* Retrieves the unique identifier of the user.
|
|
116
|
+
*
|
|
117
|
+
* @returns {string | Promise<string>} The user ID, or a rejected promise if not implemented.
|
|
118
|
+
* @throws {Error} If the method is not overridden by a subclass.
|
|
119
|
+
*/
|
|
42
120
|
getUserId(): string | Promise<string>;
|
|
121
|
+
/**
|
|
122
|
+
* Retrieves the roles assigned to a user based on their ID.
|
|
123
|
+
*
|
|
124
|
+
* @param {string} id - The user ID.
|
|
125
|
+
* @returns {string[] | Promise<string[]>} An array of role identifiers, or a rejected promise if not implemented.
|
|
126
|
+
* @throws {Error} If the method is not overridden by a subclass.
|
|
127
|
+
*/
|
|
43
128
|
getRoles(id: string): string[] | Promise<string[]>;
|
|
129
|
+
/**
|
|
130
|
+
* Retrieves the attributes associated with a user based on their ID.
|
|
131
|
+
*
|
|
132
|
+
* @param {string} id - The user ID.
|
|
133
|
+
* @returns {TUserAttrs | Promise<TUserAttrs>} The user attributes, or a rejected promise if not implemented.
|
|
134
|
+
* @throws {Error} If the method is not overridden by a subclass.
|
|
135
|
+
*/
|
|
44
136
|
getAttrs(id: string): TUserAttrs | Promise<TUserAttrs>;
|
|
45
137
|
}
|
|
46
138
|
|
|
47
|
-
export { ArbacAction, ArbacAuthorize, ArbacPublic, ArbacResource, ArbacScopes, ArbacUserProvider, MoostArbac,
|
|
139
|
+
export { ArbacAction, ArbacAuthorize, ArbacPublic, ArbacResource, ArbacScopes, ArbacUserProvider, MoostArbac, arbackAuthorizeInterceptor, getArbacMate, useArbac };
|
|
140
|
+
export type { TArbacMeta };
|
package/dist/index.mjs
CHANGED
|
@@ -27,7 +27,7 @@ var ArbacUserProvider = class {
|
|
|
27
27
|
* @returns {string | Promise<string>} The user ID, or a rejected promise if not implemented.
|
|
28
28
|
* @throws {Error} If the method is not overridden by a subclass.
|
|
29
29
|
*/ getUserId() {
|
|
30
|
-
return Promise.reject(new Error("ArbacUserProvider class must be extended"));
|
|
30
|
+
return Promise.reject(/* @__PURE__ */ new Error("ArbacUserProvider class must be extended"));
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
33
|
* Retrieves the roles assigned to a user based on their ID.
|
|
@@ -36,7 +36,7 @@ var ArbacUserProvider = class {
|
|
|
36
36
|
* @returns {string[] | Promise<string[]>} An array of role identifiers, or a rejected promise if not implemented.
|
|
37
37
|
* @throws {Error} If the method is not overridden by a subclass.
|
|
38
38
|
*/ getRoles(id) {
|
|
39
|
-
return Promise.reject(new Error("ArbacUserProvider class must be extended"));
|
|
39
|
+
return Promise.reject(/* @__PURE__ */ new Error("ArbacUserProvider class must be extended"));
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* Retrieves the attributes associated with a user based on their ID.
|
|
@@ -45,14 +45,18 @@ var ArbacUserProvider = class {
|
|
|
45
45
|
* @returns {TUserAttrs | Promise<TUserAttrs>} The user attributes, or a rejected promise if not implemented.
|
|
46
46
|
* @throws {Error} If the method is not overridden by a subclass.
|
|
47
47
|
*/ getAttrs(id) {
|
|
48
|
-
return Promise.reject(new Error("ArbacUserProvider class must be extended"));
|
|
48
|
+
return Promise.reject(/* @__PURE__ */ new Error("ArbacUserProvider class must be extended"));
|
|
49
49
|
}
|
|
50
50
|
};
|
|
51
51
|
ArbacUserProvider = _ts_decorate([Injectable()], ArbacUserProvider);
|
|
52
52
|
|
|
53
53
|
//#endregion
|
|
54
54
|
//#region packages/arbac/src/arbac.composables.ts
|
|
55
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Composable for ARBAC (Advanced Role-Based Access Control) utilities within MoostJS.
|
|
57
|
+
*
|
|
58
|
+
* @template TScope - Type representing the scope of access control.
|
|
59
|
+
*/ function useArbac() {
|
|
56
60
|
const store = useAsyncEventContext().store("arbac");
|
|
57
61
|
const cc = useControllerContext();
|
|
58
62
|
const getScopes = () => store.get("scopes");
|
|
@@ -92,7 +96,12 @@ function getArbacMate() {
|
|
|
92
96
|
|
|
93
97
|
//#endregion
|
|
94
98
|
//#region packages/arbac/src/arbac.decorator.ts
|
|
95
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Interceptor function that enforces authorization checks based on ARBAC rules.
|
|
101
|
+
* It evaluates the user's permissions against the requested resource and action.
|
|
102
|
+
*
|
|
103
|
+
* @constant
|
|
104
|
+
*/ const arbackAuthorizeInterceptor = defineInterceptorFn(async (before, after, onError) => {
|
|
96
105
|
const logger = useEventLogger("arbac");
|
|
97
106
|
const { setScopes, evaluate, resource, action, isPublic } = useArbac();
|
|
98
107
|
if (!action || !resource || isPublic) return;
|
|
@@ -110,11 +119,33 @@ const arbackAuthorizeInterceptor = defineInterceptorFn(async (before, after, onE
|
|
|
110
119
|
throw new HttpError(401, `Authorization error`);
|
|
111
120
|
}
|
|
112
121
|
}, TInterceptorPriority.GUARD);
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const
|
|
122
|
+
/**
|
|
123
|
+
* Decorator that applies the `arbackAuthorizeInterceptor` to enforce authorization.
|
|
124
|
+
*
|
|
125
|
+
* @returns {MethodDecorator} A method decorator that enforces ARBAC.
|
|
126
|
+
*/ const ArbacAuthorize = () => Intercept(arbackAuthorizeInterceptor);
|
|
127
|
+
/**
|
|
128
|
+
* Resolves and retrieves the current ARBAC scopes in the request context.
|
|
129
|
+
*
|
|
130
|
+
* @returns {Function} A resolver function that returns user access scopes.
|
|
131
|
+
*/ const ArbacScopes = () => Resolve(() => useArbac().getScopes());
|
|
132
|
+
/**
|
|
133
|
+
* Decorator to specify a resource for ARBAC evaluation.
|
|
134
|
+
*
|
|
135
|
+
* @param {string} name - The name of the resource.
|
|
136
|
+
* @returns {PropertyDecorator} A property decorator for ARBAC resource identification.
|
|
137
|
+
*/ const ArbacResource = (name) => getArbacMate().decorate("arbacResourceId", name);
|
|
138
|
+
/**
|
|
139
|
+
* Decorator to specify an action for ARBAC evaluation.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} name - The name of the action.
|
|
142
|
+
* @returns {PropertyDecorator} A property decorator for ARBAC action identification.
|
|
143
|
+
*/ const ArbacAction = (name) => getArbacMate().decorate("arbacActionId", name);
|
|
144
|
+
/**
|
|
145
|
+
* Marks a resource or action as publicly accessible, bypassing authorization checks.
|
|
146
|
+
*
|
|
147
|
+
* @returns {PropertyDecorator} A property decorator that marks an entity as public.
|
|
148
|
+
*/ const ArbacPublic = () => getArbacMate().decorate("arbacPublic", true);
|
|
118
149
|
|
|
119
150
|
//#endregion
|
|
120
151
|
export { ArbacAction, ArbacAuthorize, ArbacPublic, ArbacResource, ArbacScopes, ArbacUserProvider, MoostArbac, arbackAuthorizeInterceptor, getArbacMate, useArbac };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/arbac",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.30",
|
|
4
4
|
"description": "Access Control @prostojs/arbac",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -36,13 +36,15 @@
|
|
|
36
36
|
"url": "https://github.com/moostjs/moostjs/issues"
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/arbac#readme",
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@wooksjs/event-http": "^0.6.1",
|
|
41
|
+
"moost": "^0.5.30"
|
|
42
|
+
},
|
|
39
43
|
"dependencies": {
|
|
40
|
-
"@
|
|
41
|
-
"@prostojs/arbac": "^0.0.2",
|
|
42
|
-
"moost": "^0.5.28"
|
|
44
|
+
"@prostojs/arbac": "^0.0.2"
|
|
43
45
|
},
|
|
44
46
|
"devDependencies": {
|
|
45
|
-
"vitest": "
|
|
47
|
+
"vitest": "3.2.4"
|
|
46
48
|
},
|
|
47
49
|
"scripts": {
|
|
48
50
|
"pub": "pnpm publish --access public",
|