Skip to content
This repository was archived by the owner on Sep 15, 2023. It is now read-only.

Commit 1447b57

Browse files
authored
Merge pull request #21 from admin-ch/fixups
Fixups
2 parents 3c951e4 + b2a842b commit 1447b57

15 files changed

Lines changed: 88 additions & 18 deletions

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ha-ui",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"scripts": {
55
"start": "ng serve",
66
"build": "ng build",

src/app/app.module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
multiTranslateLoader,
1414
ObDocumentMetaService,
1515
ObHttpApiInterceptor,
16+
ObHttpApiInterceptorConfig,
1617
ObMasterLayoutConfig,
1718
ObMasterLayoutModule,
1819
ObOffCanvasModule
@@ -63,7 +64,12 @@ registerLocaleData(localeENGB);
6364
schemas: [CUSTOM_ELEMENTS_SCHEMA]
6465
})
6566
export class AppModule {
66-
constructor(private readonly config: ObMasterLayoutConfig, meta: ObDocumentMetaService) {
67+
constructor(
68+
private readonly config: ObMasterLayoutConfig,
69+
meta: ObDocumentMetaService,
70+
interceptor: ObHttpApiInterceptorConfig
71+
) {
72+
interceptor.api.url = '/v1/authcode';
6773
meta.titleSuffix = 'application.title';
6874
meta.description = 'application.description';
6975
config.layout.hasMainNavigation = false;

src/app/auth/auth-guard.service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('AuthGuardService', () => {
9393

9494
it('should redirect to auto-login', done => {
9595
service.canLoad(null).subscribe(a => {
96-
expect(window.location.href).toEqual('https://www.eiam.admin.ch/?c=f!403pts!pub&l=en');
96+
expect(window.location.href).toEqual('https://www.eiam.admin.ch/403pts?l=en&stage=');
9797
done();
9898
});
9999
});
@@ -122,7 +122,7 @@ describe('AuthGuardService', () => {
122122

123123
it('should redirect to auto-login', done => {
124124
service.canLoad(null).subscribe(a => {
125-
expect(window.location.href).toEqual('https://www.eiam.admin.ch/chloginforbidden?l=en');
125+
expect(window.location.href).toEqual('https://www.eiam.admin.ch/chloginforbidden?l=en&stage=');
126126
done();
127127
});
128128
});

src/app/auth/auth-guard.service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {map, take} from 'rxjs/operators';
1313
import {TranslateService} from '@ngx-translate/core';
1414
import {WINDOW} from '@oblique/oblique';
1515
import {Claims, OauthService} from './oauth.service';
16+
import {environment} from '../../environments/environment';
1617

1718
export enum Role {
1819
HaUI = 'bag-pts-allow'
@@ -22,12 +23,15 @@ export enum Role {
2223
providedIn: 'root'
2324
})
2425
export class AuthGuardService implements CanActivate, CanActivateChild, CanLoad {
26+
private readonly stage: string;
2527
constructor(
2628
private readonly oauthService: OauthService,
2729
private readonly router: Router,
2830
private readonly translate: TranslateService,
2931
@Inject(WINDOW) private readonly window
30-
) {}
32+
) {
33+
this.stage = environment.stage;
34+
}
3135

3236
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
3337
return this.checkExpectedRole();
@@ -56,12 +60,12 @@ export class AuthGuardService implements CanActivate, CanActivateChild, CanLoad
5660

5761
const hasAccess = this.oauthService.hasUserRole(Role.HaUI, claims);
5862
if (!hasAccess) {
59-
this.window.location.href = `https://www.eiam.admin.ch/?c=f!403pts!pub&l=${this.translate.currentLang}`;
63+
this.window.location.href = `https://www.eiam.admin.ch/403pts?l=${this.translate.currentLang}&stage=${this.stage}`;
6064
return false;
6165
}
6266

6367
if (claims.homeName === 'E-ID CH-LOGIN' && claims.unitName?.indexOf('HIN') === 0) {
64-
this.window.location.href = `https://www.eiam.admin.ch/chloginforbidden?l=${this.translate.currentLang}`;
68+
this.window.location.href = `https://www.eiam.admin.ch/chloginforbidden?l=${this.translate.currentLang}&stage=${this.stage}`;
6569
return false;
6670
}
6771
return hasAccess;

src/app/auth/oauth-service.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {TestBed} from '@angular/core/testing';
1+
import {fakeAsync, TestBed} from '@angular/core/testing';
22
import {EMPTY, of} from 'rxjs';
3+
import {skip} from 'rxjs/operators';
34
import {LoggerService, OidcSecurityService} from 'angular-auth-oidc-client';
45
import {OauthService} from './oauth.service';
56
import {OpenIdConfigService} from './open-id-config-service';
@@ -50,6 +51,15 @@ describe('OauthService', () => {
5051
expect(service.name$).toBeDefined();
5152
});
5253

54+
it('should not emit if no claims', fakeAsync(() => {
55+
let name;
56+
// @ts-ignore
57+
service.claims.next(undefined);
58+
service.name$.subscribe(n => (name = n));
59+
skip(1000);
60+
expect(name).toBeUndefined();
61+
}));
62+
5363
it('should emit name if present', done => {
5464
// @ts-ignore
5565
service.claims.next({name: 'test'});

src/app/auth/oauth.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export class OauthService {
5252
) {
5353
this.claims$ = this.claims.asObservable();
5454
this.isAuthenticated$ = this.isAuthenticated.asObservable();
55-
this.name$ = this.claims$.pipe(map(claims => claims.displayName || claims.name));
55+
this.name$ = this.claims$.pipe(
56+
filter(claims => !!claims),
57+
map(claims => claims.displayName || claims.name)
58+
);
5659
}
5760

5861
logout(): void {

src/app/home/home.component.spec.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('HomeComponent', () => {
2525
currentLang: 'en'
2626
}
2727
},
28-
{provide: OauthService, useValue: {logout: jest.fn()}}
28+
{provide: OauthService, useValue: {logout: jest.fn(), isAuthenticated$: of(false)}}
2929
],
3030
schemas: [NO_ERRORS_SCHEMA],
3131
declarations: [HomeComponent]
@@ -73,7 +73,7 @@ describe('HomeComponent', () => {
7373
});
7474
});
7575

76-
describe('logout access', () => {
76+
describe('logout access while authenticated', () => {
7777
beforeEach(async(() => {
7878
TestBed.configureTestingModule({
7979
imports: [ObliqueTestingModule],
@@ -85,7 +85,7 @@ describe('HomeComponent', () => {
8585
currentLang: 'en'
8686
}
8787
},
88-
{provide: OauthService, useValue: {logout: jest.fn()}},
88+
{provide: OauthService, useValue: {logout: jest.fn(), isAuthenticated$: of(true)}},
8989
{provide: ActivatedRoute, useValue: {data: of({logout: true})}}
9090
],
9191
schemas: [NO_ERRORS_SCHEMA],
@@ -103,9 +103,45 @@ describe('HomeComponent', () => {
103103
expect(component).toBeTruthy();
104104
});
105105

106-
it('should logout', () => {
106+
it('should not logout', () => {
107107
const oauth = TestBed.inject(OauthService);
108108
expect(oauth.logout).toHaveBeenCalled();
109109
});
110110
});
111+
112+
describe('logout access while not authenticated', () => {
113+
beforeEach(async(() => {
114+
TestBed.configureTestingModule({
115+
imports: [ObliqueTestingModule],
116+
providers: [
117+
{
118+
provide: TranslateService,
119+
useValue: {
120+
onLangChange: new EventEmitter<LangChangeEvent>(),
121+
currentLang: 'en'
122+
}
123+
},
124+
{provide: OauthService, useValue: {logout: jest.fn(), isAuthenticated$: of(false)}},
125+
{provide: ActivatedRoute, useValue: {data: of({logout: true})}}
126+
],
127+
schemas: [NO_ERRORS_SCHEMA],
128+
declarations: [HomeComponent]
129+
}).compileComponents();
130+
}));
131+
132+
beforeEach(() => {
133+
fixture = TestBed.createComponent(HomeComponent);
134+
component = fixture.componentInstance;
135+
fixture.detectChanges();
136+
});
137+
138+
it('should create', () => {
139+
expect(component).toBeTruthy();
140+
});
141+
142+
it('should logout', () => {
143+
const oauth = TestBed.inject(OauthService);
144+
expect(oauth.logout).not.toHaveBeenCalled();
145+
});
146+
});
111147
});

src/app/home/home.component.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Component} from '@angular/core';
22
import {TranslateService} from '@ngx-translate/core';
33
import {ActivatedRoute} from '@angular/router';
4-
import {filter, map, startWith} from 'rxjs/operators';
4+
import {filter, map, startWith, switchMap} from 'rxjs/operators';
55
import {Observable} from 'rxjs';
66
import {OauthService} from '../auth/oauth.service';
77
import {environment} from '../../environments/environment';
@@ -18,7 +18,13 @@ export class HomeComponent {
1818
showWarning = environment.showWarning;
1919

2020
constructor(translate: TranslateService, route: ActivatedRoute, oauthService: OauthService) {
21-
route.data.pipe(filter(data => data.logout)).subscribe(() => oauthService.logout());
21+
oauthService.isAuthenticated$
22+
.pipe(
23+
filter(isAuthenticated => isAuthenticated),
24+
switchMap(() => route.data),
25+
filter(data => data.logout)
26+
)
27+
.subscribe(() => oauthService.logout());
2228
this.lang$ = translate.onLangChange.pipe(
2329
map(lang => lang.lang),
2430
startWith(translate.currentLang)

src/environments/environment.abn.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {LogLevel} from 'angular-auth-oidc-client';
44
export const environment = {
55
production: true,
66
showWarning: true,
7+
stage: 'a',
78
host: 'https://codegen-service-a.bag.admin.ch',
89
eiamSelfAdmin:
910
'https://sts-a.pts.admin.ch/_pep/myaccount?returnURL=https%3A%2F%2Fwww.covidcode-a.admin.chCURRENT_PAGE&language=LANGUAGE',

0 commit comments

Comments
 (0)