Skip to content

Commit

Permalink
Merge pull request #362 from auth0/update-telemetry
Browse files Browse the repository at this point in the history
Fix telemetry header
  • Loading branch information
Luciano Balmaceda authored May 11, 2019
2 parents 5b10393 + ff39d25 commit 699f285
Show file tree
Hide file tree
Showing 14 changed files with 705 additions and 100 deletions.
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"version": "2.17.0",
"description": "SDK for Auth0 API v2",
"main": "src/index.js",
"files": ["src"],
"files": [
"src"
],
"scripts": {
"test": "mocha -R spec ./test/**/*.tests.js ./test/*.tests.js",
"test:ci":
"istanbul cover _mocha --report lcovonly -R $(find ./test -name *.tests.js) -- -R mocha-multi --reporter-options spec=-,mocha-junit-reporter=-",
"test:ci": "istanbul cover _mocha --report lcovonly -R $(find ./test -name *.tests.js) -- -R mocha-multi --reporter-options spec=-,mocha-junit-reporter=-",
"test:coverage": "codecov",
"test:watch":
"cross-env NODE_ENV=test mocha --timeout 5000 ./test/**/*.tests.js ./test/*.tests.js --watch",
"test:watch": "cross-env NODE_ENV=test mocha --timeout 5000 ./test/**/*.tests.js ./test/*.tests.js --watch",
"jsdoc:generate": "jsdoc --configure .jsdoc.json --verbose",
"release:clean": "node scripts/cleanup.js",
"preversion": "node scripts/prepare.js",
Expand All @@ -22,7 +22,10 @@
"type": "git",
"url": "https://github.com/auth0/node-auth0"
},
"keywords": ["auth0", "api"],
"keywords": [
"auth0",
"api"
],
"author": "Auth0",
"license": "MIT",
"bugs": {
Expand Down
3 changes: 2 additions & 1 deletion src/auth/DatabaseAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var DatabaseAuthenticator = function(options, oauth) {
* @type {Object}
*/
var clientOptions = {
errorFormatter: { message: 'message', name: 'error' }
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers
};

this.oauth = oauth;
Expand Down
3 changes: 2 additions & 1 deletion src/auth/OAuthAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ var OAuthAuthenticator = function(options) {
* @type {Object}
*/
var clientOptions = {
errorFormatter: { message: 'message', name: 'error' }
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers
};

this.oauth = new RestClient(options.baseUrl + '/oauth/:type', clientOptions);
Expand Down
3 changes: 2 additions & 1 deletion src/auth/PasswordlessAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var PasswordlessAuthenticator = function(options, oauth) {
* @type {Object}
*/
var clientOptions = {
errorFormatter: { message: 'message', name: 'error' }
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers
};

this.oauth = oauth;
Expand Down
41 changes: 5 additions & 36 deletions src/auth/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
var telemetry = jsonToBase64(clientInfo);
managerOptions.headers['Auth0-Client'] = telemetry;
}
}

/**
Expand Down Expand Up @@ -107,38 +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() {
var clientInfo = {
name: 'node-auth0',
version: pkg.version,
dependencies: [],
environment: [
{
name: 'node.js',
version: process.version.replace('v', '')
}
]
};

// Add the dependencies to the client info object.
Object.keys(pkg.dependencies).forEach(function(name) {
clientInfo.dependencies.push({
name: name,
version: pkg.dependencies[name]
});
});

return clientInfo;
};

/**
* Start passwordless flow sending an email.
*
Expand Down
3 changes: 2 additions & 1 deletion src/management/ManagementTokenProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ var ManagementTokenProvider = function(options) {
domain: this.options.domain,
clientId: this.options.clientId,
clientSecret: this.options.clientSecret,
telemetry: this.options.telemetry
telemetry: this.options.telemetry,
clientInfo: this.options.clientInfo
};
this.authenticationClient = new AuthenticationClient(authenticationClientOptions);
};
Expand Down
10 changes: 5 additions & 5 deletions src/management/StatsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var StatsManager = function(options) {
clientOptions,
options.tokenProvider
);
this.stats = new RetryRestClient(auth0RestClient, options.retry);
this.resource = new RetryRestClient(auth0RestClient, options.retry);
};

/**
Expand Down Expand Up @@ -84,10 +84,10 @@ StatsManager.prototype.getDaily = function(params, cb) {
params.type = 'daily';

if (cb && cb instanceof Function) {
return this.stats.get(params, cb);
return this.resource.get(params, cb);
}

return this.stats.get(params);
return this.resource.get(params);
};

/**
Expand All @@ -113,11 +113,11 @@ StatsManager.prototype.getActiveUsersCount = function(cb) {
var options = { type: 'active-users' };

if (cb && cb instanceof Function) {
return this.stats.get(options, cb);
return this.resource.get(options, cb);
}

// Return a promise.
return this.stats.get(options);
return this.resource.get(options);
};

module.exports = StatsManager;
10 changes: 5 additions & 5 deletions src/management/TenantManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var TenantManager = function(options) {
clientOptions,
options.tokenProvider
);
this.tenant = new RetryRestClient(auth0RestClient, options.retry);
this.resource = new RetryRestClient(auth0RestClient, options.retry);
};

/**
Expand All @@ -72,11 +72,11 @@ var TenantManager = function(options) {
*/
TenantManager.prototype.updateSettings = function(data, cb) {
if (cb && cb instanceof Function) {
return this.tenant.patch({}, data, cb);
return this.resource.patch({}, data, cb);
}

// Return a promise.
return this.tenant.patch({}, data);
return this.resource.patch({}, data);
};

/**
Expand All @@ -100,11 +100,11 @@ TenantManager.prototype.updateSettings = function(data, cb) {
*/
TenantManager.prototype.getSettings = function(cb) {
if (cb && cb instanceof Function) {
return this.tenant.get({}, cb);
return this.resource.get({}, cb);
}

// Return a promise.
return this.tenant.get({});
return this.resource.get({});
};

module.exports = TenantManager;
10 changes: 5 additions & 5 deletions src/management/TicketsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var TicketsManager = function(options) {
clientOptions,
options.tokenProvider
);
this.ticket = new RetryRestClient(auth0RestClient, options.retry);
this.resource = new RetryRestClient(auth0RestClient, options.retry);
};

/**
Expand Down Expand Up @@ -73,11 +73,11 @@ TicketsManager.prototype.changePassword = function(data, cb) {
var params = { type: 'password-change' };

if (cb && cb instanceof Function) {
return this.ticket.create(params, data, cb);
return this.resource.create(params, data, cb);
}

// Return a promise.
return this.ticket.create(params, data);
return this.resource.create(params, data);
};

/**
Expand Down Expand Up @@ -105,11 +105,11 @@ TicketsManager.prototype.verifyEmail = function(data, cb) {
var params = { type: 'email-verification' };

if (cb && cb instanceof Function) {
return this.ticket.create(params, data, cb);
return this.resource.create(params, data, cb);
}

// Return a promise.
return this.ticket.create(params, data);
return this.resource.create(params, data);
};

module.exports = TicketsManager;
41 changes: 6 additions & 35 deletions src/management/index.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -290,37 +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() {
var clientInfo = {
name: 'node-auth0',
version: pkg.version,
dependencies: [],
environment: [
{
name: 'node.js',
version: process.version.replace('v', '')
}
]
};
// Add the dependencies to the client info object.
Object.keys(pkg.dependencies).forEach(function(name) {
clientInfo.dependencies.push({
name: name,
version: pkg.dependencies[name]
});
});

return clientInfo;
};

/**
* Get all connections.
*
Expand Down
19 changes: 19 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Promise = require('bluebird');
var request = require('request');
var pkg = require('../package.json');

/**
* @module utils
Expand All @@ -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
Expand Down
Loading

0 comments on commit 699f285

Please sign in to comment.