From b77dce25cef94b238345f8a54b59f6f9d5cf9f2e Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Thu, 17 Sep 2020 10:44:10 +0200 Subject: [PATCH] 72699: Shibboleth login redirect fix --- .../shibboleth/log-in-shibboleth.component.ts | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.ts b/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.ts index 5cc50e46c6..b0704d5cb1 100644 --- a/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.ts +++ b/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.ts @@ -11,9 +11,11 @@ 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 { isNotNull, isEmpty } from '../../../empty.util'; import { AuthService } from '../../../../core/auth/auth.service'; import { HardRedirectService } from '../../../../core/services/hard-redirect.service'; +import { take } from 'rxjs/operators'; +import { URLCombiner } from '../../../../core/url-combiner/url-combiner'; @Component({ selector: 'ds-log-in-shibboleth', @@ -83,26 +85,31 @@ export class LogInShibbolethComponent implements OnInit { } 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); - } + this.authService.getRedirectUrl().pipe(take(1)).subscribe((redirectRoute) => { + if (!this.isStandalonePage) { + redirectRoute = this.hardRedirectService.getCurrentRoute(); + } else if (isEmpty(redirectRoute)) { + redirectRoute = '/'; + } + const correctRedirectUrl = new URLCombiner(this._window.nativeWindow.origin, redirectRoute).toString(); + + let shibbolethServerUrl = this.location; + const myRegexp = /\?redirectUrl=(.*)/g; + const match = myRegexp.exec(this.location); + const redirectUrlFromServer = (match && match[1]) ? match[1] : null; + + // Check whether the current page is different from the redirect url received from rest + if (isNotNull(redirectUrlFromServer) && redirectUrlFromServer !== correctRedirectUrl) { + // change the redirect url with the current page url + const newRedirectUrl = `?redirectUrl=${correctRedirectUrl}`; + shibbolethServerUrl = this.location.replace(/\?redirectUrl=(.*)/g, newRedirectUrl); + } + + // redirect to shibboleth authentication url + this.hardRedirectService.redirect(shibbolethServerUrl); + }); - // redirect to shibboleth authentication url - this._window.nativeWindow.location.href = newLocationUrl; } }