|
| 1 | +const crypto = require('crypto'); |
| 2 | +const { URL } = require('url'); |
| 3 | + |
| 4 | +module.exports = { |
| 5 | + log: { |
| 6 | + level: 'trace', |
| 7 | + supportAnsiColors: true, |
| 8 | + prettyPrint: true, |
| 9 | + }, |
| 10 | + request: { |
| 11 | + https: { |
| 12 | + rejectUnauthorized: false |
| 13 | + } |
| 14 | + }, |
| 15 | + environments: { |
| 16 | + local: { |
| 17 | + host: "https://localhost:7134", |
| 18 | + tokenId: "SampleClient", |
| 19 | + tokenSecret: "ci3JaJZRDQGq6juXVvfp89TnAzS43ASaK/uB38R6ndzr7NN/Wlbstvg+2ZaI2qUVHkvvD3+hPvvzL58Z/bPq6A==", |
| 20 | + } |
| 21 | + }, |
| 22 | + configureHooks: function (api) { |
| 23 | + api.hooks.replaceVariable.addHook('replaceHmacHeader', replaceHmacHeader); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function replaceHmacHeader(text, type, context) { |
| 28 | + if (type.toLowerCase() !== 'authorization') |
| 29 | + return text; |
| 30 | + |
| 31 | + if (!text.startsWith('HMAC')) |
| 32 | + return text; |
| 33 | + |
| 34 | + const { request } = context; |
| 35 | + if (!request) |
| 36 | + return text; |
| 37 | + |
| 38 | + // format: HMAC client:myclient secret:mysecret |
| 39 | + const hmacRegex = /^hmac\s*client\s*[:=]\s*(?<client>\S+)[\s&]+secret\s*[:=]\s*(?<secret>\S+)\s*$/i; |
| 40 | + const match = hmacRegex.exec(text); |
| 41 | + if (!match || !match.groups) |
| 42 | + return text; |
| 43 | + |
| 44 | + const client = match.groups.client; |
| 45 | + const secretKey = match.groups.secret; |
| 46 | + |
| 47 | + const timestamp = Math.floor(Date.now() / 1000); |
| 48 | + |
| 49 | + const url = new URL(request.url); |
| 50 | + const host = url.hostname; |
| 51 | + const pathAndQuery = url.pathname + url.search; |
| 52 | + |
| 53 | + const method = request.method.toUpperCase(); |
| 54 | + const body = request.body ? request.body.toString() : ''; |
| 55 | + |
| 56 | + // Create content hash |
| 57 | + const contentHash = crypto |
| 58 | + .createHash('sha256') |
| 59 | + .update(body, 'utf8') |
| 60 | + .digest('base64'); |
| 61 | + |
| 62 | + // Generate unique nonce |
| 63 | + const nonce = crypto.randomUUID(); |
| 64 | + |
| 65 | + // Create signed headers and string to sign |
| 66 | + const headerValues = `${host};${timestamp};${contentHash};${nonce}`; |
| 67 | + const stringToSign = `${method}\n${pathAndQuery}\n${headerValues}`; |
| 68 | + |
| 69 | + // Generate signature |
| 70 | + const signature = crypto |
| 71 | + .createHmac('sha256', secretKey) |
| 72 | + .update(stringToSign, 'utf8') |
| 73 | + .digest('base64'); |
| 74 | + |
| 75 | + // Construct Authorization header |
| 76 | + const signedHeader = 'host;x-timestamp;x-content-sha256;x-nonce'; |
| 77 | + const authorization = `HMAC Client=${client}&SignedHeaders=${signedHeader}&Signature=${signature}`; |
| 78 | + |
| 79 | + // Set required headers |
| 80 | + request.headers['host'] = host; |
| 81 | + request.headers['x-timestamp'] = timestamp.toString(); |
| 82 | + request.headers['x-content-sha256'] = contentHash; |
| 83 | + request.headers['x-nonce'] = nonce; |
| 84 | + |
| 85 | + console.info('Generated HMAC Authorization Header:', authorization); |
| 86 | + // Return the new Authorization header |
| 87 | + return authorization; |
| 88 | + |
| 89 | +} |
0 commit comments