import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; 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 { environment } from '../environments/environment'; import { IdP, User, Service, AuthInfo, AllAuthInfo, UserState, SSHKey, NewSSHKey } from './types/types.module'; @Injectable() export class UserService { public loggedIn = false; public user: User; public sshKeyData: MatTableDataSource; public userInfoData: MatTableDataSource; public services: Service[]; constructor( public cookieService: CookieService, public http: HttpClient, public snackBar: SnackBarService, ) { this.update(); } public setIdPPreference(idp: IdP) { this.cookieService.set(environment.idpCookieName, String(idp.id)); } public getIdPPreference(): Observable { let idpID = Number(this.cookieService.get(environment.idpCookieName)); return this.http.get('/backend/auth/v1/info/').map( (authInfo: AuthInfo) => { let selected = authInfo.idps[1]; if (!idpID) { idpID = authInfo.default; } for (const idp of authInfo.idps) { if (idp.id === idpID) { selected = idp; } } return { idps: authInfo.idps, selected: selected, }; } ); } public updateData(data: any) { if (data['error']) { this.snackBar.open(data['error']); } if (!this.loggedIn && data.logged_in) { this.snackBar.open('Logged in'); } this.loggedIn = data.logged_in; if (!this.services) { this.services = data.services; } this.user = data.user; if (this.user) { // build sshKeyData if ('ssh_keys' in this.user) { this.sshKeyData = new MatTableDataSource(this.user.ssh_keys); } else { this.sshKeyData = null; } // build userInfoData const userInfoList = []; for (const key in this.user.userinfo) { if (this.user.userinfo.hasOwnProperty(key)) { userInfoList.push({name: key, info: this.user.userinfo[key]}); } } this.userInfoData = new MatTableDataSource(userInfoList); } else { this.sshKeyData = null; this.userInfoData = null; } } public updateState() { this.http .get('/backend/api/state/') .subscribe( (data: UserState) => { this.updateData(data); }, (err: HttpErrorResponse) => { console.log('Error', err); this.snackBar.open('Error receiving data from the server'); } ); } public update() { this.updateState(); } public login(idp: IdP) { this.setIdPPreference(idp); window.location.href = 'https://hdf-portal.data.kit.edu/backend/auth/v1/request'; } public logout() { this.http.post('/backend/auth/v1/logout/', {}).subscribe( data => { this.updateData(data); } ); } public addSshKey(key: NewSSHKey) { const body = { 'type': 'add', 'key': key, }; return this.http.post('/backend/api/sshkey/', body).subscribe( data => { this.updateData(data); }, (err) => { this.snackBar.open('Error uploading key'); console.log(err); this.update(); } ); } public removeSshKey(key: SSHKey) { const body = { 'type': 'remove', 'id': key.id, }; return this.http.post('/backend/api/sshkey/', body).subscribe( (data) => { this.updateData(data); }, (err) => { this.snackBar.open('Error deleting key'); console.log(err); this.update(); } ); } public addDeployment(service: Service, key: SSHKey) { const body = { 'type': 'add', 'key': key.id, 'service': service.id, }; return this.http.post('/backend/api/deployments/', body).subscribe( (data) => { this.snackBar.open('Deployed key ' + key.name); this.updateData(data); }, (err) => { this.snackBar.open('Error deploying key ' + key.name); console.log(err); this.update(); } ); } public removeDeployment(service: Service, key: SSHKey) { const body = { 'type': 'remove', 'key': key.id, 'service': service.id, }; return this.http.post('/backend/api/deployments/', body).subscribe( (data) => { this.snackBar.open('Withdrew key ' + key.name); this.updateData(data); }, (err) => { this.snackBar.open('Error withdrawing key ' + key.name); console.log(err); this.update(); } ); } }