@go-mailer/jarvis 10.2.2 → 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.
- package/lib/constants/automation.js +3 -2
- package/lib/middlewares/auth.js +10 -1
- package/lib/redis/cache.js +31 -0
- package/package.json +1 -1
|
@@ -11,10 +11,11 @@ module.exports = {
|
|
|
11
11
|
//
|
|
12
12
|
EFFECT_ADD_CONTACT_ATTRIBUTE: 'add_attribute_to_contact',
|
|
13
13
|
EFFECT_ADD_CONTACT_TAG: 'add_tag_to_contact',
|
|
14
|
-
EFFECT_REMOVE_CONTACT_TAG: 'remove_tag_from_contact',
|
|
15
14
|
EFFECT_REMOVE_CONTACT: 'delete_contact',
|
|
16
|
-
|
|
15
|
+
EFFECT_REMOVE_CONTACT_TAG: 'remove_tag_from_contact',
|
|
16
|
+
EFFECT_SEND_MOBILE_PUSH: 'send_mobile_push_message',
|
|
17
17
|
EFFECT_SEND_SMS: 'send_sms',
|
|
18
|
+
EFFECT_SEND_TRANSACTIONAL: 'send_transactional_email',
|
|
18
19
|
EFFECT_SEND_WEB_PUSH: 'send_web_push_message',
|
|
19
20
|
EFFECT_SUBSCRIBE: 'add_to_audience',
|
|
20
21
|
EFFECT_UNSUBSCRIBE: 'remove_from_audience'
|
package/lib/middlewares/auth.js
CHANGED
|
@@ -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
|
-
|
|
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
|
+
}
|