Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making Authentication constructor accept one or two params #657

Merged
merged 2 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ var DBConnection = require('./db-connection');
* @see {@link https://auth0.com/docs/api/authentication}
*/
function Authentication(auth0, options) {
// If we have two arguments, the first one is a WebAuth instance, so we assign that
// if not, it's an options object and then we should use that as options instead
// this is here because we don't want to break people coming from v8
if (arguments.length === 2) {
this.auth0 = auth0;
} else {
options = auth0;
}

/* eslint-disable */
assert.check(
options,
Expand Down Expand Up @@ -58,7 +67,6 @@ function Authentication(auth0, options) {
/* eslint-enable */

this.baseOptions = options;
this.auth0 = auth0;
this.baseOptions._sendTelemetry = this.baseOptions._sendTelemetry === false
? this.baseOptions._sendTelemetry
: true;
Expand Down Expand Up @@ -361,6 +369,12 @@ Authentication.prototype.loginWithResourceOwner = function(options, cb) {
* @param {Function} cb
*/
Authentication.prototype.getSSOData = function(withActiveDirectories, cb) {
/* istanbul ignore if */
if (!this.auth0) {
// we can't import this in the constructor because it'd be a ciclic dependency
var WebAuth = require('../web-auth/index'); // eslint-disable-line
this.auth0 = new WebAuth(this.baseOptions);
}
if (typeof withActiveDirectories === 'function') {
cb = withActiveDirectories;
}
Expand Down
2 changes: 1 addition & 1 deletion src/web-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function WebAuth(options) {

this.transactionManager = new TransactionManager(this.baseOptions.transaction);

this.client = new Authentication(this, this.baseOptions);
this.client = new Authentication(this.baseOptions);
this.redirect = new Redirect(this, this.baseOptions);
this.popup = new Popup(this, this.baseOptions);
this.crossOriginAuthentication = new CrossOriginAuthentication(this, this.baseOptions);
Expand Down
11 changes: 10 additions & 1 deletion test/authentication/authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var request = require('superagent');
var RequestBuilder = require('../../src/helper/request-builder');
var storage = require('../../src/helper/storage');
var Authentication = require('../../src/authentication');
var WebAuth = require('../../src/web-auth');

var telemetryInfo = new RequestBuilder({}).getTelemetryData();

Expand All @@ -20,9 +21,17 @@ describe('auth0.authentication', function() {
};
});
describe('initialization', function() {
it('should use first argument as options when only one argument is used', function() {
var auth0 = new Authentication({ domain: 'foo', clientID: 'cid' });
expect(auth0.baseOptions.domain).to.be.equal('foo');
});
it('should use second argument as options when two arguments are used', function() {
var auth0 = new Authentication({}, { domain: 'foo', clientID: 'cid' });
expect(auth0.baseOptions.domain).to.be.equal('foo');
});
it('should check that options is passed', function() {
expect(function() {
var auth0 = new Authentication(this.webAuthSpy);
var auth0 = new Authentication();
}).to.throwException(function(e) {
expect(e.message).to.be('options parameter is not valid');
});
Expand Down
32 changes: 13 additions & 19 deletions test/authentication/db-connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ var telemetryInfo = new RequestBuilder({}).getTelemetryData();
describe('auth0.authentication', function() {
context('dbConnection signup options', function() {
before(function() {
this.auth0 = new Authentication(
{},
{
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code'
}
);
this.auth0 = new Authentication({
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code'
});
});

it('should check that options is passed', function() {
Expand Down Expand Up @@ -176,16 +173,13 @@ describe('auth0.authentication', function() {

context('change password options', function() {
before(function() {
this.auth0 = new Authentication(
{},
{
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
}
);
this.auth0 = new Authentication({
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
});
});

it('should check that options is passed', function() {
Expand Down
68 changes: 28 additions & 40 deletions test/authentication/passwordless.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ var Authentication = require('../../src/authentication');
describe('auth0.authentication', function() {
context('passwordless start options', function() {
before(function() {
this.auth0 = new Authentication(
{},
{
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
}
);
this.auth0 = new Authentication({
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
});
});

it('should check that options is passed', function() {
Expand Down Expand Up @@ -74,16 +71,13 @@ describe('auth0.authentication', function() {

context('passwordless verify options', function() {
before(function() {
this.auth0 = new Authentication(
{},
{
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
}
);
this.auth0 = new Authentication({
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
});
});

it('should check that options is passed', function() {
Expand Down Expand Up @@ -172,16 +166,13 @@ describe('auth0.authentication', function() {

context('passwordless start', function() {
before(function() {
this.auth0 = new Authentication(
{},
{
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
}
);
this.auth0 = new Authentication({
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
});
});

afterEach(function() {
Expand Down Expand Up @@ -271,16 +262,13 @@ describe('auth0.authentication', function() {

context('passwordless verify', function() {
before(function() {
this.auth0 = new Authentication(
{},
{
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
}
);
this.auth0 = new Authentication({
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
});
});

afterEach(function() {
Expand Down
15 changes: 6 additions & 9 deletions test/authentication/ro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ var telemetryInfo = new RequestBuilder({}).getTelemetryData();
describe('auth0.authentication', function() {
context('/oauth/ro', function() {
before(function() {
this.auth0 = new Authentication(
{},
{
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code'
}
);
this.auth0 = new Authentication({
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code'
});
});

afterEach(function() {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1956,7 +1956,7 @@ husky@^0.13.3:
is-ci "^1.0.9"
normalize-path "^1.0.0"

idtoken-verifier@^1.1.0:
idtoken-verifier@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/idtoken-verifier/-/idtoken-verifier-1.1.1.tgz#bc6eb46e6a153bb2058aed1cac2c27ac6751b5e5"
dependencies:
Expand Down