diff --git a/lib/core/src/lib/auth/oidc/redirect-auth.service.spec.ts b/lib/core/src/lib/auth/oidc/redirect-auth.service.spec.ts index 6134b276930..f26248fc7fb 100644 --- a/lib/core/src/lib/auth/oidc/redirect-auth.service.spec.ts +++ b/lib/core/src/lib/auth/oidc/redirect-auth.service.spec.ts @@ -23,6 +23,11 @@ import { AUTH_MODULE_CONFIG } from './auth-config'; describe('RedirectAuthService', () => { let service: RedirectAuthService; + let oauthService: OAuthService; + let spyOnOauthServiceRefreshToken: jasmine.Spy; + let spyOnOauthServiceGetAccessToken: jasmine.Spy; + let spyOnOauthServiceSilentRefresh: jasmine.Spy; + const mockOAuthStorage: Partial = { getItem: jasmine.createSpy('getItem'), removeItem: jasmine.createSpy('removeItem'), @@ -37,7 +42,10 @@ describe('RedirectAuthService', () => { setupAutomaticSilentRefresh: () => { mockOauthService.silentRefresh(); mockOauthService.refreshToken(); - } + }, + refreshToken: () => Promise.resolve({} as TokenResponse), + silentRefresh: () => Promise.resolve({} as OAuthEvent), + getAccessToken: () => 'access-token' }; beforeEach(() => { @@ -51,10 +59,14 @@ describe('RedirectAuthService', () => { ] }); - TestBed.inject(OAuthService); + oauthService = TestBed.inject(OAuthService); service = TestBed.inject(RedirectAuthService); spyOn(service, 'ensureDiscoveryDocument').and.resolveTo(true); mockOauthService.getAccessToken = () => 'access-token'; + + spyOnOauthServiceGetAccessToken = spyOn(oauthService, 'getAccessToken').and.returnValue('access-token'); + spyOnOauthServiceRefreshToken = spyOn(oauthService, 'refreshToken').and.resolveTo({} as TokenResponse); + spyOnOauthServiceSilentRefresh = spyOn(oauthService, 'silentRefresh').and.resolveTo({} as OAuthEvent); }); it('should emit event when token_received event is received', () => { @@ -93,4 +105,36 @@ describe('RedirectAuthService', () => { expect(refreshTokenCalled).toBe(true); expect(silentRefreshCalled).toBe(true); }); + + it('should not refresh token if access_token not exists', async () => { + mockOauthService.setupAutomaticSilentRefresh = () => { }; + spyOnOauthServiceGetAccessToken.and.returnValue(undefined); + + await service.init(); + + expect(spyOnOauthServiceRefreshToken).not.toHaveBeenCalled(); + expect(spyOnOauthServiceSilentRefresh).not.toHaveBeenCalled(); + }); + + it('should refresh token if access_token exists and code flow is set', async () => { + mockOauthService.setupAutomaticSilentRefresh = () => { }; + spyOnOauthServiceGetAccessToken.and.returnValue('access-token'); + mockOauthService.responseType = 'implicit'; + + await service.init(); + + expect(spyOnOauthServiceRefreshToken).not.toHaveBeenCalled(); + expect(spyOnOauthServiceSilentRefresh).toHaveBeenCalled(); + }); + + it('should run silent refresh if access_token exists and implicit flow is set', async () => { + mockOauthService.setupAutomaticSilentRefresh = () => { }; + spyOnOauthServiceGetAccessToken.and.returnValue('access-token'); + mockOauthService.responseType = 'code'; + + await service.init(); + + expect(spyOnOauthServiceRefreshToken).toHaveBeenCalled(); + expect(spyOnOauthServiceSilentRefresh).not.toHaveBeenCalled(); + }); }); diff --git a/lib/core/src/lib/auth/oidc/redirect-auth.service.ts b/lib/core/src/lib/auth/oidc/redirect-auth.service.ts index edfc2d141a4..45f392badf4 100644 --- a/lib/core/src/lib/auth/oidc/redirect-auth.service.ts +++ b/lib/core/src/lib/auth/oidc/redirect-auth.service.ts @@ -169,7 +169,10 @@ export class RedirectAuthService extends AuthService { }); } - return this.ensureDiscoveryDocument().then(() => { + return this.ensureDiscoveryDocument().then(async () => { + if(this.oauthService.getAccessToken()) { + await this.refreshToken(); + } this.oauthService.setupAutomaticSilentRefresh(); return void this.allowRefreshTokenAndSilentRefreshOnMultipleTabs(); }).catch(() => { @@ -223,4 +226,13 @@ export class RedirectAuthService extends AuthService { updateIDPConfiguration(config: AuthConfig) { this.oauthService.configure(config); } + + async refreshToken(): Promise | Promise> { + try { + return await (this.oauthService.responseType === 'code' ? this.oauthService.refreshToken() : this.oauthService.silentRefresh()); + } catch (error) { + console.error('Error refreshing token:', error); + throw error; + } + } }