Skip to content

Commit

Permalink
AAE-24139 Refresh the token if access token is set
Browse files Browse the repository at this point in the history
  • Loading branch information
alep85 committed Aug 8, 2024
1 parent aa75a26 commit 97692dd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
48 changes: 46 additions & 2 deletions lib/core/src/lib/auth/oidc/redirect-auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OAuthStorage> = {
getItem: jasmine.createSpy('getItem'),
removeItem: jasmine.createSpy('removeItem'),
Expand All @@ -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(() => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
14 changes: 13 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 @@ -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(() => {
Expand Down Expand Up @@ -223,4 +226,13 @@ export class RedirectAuthService extends AuthService {
updateIDPConfiguration(config: AuthConfig) {
this.oauthService.configure(config);
}

async refreshToken(): Promise<Promise<TokenResponse> | Promise<OAuthEvent>> {
try {
return await (this.oauthService.responseType === 'code' ? this.oauthService.refreshToken() : this.oauthService.silentRefresh());
} catch (error) {
console.error('Error refreshing token:', error);
throw error;
}
}
}

0 comments on commit 97692dd

Please sign in to comment.