-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjavascript.js
38 lines (30 loc) · 1.01 KB
/
javascript.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
import crypto from 'crypto'
import moment from 'moment'
export default (user) => {
if (!user.expires) {
user.expires = moment().add(1, 'days').format('YYYY-MM-DD hh:mm:ss') // example expiration one day from now
}
const mode = 'AES-128-CBC'
const ssoKey = process.env.USERVOICE_API_KEY
const accountKey = process.env.USERVOICE_ACCOUNT_KEY
const initVector = 'OpenSSL for Ruby'
user = Buffer.from(JSON.stringify(user))
const iv = Buffer.from(initVector)
Array(16).fill().forEach((i) => {
user[i] ^= iv[i]
})
const saltedHash = crypto
.createHash('sha1')
.update((ssoKey + accountKey), 'utf-8')
.digest()
.slice(0, 16)
const padLen = 16 - user.length % 16
Array(padLen).fill().forEach((i) => {
user += String.fromCharCode(padLen)
})
const cipher = crypto.createCipheriv(mode, saltedHash, iv)
cipher.setAutoPadding(false)
const token = cipher.update(Buffer.from(user, 'utf-8'), 'utf-8')
const encoded = encodeURIComponent(token.toString('base64'))
return encoded
}