From b53427b910106d1cdac547d6b0f8433c0f892b70 Mon Sep 17 00:00:00 2001 From: Anton Shepilov Date: Fri, 23 Jun 2023 11:53:47 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20infinite=20loop=20when=20access?= =?UTF-8?q?=20token=20expired=20(#107)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/components/modal/modal-component.tsx | 2 +- .../src/app/features/auth/auth-service.ts | 5 ++ .../oidc/oidc-auth-provider-service.ts | 67 +++---------------- 3 files changed, 17 insertions(+), 57 deletions(-) diff --git a/tdrive/frontend/src/app/components/modal/modal-component.tsx b/tdrive/frontend/src/app/components/modal/modal-component.tsx index bc69bec4..c4f4f680 100755 --- a/tdrive/frontend/src/app/components/modal/modal-component.tsx +++ b/tdrive/frontend/src/app/components/modal/modal-component.tsx @@ -40,7 +40,7 @@ export default () => { centered closable={false} title={null} - visible={open} + open={open} footer={null} destroyOnClose={true} width={ModalManager.getPosition()?.size?.width || '700px'} diff --git a/tdrive/frontend/src/app/features/auth/auth-service.ts b/tdrive/frontend/src/app/features/auth/auth-service.ts index 058dd461..16d5016a 100644 --- a/tdrive/frontend/src/app/features/auth/auth-service.ts +++ b/tdrive/frontend/src/app/features/auth/auth-service.ts @@ -48,11 +48,16 @@ class AuthService { return this.provider; } + this.logger.debug(`Configured '${accountType}' auth provider`) const config = InitService.server_infos?.configuration?.accounts[accountType]; + this.logger.debug("Using configuration ..."); + this.logger.debug(config); + if (Globals.environment.env_dev_auth) accountType = Globals.environment.env_dev_auth; if (accountType === 'remote') { + this.logger.debug('Using remote OIDC auth provider'); this.provider = new OIDCAuthProviderService(config as ConsoleConfiguration); } else if (accountType === 'internal') { this.provider = new InternalAuthProviderService(config as InternalConfiguration); diff --git a/tdrive/frontend/src/app/features/auth/provider/oidc/oidc-auth-provider-service.ts b/tdrive/frontend/src/app/features/auth/provider/oidc/oidc-auth-provider-service.ts index 14617adf..c0c2e052 100644 --- a/tdrive/frontend/src/app/features/auth/provider/oidc/oidc-auth-provider-service.ts +++ b/tdrive/frontend/src/app/features/auth/provider/oidc/oidc-auth-provider-service.ts @@ -8,8 +8,6 @@ import { getAsFrontUrl } from '@features/global/utils/URLUtils'; import { TdriveService } from '../../../global/framework/registry-decorator-service'; import EnvironmentService from '../../../global/framework/environment-service'; import { AuthProvider, InitParameters } from '../auth-provider'; -import ConsoleService from '@features/console/services/console-service'; -import jwtStorageService, { JWTDataType } from '@features/auth/jwt-storage-service'; import LocalStorage from '@features/global/framework/local-storage-service'; const OIDC_CALLBACK_URL = '/oidccallback'; @@ -55,11 +53,11 @@ export default class OIDCAuthProviderService scope: 'openid profile email address phone offline_access', post_logout_redirect_uri: getAsFrontUrl(OIDC_SIGNOUT_URL), silent_redirect_uri: getAsFrontUrl(OIDC_SILENT_URL), - automaticSilentRenew: true, + automaticSilentRenew: false, loadUserInfo: true, accessTokenExpiringNotificationTime: 10, filterProtocolClaims: true, - monitorSession: false, + monitorSession: true, }); // For logout if signout or logout endpoint called @@ -69,6 +67,14 @@ export default class OIDCAuthProviderService this.signOut(); } + this.userManager.events.addUserSignedIn(() => { + this.logger.debug('User Signed In:'); + }); + + this.userManager.events.addUserSignedOut(() => { + this.logger.debug('User Signed Out:'); + }); + this.userManager.events.addUserLoaded((user: any, ...args) => { this.logger.debug('New User Loaded:', user, args); this.logger.debug('Acess_token: ', user.access_token); @@ -94,7 +100,7 @@ export default class OIDCAuthProviderService this.logger.info('Signin'); try { - await this.userManager!.signinRedirectCallback(); + await this.userManager?.signinRedirectCallback(); } catch (e) { console.log('Not connected, connect through SSO'); } @@ -102,22 +108,10 @@ export default class OIDCAuthProviderService const user = await this.userManager?.getUser(); if (user) { - this.getJWTFromOidcToken(user, (err, jwt) => { - if (err) { - this.logger.error( - 'OIDC user loaded listener, error while getting the JWT from OIDC token', - err, - ); - this.signinRedirect(); - } - if (!this.initialized) { this.onInitialized(); this.initialized = true; - } else { - jwt && this.params!.onNewToken(jwt); } - }); } else { this.userManager?.signinRedirect(); } @@ -148,45 +142,6 @@ export default class OIDCAuthProviderService } } - /** - * Try to get a new JWT token from the OIDC one: - * Call the backend with the OIDC token, it will use it to get a new token from console - */ - private async getJWTFromOidcToken( - user: Oidc.User, - callback: (err?: Error, accessToken?: JWTDataType) => void, - ): Promise { - if (!user) { - this.logger.info('getJWTFromOidcToken, Cannot getJWTFromOidcToken with a null user'); - callback(new Error('Cannot getJWTFromOidcToken with a null user')); - return; - } - - if (user.expired) { - // TODO: try to get a new token from refresh one before asking for a JWT token - this.logger.info('getJWTFromOidcToken, user expired'); - } - - ConsoleService.getNewAccessToken( - { id_token: user.id_token, access_token: user.access_token }, - callback, - ); - } - - signinRedirect() { - if (document.location.href.indexOf('/login') === -1) { - //Save requested URL for after redirect / sign-in - LocalStorage.setItem('requested_url', { - url: document.location.href, - time: new Date().getTime(), - }); - } - - jwtStorageService.clear(); - - if (this.userManager) this.userManager.signinRedirect(); - } - onInitialized() { //If user requested an url in the last 10 minutes, we open it const ref = LocalStorage.getItem('requested_url') as { url: string; time: number };