Skip to content

Commit

Permalink
Merge pull request #1592 from damienbod/adding-silent-renew-error-event
Browse files Browse the repository at this point in the history
adding silent renew error event
  • Loading branch information
damienbod authored Nov 20, 2022
2 parents 3d39180 + c777420 commit 6eb8fd1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export enum EventTypes {
TokenExpired,
IdTokenExpired,
SilentRenewStarted,
SilentRenewFailed,
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { FlowsDataService } from '../flows/flows-data.service';
import { ResetAuthDataService } from '../flows/reset-auth-data.service';
import { RefreshSessionIframeService } from '../iframe/refresh-session-iframe.service';
import { LoggerService } from '../logging/logger.service';
import { EventTypes } from '../public-events/event-types';
import { PublicEventsService } from '../public-events/public-events.service';
import { StoragePersistenceService } from '../storage/storage-persistence.service';
import { UserService } from '../user-data/user.service';
Expand All @@ -28,6 +29,7 @@ describe('PeriodicallyTokenCheckService', () => {
let storagePersistenceService: StoragePersistenceService;
let resetAuthDataService: ResetAuthDataService;
let configurationService: ConfigurationService;
let publicEventsService: PublicEventsService;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -46,7 +48,7 @@ describe('PeriodicallyTokenCheckService', () => {
{ provide: RefreshSessionRefreshTokenService, useClass: mockClass(RefreshSessionRefreshTokenService) },
IntervalService,
{ provide: StoragePersistenceService, useClass: mockClass(StoragePersistenceService) },
PublicEventsService,
{ provide: PublicEventsService, useClass: mockClass(PublicEventsService) },
{ provide: ConfigurationService, useClass: mockClass(ConfigurationService) },
],
});
Expand All @@ -62,6 +64,7 @@ describe('PeriodicallyTokenCheckService', () => {
userService = TestBed.inject(UserService);
storagePersistenceService = TestBed.inject(StoragePersistenceService);
resetAuthDataService = TestBed.inject(ResetAuthDataService);
publicEventsService = TestBed.inject(PublicEventsService);
configurationService = TestBed.inject(ConfigurationService);
});

Expand Down Expand Up @@ -137,6 +140,29 @@ describe('PeriodicallyTokenCheckService', () => {
expect(resetSilentRenewRunning).toHaveBeenCalledOnceWith(configs[0]);
}));

it('interval throws silent renew failed event with data in case of an error', fakeAsync(() => {
const configs = [{ silentRenew: true, configId: 'configId1', tokenRefreshInSeconds: 1 }];

spyOn(intervalService, 'startPeriodicTokenCheck').and.returnValue(of(null));
spyOn(periodicallyTokenCheckService as any, 'shouldStartPeriodicallyCheckForConfig').and.returnValue(true);
spyOn(flowsDataService, 'resetSilentRenewRunning');
const publicEventsServiceSpy = spyOn(publicEventsService, 'fireEvent');

spyOn(flowHelper, 'isCurrentFlowCodeFlowWithRefreshTokens').and.returnValue(true);
spyOn(refreshSessionRefreshTokenService, 'refreshSessionWithRefreshTokens').and.returnValue(throwError(() => new Error('error')));
spyOn(configurationService, 'getOpenIDConfiguration').and.returnValue(of(configs[0]));

periodicallyTokenCheckService.startTokenValidationPeriodically(configs, configs[0]);

tick(1000);

expect(periodicallyTokenCheckService.startTokenValidationPeriodically).toThrowError();
expect(publicEventsServiceSpy.calls.allArgs()).toEqual([
[EventTypes.SilentRenewStarted],
[EventTypes.SilentRenewFailed, new Error('error')],
]);
}));

it('calls resetAuthorizationData and returns if no silent renew is configured', fakeAsync(() => {
const configs = [{ silentRenew: true, configId: 'configId1', tokenRefreshInSeconds: 1 }];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class PeriodicallyTokenCheckService {
});
}

private getRefreshEvent(config: OpenIdConfiguration, allConfigs: OpenIdConfiguration[]): Observable<any> {
private getRefreshEvent(config: OpenIdConfiguration, allConfigs: OpenIdConfiguration[]): Observable<boolean | CallbackContext> {
const shouldStartRefreshEvent = this.shouldStartPeriodicallyCheckForConfig(config);

if (!shouldStartRefreshEvent) {
Expand All @@ -92,6 +92,7 @@ export class PeriodicallyTokenCheckService {
const refreshEventWithErrorHandler$ = refreshEvent$.pipe(
catchError((error) => {
this.loggerService.logError(config, 'silent renew failed!', error);
this.publicEventsService.fireEvent(EventTypes.SilentRenewFailed, error);
this.flowsDataService.resetSilentRenewRunning(config);

return throwError(() => new Error(error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export enum EventTypes {
TokenExpired,
IdTokenExpired,
SilentRenewStarted,
SilentRenewFailed,
}

0 comments on commit 6eb8fd1

Please sign in to comment.