@go-mailer/jarvis 4.2.5 → 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,13 +20,14 @@ 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;
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];
28
29
 
29
- return tenant_id;
30
+ return id;
30
31
  };
31
32
 
32
33
  // main
@@ -40,7 +41,7 @@ const authenticateAdmin = async (request, response, next) => {
40
41
 
41
42
  const authenticateBearerKey = async (request, response, next) => {
42
43
  try {
43
- const token = extract_token(request.headers);
44
+ const token = extractToken(request.headers);
44
45
  request.tenant_id = await verifyAPIKey(token);
45
46
  next();
46
47
  } catch (e) {
@@ -49,6 +50,12 @@ const authenticateBearerKey = async (request, response, next) => {
49
50
  }
50
51
  };
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
+
52
59
  const authenticateParamKey = async (request, response, next) => {
53
60
  try {
54
61
  const { api_key: token } = request.params;
@@ -67,18 +74,21 @@ const authenticateUser = async (request, response, next) => {
67
74
  const ISSUER = Env.fetch("JWT_ISSUER", true);
68
75
  const SECRET = Env.fetch("JWT_SECRET", true);
69
76
 
70
- const token = extract_token(request.headers);
77
+ const token = extractToken(request.headers);
71
78
  if (token === DEFAULT_TOKEN) {
72
79
  // inter-service requests
73
80
  request.is_service_request = true;
74
81
  // typically scope requests by tenant_id
75
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 };
76
84
  return next();
77
85
  }
78
86
 
79
- const { tenant_id, is_admin } = await jwt.verify(token, SECRET, { issuer: ISSUER });
87
+ const { id: user_id, is_admin, memberships } = await jwt.verify(token, SECRET, { issuer: ISSUER });
88
+ const tenant_id = extractId(request, "tenant_id");
80
89
  request.is_admin = !!is_admin;
81
- request.tenant_id = is_admin ? extract_tenant_id(request) : tenant_id;
90
+ request.user_id = is_admin ? extractId(request, "user_id") : user_id;
91
+ request.tenant_id = is_admin ? tenant_id : authenticateMembership(tenant_id, memberships);
82
92
 
83
93
  next();
84
94
  } catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "4.2.5",
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>",