Repository: auth0/auth0.js
Pull request #627 (2018-01-15 21:38:38)

Use window.location.origin instead of window.origin
This is a repeat of https://github.com/auth0/auth0.js/pull/598 (which was against v8 instead of master). Basically I just cherry-picked 722497e752e7075d9d37abf35ba7a6e67470d44d.

luisrudge: Thanks very much @thoean 🎉 

Patch:
diff --git a/src/web-auth/silent-authentication-handler.js b/src/web-auth/silent-authentication-handler.js
index 205c626..9791942 100644
--- a/src/web-auth/silent-authentication-handler.js
+++ b/src/web-auth/silent-authentication-handler.js
@@ -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) {


Test patch:
diff --git a/test/web-auth/silent-authentication-handler.test.js b/test/web-auth/silent-authentication-handler.test.js
index a41c49b..0f93560 100644
--- a/test/web-auth/silent-authentication-handler.test.js
+++ b/test/web-auth/silent-authentication-handler.test.js
@@ -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() {
@@ -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');
+    });
+  });
 });
diff --git a/test/web-auth/web-auth.test.js b/test/web-auth/web-auth.test.js
index f74ef4d..16f5b40 100644
--- a/test/web-auth/web-auth.test.js
+++ b/test/web-auth/web-auth.test.js
@@ -77,6 +77,7 @@ describe('auth0.WebAuth', function() {
           appState: null
         });
       };
+      global.window.location = {};
       storage.reload();
     });
 
@@ -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() {
@@ -861,6 +863,7 @@ describe('auth0.WebAuth', function() {
     beforeEach(function() {
       global.window = {};
       global.window.document = {};
+      global.window.origin = 'unit-test-origin';
     });
 
     afterEach(function() {
@@ -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' });