import { Component, Inject, OnInit, } from '@angular/core'; import { Observable } from 'rxjs'; import { select, Store } from '@ngrx/store'; import { renderAuthMethodFor } from '../log-in.methods-decorator'; import { AuthMethodType } from '../../../../core/auth/models/auth.method-type'; import { AuthMethod } from '../../../../core/auth/models/auth.method'; import { CoreState } from '../../../../core/core.reducers'; import { isAuthenticated, isAuthenticationLoading } from '../../../../core/auth/selectors'; import { RouteService } from '../../../../core/services/route.service'; import { NativeWindowRef, NativeWindowService } from '../../../../core/services/window.service'; import { isNotNull } from '../../../empty.util'; import { AuthService } from '../../../../core/auth/auth.service'; import { HardRedirectService } from '../../../../core/services/hard-redirect.service'; @Component({ selector: 'ds-log-in-shibboleth', templateUrl: './log-in-shibboleth.component.html', styleUrls: ['./log-in-shibboleth.component.scss'], }) @renderAuthMethodFor(AuthMethodType.Shibboleth) export class LogInShibbolethComponent implements OnInit { /** * The authentication method data. * @type {AuthMethod} */ public authMethod: AuthMethod; /** * True if the authentication is loading. * @type {boolean} */ public loading: Observable; /** * The shibboleth authentication location url. * @type {string} */ public location: string; /** * Whether user is authenticated. * @type {Observable} */ public isAuthenticated: Observable; /** * @constructor * @param {AuthMethod} injectedAuthMethodModel * @param {boolean} isStandalonePage * @param {NativeWindowRef} _window * @param {RouteService} route * @param {AuthService} authService * @param {HardRedirectService} hardRedirectService * @param {Store} store */ constructor( @Inject('authMethodProvider') public injectedAuthMethodModel: AuthMethod, @Inject('isStandalonePage') public isStandalonePage: boolean, @Inject(NativeWindowService) protected _window: NativeWindowRef, private route: RouteService, private authService: AuthService, private hardRedirectService: HardRedirectService, private store: Store ) { this.authMethod = injectedAuthMethodModel; } ngOnInit(): void { // set isAuthenticated this.isAuthenticated = this.store.pipe(select(isAuthenticated)); // set loading this.loading = this.store.pipe(select(isAuthenticationLoading)); // set location this.location = decodeURIComponent(this.injectedAuthMethodModel.location); } redirectToShibboleth() { if (!this.isStandalonePage) { this.authService.setRedirectUrl(this.hardRedirectService.getCurrentRoute()); } else { this.authService.setRedirectUrlIfNotSet('/'); } let newLocationUrl = this.location; const currentUrl = this._window.nativeWindow.location.href; const myRegexp = /\?redirectUrl=(.*)/g; const match = myRegexp.exec(this.location); const redirectUrl = (match && match[1]) ? match[1] : null; // Check whether the current page is different from the redirect url received from rest if (isNotNull(redirectUrl) && redirectUrl !== currentUrl) { // change the redirect url with the current page url const newRedirectUrl = `?redirectUrl=${currentUrl}`; newLocationUrl = this.location.replace(/\?redirectUrl=(.*)/g, newRedirectUrl); } // redirect to shibboleth authentication url this._window.nativeWindow.location.href = newLocationUrl; } }