forked from ceejbot/recurring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigner.js
59 lines (48 loc) · 1.13 KB
/
signer.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
'use strict'
const crypto = require('crypto')
const qs = require('qs')
const uuid = require('uuid')
class SignedQuery {
constructor(key) {
this.params = { }
this.key = key
}
serialize() {
if (!this.qs) {
if (!this.params.nonce) {
this.params.nonce = uuid.v4()
}
if (!this.params.timestamp) {
this.params.timestamp = Math.ceil(Date.now() / 1000)
}
// alphabetize keys
const tmp = { }
const keys = Object.keys(this.params).sort()
for (let i = 0; i < keys.length; i++) {
tmp[keys[i]] = this.params[keys[i]]
}
this.params = tmp
this.qs = decodeURI(qs.stringify(this.params))
}
return this.qs
}
set(key, value) {
this.qs = null
if ((typeof key === 'object') && !value) {
this.params = key
}
else {
this.params[key] = value
}
}
HMAC(data) {
const hmac = crypto.createHmac('sha1', this.key)
hmac.update(data)
return hmac.digest('hex')
}
toString() {
const query = encodeURI(this.serialize())
return `${this.HMAC(query)}|${query}`
}
}
exports.SignedQuery = SignedQuery