KEYCLOAK-1082

Make sure session is valid if keycloak.js is initialized with tokens
This commit is contained in:
Stian Thorgersen 2015-03-05 07:42:13 +01:00
parent 8dea0d50ee
commit 4cb3d51781

View file

@ -50,50 +50,68 @@
var configPromise = loadConfig(config); var configPromise = loadConfig(config);
function onLoad() {
var doLogin = function(prompt) {
if (!prompt) {
options.prompt = 'none';
}
kc.login(options).success(function () {
initPromise.setSuccess();
}).error(function () {
initPromise.setError();
});
}
var options = {};
switch (initOptions.onLoad) {
case 'check-sso':
if (loginIframe.enable) {
setupCheckLoginIframe().success(function() {
checkLoginIframe().success(function () {
doLogin(false);
}).error(function () {
initPromise.setSuccess();
});
});
} else {
doLogin(false);
}
break;
case 'login-required':
doLogin(true);
break;
default:
throw 'Invalid value for onLoad';
}
}
function processInit() { function processInit() {
var callback = parseCallback(window.location.href); var callback = parseCallback(window.location.href);
if (callback) { if (callback) {
setupCheckLoginIframe();
window.history.replaceState({}, null, callback.newUrl); window.history.replaceState({}, null, callback.newUrl);
processCallback(callback, initPromise); processCallback(callback, initPromise);
return; return;
} else if (initOptions) { } else if (initOptions) {
var doLogin = function(prompt) {
if (!prompt) {
options.prompt = 'none';
}
kc.login(options).success(function () {
initPromise.setSuccess();
}).error(function () {
initPromise.setError();
});
}
if (initOptions.token || initOptions.refreshToken) { if (initOptions.token || initOptions.refreshToken) {
setToken(initOptions.token, initOptions.refreshToken, initOptions.idToken); setToken(initOptions.token, initOptions.refreshToken, initOptions.idToken);
initPromise.setSuccess();
} else if (initOptions.onLoad) { if (loginIframe.enable) {
var options = {}; setupCheckLoginIframe().success(function() {
switch (initOptions.onLoad) { checkLoginIframe().success(function () {
case 'check-sso': initPromise.setSuccess();
if (loginIframe.enable) { }).error(function () {
setupCheckLoginIframe().success(function() { if (initOptions.onLoad) {
checkLoginIframe().success(function () { onLoad();
doLogin(false); }
}).error(function () { });
initPromise.setSuccess(); });
}); } else {
}); initPromise.setSuccess();
} else {
doLogin(false);
}
break;
case 'login-required':
doLogin(true);
break;
default:
throw 'Invalid value for onLoad';
} }
} else if (initOptions.onLoad) {
onLoad();
} }
} else { } else {
initPromise.setSuccess(); initPromise.setSuccess();
@ -122,8 +140,13 @@
sessionStorage.oauthState = JSON.stringify({ state: state, redirectUri: encodeURIComponent(redirectUri) }); sessionStorage.oauthState = JSON.stringify({ state: state, redirectUri: encodeURIComponent(redirectUri) });
var action = 'login';
if (options && options.action == 'register') {
action = 'registrations';
}
var url = getRealmUrl() var url = getRealmUrl()
+ '/protocol/openid-connect/login' + '/protocol/openid-connect/' + action
+ '?client_id=' + encodeURIComponent(kc.clientId) + '?client_id=' + encodeURIComponent(kc.clientId)
+ '&redirect_uri=' + encodeURIComponent(redirectUri) + '&redirect_uri=' + encodeURIComponent(redirectUri)
+ '&state=' + encodeURIComponent(state) + '&state=' + encodeURIComponent(state)
@ -451,10 +474,6 @@
} }
function setToken(token, refreshToken, idToken) { function setToken(token, refreshToken, idToken) {
if (token || refreshToken) {
setupCheckLoginIframe();
}
if (token) { if (token) {
kc.token = token; kc.token = token;
kc.tokenParsed = decodeToken(token); kc.tokenParsed = decodeToken(token);
@ -633,15 +652,17 @@
var promise = createPromise(); var promise = createPromise();
if (!loginIframe.enable) { if (!loginIframe.enable) {
return; promise.setSuccess();
return promise.promise;
} }
if (loginIframe.iframe) { if (loginIframe.iframe) {
promise.setSuccess(); promise.setSuccess();
return; return promise.promise;
} }
var iframe = document.createElement('iframe'); var iframe = document.createElement('iframe');
loginIframe.iframe = iframe;
iframe.onload = function() { iframe.onload = function() {
var realmUrl = getRealmUrl(); var realmUrl = getRealmUrl();
@ -650,8 +671,9 @@
} else { } else {
loginIframe.iframeOrigin = realmUrl.substring(0, realmUrl.indexOf('/', 8)); loginIframe.iframeOrigin = realmUrl.substring(0, realmUrl.indexOf('/', 8));
} }
loginIframe.iframe = iframe;
promise.setSuccess(); promise.setSuccess();
setTimeout(check, loginIframe.interval * 1000);
} }
var src = getRealmUrl() + '/protocol/openid-connect/login-status-iframe.html?client_id=' + encodeURIComponent(kc.clientId) + '&origin=' + getOrigin(); var src = getRealmUrl() + '/protocol/openid-connect/login-status-iframe.html?client_id=' + encodeURIComponent(kc.clientId) + '&origin=' + getOrigin();
@ -683,8 +705,6 @@
} }
}; };
setTimeout(check, loginIframe.interval * 1000);
return promise.promise; return promise.promise;
} }