Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added event to throw when config could not be loaded #929

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { TestBed, waitForAsync } from '@angular/core/testing';
import { of } from 'rxjs';
import { of, throwError } from 'rxjs';
import { DataService } from '../api/data.service';
import { DataServiceMock } from '../api/data.service-mock';
import { EventTypes } from '../public-events/event-types';
import { PublicEventsService } from '../public-events/public-events.service';
import { StoragePersistanceService } from '../storage/storage-persistance.service';
import { StoragePersistanceServiceMock } from '../storage/storage-persistance.service-mock';
import { AuthWellKnownDataService } from './auth-well-known-data.service';
Expand All @@ -11,6 +13,7 @@ describe('AuthWellKnownService', () => {
let service: AuthWellKnownService;
let dataService: AuthWellKnownDataService;
let storagePersistanceService: StoragePersistanceService;
let publicEventsService: PublicEventsService;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -19,6 +22,7 @@ describe('AuthWellKnownService', () => {
{ provide: StoragePersistanceService, useClass: StoragePersistanceServiceMock },
{ provide: DataService, useClass: DataServiceMock },
AuthWellKnownDataService,
PublicEventsService,
],
});
});
Expand All @@ -27,6 +31,7 @@ describe('AuthWellKnownService', () => {
service = TestBed.inject(AuthWellKnownService);
dataService = TestBed.inject(AuthWellKnownDataService);
storagePersistanceService = TestBed.inject(StoragePersistanceService);
publicEventsService = TestBed.inject(PublicEventsService);
});

it('should create', () => {
Expand Down Expand Up @@ -71,5 +76,20 @@ describe('AuthWellKnownService', () => {
});
})
);

it(
'throws `ConfigLoadingFailed` event when error happens from http',
waitForAsync(() => {
spyOn(dataService, 'getWellKnownEndPointsFromUrl').and.returnValue(throwError('This is an error'));
const publicEventsServiceSpy = spyOn(publicEventsService, 'fireEvent');
service.getAuthWellKnownEndPoints('any-url').subscribe({
error: (err) => {
expect(err).toBeTruthy();
expect(publicEventsServiceSpy).toHaveBeenCalledTimes(1);
expect(publicEventsServiceSpy).toHaveBeenCalledWith(EventTypes.ConfigLoadingFailed, null);
},
});
})
);
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Injectable } from '@angular/core';
import { of } from 'rxjs';
import { tap } from 'rxjs/operators';
import { of, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { EventTypes } from '../public-events/event-types';
import { PublicEventsService } from '../public-events/public-events.service';
import { StoragePersistanceService } from '../storage/storage-persistance.service';
import { AuthWellKnownDataService } from './auth-well-known-data.service';
import { AuthWellKnownEndpoints } from './auth-well-known-endpoints';
import { PublicConfiguration } from './public-configuration';

@Injectable()
export class AuthWellKnownService {
constructor(private dataService: AuthWellKnownDataService, private storagePersistanceService: StoragePersistanceService) {}
constructor(
private publicEventsService: PublicEventsService,
private dataService: AuthWellKnownDataService,
private storagePersistanceService: StoragePersistanceService
) {}

getAuthWellKnownEndPoints(authWellknownEndpoint: string) {
const alreadySavedWellKnownEndpoints = this.storagePersistanceService.read('authWellKnownEndPoints');
Expand All @@ -16,7 +23,11 @@ export class AuthWellKnownService {
}

return this.getWellKnownEndPointsFromUrl(authWellknownEndpoint).pipe(
tap((mappedWellKnownEndpoints) => this.storeWellKnownEndpoints(mappedWellKnownEndpoints))
tap((mappedWellKnownEndpoints) => this.storeWellKnownEndpoints(mappedWellKnownEndpoints)),
catchError((error) => {
this.publicEventsService.fireEvent<PublicConfiguration>(EventTypes.ConfigLoadingFailed, null);
return throwError(error);
})
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import { PublicConfiguration } from './public-configuration';
@Injectable()
export class OidcConfigService {
constructor(
private readonly loggerService: LoggerService,
private readonly publicEventsService: PublicEventsService,
private readonly configurationProvider: ConfigurationProvider,
private readonly authWellKnownService: AuthWellKnownService,
private loggerService: LoggerService,
private publicEventsService: PublicEventsService,
private configurationProvider: ConfigurationProvider,
private authWellKnownService: AuthWellKnownService,
private storagePersistanceService: StoragePersistanceService,
private configValidationService: ConfigValidationService
) {}

withConfig(passedConfig: OpenIdConfiguration, passedAuthWellKnownEndpoints?: AuthWellKnownEndpoints): Promise<any> {
withConfig(passedConfig: OpenIdConfiguration, passedAuthWellKnownEndpoints?: AuthWellKnownEndpoints): Promise<void> {
return new Promise((resolve, reject) => {
if (!this.configValidationService.validateConfig(passedConfig)) {
this.loggerService.logError('Validation of config rejected with errors. Config is NOT set.');
return resolve();
resolve();
}

if (!passedConfig.authWellknownEndpoint) {
Expand All @@ -43,7 +43,7 @@ export class OidcConfigService {
wellknown: alreadyExistingAuthWellKnownEndpoints,
});

return resolve();
resolve();
}

if (!!passedAuthWellKnownEndpoints) {
Expand All @@ -53,9 +53,8 @@ export class OidcConfigService {
wellknown: passedAuthWellKnownEndpoints,
});

return resolve();
resolve();
}

if (usedConfig.eagerLoadAuthWellKnownEndpoints) {
this.authWellKnownService
.getAuthWellKnownEndPoints(usedConfig.authWellknownEndpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum EventTypes {
* This only works in the AppModule Constructor
*/
ConfigLoaded,
ConfigLoadingFailed,
CheckSessionReceived,
UserDataChanged,
NewAuthorizationResult,
Expand Down
5 changes: 5 additions & 0 deletions projects/sample-code-flow/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ export class AppModule {
.registerForEvents()
.pipe(filter((notification) => notification.type === EventTypes.ConfigLoaded))
.subscribe((config) => console.log('ConfigLoaded', config));

this.eventService
.registerForEvents()
.pipe(filter((notification) => notification.type === EventTypes.ConfigLoadingFailed))
.subscribe(({ value }) => console.log('ConfigLoadingFailed', value));
}
}