diff --git a/src/auth/index.js b/src/auth/index.js index 2280b42b7..8ee5d0903 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -1,8 +1,6 @@ /** @module auth **/ var util = require('util'); - -var pkg = require('../../package.json'); var utils = require('../utils'); var jsonToBase64 = utils.jsonToBase64; var ArgumentError = require('rest-facade').ArgumentError; @@ -67,8 +65,11 @@ var AuthenticationClient = function(options) { }; if (options.telemetry !== false) { - var telemetry = jsonToBase64(options.clientInfo || this.getClientInfo()); - managerOptions.headers['Auth0-Client'] = telemetry; + var clientInfo = options.clientInfo || utils.generateClientInfo(); + if ('string' === typeof clientInfo.name && clientInfo.name.length > 0) { + var telemetry = jsonToBase64(clientInfo); + managerOptions.headers['Auth0-Client'] = telemetry; + } } /** @@ -107,24 +108,6 @@ var AuthenticationClient = function(options) { this.tokens = new TokensManager(managerOptions); }; -/** - * Return an object with information about the current client, - * - * @method getClientInfo - * @memberOf module:auth.AuthenticationClient.prototype - * - * @return {Object} Object containing client information. - */ -AuthenticationClient.prototype.getClientInfo = function() { - return { - name: 'node-auth0', - version: pkg.version, - env: { - node: process.version.replace('v', '') - } - }; -}; - /** * Start passwordless flow sending an email. * diff --git a/src/management/index.js b/src/management/index.js index c5ceabb54..33a544ccc 100644 --- a/src/management/index.js +++ b/src/management/index.js @@ -1,10 +1,9 @@ /** @module management */ var util = require('util'); - -var pkg = require('../../package.json'); var utils = require('../utils'); var jsonToBase64 = utils.jsonToBase64; +var generateClientInfo = utils.generateClientInfo; var ArgumentError = require('rest-facade').ArgumentError; var assign = Object.assign || require('object.assign'); @@ -129,8 +128,11 @@ var ManagementClient = function(options) { } if (options.telemetry !== false) { - var telemetry = jsonToBase64(options.clientInfo || this.getClientInfo()); - managerOptions.headers['Auth0-Client'] = telemetry; + var clientInfo = options.clientInfo || generateClientInfo(); + if ('string' === typeof clientInfo.name && clientInfo.name.length > 0) { + var telemetry = jsonToBase64(clientInfo); + managerOptions.headers['Auth0-Client'] = telemetry; + } } managerOptions.retry = options.retry; @@ -290,24 +292,6 @@ var ManagementClient = function(options) { this.roles = new RolesManager(managerOptions); }; -/** - * Return an object with information about the current client, - * - * @method getClientInfo - * @memberOf module:management.ManagementClient.prototype - * - * @return {Object} Object containing client information. - */ -ManagementClient.prototype.getClientInfo = function() { - return { - name: 'node-auth0', - version: pkg.version, - env: { - node: process.version.replace('v', '') - } - }; -}; - /** * Get all connections. * diff --git a/src/utils.js b/src/utils.js index 8ba259f61..e0e934647 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,6 @@ var Promise = require('bluebird'); var request = require('request'); +var pkg = require('../package.json'); /** * @module utils @@ -22,6 +23,24 @@ utils.jsonToBase64 = function(json) { .replace(/=+$/, ''); }; +/** + * Return an object with information about the current client, + * + * @method generateClientInfo + * @memberOf module:utils + * + * @return {Object} Object containing client information. + */ +utils.generateClientInfo = function() { + return { + name: 'node-auth0', + version: pkg.version, + env: { + node: process.version.replace('v', '') + } + }; +}; + /** * Simple wrapper that, given a class, a property name and a method name, * creates a new method in the class that is a wrapper for the given diff --git a/test/auth/authentication-client.tests.js b/test/auth/authentication-client.tests.js index af1727463..7cb62e23f 100644 --- a/test/auth/authentication-client.tests.js +++ b/test/auth/authentication-client.tests.js @@ -1,5 +1,6 @@ var expect = require('chai').expect; var sinon = require('sinon'); +var proxyquire = require('proxyquire'); var ArgumentError = require('rest-facade').ArgumentError; @@ -69,22 +70,17 @@ describe('AuthenticationClient', function() { }); describe('client info', function() { - it('should generate default telemetry value', function() { - var client = new AuthenticationClient({ token: 'token', domain: 'auth0.com' }); - var pkgVersion = require('../../package.json').version; - var nodeVersion = process.version.replace('v', ''); - expect(client.getClientInfo()).to.deep.equal({ - name: 'node-auth0', - version: pkgVersion, - env: { node: nodeVersion } - }); - }); - it('should configure instances with default telemetry header', function() { - sinon.stub(AuthenticationClient.prototype, 'getClientInfo', function() { - return { name: 'test-sdk', version: 'ver-123' }; + var utilsStub = { + generateClientInfo: sinon.spy(function() { + return { name: 'test-sdk', version: 'ver-123' }; + }) + }; + var AuthenticationClientProxy = proxyquire('../../src/auth/', { + '../utils': utilsStub }); - var client = new AuthenticationClient({ token: 'token', domain: 'auth0.com' }); + + var client = new AuthenticationClientProxy({ token: 'token', domain: 'auth0.com' }); var requestHeaders = { 'Auth0-Client': 'eyJuYW1lIjoidGVzdC1zZGsiLCJ2ZXJzaW9uIjoidmVyLTEyMyJ9', @@ -95,8 +91,6 @@ describe('AuthenticationClient', function() { expect(client.passwordless.passwordless.options.headers).to.contain(requestHeaders); expect(client.users.headers).to.contain(requestHeaders); expect(client.tokens.headers).to.contain(requestHeaders); - - AuthenticationClient.prototype.getClientInfo.restore(); }); it('should configure instances with custom telemetry header', function() { @@ -119,6 +113,21 @@ describe('AuthenticationClient', function() { expect(client.tokens.headers).to.contain(requestHeaders); }); + it('should configure instances without telemetry when "name" property is empty', function() { + var customTelemetry = { name: '', version: 'beta-01', env: { node: 'v10' } }; + var client = new AuthenticationClient({ + token: 'token', + domain: 'auth0.com', + clientInfo: customTelemetry + }); + + expect(client.oauth.oauth.options.headers).to.not.have.property('Auth0-Client'); + expect(client.database.dbConnections.options.headers).to.not.have.property('Auth0-Client'); + expect(client.passwordless.passwordless.options.headers).to.not.have.property('Auth0-Client'); + expect(client.users.headers).to.not.have.property('Auth0-Client'); + expect(client.tokens.headers).to.not.have.property('Auth0-Client'); + }); + it('should configure instances without telemetry header when disabled', function() { var client = new AuthenticationClient({ token: 'token', diff --git a/test/management/management-client.tests.js b/test/management/management-client.tests.js index 1dc2b2424..4b0f92fdd 100644 --- a/test/management/management-client.tests.js +++ b/test/management/management-client.tests.js @@ -1,6 +1,7 @@ var expect = require('chai').expect; var sinon = require('sinon'); var assign = Object.assign || require('object.assign'); +var proxyquire = require('proxyquire'); var ManagementClient = require('../../src/management'); @@ -17,7 +18,6 @@ var JobsManager = require('../../src/management/JobsManager'); var RulesManager = require('../../src/management/RulesManager'); var StatsManager = require('../../src/management/StatsManager'); var RulesConfigsManager = require('../../src/management/RulesConfigsManager'); - var TenantManager = require('../../src/management/TenantManager'); describe('ManagementClient', function() { @@ -152,22 +152,16 @@ describe('ManagementClient', function() { }; describe('client info', function() { - it('should generate default telemetry value', function() { - var client = new ManagementClient(withTokenConfig); - var pkgVersion = require('../../package.json').version; - var nodeVersion = process.version.replace('v', ''); - expect(client.getClientInfo()).to.deep.equal({ - name: 'node-auth0', - version: pkgVersion, - env: { node: nodeVersion } - }); - }); - it('should configure instances with default telemetry header', function() { - sinon.stub(ManagementClient.prototype, 'getClientInfo', function() { - return { name: 'test-sdk', version: 'ver-123' }; + var utilsStub = { + generateClientInfo: sinon.spy(function() { + return { name: 'test-sdk', version: 'ver-123' }; + }) + }; + var ManagementClientProxy = proxyquire('../../src/management/', { + '../utils': utilsStub }); - var client = new ManagementClient(withTokenConfig); + var client = new ManagementClientProxy(withTokenConfig); var requestHeaders = { 'Auth0-Client': 'eyJuYW1lIjoidGVzdC1zZGsiLCJ2ZXJzaW9uIjoidmVyLTEyMyJ9', @@ -293,8 +287,6 @@ describe('ManagementClient', function() { requestHeaders ); expect(client.roles.users.restClient.restClient.options.headers).to.contain(requestHeaders); - - ManagementClient.prototype.getClientInfo.restore(); }); it('should configure instances with custom telemetry header', function() { @@ -432,6 +424,143 @@ describe('ManagementClient', function() { expect(client.roles.users.restClient.restClient.options.headers).to.contain(requestHeaders); }); + it('should configure instances without telemetry when "name" property is empty', function() { + var customTelemetry = { name: '', version: 'beta-01', env: { node: 'v10' } }; + var client = new ManagementClient({ + token: 'token', + domain: 'auth0.com', + clientInfo: customTelemetry + }); + + expect(client.clients.resource.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + + expect( + client.clientGrants.resource.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + + expect(client.grants.resource.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + + expect(client.users.users.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect(client.users.multifactor.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect(client.users.identities.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect(client.users.userLogs.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect(client.users.enrollments.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect( + client.users.usersByEmail.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + expect( + client.users.recoveryCodeRegenerations.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + expect(client.users.roles.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect(client.users.permissions.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + + expect( + client.guardian.enrollments.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + expect(client.guardian.tickets.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect(client.guardian.factors.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect( + client.guardian.factorsTemplates.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + expect( + client.guardian.factorsProviders.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + + expect( + client.customDomains.resource.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + expect( + client.customDomains.vefifyResource.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + + expect( + client.connections.resource.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + + expect( + client.deviceCredentials.resource.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + + expect(client.rules.resource.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + + expect( + client.blacklistedTokens.resource.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + + expect( + client.emailProvider.resource.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + + expect(client.stats.resource.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + + expect(client.tenant.resource.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + + expect(client.jobs.jobs.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect(client.jobs.usersExports.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + + expect(client.tickets.resource.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + + expect(client.logs.resource.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + + expect( + client.resourceServers.resource.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + + expect( + client.emailTemplates.resource.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + + expect( + client.rulesConfigs.resource.restClient.restClient.options.headers + ).to.not.have.property('Auth0-Client'); + + expect(client.roles.resource.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect(client.roles.permissions.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + expect(client.roles.users.restClient.restClient.options.headers).to.not.have.property( + 'Auth0-Client' + ); + }); + it('should configure instances without telemetry header when disabled', function() { var client = new ManagementClient({ token: 'token', diff --git a/test/utils.tests.js b/test/utils.tests.js new file mode 100644 index 000000000..919b8ae01 --- /dev/null +++ b/test/utils.tests.js @@ -0,0 +1,16 @@ +var expect = require('chai').expect; +var utils = require('../src/utils.js'); + +describe('Utils', function() { + describe('client info', function() { + it('should generate default telemetry value', function() { + var pkgVersion = require('../package.json').version; + var nodeVersion = process.version.replace('v', ''); + expect(utils.generateClientInfo()).to.deep.equal({ + name: 'node-auth0', + version: pkgVersion, + env: { node: nodeVersion } + }); + }); + }); +});