Skip to content
Permalink
Browse files

Thrid implementation: use two setInterval in both original frame and …

…popup. Waith for the childWindow to get the hash containing token_id. The opened window close itself when a certain boolean value is set to true.

Use two redirect urls one for login and one for the other tasks
  • Loading branch information...
bpatra committed May 25, 2015
1 parent 30aa200 commit 62c28535b006144c29ddd2e9abf408aa2fb1f458
Showing with 59 additions and 40 deletions.
  1. +0 −6 lib/adal-angular.js
  2. +47 −18 lib/adal.js
  3. +10 −14 lib/popupwindow.html
  4. +2 −2 sandboxedSample/App.routes.js
@@ -80,12 +80,6 @@ if (typeof module !== 'undefined' && module.exports) {
this.$get = ['$rootScope', '$window', '$q', '$location', '$timeout', function ($rootScope, $window, $q, $location, $timeout) {

var locationChangeHandler = function () {
if(window.opener){
window.opener.postMessage(window.location.href, _adal.config.frameHost);
window.close();
return;
}

var hash = $window.location.hash;

if (_adal.isCallback(hash)) {
@@ -141,6 +141,10 @@ AuthenticationContext = function (config) {
if (!this.config.redirectUri) {
this.config.redirectUri = window.location.href;
}
if(!this.config.loginConfigUrl) {
this.config.loginConfigUrl = window.location.href;
}


this.config.resource = this.config.loginResource || '';
};
@@ -164,7 +168,7 @@ AuthenticationContext.prototype.login = function () {
this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION, '');


var urlNavigate = this._getNavigateUrl('id_token', null) + '&nonce=' + encodeURIComponent(this._idTokenNonce);
var urlNavigate = this._getNavigateUrl('id_token', null,true) + '&nonce=' + encodeURIComponent(this._idTokenNonce);
this.frameCallInProgress = false;
this._loginInProgress = true;
if (this.config.displayCall) {
@@ -252,7 +256,7 @@ AuthenticationContext.prototype._renewToken = function (resource, callback) {
this._saveItem(this.CONSTANTS.STORAGE.FAILED_RENEW, '');

this._logstatus('Renew token Expected state: ' + expectedState);
var urlNavigate = this._getNavigateUrl('token', resource) + '&prompt=none&login_hint=' + encodeURIComponent(this._user.userName);
var urlNavigate = this._getNavigateUrl('token', resource,false) + '&prompt=none&login_hint=' + encodeURIComponent(this._user.userName);
urlNavigate += '&domain_hint=' + encodeURIComponent(this._getDomainHint());
urlNavigate += '&nonce=' + encodeURIComponent(this._idTokenNonce);
this.callback = callback;
@@ -283,7 +287,7 @@ AuthenticationContext.prototype._renewIdToken = function (callback) {
this._saveItem(this.CONSTANTS.STORAGE.FAILED_RENEW, '');

this._logstatus('Renew token Expected state: ' + expectedState);
var urlNavigate = this._getNavigateUrl('id_token', null) + '&prompt=none&login_hint=' + encodeURIComponent(this._user.userName);
var urlNavigate = this._getNavigateUrl('id_token', null,false) + '&prompt=none&login_hint=' + encodeURIComponent(this._user.userName);
urlNavigate += '&domain_hint=' + encodeURIComponent(this._getDomainHint());
urlNavigate += '&nonce=' + encodeURIComponent(this._idTokenNonce);
this.callback = callback;
@@ -369,19 +373,37 @@ AuthenticationContext.prototype.promptUser = function (urlNavigate) {
if (urlNavigate) {
that._logstatus('Navigate to:' + urlNavigate);
if(that._inIframe()){
var popup = window.open(that.config.popupWindowUri,'Auth window', 'height=500,width=500');
var interval;
var pingFunction = function() {
if(popup.closed === false){
popup.postMessage(urlNavigate, that.config.frameHost);
}else {
var currentHref = window.location.href;
var childWindow =window.open(urlNavigate,'Auth window', 'height=500,width=500');
var interval;
var pollChildWindow = function () {
if (childWindow.closed === false) {
var oauthHash;
try
{
oauthHash = childWindow.oauthHash
}catch(ex){}

if (typeof oauthHash === "string") {
//childWindow.close(); // does not work with Chrome
childWindow.canclosewindow = true; // set boolean value to child window instead.
var startWith = oauthHash.indexOf('#id_token');
var currentRef = window.location.href.split('#')[0];
if (startWith === 0) {//authentication process has been completed and respond is Ok
var newHash = oauthHash.split('#')[1];
var newHref =currentRef +'#/'+newHash;
window.location.replace(newHref);
} else { //process is completed and response is not Ok
window.location.replace(currentRef);
}
}
} else {
clearInterval(interval);
}
};
interval = setInterval(pingFunction,50);
window.addEventListener("message", function receiveMessage(event) {
window.location.replace(event.data);
});
};

interval = setInterval(pollChildWindow, 200);

}else{
window.location.replace(urlNavigate);
}
@@ -779,7 +801,7 @@ AuthenticationContext.prototype.handleWindowCallback = function () {
}
};

AuthenticationContext.prototype._getNavigateUrl = function (responseType, resource) {
AuthenticationContext.prototype._getNavigateUrl = function (responseType, resource,useLoginConfigUrl) {
var tenant = 'common';
if (this.config.tenant) {
tenant = this.config.tenant;
@@ -789,7 +811,7 @@ AuthenticationContext.prototype._getNavigateUrl = function (responseType, resour
this.instance = this.config.instance;
}

var urlNavigate = this.instance + tenant + '/oauth2/authorize' + this._serialize(responseType, this.config, resource) + this._addClientId();
var urlNavigate = this.instance + tenant + '/oauth2/authorize' + this._serialize(responseType, this.config, resource, useLoginConfigUrl) + this._addClientId();
this._logstatus('Navigate url:' + urlNavigate);
return urlNavigate;
};
@@ -871,7 +893,7 @@ AuthenticationContext.prototype._convertUrlSafeToRegularBase64EncodedString = fu
return str.replace('-', '+').replace('_', '/');
};

AuthenticationContext.prototype._serialize = function (responseType, obj, resource) {
AuthenticationContext.prototype._serialize = function (responseType, obj, resource,useLoginConfigUrl) {
var str = [];
if (obj !== null) {
str.push('?response_type=' + responseType);
@@ -880,7 +902,14 @@ AuthenticationContext.prototype._serialize = function (responseType, obj, resour
str.push('resource=' + encodeURIComponent(resource));
}

str.push('redirect_uri=' + encodeURIComponent(obj.redirectUri));
if(useLoginConfigUrl === true){
str.push('redirect_uri=' + encodeURIComponent(obj.loginConfigUrl));
}
else{
str.push('redirect_uri=' + encodeURIComponent(obj.redirectUri));
}


str.push('state=' + encodeURIComponent(obj.state));

if (obj.hasOwnProperty('slice')) {
@@ -4,22 +4,18 @@
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<title>Authentication popup</title>
<script>
(function(){
"use strict";
window.onload = function(){
var processed = false;
window.addEventListener("message", function receiveMessage(event) {
if(processed === false){
processed = true;
window.location.replace(event.data);
}
});
};
})();
</script>

</head>
<body>
<div id="oauthurl"></div>
<script>
var closeWindowCheck = function () {
window.oauthHash = location.hash
if (window.canclosewindow === true) {
window.close();
}
}
var interval = setInterval(closeWindowCheck, 200);
</script>
</body>
</html>
@@ -27,8 +27,8 @@
adalProvider.init({
tenant: adalSettings.tenant,
clientId: adalSettings.clientId,
postLogoutRedirectUri: adalSettings.frameHost + adalSettings.postLogoutPath,
popupWindowUri: adalSettings.frameHost + adalSettings.popupPath,
postLogoutRedirectUri: adalSettings.frameHost + adalSettings.popupPath,
loginConfigUrl: adalSettings.frameHost + adalSettings.popupPath,
frameHost : adalSettings.frameHost,
endpoints: adalSettings.aadEndpoints
}, $httpProvider);

0 comments on commit 62c2853

Please sign in to comment.
You can’t perform that action at this time.