@go-mailer/jarvis 10.2.3 → 10.3.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.
@@ -11,6 +11,7 @@ const Env = require('../env')
11
11
  const Errors = require('./errors')
12
12
  const { ProcessLogger } = require('./logger')
13
13
  const { checkAuthority, verifyAPIKey } = require('../clients/iam')
14
+ const { localCache } = require('../redis/cache')
14
15
  const authLogger = new ProcessLogger('Authenticator')
15
16
 
16
17
  // helpers
@@ -115,7 +116,15 @@ const authorizeUser = ({ action, resource }) => {
115
116
  const { is_admin, is_service_request, tenant_id, user_id } = request
116
117
  if (is_admin || is_service_request) return next()
117
118
 
118
- await checkAuthority({ action, resource, tenant_id, user_id })
119
+ const key = `${resource}:${action}:${tenant_id}:${user_id}`
120
+ const has_authority = localCache.get_item(key)
121
+ if (has_authority == null) {
122
+ const is_permitted = await checkAuthority({ action, resource, tenant_id, user_id })
123
+ localCache.add_item(key, is_permitted, 15 * 60)
124
+ } else if (has_authority === false){
125
+ throw new Error('Unauthorized')
126
+ }
127
+
119
128
  next()
120
129
  } catch (e) {
121
130
  authLogger.error(e, 'authorizeUser')
@@ -0,0 +1,31 @@
1
+ class LocalCache {
2
+ constructor () {
3
+ this.cache = new Map()
4
+ }
5
+
6
+ add_item (key='', value, duration) {
7
+ if (!key || !value) return
8
+ this.cache.set(key, value)
9
+ this.expire_item(key, duration)
10
+ }
11
+
12
+ expire_item(key, duration = 5) {
13
+ setTimeout(() => {
14
+ this.cache.delete(key)
15
+ }, duration * 1000)
16
+ }
17
+
18
+ get_item (key) {
19
+ if (!key || !this.cache.has(key)) return null
20
+ return this.cache.get(key)
21
+ }
22
+
23
+ remove_item(key) {
24
+ if (!key || !this.cache.has(key)) return
25
+ this.cache.delete(key)
26
+ }
27
+ }
28
+
29
+ module.exports = {
30
+ localCache: new LocalCache()
31
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "10.2.3",
3
+ "version": "10.3.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>",