From 3b963418a99aed4b68d36dd1f7f1a219ec746e9e Mon Sep 17 00:00:00 2001 From: Lukas Burgey Date: Tue, 23 Oct 2018 17:01:21 +0200 Subject: [PATCH] Add IdP data service --- src/app/app.module.ts | 5 ++ src/app/header/header.component.html | 7 +- src/app/header/header.component.ts | 21 +++-- src/app/idp.service.ts | 128 +++++++++++++++++++++++++++ src/app/user.service.ts | 67 ++------------ 5 files changed, 157 insertions(+), 71 deletions(-) create mode 100644 src/app/idp.service.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 7616451..6848b36 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -10,6 +10,11 @@ import {HttpClientModule, HttpClientXsrfModule} from '@angular/common/http'; import {StompRService} from '@stomp/ng2-stompjs'; import {CookieService} from 'ngx-cookie-service'; +// rxjs +import 'rxjs/add/operator/map'; +import 'rxjs/add/operator/catch'; +import 'rxjs/add/observable/of'; + // Our stuff // // services diff --git a/src/app/header/header.component.html b/src/app/header/header.component.html index fc27b3e..33bfd6c 100644 --- a/src/app/header/header.component.html +++ b/src/app/header/header.component.html @@ -5,11 +5,10 @@
-
+ - - + + {{ idp.name }} diff --git a/src/app/header/header.component.ts b/src/app/header/header.component.ts index 9ad11b4..082048c 100644 --- a/src/app/header/header.component.ts +++ b/src/app/header/header.component.ts @@ -1,8 +1,10 @@ import { Component, OnInit } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; import { UserService } from '../user.service'; import { AllAuthInfo, IdP } from '../types/types.module'; +import { IdpService } from '../idp.service'; import { DialogService } from '../dialogues/dialog.service'; @Component({ @@ -11,21 +13,24 @@ import { DialogService } from '../dialogues/dialog.service'; styleUrls: ['./header.component.css'] }) export class HeaderComponent implements OnInit { - public idps: IdP[]; + + idps$: Observable; + selectedIdP$: Observable; + public selectedIdP: IdP; constructor( + public idpService: IdpService, public userService: UserService, public dialog: DialogService, ) { + this.idps$ = this.idpService.subscribeIdps(); + this.selectedIdP$ = this.idpService.subscribeSelectedIdp(); + if (!userService.loggedIn()) { - this.userService.getIdPPreference().subscribe( - (allAuthInfo: AllAuthInfo) => { - if (allAuthInfo === null) { - return; - } - this.idps = allAuthInfo.idps; - this.selectedIdP = allAuthInfo.selected; + this.selectedIdP$.subscribe( + (idp: IdP) => { + this.selectedIdP = idp; } ); } diff --git a/src/app/idp.service.ts b/src/app/idp.service.ts new file mode 100644 index 0000000..750b259 --- /dev/null +++ b/src/app/idp.service.ts @@ -0,0 +1,128 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; + +import { Observable } from 'rxjs/Observable'; +import { BehaviorSubject } from 'rxjs/BehaviorSubject'; + +import { CookieService } from 'ngx-cookie-service'; +import {environment} from '../environments/environment'; + +import { IdP, AuthInfo } from './types/types.module'; + +@Injectable({ + providedIn: 'root' +}) +export class IdpService { + + idps: IdP[]; + idps$: BehaviorSubject; + + selectedIdp: IdP; + selectedIdp$: BehaviorSubject; + + constructor( + public http: HttpClient, + public cookieService: CookieService, + ) { + this.initializeDataService(); + } + + initializeDataService() { + if (!this.idps$) { + this.idps$ = > new BehaviorSubject(new Array()); + } + if (!this.selectedIdp$) { + this.selectedIdp$ = > new BehaviorSubject(new Object); + } + this.apiCall(); + } + + apiCall(): void { + // initialize the subject with data + this.http.get('/backend/auth/v1/info').subscribe( + (info: AuthInfo) => { + this.idps = info.idps; + this.idps$.next(this.idps); + + let preferredIdP: IdP = this.idps.find((idp: IdP) => idp.id === this.getIdPPreference()); + let defaultIdP: IdP = this.idps.find((idp: IdP) => idp.id === info.default); + + if (preferredIdP) { + this.selectedIdp = preferredIdP; + } else if (defaultIdP) { + this.selectedIdp = defaultIdP; + } else if (this.idps.length > 0) { + this.selectedIdp = this.idps[0]; + } else { + console.log("No IdPs available. Unable to login"); + } + + this.selectedIdp$.next(this.selectedIdp); + } + ); + + } + + handleError(error: any): Observable { + return Observable.of(null); + } + + subscribeIdps(): Observable { + return this.idps$.asObservable(); + } + + subscribeSelectedIdp(): Observable { + return this.selectedIdp$.asObservable(); + } + + private setIdPPreference(idp: IdP) { + this.cookieService.set(environment.idpCookieName, String(idp.id)); + } + + public getIdPPreference(): number { + return Number(this.cookieService.get(environment.idpCookieName)); + } + + /* + public getIdPs(): Observable { + return this.http.get('/backend/auth/v1/info').map( + (authInfo: t.AuthInfo) => { + return authInfo; + } + ).catch( + (error: any) => { + this.errorHandler(error); + return Observable.of(null); + } + ); + } + + public getIdPPreference(): Observable { + let idpID = Number(this.cookieService.get(environment.idpCookieName)); + + return this.idps.map( + (authInfo: t.AuthInfo) => { + let selected = authInfo.idps[0]; + + if (!idpID) { + idpID = authInfo.default; + } + for (const idp of authInfo.idps) { + if (idp.id === idpID) { + selected = idp; + } + } + return { + idps: authInfo.idps, + selected: selected, + }; + } + ).catch( + (error: any) => { + this.errorHandler(error); + return Observable.of(null); + } + ); + } + */ +} diff --git a/src/app/user.service.ts b/src/app/user.service.ts index aa5ac32..6935968 100644 --- a/src/app/user.service.ts +++ b/src/app/user.service.ts @@ -1,18 +1,14 @@ import {Injectable} from '@angular/core'; import {HttpClient, HttpErrorResponse} from '@angular/common/http'; +import {MatTableDataSource} from '@angular/material'; + import {Observable} from 'rxjs/Observable'; -import 'rxjs/add/operator/map'; -import 'rxjs/add/operator/catch'; -import 'rxjs/add/observable/of'; import {CookieService} from 'ngx-cookie-service'; -import {MatTableDataSource} from '@angular/material'; - -import {SnackBarService} from './snackbar.service'; import {StompConfig, StompRService} from '@stomp/ng2-stompjs'; import {Message} from '@stomp/stompjs'; -import {environment} from '../environments/environment'; +import {SnackBarService} from './snackbar.service'; import * as t from './types/types.module'; @@ -28,7 +24,6 @@ export class UserService { public services: t.Service[] = []; public groupMap: Map = new Map(); public groupsParsed: boolean = false; - public idps: Observable; constructor( @@ -38,6 +33,7 @@ export class UserService { private _stompService: StompRService, ) { this.update(); + /* this.idps = this.getIdPs(); // if there is only one IdP immediately use it to login @@ -48,6 +44,7 @@ export class UserService { } } ); + */ } // PRIVATE API @@ -184,10 +181,6 @@ export class UserService { } } - private setIdPPreference(idp: t.IdP) { - this.cookieService.set(environment.idpCookieName, String(idp.id)); - } - // PUBLIC API public serviceDescription(service: t.Service): string { if (service.description != "") { @@ -210,9 +203,8 @@ export class UserService { let s = this.groupMap.get(group.name); if (s == undefined) { return [] - } else { - return s } + return s } private getDeploymentByGroup(group: t.Group): t.Deployment | undefined { @@ -264,9 +256,7 @@ export class UserService { public update(): void { this.http.get('/backend/api/state').subscribe( - (data: t.StateAPIResult) => { - this.stateAPIUpdate(data); - }, + (data: t.StateAPIResult) => this.stateAPIUpdate(data), (err: HttpErrorResponse) => { console.log('Error', err); this.snackBar.open('Error receiving data from the server'); @@ -280,49 +270,8 @@ export class UserService { } } - public getIdPs(): Observable { - return this.http.get('/backend/auth/v1/info').map( - (authInfo: t.AuthInfo) => { - return authInfo; - } - ).catch( - (error: any) => { - this.errorHandler(error); - return Observable.of(null); - } - ); - } - - public getIdPPreference(): Observable { - let idpID = Number(this.cookieService.get(environment.idpCookieName)); - - return this.idps.map( - (authInfo: t.AuthInfo) => { - let selected = authInfo.idps[0]; - - if (!idpID) { - idpID = authInfo.default; - } - for (const idp of authInfo.idps) { - if (idp.id === idpID) { - selected = idp; - } - } - return { - idps: authInfo.idps, - selected: selected, - }; - } - ).catch( - (error: any) => { - this.errorHandler(error); - return Observable.of(null); - } - ); - } - public login(idp: t.IdP) { - this.setIdPPreference(idp); + //this.setIdPPreference(idp); if (!this.loggedIn()) { window.location.href = '/backend/auth/v1/request'; -- GitLab