@go-mailer/jarvis 4.2.4 → 5.0.0

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.
@@ -10,7 +10,7 @@ const { verifyAPIKey } = require("../clients/iam");
10
10
  const authLogger = new ProcessLogger("Authenticator");
11
11
 
12
12
  // helpers
13
- const extract_token = (headers) => {
13
+ const extractToken = (headers) => {
14
14
  const { authorization } = headers;
15
15
  if (!authorization) throw new Error("Unauthorized");
16
16
 
@@ -20,43 +20,42 @@ const extract_token = (headers) => {
20
20
  return token;
21
21
  };
22
22
 
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
- }
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
+ };
31
32
 
32
33
  // 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
34
 
49
- const { tenant_id, is_admin } = await jwt.verify(token, SECRET, { issuer: ISSUER });
50
- request.is_admin = !!is_admin;
51
- request.tenant_id = is_admin ? extract_tenant_id(request) : tenant_id;
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
+ };
52
41
 
42
+ const authenticateBearerKey = async (request, response, next) => {
43
+ try {
44
+ const token = extractToken(request.headers);
45
+ request.tenant_id = await verifyAPIKey(token);
53
46
  next();
54
47
  } catch (e) {
55
- authLogger.error(e, "authenticateUser");
48
+ authLogger.error(e, "authenticateBearerKey");
56
49
  return response.status(403).json(Errors.UNAUTHORIZED);
57
50
  }
58
51
  };
59
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
+
60
59
  const authenticateParamKey = async (request, response, next) => {
61
60
  try {
62
61
  const { api_key: token } = request.params;
@@ -68,15 +67,34 @@ const authenticateParamKey = async (request, response, next) => {
68
67
  }
69
68
  };
70
69
 
71
- const authenticateBearerKey = async (request, response, next) => {
70
+ const authenticateUser = async (request, response, next) => {
72
71
  try {
73
- const token = extract_token(request.headers);
74
- request.tenant_id = await verifyAPIKey(token);
72
+ // env vars
73
+ const DEFAULT_TOKEN = Env.fetch("DEFAULT_TOKEN", true);
74
+ const ISSUER = Env.fetch("JWT_ISSUER", true);
75
+ const SECRET = Env.fetch("JWT_SECRET", true);
76
+
77
+ const token = extractToken(request.headers);
78
+ if (token === DEFAULT_TOKEN) {
79
+ // inter-service requests
80
+ request.is_service_request = true;
81
+ // typically scope requests by tenant_id
82
+ request.tenant_id = request.body.tenant_id || request.query.tenant_id || { $exists: true };
83
+ request.user_id = request.body.user_id || request.query.user_id || { $exists: true };
84
+ return next();
85
+ }
86
+
87
+ const { id: user_id, is_admin, memberships } = await jwt.verify(token, SECRET, { issuer: ISSUER });
88
+ const tenant_id = extractId(request, "tenant_id");
89
+ request.is_admin = !!is_admin;
90
+ request.user_id = is_admin ? extractId(request, "user_id") : user_id;
91
+ request.tenant_id = is_admin ? tenant_id : authenticateMembership(tenant_id, memberships);
92
+
75
93
  next();
76
94
  } catch (e) {
77
- authLogger.error(e, "authenticateBearerKey");
95
+ authLogger.error(e, "authenticateUser");
78
96
  return response.status(403).json(Errors.UNAUTHORIZED);
79
97
  }
80
98
  };
81
99
 
82
- module.exports = { authenticateBearerKey, authenticateParamKey, authenticateUser };
100
+ 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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "4.2.4",
3
+ "version": "5.0.0",
4
4
  "main": "index.js",
5
5
  "repository": "git@github.com:go-mailer-ltd/jarvis-node.git",
6
6
  "author": "Nathan Oguntuberu <nateoguns.work@gmail.com>",