@modernlock/common 1.0.55 → 1.0.56
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/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -54,3 +54,4 @@ __exportStar(require("./things/events"), exports);
|
|
|
54
54
|
__exportStar(require("./mqtt/topics"), exports);
|
|
55
55
|
__exportStar(require("./things/commands"), exports);
|
|
56
56
|
__exportStar(require("./@types/notification"), exports);
|
|
57
|
+
__exportStar(require("./@types/express"), exports);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
type Role = "client" | "owner" | "superAdmin" | "developer" | "supportAgent";
|
|
3
|
+
declare const verifyPermission: (role: Role) => (req: Request, res: Response, next: NextFunction) => void;
|
|
4
|
+
export default verifyPermission;
|
|
@@ -1,60 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.weakCheck = exports.strongCheck = void 0;
|
|
4
3
|
const auth_error_1 = require("../errors/auth-error");
|
|
5
|
-
const
|
|
6
|
-
const internal_server_error_1 = require("../errors/internal-server-error");
|
|
7
|
-
const jwtEncryption_1 = require("../utils/jwtEncryption");
|
|
8
|
-
function strongCheck(read, write) {
|
|
4
|
+
const verifyPermission = (role) => {
|
|
9
5
|
return (req, res, next) => {
|
|
10
|
-
|
|
11
|
-
if (!(write && read))
|
|
12
|
-
throw new internal_server_error_1.InternalServerError();
|
|
13
|
-
const { token } = req.session;
|
|
14
|
-
if (!token)
|
|
6
|
+
if (role !== req.user.accountType)
|
|
15
7
|
throw new auth_error_1.AuthenticationError();
|
|
16
|
-
const user = (0, jwtEncryption_1.jwtDecrypt)(token);
|
|
17
|
-
if (!user)
|
|
18
|
-
throw new auth_error_1.AuthenticationError();
|
|
19
|
-
if (!user.isAdmin)
|
|
20
|
-
throw new authorization_error_1.AuthorizationError();
|
|
21
|
-
if (write && !((_a = user.permissions) === null || _a === void 0 ? void 0 : _a.write))
|
|
22
|
-
throw new authorization_error_1.AuthorizationError();
|
|
23
|
-
if (read && !((_b = user.permissions) === null || _b === void 0 ? void 0 : _b.read))
|
|
24
|
-
throw new authorization_error_1.AuthorizationError();
|
|
25
|
-
req.user = user;
|
|
26
|
-
next();
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
exports.strongCheck = strongCheck;
|
|
30
|
-
;
|
|
31
|
-
function weakCheck(read, write) {
|
|
32
|
-
return (req, res, next) => {
|
|
33
|
-
var _a, _b;
|
|
34
|
-
if (!(write && read))
|
|
35
|
-
throw new internal_server_error_1.InternalServerError();
|
|
36
|
-
const { token } = req.session;
|
|
37
|
-
if (!token)
|
|
38
|
-
throw new auth_error_1.AuthenticationError();
|
|
39
|
-
const user = (0, jwtEncryption_1.jwtDecrypt)(token);
|
|
40
|
-
if (!user)
|
|
41
|
-
throw new auth_error_1.AuthenticationError();
|
|
42
|
-
req.user = user;
|
|
43
|
-
if (!user.isAdmin) {
|
|
44
|
-
req.user.allowed = false;
|
|
45
|
-
return next();
|
|
46
|
-
}
|
|
47
|
-
if (write && !((_a = user.permissions) === null || _a === void 0 ? void 0 : _a.write)) {
|
|
48
|
-
req.user.allowed = false;
|
|
49
|
-
return next();
|
|
50
|
-
}
|
|
51
|
-
if (read && !((_b = user.permissions) === null || _b === void 0 ? void 0 : _b.read)) {
|
|
52
|
-
req.user.allowed = false;
|
|
53
|
-
return next();
|
|
54
|
-
}
|
|
55
|
-
req.user.allowed = true;
|
|
56
8
|
next();
|
|
57
9
|
};
|
|
58
|
-
}
|
|
59
|
-
exports.
|
|
60
|
-
;
|
|
10
|
+
};
|
|
11
|
+
exports.default = verifyPermission;
|