UNPKG

connect-sdk-nodejs

Version:

SDK to communicate with the Worldline Global Collect platform using the Worldline Connect Server API

42 lines 1.47 kB
"use strict"; // V1HMAC Object.defineProperty(exports, "__esModule", { value: true }); exports.V1HMACAuthenticator = exports.getV1HMACSignature = void 0; const crypto = require("crypto"); function compareHeaders(a, b) { const keyA = a.key.toUpperCase(); const keyB = b.key.toUpperCase(); if (keyA < keyB) { return -1; } else if (keyA > keyB) { return 1; } else { return 0; } } function getV1HMACSignature(method, contentType, date, headers, path, secretApiKey) { const sortedHeaders = headers .filter(header => header.key.toUpperCase().indexOf("X-GCS") === 0) .sort(compareHeaders) .map(header => `${header.key.toLowerCase()}:${header.value}\n`) .join(""); return crypto .createHmac("SHA256", secretApiKey) .update(`${method}\n${contentType}\n${date}\n${sortedHeaders}${path}\n`) .digest("base64"); } exports.getV1HMACSignature = getV1HMACSignature; class V1HMACAuthenticator { constructor(apiKeyId, secretApiKey) { this.apiKeyId = apiKeyId; this.secretApiKey = secretApiKey; } getAuthorization(method, contentType, date, headers, path) { const signature = getV1HMACSignature(method, contentType, date, headers, path, this.secretApiKey); return Promise.resolve(`GCS v1HMAC:${this.apiKeyId}:${signature}`); } } exports.V1HMACAuthenticator = V1HMACAuthenticator; //# sourceMappingURL=authentication.js.map