diff --git a/http-interceptor.service.ts b/http-interceptor.service.ts index a2d3ae12..00c4cfb2 100644 --- a/http-interceptor.service.ts +++ b/http-interceptor.service.ts @@ -4,15 +4,17 @@ import { Observable, of } from 'rxjs'; import { tap } from 'rxjs/operators'; import { TransferState, makeStateKey, StateKey } from '@angular/platform-browser'; import { isPlatformServer } from '@angular/common'; +import {properties} from "../../environments/environment"; @Injectable() export class HttpInterceptorService implements HttpInterceptor { + private static HTTP_WHITELIST = [ properties.monitorServiceAPIURL + '/stakeholder' ]; + constructor(private transferState: TransferState, @Inject(PLATFORM_ID) private platformId: any) {} public intercept(request: HttpRequest, next: HttpHandler): Observable> { - //console.info("intercept transferstate"); - if (request.method !== 'GET') { + if (request.method !== 'GET' || this.isService(request, HttpInterceptorService.HTTP_WHITELIST)) { return next.handle(request); } @@ -38,4 +40,18 @@ export class HttpInterceptorService implements HttpInterceptor { } } } + + isService(req: HttpRequest, service: string | string[]):boolean { + if(Array.isArray(service)) { + return !!service.find(element => req.url.indexOf(element) !== -1); + } else { + return req.url.indexOf(service) !== -1; + } + } + + // public cleanRequest(requestUrl: string) { + // console.log("cleaned"); + // const key: StateKey = makeStateKey(requestUrl); + // this.transferState.remove(key); + // } } diff --git a/login/utils/helper.class.ts b/login/utils/helper.class.ts index 28ba612e..569351f2 100644 --- a/login/utils/helper.class.ts +++ b/login/utils/helper.class.ts @@ -1,5 +1,3 @@ -import {StringUtils} from "../../utils/string-utils.class"; - export class User { email: string; firstname: string; @@ -15,8 +13,6 @@ export class User { export class Session { public static removeUser() { COOKIE.deleteCookie(COOKIE.cookieName_id); - //COOKIE.deleteCookie("openAIRESession"); - COOKIE.deleteCookie("openAIREUser"); } public static isLoggedIn(): boolean { diff --git a/services/user-management.service.ts b/services/user-management.service.ts index 5c184c03..ab3666c4 100644 --- a/services/user-management.service.ts +++ b/services/user-management.service.ts @@ -1,25 +1,33 @@ import {Injectable} from '@angular/core'; import {HttpClient} from '@angular/common/http'; -import {BehaviorSubject, from, Observable, of} from "rxjs"; +import {BehaviorSubject, from, Observable} from "rxjs"; import {COOKIE, Session, User} from "../login/utils/helper.class"; import {map} from "rxjs/operators"; import {NavigationEnd, Router} from "@angular/router"; import {properties} from "../../../environments/environment"; import {StringUtils} from "../utils/string-utils.class"; +import {CustomOptions} from "./servicesUtils/customOptions.class"; +import {AdvancedAsyncSubject} from "../utils/AdvancedAsyncSubject"; @Injectable({ providedIn: 'root' }) -export class UserManagementService{ +export class UserManagementService { - private getUserInfoSubject: BehaviorSubject = new BehaviorSubject(null); + private getUserInfoSubject: AdvancedAsyncSubject | BehaviorSubject; public fixRedirectURL: string = null; + private redirectUrl: string = null; private readonly promise: Promise; sub; routeSub; constructor(private http: HttpClient, private router: Router) { + if(properties.environment === "development") { + this.getUserInfoSubject = new AdvancedAsyncSubject(); + } else { + this.getUserInfoSubject = new BehaviorSubject(null) + } this.promise = new Promise((resolve => { this.updateUserInfo(resolve); })); @@ -32,12 +40,12 @@ export class UserManagementService{ } }); } - + clearSubscriptions() { - if(this.routeSub){ + if (this.routeSub) { this.routeSub.unsubscribe(); } - if(this.sub){ + if (this.sub) { this.sub.unsubscribe(); } } @@ -54,29 +62,32 @@ export class UserManagementService{ const token = COOKIE.getCookie('AccessToken'); if (!token) { this.getUserInfoSubject.next(null); - if(resolve) { + if (resolve) { resolve(); } } else { - this.sub = this.http.get(properties.userInfoUrl + token).pipe(map(userInfo => { + let userInfoUrl = (properties.userInfoUrl.includes("accessToken")?(properties.userInfoUrl + token):properties.userInfoUrl); + this.sub = this.http.get(userInfoUrl, CustomOptions.registryOptions()).pipe(map(userInfo => { return this.parseUserInfo(userInfo); })).subscribe(user => { this.getUserInfoSubject.next(user); - if(resolve) { + if (resolve) { resolve(); } }, error => { - this.getUserInfoSubject.next(null); - if(resolve) { - resolve(); - } - }); + if(this.getUserInfoSubject.getValue()) { + this.getUserInfoSubject.next(null); + } + if (resolve) { + resolve(); + } + }); } } private async getUserInfoAsync(): Promise { await this.promise; - if(this.sub){ + if (this.sub) { this.sub.unsubscribe(); } return this.getUserInfoSubject.getValue(); @@ -105,7 +116,7 @@ export class UserManagementService{ info.roles.forEach(role => { user.role.push(role); }); - }else{ + } else { if (info.edu_person_entitlements) { user.role = info.edu_person_entitlements; } @@ -115,7 +126,7 @@ export class UserManagementService{ } public setRedirectUrl(url: string = null) { - if(url) { + if (url) { let parts = url.split('?'); let path = properties.baseLink + parts[0]; let params = null; @@ -123,34 +134,36 @@ export class UserManagementService{ params = parts[1]; } let hash = path.indexOf("#"); - let fragment = (hash !== -1)?path.slice(hash + 1):null; - if(fragment) { + let fragment = (hash !== -1) ? path.slice(hash + 1) : null; + if (fragment) { path = path.slice(0, hash); } else { fragment = ""; } - if(!path.includes('/reload')) { + if (!path.includes('/reload')) { Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment); } + this.redirectUrl = StringUtils.URIEncode(location.protocol + "//" + location.host + this.fixRedirectURL); } else { + this.redirectUrl = StringUtils.URIEncode(location.href); Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash); } } public login() { - if(this.fixRedirectURL) { + if (this.fixRedirectURL) { this.setRedirectUrl(this.fixRedirectURL); } else { this.setRedirectUrl(); } - window.location.href = properties.loginUrl; + window.location.href = properties.loginUrl + "?redirect=" + this.redirectUrl; } public logout() { this.setRedirectUrl(); Session.removeUser(); if (properties.logoutUrl.includes('openid_logout')) { - window.location.href = properties.logoutUrl; + window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl; } else { window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href); } diff --git a/utils/AdvancedAsyncSubject.ts b/utils/AdvancedAsyncSubject.ts new file mode 100644 index 00000000..e3c657b6 --- /dev/null +++ b/utils/AdvancedAsyncSubject.ts @@ -0,0 +1,50 @@ +import {Subject, Subscriber, Subscription} from "rxjs"; + +export class AdvancedAsyncSubject extends Subject{ + private _value: T = null; + private hasNext: boolean = false; + private hasFinished: boolean = false; + + /** @deprecated This is an internal implementation detail, do not use. */ + _subscribe(subscriber: Subscriber): Subscription { + if (this.hasError) { + subscriber.error(this.thrownError); + return Subscription.EMPTY; + } else if (this.hasFinished && this.hasNext) { + subscriber.next(this._value); + } + return super._subscribe(subscriber); + } + + next(value: T): void { + if (!this.hasFinished) { + this._value = value; + this.hasNext = true; + this.finish(); + } else { + this._value = value; + super.next(value); + } + } + + get value() { + return this.getValue(); + } + + getValue() { + return this._value; + } + + error(error: any): void { + if (!this.hasFinished) { + super.error(error); + } + } + + finish(): void { + this.hasFinished = true; + if (this.hasNext) { + super.next(this._value); + } + } +}