Skip to content

Commit

Permalink
[DXEX-455] Allow custom headers to be set in Management/Auth Clients.
Browse files Browse the repository at this point in the history
In order to better determine what version of the Deploy CLI tool is being used
by our customers, we would like to include the Deploy CLI's version in the
User-agent header as opposed to the current behavior (the version of node under
which the CLI is running).

The Deploy CLI uses ManagementClient to do its work, but the User-agent header
was not configurable before this change. This commit provides a new option
called `headers` that will allow the caller to provide an optional set of
additional headers to outgoing requests.

In the case of the `User-agent` header, if it is not provided by the caller,
the original User-agent string will be used (e.g. `node.js/13.1.2`).
  • Loading branch information
seejee authored and lbalmaceda committed Feb 21, 2020
1 parent 035c4c2 commit 2075875
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var util = require('util');
var utils = require('../utils');
var jsonToBase64 = utils.jsonToBase64;
var ArgumentError = require('rest-facade').ArgumentError;
var assign = Object.assign || require('object.assign');

// Authenticators.
var OAuthAuthenticator = require('./OAuthAuthenticator');
Expand Down Expand Up @@ -43,6 +44,7 @@ var BASE_URL_FORMAT = 'https://%s';
* @param {String} [options.clientSecret] Default client Secret.
* @param {String} [options.supportedAlgorithms] Algorithms that your application expects to receive
* @param {Boolean} [options.__bypassIdTokenValidation] Whether the id_token should be validated or not
* @param {Object} [options.headers] Additional headers that will be added to the outgoing requests.
*/
var AuthenticationClient = function(options) {
if (!options || typeof options !== 'object') {
Expand All @@ -53,14 +55,16 @@ var AuthenticationClient = function(options) {
throw new ArgumentError('Must provide a domain');
}

var defaultHeaders = {
'User-agent': 'node.js/' + process.version.replace('v', ''),
'Content-Type': 'application/json'
};

var managerOptions = {
clientId: options.clientId,
domain: options.domain,
clientSecret: options.clientSecret,
headers: {
'User-agent': 'node.js/' + process.version.replace('v', ''),
'Content-Type': 'application/json'
},
headers: assign(defaultHeaders, options.headers || {}),
baseUrl: util.format(BASE_URL_FORMAT, options.domain),
supportedAlgorithms: options.supportedAlgorithms,
__bypassIdTokenValidation: options.__bypassIdTokenValidation
Expand Down
13 changes: 9 additions & 4 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var MANAGEMENT_API_AUD_FORMAT = 'https://%s/api/v2/';
* @param {Number} [options.tokenProvider.cacheTTLInSeconds] By default the `expires_in` value will be used to determine the cached time of the token, this can be overridden.
* @param {Boolean} [options.retry.enabled=true] Enabled or Disable Retry Policy functionality.
* @param {Number} [options.retry.maxRetries=10] Retry failed requests X times.
* @param {Object} [options.headers] Additional headers that will be added to the outgoing requests.
*
*/
var ManagementClient = function(options) {
Expand All @@ -102,11 +103,15 @@ var ManagementClient = function(options) {
}

var baseUrl = util.format(BASE_URL_FORMAT, options.domain);
var userAgent = options.userAgent || 'node.js/' + process.version.replace('v', '');

var defaultHeaders = {
'User-agent': 'node.js/' + process.version.replace('v', ''),
'Content-Type': 'application/json'
};

var managerOptions = {
headers: {
'User-agent': 'node.js/' + process.version.replace('v', ''),
'Content-Type': 'application/json'
},
headers: assign(defaultHeaders, options.headers || {}),
baseUrl: baseUrl
};

Expand Down
36 changes: 36 additions & 0 deletions test/auth/authentication-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,42 @@ describe('AuthenticationClient', function() {
});
});

describe('user agent', function() {
it('should use the node version when the user agent option is not provided', function() {
var client = new AuthenticationClient({
token: 'token',
domain: 'auth0.com'
});

var expected = { 'User-agent': 'node.js/' + process.version.replace('v', '') };

expect(client.oauth.oauth.options.headers).to.contain(expected);
expect(client.database.dbConnections.options.headers).to.contain(expected);
expect(client.passwordless.passwordless.options.headers).to.contain(expected);
expect(client.users.headers).to.contain(expected);
expect(client.tokens.headers).to.contain(expected);
});

it('should include additional headers when provided', function() {
var customHeaders = {
'User-agent': 'my-user-agent',
'Another-header': 'test-header'
};

var client = new AuthenticationClient({
token: 'token',
domain: 'auth0.com',
headers: customHeaders
});

expect(client.oauth.oauth.options.headers).to.contain(customHeaders);
expect(client.database.dbConnections.options.headers).to.contain(customHeaders);
expect(client.passwordless.passwordless.options.headers).to.contain(customHeaders);
expect(client.users.headers).to.contain(customHeaders);
expect(client.tokens.headers).to.contain(customHeaders);
});
});

describe('instance methods', function() {
var methods = [];
var client = new AuthenticationClient({ token: 'token', domain: 'auth0.com' });
Expand Down
33 changes: 33 additions & 0 deletions test/management/management-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,39 @@ describe('ManagementClient', function() {
}
};

describe('user agent', function() {
for (var name in managers) {
manager = managers[name];

it(manager + ' should use the node version by default', function() {
var client = new ManagementClient(withTokenConfig);

expect(
client[manager.property].resource.restClient.restClient.options.headers
).to.contain({
'User-agent': 'node.js/' + process.version.replace('v', '')
});
});

it(manager + ' should include additional headers when provided', function() {
var customHeaders = {
'User-agent': 'my-user-agent',
'Another-header': 'test-header'
};

var options = assign({ headers: customHeaders }, withTokenConfig);
var client = new ManagementClient(options);

expect(
client[manager.property].resource.restClient.restClient.options.headers
).to.contain({
'User-agent': 'my-user-agent',
'Another-header': 'test-header'
});
});
}
});

describe('client info', function() {
it('should configure instances with default telemetry header', function() {
var utilsStub = {
Expand Down

0 comments on commit 2075875

Please sign in to comment.