Skip to content

Commit 3b2569c

Browse files
util: in auth.js get rid of class, add createSocketAuthHash fn.
1 parent e21a05b commit 3b2569c

File tree

1 file changed

+59
-48
lines changed

1 file changed

+59
-48
lines changed

lib/util/auth.js

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,64 @@
88

99
const crypto = require('crypto')
1010

11-
class AuthUtil {
12-
/**
13-
* Creates keyed-hash message authentication code (HMAC).
14-
* Used core `crypto` module cryptographic hash function.
15-
* Secret key - sha512.
16-
* @param {string} text
17-
* @param {string} key
18-
* @returns {string}
19-
*/
20-
static createHMAC (text, key) {
21-
return crypto.createHmac('sha512', key).update(text).digest('hex')
22-
}
23-
24-
/**
25-
* Generates HMAC based on source and key.
26-
* Source is defined as combination of clientId, path, qs, body, nonce and timestamp respectively.
27-
* @param {{clientId, qs, path, body, nonce, timestamp}} options
28-
* @param {string} key
29-
* @returns {string}
30-
*/
31-
static generateHash (options, key) {
32-
const clientId = options.clientId
33-
const qs = options.qs
34-
const path = options.path
35-
const body = options.body
36-
const nonce = options.nonce
37-
const timestamp = options.timestamp
38-
const hashSource = `${clientId}${path}${qs}${body}${nonce}${timestamp}`
39-
40-
return AuthUtil.createHMAC(hashSource, key)
41-
}
42-
43-
/**
44-
* Generates nonce.
45-
* Creates timestamp
46-
* Gets the last 6 chars of the timestamp
47-
* Generates random number between 10-99
48-
* Combined the last two ones.
49-
* @returns {string}
50-
*/
51-
static generateNonce () {
52-
const timestamp = Date.now().toString()
53-
const str = timestamp.substring(timestamp.length - 6)
54-
const suffix = Math.floor(Math.random() * 90 + 9)
55-
56-
return `${str}${suffix}`
57-
}
11+
/**
12+
* Creates keyed-hash message authentication code (HMAC).
13+
* Used core `crypto` module cryptographic hash function.
14+
* Secret key - sha512.
15+
* @param {string} text
16+
* @param {string} key
17+
* @returns {string}
18+
*/
19+
const createHMAC = (text, key) => {
20+
return crypto.createHmac('sha512', key).update(text).digest('hex')
21+
}
22+
23+
/**
24+
* Generates HMAC based on source and key.
25+
* Source is defined as combination of clientId, path, qs, body, nonce and timestamp respectively.
26+
* @param {{clientId, qs, path, body, nonce, timestamp}} options
27+
* @param {string} key
28+
* @returns {string}
29+
*/
30+
const generateHash = (options, key) => {
31+
const { clientId, qs, path, body, nonce, timestamp } = options
32+
const hashSource = `${clientId}${path}${qs}${body}${nonce}${timestamp}`
33+
34+
return createHMAC(hashSource, key)
35+
}
36+
37+
/**
38+
* Generates nonce.
39+
* Creates timestamp
40+
* Gets the last 6 chars of the timestamp
41+
* Generates random number between 10-99
42+
* Combined the last two ones.
43+
* @returns {string}
44+
*/
45+
const generateNonce = () => {
46+
const timestamp = Date.now().toString()
47+
const str = timestamp.substring(timestamp.length - 6)
48+
const suffix = Math.floor(Math.random() * 90 + 9)
49+
50+
return `${str}${suffix}`
5851
}
5952

60-
module.exports = AuthUtil
53+
/**
54+
* Generates HMAC based on source and key.
55+
* Source is defined as combination of clientId, originalUrl, timestamp, signKey respectively.
56+
* @param {{clientId, originalUrl, timestamp, signKey}} options
57+
* @param {string} key
58+
* @return {string}
59+
*/
60+
const createSocketAuthHash = (options, key) => {
61+
const { clientId, originalUrl, timestamp } = options
62+
const hashSource = `${clientId}${originalUrl}${timestamp}`
63+
64+
return createHMAC(hashSource, key)
65+
}
66+
67+
module.exports = {
68+
createSocketAuthHash,
69+
generateHash,
70+
generateNonce
71+
}

0 commit comments

Comments
 (0)