|
8 | 8 |
|
9 | 9 | const crypto = require('crypto') |
10 | 10 |
|
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}` |
58 | 51 | } |
59 | 52 |
|
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