moved also refresh token to fetch

This commit is contained in:
dcore94 2022-09-15 15:38:30 +02:00
parent 7f50e2900e
commit 9411728b3a
1 changed files with 30 additions and 38 deletions

View File

@ -649,46 +649,38 @@
refreshQueue.push(promise); refreshQueue.push(promise);
if (refreshQueue.length == 1) { if (refreshQueue.length == 1) {
var req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.withCredentials = true;
params += '&client_id=' + encodeURIComponent(kc.clientId); params += '&client_id=' + encodeURIComponent(kc.clientId);
var timeLocal = new Date().getTime(); var timeLocal = new Date().getTime();
req.onreadystatechange = function () { fetch(url, {
if (req.readyState == 4) { method: "POST",
if (req.status == 200) { headers: {'Content-type':'application/x-www-form-urlencoded'},
logInfo('[KEYCLOAK] Token refreshed'); body : params,
credentials: "include"
timeLocal = (timeLocal + new Date().getTime()) / 2; }).then(resp=>{
if(req.status == 200){
var tokenResponse = JSON.parse(req.responseText); return req.json()
}
setToken(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token'], timeLocal); if (req.status == 400) {
kc.clearToken();
kc.onAuthRefreshSuccess && kc.onAuthRefreshSuccess();
for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) {
p.setSuccess(true);
}
} else {
logWarn('[KEYCLOAK] Failed to refresh token');
if (req.status == 400) {
kc.clearToken();
}
kc.onAuthRefreshError && kc.onAuthRefreshError();
for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) {
p.setError(true);
}
}
} }
}; throw "Failed to refresh token"
}).then(body=>{
req.send(params); logInfo('[KEYCLOAK] Token refreshed');
timeLocal = (timeLocal + new Date().getTime()) / 2;
var tokenResponse = JSON.parse(req.responseText);
setToken(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token'], timeLocal);
kc.onAuthRefreshSuccess && kc.onAuthRefreshSuccess();
for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) {
p.setSuccess(true);
}
}).catch(err=>{
logWarn('[KEYCLOAK] Failed to refresh token');
kc.onAuthRefreshError && kc.onAuthRefreshError();
for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) {
p.setError(true);
}
})
} }
} }
} }
@ -765,7 +757,7 @@
var params = 'code=' + code + '&grant_type=authorization_code'; var params = 'code=' + code + '&grant_type=authorization_code';
var url = kc.endpoints.token(); var url = kc.endpoints.token();
params += '&client_id=' + encodeURIComponent(kc.clientId); params += '&client_id=' + encodeURIComponent(kc.clientId);
params += '&redirect_uri=' + oauth.redirectUri; params += '&redirect_uri=' + oauth.redirectUri;
if (oauth.pkceCodeVerifier) { if (oauth.pkceCodeVerifier) {