KEYCLOAK-668 Make sure keycloak.js doesn't send multiple concurrent requests to refresh the token

This commit is contained in:
Stian Thorgersen 2014-09-09 13:45:55 +02:00
parent 839069ca15
commit c475721dab
2 changed files with 44 additions and 31 deletions

View file

@ -33,6 +33,7 @@ angular.element(document).ready(function ($http) {
module.factory('authInterceptor', function($q, Auth) {
return {
request: function (config) {
if (!config.url.match(/.html$/)) {
var deferred = $q.defer();
if (Auth.authz.token) {
Auth.authz.updateToken(5).success(function () {
@ -45,6 +46,9 @@ module.factory('authInterceptor', function($q, Auth) {
});
}
return deferred.promise;
} else {
return config;
}
}
};
});

View file

@ -7,6 +7,7 @@
var kc = this;
var adapter;
var refreshQueue = [];
var loginIframe = {
enable: true,
@ -237,6 +238,9 @@
var params = 'grant_type=refresh_token&' + 'refresh_token=' + kc.refreshToken;
var url = getRealmUrl() + '/tokens/refresh';
refreshQueue.push(promise);
if (refreshQueue.length == 1) {
var req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
@ -253,10 +257,14 @@
var tokenResponse = JSON.parse(req.responseText);
setToken(tokenResponse['access_token'], tokenResponse['refresh_token']);
kc.onAuthRefreshSuccess && kc.onAuthRefreshSuccess();
promise.setSuccess(true);
for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) {
p.setSuccess(true);
}
} else {
kc.onAuthRefreshError && kc.onAuthRefreshError();
promise.setError();
for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) {
p.setError(true);
}
}
}
};
@ -264,6 +272,7 @@
req.send(params);
}
}
}
if (loginIframe.enable) {
var iframePromise = checkLoginIframe();