|
| 1 | +import { createHmac } from 'crypto'; |
| 2 | +import * as urlLib from 'url'; |
| 3 | +import * as sjcl from '@bitgo/sjcl'; |
| 4 | +import { |
| 5 | + CalculateHmacSubjectOptions, |
| 6 | + CalculateRequestHeadersOptions, |
| 7 | + CalculateRequestHmacOptions, |
| 8 | + RequestHeaders, |
| 9 | + VerifyResponseInfo, |
| 10 | + VerifyResponseOptions, |
| 11 | +} from './types'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Calculate the HMAC for the given key and message |
| 15 | + * @param key {String} - the key to use for the HMAC |
| 16 | + * @param message {String} - the actual message to HMAC |
| 17 | + * @returns {*} - the result of the HMAC operation |
| 18 | + */ |
| 19 | +export function calculateHMAC(key: string, message: string): string { |
| 20 | + return createHmac('sha256', key).update(message).digest('hex'); |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Calculate the subject string that is to be HMAC'ed for a HTTP request or response |
| 25 | + * @param urlPath request url, including query params |
| 26 | + * @param text request body text |
| 27 | + * @param timestamp request timestamp from `Date.now()` |
| 28 | + * @param statusCode Only set for HTTP responses, leave blank for requests |
| 29 | + * @param method request method |
| 30 | + * @returns {string} |
| 31 | + */ |
| 32 | +export function calculateHMACSubject({ |
| 33 | + urlPath, |
| 34 | + text, |
| 35 | + timestamp, |
| 36 | + statusCode, |
| 37 | + method, |
| 38 | + authVersion, |
| 39 | +}: CalculateHmacSubjectOptions): string { |
| 40 | + const urlDetails = urlLib.parse(urlPath); |
| 41 | + const queryPath = urlDetails.query && urlDetails.query.length > 0 ? urlDetails.path : urlDetails.pathname; |
| 42 | + if (statusCode !== undefined && isFinite(statusCode) && Number.isInteger(statusCode)) { |
| 43 | + if (authVersion === 3) { |
| 44 | + return [method.toUpperCase(), timestamp, queryPath, statusCode, text].join('|'); |
| 45 | + } |
| 46 | + return [timestamp, queryPath, statusCode, text].join('|'); |
| 47 | + } |
| 48 | + if (authVersion === 3) { |
| 49 | + return [method.toUpperCase(), timestamp, '3.0', queryPath, text].join('|'); |
| 50 | + } |
| 51 | + return [timestamp, queryPath, text].join('|'); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Calculate the HMAC for an HTTP request |
| 56 | + */ |
| 57 | +export function calculateRequestHMAC({ |
| 58 | + url: urlPath, |
| 59 | + text, |
| 60 | + timestamp, |
| 61 | + token, |
| 62 | + method, |
| 63 | + authVersion, |
| 64 | +}: CalculateRequestHmacOptions): string { |
| 65 | + const signatureSubject = calculateHMACSubject({ urlPath, text, timestamp, method, authVersion }); |
| 66 | + |
| 67 | + // calculate the HMAC |
| 68 | + return calculateHMAC(token, signatureSubject); |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Calculate request headers with HMAC |
| 73 | + */ |
| 74 | +export function calculateRequestHeaders({ |
| 75 | + url, |
| 76 | + text, |
| 77 | + token, |
| 78 | + method, |
| 79 | + authVersion, |
| 80 | +}: CalculateRequestHeadersOptions): RequestHeaders { |
| 81 | + const timestamp = Date.now(); |
| 82 | + const hmac = calculateRequestHMAC({ url, text, timestamp, token, method, authVersion }); |
| 83 | + |
| 84 | + // calculate the SHA256 hash of the token |
| 85 | + const hashDigest = sjcl.hash.sha256.hash(token); |
| 86 | + const tokenHash = sjcl.codec.hex.fromBits(hashDigest); |
| 87 | + return { |
| 88 | + hmac, |
| 89 | + timestamp, |
| 90 | + tokenHash, |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Verify the HMAC for an HTTP response |
| 96 | + */ |
| 97 | +export function verifyResponse({ |
| 98 | + url: urlPath, |
| 99 | + statusCode, |
| 100 | + text, |
| 101 | + timestamp, |
| 102 | + token, |
| 103 | + hmac, |
| 104 | + method, |
| 105 | + authVersion, |
| 106 | +}: VerifyResponseOptions): VerifyResponseInfo { |
| 107 | + const signatureSubject = calculateHMACSubject({ |
| 108 | + urlPath, |
| 109 | + text, |
| 110 | + timestamp, |
| 111 | + statusCode, |
| 112 | + method, |
| 113 | + authVersion, |
| 114 | + }); |
| 115 | + |
| 116 | + // calculate the HMAC |
| 117 | + const expectedHmac = calculateHMAC(token, signatureSubject); |
| 118 | + |
| 119 | + // determine if the response is still within the validity window (5 minute window) |
| 120 | + const now = Date.now(); |
| 121 | + const isInResponseValidityWindow = timestamp >= now - 1000 * 60 * 5 && timestamp <= now; |
| 122 | + |
| 123 | + // verify the HMAC and timestamp |
| 124 | + return { |
| 125 | + isValid: expectedHmac === hmac, |
| 126 | + expectedHmac, |
| 127 | + signatureSubject, |
| 128 | + isInResponseValidityWindow, |
| 129 | + verificationTime: now, |
| 130 | + }; |
| 131 | +} |
0 commit comments