Commit 3b963418 authored by Lukas Burgey's avatar Lukas Burgey

Add IdP data service

parent 990378ee
...@@ -10,6 +10,11 @@ import {HttpClientModule, HttpClientXsrfModule} from '@angular/common/http'; ...@@ -10,6 +10,11 @@ import {HttpClientModule, HttpClientXsrfModule} from '@angular/common/http';
import {StompRService} from '@stomp/ng2-stompjs'; import {StompRService} from '@stomp/ng2-stompjs';
import {CookieService} from 'ngx-cookie-service'; import {CookieService} from 'ngx-cookie-service';
// rxjs
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/of';
// Our stuff // Our stuff
// //
// services // services
......
...@@ -5,11 +5,10 @@ ...@@ -5,11 +5,10 @@
</h1> </h1>
<div class="header-bar"> <div class="header-bar">
<span *ngIf="userService.loggedIn() ? false : true"> <span *ngIf="userService.loggedIn() ? false : true">
<form *ngIf="idps" <form (ngSubmit)="userService.login(selectedIdP)" #loginForm="ngForm">
(ngSubmit)="userService.login(selectedIdP)" #loginForm="ngForm">
<mat-form-field> <mat-form-field>
<mat-select name="idp" required [(ngModel)]="selectedIdP"> <mat-select name="idp" required [(ngModel)]="selectedIdP">
<mat-option *ngFor="let idp of idps" [value]="idp"> <mat-option *ngFor="let idp of idps$ | async" [value]="idp">
{{ idp.name }} {{ idp.name }}
</mat-option> </mat-option>
</mat-select> </mat-select>
......
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { UserService } from '../user.service'; import { UserService } from '../user.service';
import { AllAuthInfo, IdP } from '../types/types.module'; import { AllAuthInfo, IdP } from '../types/types.module';
import { IdpService } from '../idp.service';
import { DialogService } from '../dialogues/dialog.service'; import { DialogService } from '../dialogues/dialog.service';
@Component({ @Component({
...@@ -11,21 +13,24 @@ import { DialogService } from '../dialogues/dialog.service'; ...@@ -11,21 +13,24 @@ import { DialogService } from '../dialogues/dialog.service';
styleUrls: ['./header.component.css'] styleUrls: ['./header.component.css']
}) })
export class HeaderComponent implements OnInit { export class HeaderComponent implements OnInit {
public idps: IdP[];
idps$: Observable<IdP[]>;
selectedIdP$: Observable<IdP>;
public selectedIdP: IdP; public selectedIdP: IdP;
constructor( constructor(
public idpService: IdpService,
public userService: UserService, public userService: UserService,
public dialog: DialogService, public dialog: DialogService,
) { ) {
this.idps$ = this.idpService.subscribeIdps();
this.selectedIdP$ = this.idpService.subscribeSelectedIdp();
if (!userService.loggedIn()) { if (!userService.loggedIn()) {
this.userService.getIdPPreference().subscribe( this.selectedIdP$.subscribe(
(allAuthInfo: AllAuthInfo) => { (idp: IdP) => {
if (allAuthInfo === null) { this.selectedIdP = idp;
return;
}
this.idps = allAuthInfo.idps;
this.selectedIdP = allAuthInfo.selected;
} }
); );
} }
......
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<IdP[]>;
selectedIdp: IdP;
selectedIdp$: BehaviorSubject<IdP>;
constructor(
public http: HttpClient,
public cookieService: CookieService,
) {
this.initializeDataService();
}
initializeDataService() {
if (!this.idps$) {
this.idps$ = <BehaviorSubject<IdP[]>> new BehaviorSubject(new Array<IdP>());
}
if (!this.selectedIdp$) {
this.selectedIdp$ = <BehaviorSubject<IdP>> 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<AuthInfo> {
return Observable.of(null);
}
subscribeIdps(): Observable<IdP[]> {
return this.idps$.asObservable();
}
subscribeSelectedIdp(): Observable<IdP> {
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<t.AuthInfo> {
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<t.AllAuthInfo> {
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);
}
);
}
*/
}
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from '@angular/common/http'; import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {MatTableDataSource} from '@angular/material';
import {Observable} from 'rxjs/Observable'; 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 {CookieService} from 'ngx-cookie-service';
import {MatTableDataSource} from '@angular/material';
import {SnackBarService} from './snackbar.service';
import {StompConfig, StompRService} from '@stomp/ng2-stompjs'; import {StompConfig, StompRService} from '@stomp/ng2-stompjs';
import {Message} from '@stomp/stompjs'; import {Message} from '@stomp/stompjs';
import {environment} from '../environments/environment'; import {SnackBarService} from './snackbar.service';
import * as t from './types/types.module'; import * as t from './types/types.module';
...@@ -28,7 +24,6 @@ export class UserService { ...@@ -28,7 +24,6 @@ export class UserService {
public services: t.Service[] = []; public services: t.Service[] = [];
public groupMap: Map<string, t.Service[]> = new Map(); public groupMap: Map<string, t.Service[]> = new Map();
public groupsParsed: boolean = false; public groupsParsed: boolean = false;
public idps: Observable<t.AuthInfo>;
constructor( constructor(
...@@ -38,6 +33,7 @@ export class UserService { ...@@ -38,6 +33,7 @@ export class UserService {
private _stompService: StompRService, private _stompService: StompRService,
) { ) {
this.update(); this.update();
/*
this.idps = this.getIdPs(); this.idps = this.getIdPs();
// if there is only one IdP immediately use it to login // if there is only one IdP immediately use it to login
...@@ -48,6 +44,7 @@ export class UserService { ...@@ -48,6 +44,7 @@ export class UserService {
} }
} }
); );
*/
} }
// PRIVATE API // PRIVATE API
...@@ -184,10 +181,6 @@ export class UserService { ...@@ -184,10 +181,6 @@ export class UserService {
} }
} }
private setIdPPreference(idp: t.IdP) {
this.cookieService.set(environment.idpCookieName, String(idp.id));
}
// PUBLIC API // PUBLIC API
public serviceDescription(service: t.Service): string { public serviceDescription(service: t.Service): string {
if (service.description != "") { if (service.description != "") {
...@@ -210,9 +203,8 @@ export class UserService { ...@@ -210,9 +203,8 @@ export class UserService {
let s = this.groupMap.get(group.name); let s = this.groupMap.get(group.name);
if (s == undefined) { if (s == undefined) {
return [] return []
} else {
return s
} }
return s
} }
private getDeploymentByGroup(group: t.Group): t.Deployment | undefined { private getDeploymentByGroup(group: t.Group): t.Deployment | undefined {
...@@ -264,9 +256,7 @@ export class UserService { ...@@ -264,9 +256,7 @@ export class UserService {
public update(): void { public update(): void {
this.http.get('/backend/api/state').subscribe( this.http.get('/backend/api/state').subscribe(
(data: t.StateAPIResult) => { (data: t.StateAPIResult) => this.stateAPIUpdate(data),
this.stateAPIUpdate(data);
},
(err: HttpErrorResponse) => { (err: HttpErrorResponse) => {
console.log('Error', err); console.log('Error', err);
this.snackBar.open('Error receiving data from the server'); this.snackBar.open('Error receiving data from the server');
...@@ -280,49 +270,8 @@ export class UserService { ...@@ -280,49 +270,8 @@ export class UserService {
} }
} }
public getIdPs(): Observable<t.AuthInfo> {
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<t.AllAuthInfo> {
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) { public login(idp: t.IdP) {
this.setIdPPreference(idp); //this.setIdPPreference(idp);
if (!this.loggedIn()) { if (!this.loggedIn()) {
window.location.href = '/backend/auth/v1/request'; window.location.href = '/backend/auth/v1/request';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment