diff --git a/ui/hv2-ui/src/app/config.ts b/ui/hv2-ui/src/app/config.ts index 783dc89..7144afb 100644 --- a/ui/hv2-ui/src/app/config.ts +++ b/ui/hv2-ui/src/app/config.ts @@ -1,4 +1,5 @@ // export const serviceBaseUrl = "https://api.hv.nober.de"; // export const serviceBaseUrl = "http://172.16.10.38:5000"; -export const serviceBaseUrl = "http://localhost:8080"; +export const serviceBaseUrl = "http://localhost:8080" +export const authserviceBaseUrl = "https://authservice.hottis.de" diff --git a/ui/hv2-ui/src/app/navigation/navigation.component.html b/ui/hv2-ui/src/app/navigation/navigation.component.html index 4f76fe5..57a3be8 100644 --- a/ui/hv2-ui/src/app/navigation/navigation.component.html +++ b/ui/hv2-ui/src/app/navigation/navigation.component.html @@ -29,6 +29,7 @@ Nober Grundbesitz GbR Hausverwaltung GITTAGVERSION + Läuft aus in {{expiryTime | async }} Sekunden Login Logout diff --git a/ui/hv2-ui/src/app/navigation/navigation.component.ts b/ui/hv2-ui/src/app/navigation/navigation.component.ts index 980d0ee..3c960d7 100644 --- a/ui/hv2-ui/src/app/navigation/navigation.component.ts +++ b/ui/hv2-ui/src/app/navigation/navigation.component.ts @@ -33,6 +33,10 @@ export class NavigationComponent { }) } + get expiryTime(): Observable { + return this.tokenService.expiryTime + } + ngOnInit() { this.authenticated = this.tokenService.checkAuthenticated() } diff --git a/ui/hv2-ui/src/app/token.service.ts b/ui/hv2-ui/src/app/token.service.ts index 324aa70..06b81cb 100644 --- a/ui/hv2-ui/src/app/token.service.ts +++ b/ui/hv2-ui/src/app/token.service.ts @@ -3,7 +3,9 @@ import { MessageService } from './message.service'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { UserCreds } from './userCreds' import jwt_decode from 'jwt-decode' - +import { Observable, interval, Subject, Subscription } from 'rxjs' +import { map, takeUntil, takeWhile } from 'rxjs/operators' +import { authserviceBaseUrl } from './config' interface TokenTuple { @@ -19,6 +21,7 @@ export class TokenService { public static Id_AuthToken_Key : string = "id_authtoken"; public static Id_RefreshToken_Key : string = "id_refreshtoken"; + private _expiryTime = new Subject() constructor(private http: HttpClient, private messageService: MessageService) { } @@ -39,13 +42,24 @@ export class TokenService { return result } - logout() { + logout() : void { localStorage.removeItem(TokenService.Id_AuthToken_Key) localStorage.removeItem(TokenService.Id_RefreshToken_Key) this.messageService.add("Token removed from local storage") } - async login(login: string, password: string) { + get expiryTime(): Observable { + return this._expiryTime + } + + setExpiryTime(token: string) :void { + let exp = jwt_decode(token)["exp"] + let iat = jwt_decode(token)["iat"] + let start = exp - iat + interval(1000).pipe(map(v => start - v)).pipe(takeWhile(v => v != 0)).subscribe(v => this._expiryTime.next(v)) + } + + async login(login: string, password: string) : Promise { this.messageService.add(`TokenService: trying to login and obtain token`) const userCreds : UserCreds = { "application": "hv2", @@ -53,25 +67,29 @@ export class TokenService { "password": password } const tokenTuple: TokenTuple = await this.http.post( - "https://authservice.hottis.de/refreshable", + `${authserviceBaseUrl}/refreshable`, userCreds ).toPromise() localStorage.setItem(TokenService.Id_AuthToken_Key, tokenTuple.authToken) localStorage.setItem(TokenService.Id_RefreshToken_Key, tokenTuple.refreshToken) this.messageService.add("Token saved") + + this.setExpiryTime(tokenTuple.authToken) } - async refresh() { + async refresh() : Promise { try { this.messageService.add(`TokenService: trying to refresh tokens`) const refreshToken = localStorage.getItem(TokenService.Id_RefreshToken_Key) const tokenTuple: TokenTuple = await this.http.post( - "https://authservice.hottis.de/refresh", + `${authserviceBaseUrl}/refresh`, refreshToken ).toPromise() localStorage.setItem(TokenService.Id_AuthToken_Key, tokenTuple.authToken) localStorage.setItem(TokenService.Id_RefreshToken_Key, tokenTuple.refreshToken) this.messageService.add("Token saved") + + this.setExpiryTime(tokenTuple.authToken) } catch (err) { this.messageService.add(`error when trying to refresh: ${ JSON.stringify(err, undefined, 4)}`) localStorage.removeItem(TokenService.Id_AuthToken_Key)