@go-mailer/jarvis 5.0.7 → 5.0.9

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.
@@ -3,6 +3,10 @@
3
3
  */
4
4
 
5
5
  const jwt = require('jsonwebtoken')
6
+ const requestIP = require('request-ip')
7
+ const DeviceDetector = require('node-device-detector')
8
+ const geoip = require('geoip-lite')
9
+
6
10
  const Env = require('../env')
7
11
  const Errors = require('./errors')
8
12
  const { ProcessLogger } = require('./logger')
@@ -10,16 +14,6 @@ const { checkAuthority, verifyAPIKey } = require('../clients/iam')
10
14
  const authLogger = new ProcessLogger('Authenticator')
11
15
 
12
16
  // 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
17
  const extractId = (request, key) => {
24
18
  const { params, query, body } = request
25
19
  let id = { $exists: true }
@@ -30,6 +24,31 @@ const extractId = (request, key) => {
30
24
  return id
31
25
  }
32
26
 
27
+ const extractRequestHeaders = (request) => {
28
+ const original_header_values = request.headers['x-original-headers']
29
+ if (original_header_values && Object.keys(JSON.parse(original_header_values)).length) {
30
+ const values = JSON.parse(original_header_values)
31
+ return {
32
+ ip_address: values.ip_address || '',
33
+ user_agent: values.user_agent || ''
34
+ }
35
+ }
36
+ return {
37
+ ip_address: requestIP.getClientIp(request),
38
+ user_agent: request.headers['user-agent']
39
+ }
40
+ }
41
+
42
+ const extractToken = (headers) => {
43
+ const { authorization } = headers
44
+ if (!authorization) throw new Error('Unauthorized')
45
+
46
+ const [, token] = authorization.split(' ')
47
+ if (!token) throw new Error('Unauthorized')
48
+
49
+ return token
50
+ }
51
+
33
52
  // main
34
53
 
35
54
  const authenticateAdmin = async (request, response, next) => {
@@ -94,7 +113,7 @@ const authorizeUser = ({ action, resource }) => {
94
113
  return async (request, response, next) => {
95
114
  try {
96
115
  const { is_admin, tenant_id, user_id } = request
97
- if (is_admin) next()
116
+ if (is_admin) return next()
98
117
 
99
118
  await checkAuthority({ action, resource, tenant_id, user_id })
100
119
  next()
@@ -105,10 +124,40 @@ const authorizeUser = ({ action, resource }) => {
105
124
  }
106
125
  }
107
126
 
127
+ const extractDeviceInformation = (request) => {
128
+ const { ip_address, user_agent } = extractRequestHeaders(request)
129
+ const ip_parts = ip_address.split(':')
130
+ const ip4 = ip_parts.pop()
131
+ const ip6 = ip_parts.join(':')
132
+ const geolocation = geoip.lookup(ip4 || ip6) || {}
133
+
134
+ const detector = new DeviceDetector({
135
+ clientIndexes: true,
136
+ deviceIndexes: true,
137
+ deviceAliasCode: false
138
+ })
139
+ const { os, client } = detector.detect(user_agent)
140
+
141
+ const user_device_details = {
142
+ city: geolocation?.city || 'NA',
143
+ country: geolocation?.country || 'NA',
144
+ ll: geolocation?.ll,
145
+ os: { name: os?.name, short_name: os?.short_name },
146
+ client: {
147
+ type: client?.type,
148
+ name: client?.name,
149
+ short_name: client?.short_name,
150
+ family: client?.family
151
+ }
152
+ }
153
+ return user_device_details
154
+ }
155
+
108
156
  module.exports = {
109
157
  authenticateAdmin,
110
158
  authenticateBearerKey,
111
159
  authenticateParamKey,
112
160
  authenticateUser,
113
- authorizeUser
161
+ authorizeUser,
162
+ extractDeviceInformation
114
163
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "5.0.7",
3
+ "version": "5.0.9",
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>",
@@ -13,9 +13,12 @@
13
13
  "@logtail/node": "^0.3.3",
14
14
  "axios": "^1.3.4",
15
15
  "dotenv": "^16.0.3",
16
+ "geoip-lite": "^1.4.10",
16
17
  "jsonwebtoken": "^9.0.0",
18
+ "node-device-detector": "^2.1.1",
17
19
  "redis": "^4.6.12",
18
- "redis-om": "^0.4.3"
20
+ "redis-om": "^0.4.3",
21
+ "request-ip": "^3.3.0"
19
22
  },
20
23
  "devDependencies": {
21
24
  "chai": "^4.3.7",