@go-mailer/jarvis 4.2.5 → 5.0.1
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/lib/middlewares/auth.js +114 -86
- package/package.json +1 -1
package/lib/middlewares/auth.js
CHANGED
|
@@ -2,89 +2,117 @@
|
|
|
2
2
|
* User Authentication Middleware
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
const jwt = require("jsonwebtoken");
|
|
6
|
-
const Env = require("../env");
|
|
7
|
-
const Errors = require("./errors");
|
|
8
|
-
const { ProcessLogger } = require("./logger");
|
|
9
|
-
const { verifyAPIKey } = require("../clients/iam");
|
|
10
|
-
const authLogger = new ProcessLogger("Authenticator");
|
|
11
|
-
|
|
12
|
-
// helpers
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
5
|
+
const jwt = require("jsonwebtoken");
|
|
6
|
+
const Env = require("../env");
|
|
7
|
+
const Errors = require("./errors");
|
|
8
|
+
const { ProcessLogger } = require("./logger");
|
|
9
|
+
const { verifyAPIKey } = require("../clients/iam");
|
|
10
|
+
const authLogger = new ProcessLogger("Authenticator");
|
|
11
|
+
|
|
12
|
+
// helpers
|
|
13
|
+
const extractToken = (headers) => {
|
|
14
|
+
const { authorization } = headers;
|
|
15
|
+
if (!authorization) throw new Error("Unauthorized");
|
|
16
|
+
|
|
17
|
+
const [, token] = authorization.split(" ");
|
|
18
|
+
if (!token) throw new Error("Unauthorized");
|
|
19
|
+
|
|
20
|
+
return token;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const extractId = (request, key) => {
|
|
24
|
+
const { params, query, body } = request;
|
|
25
|
+
let id = { $exists: true };
|
|
26
|
+
if (query[key]) id = query[key];
|
|
27
|
+
if (params[key]) id = params[key];
|
|
28
|
+
if (body[key]) id = body[key];
|
|
29
|
+
|
|
30
|
+
return id;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// main
|
|
34
|
+
|
|
35
|
+
const authenticateAdmin = async (request, response, next) => {
|
|
36
|
+
const { is_admin } = request;
|
|
37
|
+
if (!is_admin) return response.status(403).json(Errors.UNAUTHORIZED);
|
|
38
|
+
|
|
39
|
+
next();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const authenticateBearerKey = async (request, response, next) => {
|
|
43
|
+
try {
|
|
44
|
+
const token = extractToken(request.headers);
|
|
45
|
+
request.tenant_id = await verifyAPIKey(token);
|
|
46
|
+
next();
|
|
47
|
+
} catch (e) {
|
|
48
|
+
authLogger.error(e, "authenticateBearerKey");
|
|
49
|
+
return response.status(403).json(Errors.UNAUTHORIZED);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const authenticateMembership = (tenant_id, memberships = []) => {
|
|
54
|
+
const membership = memberships.find((mbrship) => Number(mbrship.tenant_id) === Number(tenant_id));
|
|
55
|
+
if (!membership || membership.status !== "active") throw new Error("Unauthorized");
|
|
56
|
+
return tenant_id;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const authenticateParamKey = async (request, response, next) => {
|
|
60
|
+
try {
|
|
61
|
+
const { api_key: token } = request.params;
|
|
62
|
+
request.tenant_id = await verifyAPIKey(token);
|
|
63
|
+
next();
|
|
64
|
+
} catch (e) {
|
|
65
|
+
authLogger.error(e, "authenticateParamKey");
|
|
66
|
+
return response.status(403).json(Errors.UNAUTHORIZED);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const authenticateTenant = async (request, response, next) => {
|
|
71
|
+
try {
|
|
72
|
+
const tenant_id = extractId(request, "tenant_id");
|
|
73
|
+
request.tenant_id = request.is_admin ? tenant_id : authenticateMembership(tenant_id, request.memberships || []);
|
|
74
|
+
|
|
75
|
+
next();
|
|
76
|
+
} catch (e) {
|
|
77
|
+
authLogger.error(e, "authenticateUser");
|
|
78
|
+
return response.status(403).json(Errors.UNAUTHORIZED);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const authenticateUser = async (request, response, next) => {
|
|
83
|
+
try {
|
|
84
|
+
// env vars
|
|
85
|
+
const DEFAULT_TOKEN = Env.fetch("DEFAULT_TOKEN", true);
|
|
86
|
+
const ISSUER = Env.fetch("JWT_ISSUER", true);
|
|
87
|
+
const SECRET = Env.fetch("JWT_SECRET", true);
|
|
88
|
+
|
|
89
|
+
const token = extractToken(request.headers);
|
|
90
|
+
if (token === DEFAULT_TOKEN) {
|
|
91
|
+
// inter-service requests
|
|
92
|
+
request.is_service_request = true;
|
|
93
|
+
// typically scope requests by tenant_id
|
|
94
|
+
request.tenant_id = request.body.tenant_id || request.query.tenant_id || { $exists: true };
|
|
95
|
+
request.user_id = request.body.user_id || request.query.user_id || { $exists: true };
|
|
96
|
+
return next();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const { id: user_id, is_admin, memberships } = await jwt.verify(token, SECRET, { issuer: ISSUER });
|
|
100
|
+
request.is_admin = !!is_admin;
|
|
101
|
+
request.user_id = is_admin ? extractId(request, "user_id") : user_id;
|
|
102
|
+
request.memberships = memberships;
|
|
103
|
+
|
|
104
|
+
next();
|
|
105
|
+
} catch (e) {
|
|
106
|
+
authLogger.error(e, "authenticateUser");
|
|
107
|
+
return response.status(403).json(Errors.UNAUTHORIZED);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
module.exports = {
|
|
112
|
+
authenticateAdmin,
|
|
113
|
+
authenticateBearerKey,
|
|
114
|
+
authenticateParamKey,
|
|
115
|
+
authenticateTenant,
|
|
116
|
+
authenticateUser,
|
|
117
|
+
};
|
|
118
|
+
|