diff --git a/CHANGELOG.md b/CHANGELOG.md index c36915b..d97332b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # RELEASE NOTES +## 3.4.5 (Apr 3, 2024) + +#### BUG FIXES +* Fixed a bug where the `max_body` parameter was not utilized when generating the authentication header. +* Implemented support for the `max_body` parameter when the configuration is provided as function parameter. + +#### IMPROVEMENTS: +* Updated various dependencies + ## 3.4.4 (Nov 15, 2023) #### IMPROVEMENTS: diff --git a/index.d.ts b/index.d.ts index ab4fe0f..16830ef 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,10 +2,11 @@ import { AxiosError, AxiosResponse } from "axios"; declare class EdgeGrid { constructor(clientTokenOrOptions: string | object, - clientSecret?: string, - accessToken?: string, - host?: string, - debug?: boolean); + clientSecret?: string, + accessToken?: string, + host?: string, + debug?: boolean, + max_body?: number); request: object; config: object; diff --git a/package-lock.json b/package-lock.json index 16f5d57..2a07a2e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "akamai-edgegrid", - "version": "3.4.4", + "version": "3.4.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "akamai-edgegrid", - "version": "3.4.4", + "version": "3.4.5", "license": "Apache-2.0", "dependencies": { "axios": "^1.1.2", diff --git a/package.json b/package.json index f7706d3..7c61828 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "akamai-edgegrid", - "version": "3.4.4", + "version": "3.4.5", "description": "Authentication handler for the Akamai OPEN EdgeGrid Authentication scheme in Node.js", "main": "index.js", "scripts": { diff --git a/src/api.js b/src/api.js index 36ec1ae..e3179f7 100644 --- a/src/api.js +++ b/src/api.js @@ -18,13 +18,13 @@ const axios = require('axios'), helpers = require('./helpers'), logger = require('./logger'); -const EdgeGrid = function (client_token, client_secret, access_token, host, debug) { +const EdgeGrid = function (client_token, client_secret, access_token, host, debug, max_body) { // accepting an object containing a path to .edgerc and a config section if (typeof arguments[0] === 'object') { let edgercPath = arguments[0]; this._setConfigFromObj(edgercPath); } else { - this._setConfigFromStrings(client_token, client_secret, access_token, host); + this._setConfigFromStrings(client_token, client_secret, access_token, host, max_body); } if (process.env.EG_VERBOSE || debug || (typeof arguments[0] === 'object' && arguments[0].debug)) { axios.interceptors.request.use(request => { @@ -75,7 +75,8 @@ EdgeGrid.prototype.auth = function (req) { this.config.client_token, this.config.client_secret, this.config.access_token, - this.config.host + this.config.host, + this.config.max_body ); if (req.headers['Accept'] === 'application/gzip' || req.headers['Accept'] === 'application/tar+gzip') { this.request["responseType"] = 'arraybuffer'; @@ -123,7 +124,7 @@ EdgeGrid.prototype._handleRedirect = function (resp, callback) { * @param {String} access_token The access token * @param {String} host The host */ -EdgeGrid.prototype._setConfigFromStrings = function (client_token, client_secret, access_token, host) { +EdgeGrid.prototype._setConfigFromStrings = function (client_token, client_secret, access_token, host, max_body) { if (!validatedArgs([client_token, client_secret, access_token, host])) { throw new Error('Insufficient Akamai credentials'); } @@ -132,7 +133,8 @@ EdgeGrid.prototype._setConfigFromStrings = function (client_token, client_secret client_token: client_token, client_secret: client_secret, access_token: access_token, - host: host.indexOf('https://') > -1 ? host : 'https://' + host + host: host.indexOf('https://') > -1 ? host : 'https://' + host, + max_body: helpers.getDefaultOrMaxBody(max_body) }; }; diff --git a/src/edgerc.js b/src/edgerc.js index d127150..2829c21 100644 --- a/src/edgerc.js +++ b/src/edgerc.js @@ -40,8 +40,7 @@ function getSection(lines, sectionName) { } function validatedConfig(config) { - - config.max_body = config.max_body || 131072 + config.max_body = helpers.getDefaultOrMaxBody(config.max_body); if (!(config.host && config.access_token && config.client_secret && config.client_token)) { @@ -81,7 +80,7 @@ function buildObj(configs) { if (index > -1 && !isComment) { key = config.substr(0, index); if (key.startsWith("max-body")) { - key = key.replace('-', '_') + key = key.replace('-', '_'); } val = config.substring(index + 1); // remove inline comments diff --git a/src/helpers.js b/src/helpers.js index 784ae6b..3240951 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -198,4 +198,11 @@ module.exports = { } return filePath; }, + + getDefaultOrMaxBody:function(max_body){ + if (max_body !== undefined && isNaN(Number(max_body))) { + throw new Error('max_body is not a valid number.'); + } + return Number(max_body) || 131072; + } }; diff --git a/test/src/api_test.js b/test/src/api_test.js index 4253825..3b17914 100644 --- a/test/src/api_test.js +++ b/test/src/api_test.js @@ -17,6 +17,8 @@ const assert = require('assert'), path = require('path'), Api = require('../../src/api'); +const EdgeGrid = require("../../index"); + describe('Api', function () { beforeEach(function () { this.api = new Api( @@ -24,7 +26,7 @@ describe('Api', function () { 'clientSecret', 'accessToken', 'base.com', - false, + false ); }); @@ -566,5 +568,105 @@ describe('Api', function () { }); }); }); + + describe('Builds the request using the properties of the local config Object (.edgerc file)', () => { + it('when max_body is provided in the config', () => { + const req = { + path: '/api/resource', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + body: {key: 'value'} + }; + + const edgercObject = { + path: path.resolve(__dirname, '../test_edgerc'), + section: 'custom:max_body' + }; + + const edgeGrid = new EdgeGrid(edgercObject); + // Call auth method + const response = edgeGrid.auth(req); + // Assertions + assert.strictEqual(response.config.max_body, 8192); + }); + + it('when max_body is provided in the config - NAN', () => { + const edgercObject = { + path: path.resolve(__dirname, '../test_edgerc'), + section: 'custom:max_body_NAN' + }; + assert.throws( + function () { + return new EdgeGrid(edgercObject); + }, + /max_body is not a valid number./ + ); + }); + + it('when max_body is not provided in the configuration, the default value is used', () => { + const req = { + path: '/api/resource', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + body: {key: 'value'} + }; + + const edgercObject = { + path: path.resolve(__dirname, '../test_edgerc'), + section: 'no-max-body' + }; + + const edgeGrid = new EdgeGrid(edgercObject, undefined, undefined, undefined, undefined, 1000); + // Call auth method + const response = edgeGrid.auth(req); + // Assertions + assert.strictEqual(response.config.max_body, 131072); // picks default max_body 131072 + }); + }); + + describe('Builds the request using the config as string parameters', () => { + it('when max_body is provided in the config', () => { + const req = { + path: '/api/resource', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + body: {key: 'value'} + }; + + const edgeGrid = new EdgeGrid('clientToken', 'clientSecret', 'accessToken', 'example.com', false, 8192); + // Call auth method + const response = edgeGrid.auth(req); + // Assertions + assert.strictEqual(response.config.max_body, 8192); + }); + + + it('when max_body is not provided in the config', () => { + const req = { + path: '/api/resource', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + body: {key: 'value'} + }; + + const edgeGrid = new EdgeGrid('clientToken', 'clientSecret', 'accessToken', 'example.com'); + // Call auth method + const response = edgeGrid.auth(req); + // Assertions + assert.strictEqual(response.config.max_body, 131072); + }); + }); }); }); diff --git a/test/test.js b/test/test.js index 3cbdc94..d822133 100644 --- a/test/test.js +++ b/test/test.js @@ -201,4 +201,18 @@ describe('Signature Generation', function () { assert.strictEqual(test_auth.headers.Authorization, expected_header); }); }); + + describe('POST request when max_body is provided in .edgerc file', function () { + it('should return the expected string when the signing request is run.', function () { + const expected_header = "EG1-HMAC-SHA256 client_token=akab-client-token-xxx-xxxxxxxxxxxxxxxx;access_token=akab-access-token-xxx-xxxxxxxxxxxxxxxx;timestamp=20140321T19:34:21+0000;nonce=nonce-xx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;signature=lUcTuRl/Iy5vmBp7uNvya9BoRaA9/oyHKC+pCDOlg1s="; + const data = "{\"name\":\"text24.devexp-cli-dns-test.net\",\"type\":\"SRV\",\"ttl\":300,\"zone\":\"devexp-cli-dns-test.net\",\"rdata\":[\"10 40 5061 small.example.com\",\"20 10 5060 tiny.example.com\"]}"; + const request = { + "path": "testapi/v1/t3", + "method": "POST", + "body": data + }; + test_auth = auth.generateAuth(request, client_token, client_secret, access_token, base_url, 169, nonce, timestamp); + assert.strictEqual(test_auth.headers.Authorization, expected_header); + }); + }); }); diff --git a/test/test_edgerc b/test/test_edgerc index 4b992a1..f42ada3 100644 --- a/test/test_edgerc +++ b/test/test_edgerc @@ -26,6 +26,13 @@ client_secret = sectionClientSecret access_token = sectionAccessToken max_body = 8192 +[custom:max_body_NAN] +host = sectionexample.luna.akamaiapis.net +client_token = sectionClientToken +client_secret = sectionClientSecret +access_token = sectionAccessToken +max_body = 8A192 + [no-max-body] host = sectionexample.luna.akamaiapis.net client_token = sectionClientToken