@go-mailer/jarvis 4.2.3 → 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.
@@ -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
- 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;
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, "authenticateUser");
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 authenticateBearerKey = async (request, response, next) => {
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
- request.tenant_id = await verifyAPIKey(token);
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, "authenticateBearerKey");
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
 
@@ -1,11 +1,11 @@
1
1
  const { CampaignSchema } = require('./Campaign')
2
2
  const { ContactImportSchema } = require('./Contact')
3
3
  const { PostalSchema } = require('./Postal')
4
- const { AutomationTaskSchema } = require('./automation/Task')
4
+ const { TaskSchema } = require('./automation/Task')
5
5
 
6
6
  module.exports = {
7
7
  CampaignSchema,
8
8
  ContactImportSchema,
9
9
  PostalSchema,
10
- AutomationTaskSchema
10
+ AutomationTaskSchema: TaskSchema
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "4.2.3",
3
+ "version": "4.2.5",
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>",