Skip to content

Commit

Permalink
Add telemetry for the auth code exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Jul 16, 2019
1 parent 46c7428 commit db876e6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ var util = require('util'),
pkg = require('../package.json');

function encodeClientInfo(obj) {
var str = JSON.stringify(obj);
return new Buffer(str).toString('base64')
.replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_') // Convert '/' to '_'
.replace(/=+$/, ''); // Remove ending '='
return new Buffer(JSON.stringify(obj)).toString('base64');
}

var clientInfoHeader = encodeClientInfo({ name: 'passport-auth0', version: pkg.version });
var clientInfoHeader = encodeClientInfo({
name: 'passport-auth0',
version: pkg.version,
env: {
node: process.version
}
});

var Profile = require('./Profile');

Expand All @@ -36,12 +38,19 @@ function Strategy(options, verify) {
}
});

this.options = Object.assign(options, {
options.customHeaders = Object.assign(
{ 'Auth0-Client': clientInfoHeader },
typeof options.customHeaders === 'object' ? options.customHeaders : {}
);

var defaultOptions = {
authorizationURL: 'https://' + options.domain + '/authorize',
tokenURL: 'https://' + options.domain + '/oauth/token',
userInfoURL: 'https://' + options.domain + '/userinfo',
apiUrl: 'https://' + options.domain + '/api'
});
}

this.options = Object.assign(options, defaultOptions);

if (this.options.state === undefined) {
this.options.state = true;
Expand Down
41 changes: 41 additions & 0 deletions test/strategy.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Auth0Strategy = require('../lib');
var assert = require('assert');
var should = require('should');
var pkg = require('../package.json');

describe('auth0 strategy', function () {
before(function () {
Expand Down Expand Up @@ -29,6 +30,21 @@ describe('auth0 strategy', function () {
.userInfoURL.should.eql('https://test.auth0.com/userinfo');
});

it('should include a telemetry header by default', function() {
var headers = this.strategy.options.customHeaders;
should.exist(headers['Auth0-Client']);
});

it('should include a correct telemetry values', function() {
var telemetryValue = new Buffer( this.strategy.options.customHeaders['Auth0-Client'], 'base64' ).toString('ascii');
var telemetryJson = JSON.parse(telemetryValue)

telemetryJson.name.should.eql('passport-auth0');
telemetryJson.version.should.eql(pkg.version);
should.exist(telemetryJson.env);
should.exist(telemetryJson.env.node);
});

it('state should be true by default', function() {
this.strategy.options.state.should.be.true();
});
Expand Down Expand Up @@ -141,6 +157,31 @@ describe('auth0 strategy', function () {
});
});

describe('auth0 strategy with a custom header', function () {
var strategy = new Auth0Strategy(
{
domain: 'test.auth0.com',
clientID: 'testid',
clientSecret: 'testsecret',
callbackURL: '/callback',
customHeaders: {
testCustomHeader: 'Test Custom Header'
}
},
function(accessToken, idToken, profile, done) {}
);

it('should not override a custom header', function() {
should.exist(strategy.options.customHeaders);
should.exist(strategy.options.customHeaders.testCustomHeader);
strategy.options.customHeaders.testCustomHeader.should.eql('Test Custom Header');
});

it('should keep the telemetry header', function() {
should.exist(strategy.options.customHeaders['Auth0-Client']);
});
});

describe('auth0 strategy with state parameter disabled', function () {
var strategy = new Auth0Strategy({
domain: 'test.auth0.com',
Expand Down

0 comments on commit db876e6

Please sign in to comment.