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';
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
......
......@@ -5,11 +5,10 @@
</h1>
<div class="header-bar">
<span *ngIf="userService.loggedIn() ? false : true">
<form *ngIf="idps"
(ngSubmit)="userService.login(selectedIdP)" #loginForm="ngForm">
<form (ngSubmit)="userService.login(selectedIdP)" #loginForm="ngForm">
<mat-form-field>
<mat-select name="idp" required [(ngModel)]="selectedIdP">
<mat-option *ngFor="let idp of idps" [value]="idp">
<mat-select name="idp" required [(ngModel)]="selectedIdP">
<mat-option *ngFor="let idp of idps$ | async" [value]="idp">
{{ idp.name }}
</mat-option>
</mat-select>
......
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<IdP[]>;
selectedIdP$: Observable<IdP>;
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;
}
);
}
......
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 {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<string, t.Service[]> = new Map();
public groupsParsed: boolean = false;
public idps: Observable<t.AuthInfo>;
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<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) {
this.setIdPPreference(idp);
//this.setIdPPreference(idp);
if (!this.loggedIn()) {
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