From 5b91e89172a8f316ae9676683fc098b58b4e2e31 Mon Sep 17 00:00:00 2001 From: Joris Mancini Date: Mon, 18 Dec 2023 13:59:53 +0100 Subject: [PATCH] feat(authentication): enable token renewal in client Signed-off-by: Joris Mancini --- src/index.js | 5 +++-- src/utils/AuthService.js | 26 +++++++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 37936cab..00a0baa2 100644 --- a/src/index.js +++ b/src/index.js @@ -29,11 +29,12 @@ export { } from './utils/EquipmentType'; export { + dispatchUser, + getExpiresIn, + getPreLoginPath, initializeAuthenticationDev, initializeAuthenticationProd, logout, - dispatchUser, - getPreLoginPath, } from './utils/AuthService'; export { elementType, getFileIcon } from './utils/ElementType'; diff --git a/src/utils/AuthService.js b/src/utils/AuthService.js index 91ffe330..2e638113 100644 --- a/src/utils/AuthService.js +++ b/src/utils/AuthService.js @@ -170,9 +170,9 @@ function initializeAuthenticationProd( post_logout_redirect_uri: idpSettings.post_logout_redirect_uri, silent_redirect_uri: idpSettings.silent_redirect_uri, scope: idpSettings.scope, - automaticSilentRenew: !isSilentRenew, - accessTokenExpiringNotificationTime: - accessTokenExpiringNotificationTime, + automaticSilentRenew: + !isSilentRenew && !authorizationCodeFlowEnabled, + accessTokenExpiringNotificationTime, ...responseSettings, }; let userManager = new UserManager(settings); @@ -385,13 +385,25 @@ function handleUser(dispatch, userManager, validateUser) { dispatchUser(dispatch, userManager, validateUser); } +function getExpiresIn(idToken, maxTokenTtl) { + const decodedIdToken = jwtDecode(idToken); + const now = Date.now() / 1000; + const livingTime = now - decodedIdToken.iat; + const expiresIn = decodedIdToken.exp - now; + if (!maxTokenTtl) { + return expiresIn; + } + return Math.min(maxTokenTtl - livingTime, expiresIn, 0); +} + export { + dispatchUser, + getExpiresIn, + getPreLoginPath, + handleSigninCallback, + handleSilentRenewCallback, initializeAuthenticationDev, initializeAuthenticationProd, - handleSilentRenewCallback, login, logout, - dispatchUser, - handleSigninCallback, - getPreLoginPath, };