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

Use window.location.origin instead of window.origin #627

Merged
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
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 @@ -77,6 +77,7 @@ describe('auth0.WebAuth', function() {
appState: null
});
};
global.window.location = {};
storage.reload();
});

Expand Down Expand Up @@ -752,6 +753,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 @@ -861,6 +863,7 @@ describe('auth0.WebAuth', function() {
beforeEach(function() {
global.window = {};
global.window.document = {};
global.window.origin = 'unit-test-origin';
});

afterEach(function() {
Expand Down Expand Up @@ -925,6 +928,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