@go-mailer/jarvis 4.2.4 → 4.2.5
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 +38 -30
- package/lib/query.js +1 -1
- package/package.json +1 -1
package/lib/middlewares/auth.js
CHANGED
|
@@ -21,38 +21,30 @@ const extract_token = (headers) => {
|
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
const extract_tenant_id = (request) => {
|
|
24
|
-
const { params, query } = request
|
|
25
|
-
let tenant_id = { $exists: true }
|
|
26
|
-
if (query.tenant_id) tenant_id = query.tenant_id
|
|
27
|
-
if (params.tenant_id) tenant_id = params.tenant_id
|
|
28
|
-
|
|
29
|
-
return tenant_id
|
|
30
|
-
}
|
|
24
|
+
const { params, query } = request;
|
|
25
|
+
let tenant_id = { $exists: true };
|
|
26
|
+
if (query.tenant_id) tenant_id = query.tenant_id;
|
|
27
|
+
if (params.tenant_id) tenant_id = params.tenant_id;
|
|
28
|
+
|
|
29
|
+
return tenant_id;
|
|
30
|
+
};
|
|
31
31
|
|
|
32
32
|
// main
|
|
33
|
-
const authenticateUser = async (request, response, next) => {
|
|
34
|
-
try {
|
|
35
|
-
// env vars
|
|
36
|
-
const DEFAULT_TOKEN = Env.fetch("DEFAULT_TOKEN", true);
|
|
37
|
-
const ISSUER = Env.fetch("JWT_ISSUER", true);
|
|
38
|
-
const SECRET = Env.fetch("JWT_SECRET", true);
|
|
39
|
-
|
|
40
|
-
const token = extract_token(request.headers);
|
|
41
|
-
if (token === DEFAULT_TOKEN) {
|
|
42
|
-
// inter-service requests
|
|
43
|
-
request.is_service_request = true;
|
|
44
|
-
// typically scope requests by tenant_id
|
|
45
|
-
request.tenant_id = request.body.tenant_id || request.query.tenant_id || { $exists: true };
|
|
46
|
-
return next();
|
|
47
|
-
}
|
|
48
33
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
34
|
+
const authenticateAdmin = async (request, response, next) => {
|
|
35
|
+
const { is_admin } = request;
|
|
36
|
+
if (!is_admin) return response.status(403).json(Errors.UNAUTHORIZED);
|
|
52
37
|
|
|
38
|
+
next();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const authenticateBearerKey = async (request, response, next) => {
|
|
42
|
+
try {
|
|
43
|
+
const token = extract_token(request.headers);
|
|
44
|
+
request.tenant_id = await verifyAPIKey(token);
|
|
53
45
|
next();
|
|
54
46
|
} catch (e) {
|
|
55
|
-
authLogger.error(e, "
|
|
47
|
+
authLogger.error(e, "authenticateBearerKey");
|
|
56
48
|
return response.status(403).json(Errors.UNAUTHORIZED);
|
|
57
49
|
}
|
|
58
50
|
};
|
|
@@ -68,15 +60,31 @@ const authenticateParamKey = async (request, response, next) => {
|
|
|
68
60
|
}
|
|
69
61
|
};
|
|
70
62
|
|
|
71
|
-
const
|
|
63
|
+
const authenticateUser = async (request, response, next) => {
|
|
72
64
|
try {
|
|
65
|
+
// env vars
|
|
66
|
+
const DEFAULT_TOKEN = Env.fetch("DEFAULT_TOKEN", true);
|
|
67
|
+
const ISSUER = Env.fetch("JWT_ISSUER", true);
|
|
68
|
+
const SECRET = Env.fetch("JWT_SECRET", true);
|
|
69
|
+
|
|
73
70
|
const token = extract_token(request.headers);
|
|
74
|
-
|
|
71
|
+
if (token === DEFAULT_TOKEN) {
|
|
72
|
+
// inter-service requests
|
|
73
|
+
request.is_service_request = true;
|
|
74
|
+
// typically scope requests by tenant_id
|
|
75
|
+
request.tenant_id = request.body.tenant_id || request.query.tenant_id || { $exists: true };
|
|
76
|
+
return next();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const { tenant_id, is_admin } = await jwt.verify(token, SECRET, { issuer: ISSUER });
|
|
80
|
+
request.is_admin = !!is_admin;
|
|
81
|
+
request.tenant_id = is_admin ? extract_tenant_id(request) : tenant_id;
|
|
82
|
+
|
|
75
83
|
next();
|
|
76
84
|
} catch (e) {
|
|
77
|
-
authLogger.error(e, "
|
|
85
|
+
authLogger.error(e, "authenticateUser");
|
|
78
86
|
return response.status(403).json(Errors.UNAUTHORIZED);
|
|
79
87
|
}
|
|
80
88
|
};
|
|
81
89
|
|
|
82
|
-
module.exports = { authenticateBearerKey, authenticateParamKey, authenticateUser };
|
|
90
|
+
module.exports = { authenticateAdmin, authenticateBearerKey, authenticateParamKey, authenticateUser };
|
package/lib/query.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const buildQuery = (options) => {
|
|
2
|
-
const sort_condition = options.sort_by ? buildSortOrderString(options.sort_by) :
|
|
2
|
+
const sort_condition = options.sort_by ? buildSortOrderString(options.sort_by) : {}
|
|
3
3
|
const fields_to_return = options.return_only ? buildReturnFieldsString(options.return_only) : ''
|
|
4
4
|
const count = options.count || false
|
|
5
5
|
|