Skip to content

Commit

Permalink
refactor: simplify renewal solution
Browse files Browse the repository at this point in the history
Signed-off-by: Joris Mancini <joris.mancini_externe@rte-france.com>
  • Loading branch information
TheMaskedTurtle committed Dec 20, 2023
1 parent 9951727 commit 5b706e2
Showing 1 changed file with 10 additions and 43 deletions.
53 changes: 10 additions & 43 deletions src/utils/AuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ function initializeAuthenticationDev(
dispatch,
isSilentRenew,
validateUser,
isSigninCallback,
navigate
isSigninCallback
) {
let userManager = new UserManagerMock({});
if (!isSilentRenew) {
Expand Down Expand Up @@ -251,25 +250,6 @@ function getIdTokenExpiresIn(user) {
return exp - now;
}

function tokenRenewal(dispatch, userManagerInstance, validateUser, id_token) {
if (userManagerInstance.tokenRenewalTimeout) {
clearTimeout(userManagerInstance.tokenRenewalTimeout);
}
const timeMs =
getExpiresIn(
id_token,
parseInt(userManagerInstance.idpSettings.maxExpiresIn)
) * 1000;
console.debug(`setting timeoutMs ${timeMs}`);
userManagerInstance.tokenRenewalTimeout = setTimeout(async () => {
console.debug('renewing tokens...');
userManagerInstance.signinSilent().catch((error) => {
console.debug('Token renewal failed', error);
handleRetryTokenRenewal(userManagerInstance, dispatch, error);
});
}, timeMs);
}

function handleRetryTokenRenewal(userManagerInstance, dispatch, error) {
userManagerInstance.getUser().then((user) => {
if (!user) {
Expand Down Expand Up @@ -309,9 +289,6 @@ function handleRetryTokenRenewal(userManagerInstance, dispatch, error) {
error
);
user.expires_in = idTokenExpiresIn;
userManagerInstance.storeUser(user).then(() => {
userManagerInstance.getUser();
});
} else {
console.log(
'Error in silent renew, but idtoken NOT expiring (expiring in' +
Expand All @@ -321,10 +298,12 @@ function handleRetryTokenRenewal(userManagerInstance, dispatch, error) {
error
);
user.expires_in = userManagerInstance.idpSettings.maxExpiresIn;
userManagerInstance.storeUser(user).then(() => {
userManagerInstance.getUser();
});
}
// It reloads events timers without triggering userLoaded event
// So we don't re-dispatch the user, but it reloads timers based on hacked value
userManagerInstance.storeUser(user).then(() => {
userManagerInstance.getUser();
});
} else {
console.log(
'Error in silent renew, unsupported configuration: token still valid for ' +
Expand Down Expand Up @@ -365,13 +344,11 @@ function dispatchUser(dispatch, userManagerInstance, validateUser) {
console.debug(
'User has been successfully loaded from store.'
);

// In authorization code flow we have to initiate the token renewal process
// because it is not hacked at page loading on the fragment
if (userManagerInstance.authorizationCodeFlowEnabled) {
tokenRenewal(
dispatch,
userManagerInstance,
validateUser,
user.id_token
);
handleRetryTokenRenewal(userManagerInstance, dispatch);
}
return dispatch(setLoggedUser(user));
})
Expand Down Expand Up @@ -430,16 +407,6 @@ function handleUser(dispatch, userManager, validateUser) {
dispatchUser(dispatch, userManager, validateUser);
}

function getExpiresIn(idToken, maxTokenTtl) {
const decodedIdToken = jwtDecode(idToken);
const now = Date.now() / 1000;
const expiresIn = decodedIdToken.exp - now;
if (!maxTokenTtl) {
return expiresIn;
}
return Math.min(maxTokenTtl, expiresIn);
}

function handleIssuerErrorForCodeFlow(error, navigate) {
const issuer = error.message.split(' ').pop();
sessionStorage.setItem(hackauthoritykey, issuer);
Expand Down

0 comments on commit 5b706e2

Please sign in to comment.