-
Notifications
You must be signed in to change notification settings - Fork 1
/
slack-cacher.js
60 lines (47 loc) · 1002 Bytes
/
slack-cacher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict'
const jwt = require('jsonwebtoken')
const secretKey = 'jwt-secret'
const oneHour = 60 * 60
let botCached
let teamInfoCached
function nowInSeconds() {
return Math.floor(Date.now() / 1000)
}
function getCachedItem(token) {
if (!token) {
return Promise.reject('No token provided')
}
return new Promise((resolve, reject) => {
jwt.verify(token, secretKey, (err, decoded) => {
if(decoded) {
resolve(decoded.data)
return
}
reject(err)
})
})
}
function getCachedBot() {
return getCachedItem(botCached)
}
function getCachedTeam() {
return getCachedItem(teamInfoCached)
}
function sign(payload) {
return jwt.sign({
exp: nowInSeconds() + oneHour,
data: payload
}, secretKey)
}
function cacheBot(bot) {
botCached = sign(bot)
}
function cacheTeam(team) {
teamInfoCached = sign(team)
}
module.exports = {
getCachedBot: getCachedBot,
getCachedTeam: getCachedTeam,
cacheBot: cacheBot,
cacheTeam: cacheTeam
}