Skip to content

Commit

Permalink
Support browsers that return undefined on window.origin (#598)
Browse files Browse the repository at this point in the history
* Support browsers that return undefined on window.origin.

* Simplify logic to leverage window.location.origin

* Include fallback option for older browsers and add a unit test for it.
  • Loading branch information
thoean authored and luisrudge committed Jan 15, 2018
1 parent 177994e commit 722497e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/web-auth/silent-authentication-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ function SilentAuthenticationHandler(options) {
this.timeout = options.timeout || 60 * 1000;
this.handler = null;
this.postMessageDataType = options.postMessageDataType || false;
this.postMessageOrigin = options.postMessageOrigin || windowHelper.getWindow().origin;

// prefer origin from options, fallback to origin from browser, and some browsers (for example MS Edge) don't support origin; fallback to construct origin manually
this.postMessageOrigin =
options.postMessageOrigin ||
windowHelper.getWindow().location.origin ||
windowHelper.getWindow().location.protocol + '//' + windowHelper.getWindow().location.hostname
+ (windowHelper.getWindow().location.port ? ':' + windowHelper.getWindow().location.port : '');
}

SilentAuthenticationHandler.create = function(options) {
Expand Down
40 changes: 40 additions & 0 deletions test/web-auth/silent-authentication-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ var iframeHandler = {};

describe('handlers silent-authentication-handler', function() {
context('with context', function() {
beforeEach(function() {
global.window = { location: { origin: 'unit-test-origin' } }
});
afterEach(function() {
if (IframeHandler.prototype.init.restore) {
IframeHandler.prototype.init.restore();
}
delete global.window;
});
it('should return correct value for usePostMessage=false', function(done) {
stub(IframeHandler.prototype, 'init', function() {
Expand Down Expand Up @@ -293,4 +297,40 @@ describe('handlers silent-authentication-handler', function() {
})).to.be(false);
});
});

context('constructor', function() {
it('sets postMessageOrigin from parameter', function() {
var expectedOrigin = 'unit-test-post-message-origin';
var param = { postMessageOrigin: expectedOrigin };

var sah = new SilentAuthenticationHandler(param);

expect(sah.postMessageOrigin).to.be(expectedOrigin);
});

it('sets postMessageOrigin from window', function() {
var expectedOrigin = 'unit-test-location-origin';
global.window = { location: { origin: expectedOrigin } };

var sah = new SilentAuthenticationHandler({});

expect(sah.postMessageOrigin).to.be(expectedOrigin);
});

it('sets postMessageOrigin from fallback (with port)', function() {
global.window = { location: { protocol: 'https:', hostname: 'unit-test', port: 1234 } };

var sah = new SilentAuthenticationHandler({});

expect(sah.postMessageOrigin).to.be('https://unit-test:1234');
});

it('sets postMessageOrigin from fallback (without port)', function() {
global.window = { location: { protocol: 'https:', hostname: 'unit-test' } };

var sah = new SilentAuthenticationHandler({});

expect(sah.postMessageOrigin).to.be('https://unit-test');
});
});
});
9 changes: 9 additions & 0 deletions test/web-auth/web-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe('auth0.WebAuth', function() {
appState: null
});
};
global.window.location = {};
storage.reload();
});

Expand Down Expand Up @@ -663,6 +664,7 @@ describe('auth0.WebAuth', function() {
context('renewAuth', function() {
beforeEach(function() {
global.window = {};
global.window.origin = 'unit-test-origin';
global.window.removeEventListener = function() {};
});
afterEach(function() {
Expand Down Expand Up @@ -751,6 +753,7 @@ describe('auth0.WebAuth', function() {
beforeEach(function() {
global.window = {};
global.window.document = {};
global.window.origin = 'unit-test-origin';
});

afterEach(function() {
Expand Down Expand Up @@ -815,6 +818,12 @@ describe('auth0.WebAuth', function() {
});
});
describe('should return the access_token', function() {
beforeEach(function() {
global.window = { origin: 'unit-test-origin' };
});
afterEach(function() {
delete global.window;
});
it('when login returns an object', function(done) {
stub(SilentAuthenticationHandler.prototype, 'login', function(usePostMessage, cb) {
cb(null, { accessToken: '123' });
Expand Down

0 comments on commit 722497e

Please sign in to comment.