-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgen_auth_sig.js
42 lines (36 loc) · 944 Bytes
/
gen_auth_sig.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
'use strict'
const crypto = require('crypto')
const getNonce = require('./nonce')
/**
* Authorization signature for WS & REST APIs.
*
* @typedef {object} AuthSignature
* @property {string} payload - request payload, default
* 'AUTH${nonce}${nonce}''
* @property {string} sig - signature in hexadecimal
* @property {number} nonce - used nonce
*/
/**
* Generates an auth signature, payload, and nonce for passing to the WS & REST
* APIs
*
* @param {string} secret - API secret
* @param {string} [payload=''] - signature payload, generated by default
* @returns {AuthSignature} authSignature
*/
const genAuthSig = (secret, payload = '') => {
const nonce = getNonce()
if (payload.length === 0) {
payload = `AUTH${nonce}${nonce}`
}
const sig = crypto
.createHmac('sha384', secret)
.update(payload)
.digest('hex')
return {
payload,
sig,
nonce: getNonce()
}
}
module.exports = genAuthSig