@zenofolio/hyper-decor 1.0.56 → 1.0.58
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.
|
@@ -7,19 +7,19 @@ const constants_1 = require("../constants");
|
|
|
7
7
|
function roleTransform(list, callback) {
|
|
8
8
|
const { roles, names, isEmtpy } = resolveRoles(list);
|
|
9
9
|
const middleware = (req, res, next) => {
|
|
10
|
-
|
|
10
|
+
var _a;
|
|
11
11
|
if (isEmtpy)
|
|
12
12
|
return next();
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (requestRoles === null || requestRoles === void 0 ? void 0 : requestRoles.includes(constants_1.FULL_ACCESS))
|
|
13
|
+
const requestRoles = new Set((_a = (0, helpers_1.getRoles)(req)) !== null && _a !== void 0 ? _a : []);
|
|
14
|
+
if (requestRoles.size === 0 && isEmtpy)
|
|
16
15
|
return next();
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
if (requestRoles.has(constants_1.FULL_ACCESS))
|
|
17
|
+
return next();
|
|
18
|
+
const role = roles.some((scope) => requestRoles.has(scope.role));
|
|
19
19
|
if (role) {
|
|
20
20
|
return next();
|
|
21
21
|
}
|
|
22
|
-
return next(new exeptions_1.NotRoleException(`Only ${Array.from(names).join(", ")} can access this resource`, requestRoles, Array.from(names)));
|
|
22
|
+
return next(new exeptions_1.NotRoleException(`Only ${Array.from(names).join(", ")} can access this resource`, Array.from(requestRoles), Array.from(names)));
|
|
23
23
|
};
|
|
24
24
|
if (names.size > 0 && callback) {
|
|
25
25
|
callback(middleware, roles, names);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MiddlewareHandler } from "hyper-express";
|
|
1
|
+
import { MiddlewareHandler } from "hyper-express/types";
|
|
2
2
|
import { ScopeMap, ScopeType } from "../../decorators";
|
|
3
3
|
type Callback = (middleware: MiddlewareHandler, scopes: ScopeMap[], names: Set<string>) => void;
|
|
4
4
|
export default function scopeTransfrom(listScopes: ScopeType[], ...callback: Callback[]): MiddlewareHandler;
|
|
@@ -5,25 +5,26 @@ const exeptions_1 = require("../../exeptions");
|
|
|
5
5
|
const helpers_1 = require("../../common/helpers");
|
|
6
6
|
const constants_1 = require("../constants");
|
|
7
7
|
function scopeTransfrom(listScopes, ...callback) {
|
|
8
|
-
const { scopes, scopeNames,
|
|
8
|
+
const { scopes, scopeNames, isEmpty } = resolveScopes(listScopes);
|
|
9
9
|
const middleware = (req, res, next) => {
|
|
10
10
|
var _a;
|
|
11
|
-
|
|
12
|
-
if (isEmtpy)
|
|
11
|
+
if (isEmpty)
|
|
13
12
|
return next();
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
const userScopesRaw = (0, helpers_1.getScopes)(req);
|
|
14
|
+
if (!userScopesRaw || userScopesRaw.length === 0) {
|
|
15
|
+
return next(new exeptions_1.NotScopeException(`FORBIDDEN`, [], Array.from(scopeNames)));
|
|
16
|
+
}
|
|
17
|
+
const userScopes = new Set(userScopesRaw);
|
|
18
|
+
if (userScopes.has(constants_1.FULL_ACCESS))
|
|
18
19
|
return next();
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
for (const scope of scopes) {
|
|
21
|
+
if (!userScopes.has(scope.scope)) {
|
|
22
|
+
return next(new exeptions_1.NotScopeException((_a = scope.message) !== null && _a !== void 0 ? _a : `FORBIDDEN`, userScopesRaw, Array.from(scopeNames)));
|
|
23
|
+
}
|
|
23
24
|
}
|
|
24
25
|
return next();
|
|
25
26
|
};
|
|
26
|
-
if (
|
|
27
|
+
if (!isEmpty && callback.length > 0) {
|
|
27
28
|
for (const cb of callback)
|
|
28
29
|
cb(middleware, scopes, scopeNames);
|
|
29
30
|
}
|
|
@@ -40,36 +41,47 @@ const resolveScopes = (scopes) => {
|
|
|
40
41
|
var _a, _b;
|
|
41
42
|
const $scopes = {};
|
|
42
43
|
for (const scope of scopes) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
const list = parseScope(scope);
|
|
45
|
+
if (list.length === 0)
|
|
46
|
+
continue;
|
|
47
|
+
for (const s of list) {
|
|
48
|
+
$scopes[s.scope] = {
|
|
49
|
+
scope: s.scope,
|
|
50
|
+
description: (_a = s.description) !== null && _a !== void 0 ? _a : "",
|
|
51
|
+
message: (_b = s.message) !== null && _b !== void 0 ? _b : `You don't have the required scopes to access this resource`,
|
|
47
52
|
};
|
|
48
53
|
}
|
|
49
|
-
else if (Array.isArray(scope)) {
|
|
50
|
-
for (const s of scope) {
|
|
51
|
-
switch (typeof s) {
|
|
52
|
-
case "string":
|
|
53
|
-
$scopes[s] = {
|
|
54
|
-
scope: s,
|
|
55
|
-
description: "",
|
|
56
|
-
};
|
|
57
|
-
break;
|
|
58
|
-
case "object":
|
|
59
|
-
$scopes[s.scope] = {
|
|
60
|
-
scope: s.scope,
|
|
61
|
-
description: (_a = s.description) !== null && _a !== void 0 ? _a : "",
|
|
62
|
-
message: (_b = s.message) !== null && _b !== void 0 ? _b : `You don't have the required scopes to access this resource`,
|
|
63
|
-
};
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
54
|
}
|
|
69
55
|
const values = Object.values($scopes);
|
|
70
56
|
return {
|
|
71
57
|
scopes: values,
|
|
72
58
|
scopeNames: new Set(Object.keys($scopes)),
|
|
73
|
-
|
|
59
|
+
isEmpty: values.length === 0,
|
|
74
60
|
};
|
|
75
61
|
};
|
|
62
|
+
/**
|
|
63
|
+
* Parse the scope to a standard format
|
|
64
|
+
*
|
|
65
|
+
* @param scope
|
|
66
|
+
* @returns
|
|
67
|
+
*/
|
|
68
|
+
const parseScope = (scope) => {
|
|
69
|
+
switch (typeof scope) {
|
|
70
|
+
case "string":
|
|
71
|
+
return [
|
|
72
|
+
{
|
|
73
|
+
scope,
|
|
74
|
+
description: "",
|
|
75
|
+
},
|
|
76
|
+
];
|
|
77
|
+
case "object":
|
|
78
|
+
if (Array.isArray(scope)) {
|
|
79
|
+
if (scope.length === 0)
|
|
80
|
+
return [];
|
|
81
|
+
return scope.map((s) => parseScope(s)).flat();
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
return [scope];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenofolio/hyper-decor",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.58",
|
|
4
4
|
"description": "Project core with utilities and features",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"author": "zenozaga",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"hyper-express": "^6.17.3"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@types/file-type": "
|
|
18
|
+
"@types/file-type": "10.6.0",
|
|
19
19
|
"@types/mocha": "^10.0.8",
|
|
20
20
|
"chai": "^5.1.2",
|
|
21
21
|
"mocha": "^10.7.3",
|