Skip to content

Commit

Permalink
AAE-24139 test all auth items are removed from the storage when acces…
Browse files Browse the repository at this point in the history
…sToken is set and token is not valid
  • Loading branch information
alep85 committed Sep 4, 2024
1 parent 33150a6 commit 3907745
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/core/src/lib/auth/oidc/redirect-auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('RedirectAuthService', () => {
events: oauthEvents$,
configure: () => {},
hasValidAccessToken: jasmine.createSpy().and.returnValue(true),
hasValidIdToken: jasmine.createSpy().and.returnValue(true),
setupAutomaticSilentRefresh: () => {
mockOauthService.silentRefresh();
mockOauthService.refreshToken();
Expand All @@ -53,6 +54,7 @@ describe('RedirectAuthService', () => {

TestBed.inject(OAuthService);
service = TestBed.inject(RedirectAuthService);
spyOn(service, 'reloadPage').and.callFake(() => {});
spyOn(service, 'ensureDiscoveryDocument').and.resolveTo(true);
mockOauthService.getAccessToken = () => 'access-token';
});
Expand Down Expand Up @@ -93,4 +95,25 @@ describe('RedirectAuthService', () => {
expect(refreshTokenCalled).toBe(true);
expect(silentRefreshCalled).toBe(true);
});

it('should remove all auth items from the storage if access token is set and is not authenticated', () => {
mockOauthService.getAccessToken = () => 'access-token';
spyOnProperty(service, 'authenticated', 'get').and.returnValue(false);
(mockOauthService.events as Subject<OAuthEvent>).next({ type: 'discovery_document_loaded' } as OAuthEvent);

expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('access_token');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('access_token_stored_at');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('expires_at');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('granted_scopes');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('id_token');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('id_token_claims_obj');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('id_token_expires_at');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('id_token_stored_at');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('nonce');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('PKCE_verifier');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('refresh_token');
expect(mockOAuthStorage.removeItem).toHaveBeenCalledWith('session_state');
expect(service.reloadPage).toHaveBeenCalledOnceWith();
});

});
7 changes: 6 additions & 1 deletion lib/core/src/lib/auth/oidc/redirect-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class RedirectAuthService extends AuthService {
this.oauthService.events.pipe(take(1)).subscribe(() => {
if(this.oauthService.getAccessToken() && !this.authenticated){
this.AUTH_STORAGE_ITEMS.map((item: string) => { this._oauthStorage.removeItem(item); });
window.location.reload();
this.reloadPage();
}
});

Expand Down Expand Up @@ -245,4 +245,9 @@ export class RedirectAuthService extends AuthService {
updateIDPConfiguration(config: AuthConfig) {
this.oauthService.configure(config);
}

reloadPage() {
window.location.reload();
}

}

0 comments on commit 3907745

Please sign in to comment.