From 08590da3c613f2b6a8556fc794f4889a3db250e0 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 14:43:46 +0100 Subject: [PATCH 01/10] Return to the guarded page after DiscoJuice login The login page puts the stored post-login target into ?redirectUrl=, so aai.js keeps it through the local-login reload and in the Shibboleth target. Co-Authored-By: Claude Opus 5.5 --- .../login-page/login-page.component.spec.ts | 51 ++++++++++++++++++- src/app/login-page/login-page.component.ts | 37 +++++++++++++- .../log-in-password.component.spec.ts | 8 +++ 3 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/app/login-page/login-page.component.spec.ts b/src/app/login-page/login-page.component.spec.ts index 50008a2f28f..5cbe8fb9241 100644 --- a/src/app/login-page/login-page.component.spec.ts +++ b/src/app/login-page/login-page.component.spec.ts @@ -4,7 +4,11 @@ import { TestBed, waitForAsync, } from '@angular/core/testing'; -import { ActivatedRoute } from '@angular/router'; +import { + ActivatedRoute, + Params, + Router, +} from '@angular/router'; import { Store } from '@ngrx/store'; import { provideMockStore } from '@ngrx/store/testing'; import { TranslateModule } from '@ngx-translate/core'; @@ -16,12 +20,14 @@ import { XSRFService } from '../core/xsrf/xsrf.service'; import { AuthServiceMock } from '../shared/mocks/auth.service.mock'; import { ActivatedRouteStub } from '../shared/testing/active-router.stub'; import { EPersonMock } from '../shared/testing/eperson.mock'; +import { RouterStub } from '../shared/testing/router.stub'; import { LoginPageComponent } from './login-page.component'; describe('LoginPageComponent', () => { let comp: LoginPageComponent; let fixture: ComponentFixture; let authService: any; + let router: RouterStub; const mockUser = EPersonMock; const activatedRouteStub = Object.assign(new ActivatedRouteStub(), { @@ -39,7 +45,9 @@ describe('LoginPageComponent', () => { authService = Object.assign(new AuthServiceMock(), { isAuthenticated: jasmine.createSpy('isAuthenticated').and.returnValue(of(false)), getAuthenticatedUserFromStore: jasmine.createSpy('getAuthenticatedUserFromStore').and.returnValue(of(mockUser)), + getRedirectUrl: jasmine.createSpy('getRedirectUrl').and.returnValue(of(undefined)), }); + router = new RouterStub(); TestBed.configureTestingModule({ imports: [ @@ -49,9 +57,10 @@ describe('LoginPageComponent', () => { providers: [ { provide: ActivatedRoute, useValue: activatedRouteStub }, { provide: AuthService, useValue: authService }, + { provide: Router, useValue: router }, { provide: XSRFService, useValue: {} }, { provide: APP_DATA_SERVICES_MAP, useValue: {} }, - provideMockStore({}), + provideMockStore({ initialState: { core: { auth: { authenticated: false } } } }), ], schemas: [NO_ERRORS_SCHEMA], }).compileComponents(); @@ -95,4 +104,42 @@ describe('LoginPageComponent', () => { expect(comp.authenticatedUser).toBeNull(); }); }); + + // aai.js (DiscoJuice) takes the post-login target only from the `redirectUrl` query param of the page. + describe('redirectUrl query param', () => { + const initWith = (storedRedirectUrl: string, queryParams: Params = {}) => { + authService.getRedirectUrl.and.returnValue(of(storedRedirectUrl)); + (comp as any).route = { queryParams: of(queryParams), snapshot: { queryParams } }; + comp.ngOnInit(); + }; + + it('puts the page stored by the auth guard into the login URL', () => { + initWith('/mydspace'); + + expect(router.navigate).toHaveBeenCalledWith(['/login'], { + queryParams: { redirectUrl: '/mydspace' }, + queryParamsHandling: 'merge', + replaceUrl: true, + }); + }); + + it('keeps a redirectUrl that is already in the URL', () => { + initWith('/mydspace', { redirectUrl: 'http://localhost:4000/login?redirectUrl=/mydspace' }); + + expect(router.navigate).not.toHaveBeenCalled(); + }); + + it('leaves the URL alone when no page is stored', () => { + initWith(undefined); + + expect(router.navigate).not.toHaveBeenCalled(); + }); + + it('does not navigate on the server', () => { + (comp as any).platformId = 'server'; + initWith('/mydspace'); + + expect(router.navigate).not.toHaveBeenCalled(); + }); + }); }); diff --git a/src/app/login-page/login-page.component.ts b/src/app/login-page/login-page.component.ts index 5868fe61b9f..1a9c3712b0e 100644 --- a/src/app/login-page/login-page.component.ts +++ b/src/app/login-page/login-page.component.ts @@ -1,10 +1,14 @@ +import { isPlatformBrowser } from '@angular/common'; import { Component, + Inject, OnDestroy, OnInit, + PLATFORM_ID, } from '@angular/core'; import { ActivatedRoute, + Router, RouterLink, } from '@angular/router'; import { Store } from '@ngrx/store'; @@ -27,7 +31,10 @@ import { AuthenticationSuccessAction, ResetAuthenticationMessagesAction, } from '../core/auth/auth.actions'; -import { AuthService } from '../core/auth/auth.service'; +import { + AuthService, + LOGIN_ROUTE, +} from '../core/auth/auth.service'; import { AuthTokenInfo } from '../core/auth/models/auth-token-info.model'; import { isAuthenticated } from '../core/auth/selectors'; import { EPerson } from '../core/eperson/models/eperson.model'; @@ -66,18 +73,25 @@ export class LoginPageComponent implements OnDestroy, OnInit { * Initialize instance variables * * @param {ActivatedRoute} route + * @param {Router} router * @param {Store} store * @param {AuthService} authService + * @param {object} platformId */ constructor(private route: ActivatedRoute, + private router: Router, private store: Store, - private authService: AuthService) {} + private authService: AuthService, + @Inject(PLATFORM_ID) private platformId: object) {} /** * Initialize instance variables */ ngOnInit() { this.initializeTheAuthenticationState(); + if (isPlatformBrowser(this.platformId)) { + this.showRedirectUrlInQueryParams(); + } const queryParamsObs = this.route.queryParams; const authenticated = this.store.select(isAuthenticated); @@ -103,6 +117,25 @@ export class LoginPageComponent implements OnDestroy, OnInit { })); } + /** + * Puts the stored post-login target (e.g. set by the auth guard) into the `redirectUrl` query param. + * The DiscoJuice scripts (aai.js) read it only from the URL: local login reloads the page, Shibboleth leaves the app. + */ + private showRedirectUrlInQueryParams() { + if (isNotEmpty(this.route.snapshot.queryParams?.redirectUrl)) { + return; + } + this.subs.push(this.authService.getRedirectUrl().pipe(take(1)).subscribe((redirectUrl: string) => { + if (isNotEmpty(redirectUrl)) { + void this.router.navigate([LOGIN_ROUTE], { + queryParams: { redirectUrl }, + queryParamsHandling: 'merge', + replaceUrl: true, + }); + } + })); + } + /** * Resolves the authenticated user from the store, or null when nobody is logged in. */ diff --git a/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts b/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts index 55c9e3c75b2..686ff1d1d1d 100644 --- a/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts +++ b/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts @@ -231,6 +231,14 @@ describe('LogInPasswordComponent', () => { expect(setRedirectUrlSpy).toHaveBeenCalledWith('/repository/items/1'); }); + it('returns to the page the auth guard put into the login URL, after the DiscoJuice local-login reload', () => { + setQueryParams({ redirectUrl: 'http://dev-6.pc:8603/repository/login?redirectUrl=/mydspace' }); + + component.submit(); + + expect(setRedirectUrlSpy).toHaveBeenCalledWith('/mydspace'); + }); + it('passes through an already-relative redirectUrl unchanged', () => { setQueryParams({ redirectUrl: '/repository/search' }); From e720f42bc12c6e1efd01c546830039804d5f5803 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 14:58:44 +0100 Subject: [PATCH 02/10] Strip the base href from the post-login target A password login started on the login page itself now ends on the home page, as in 7.x, instead of on the already-logged-in message. Co-Authored-By: Claude Opus 5.5 --- .../log-in-password.component.spec.ts | 23 ++++++++++++++----- .../password/log-in-password.component.ts | 9 ++++++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts b/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts index 686ff1d1d1d..9a50eb2e9a8 100644 --- a/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts +++ b/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts @@ -1,3 +1,4 @@ +import { APP_BASE_HREF } from '@angular/common'; import { CUSTOM_ELEMENTS_SCHEMA, PLATFORM_ID, @@ -107,6 +108,8 @@ describe('LogInPasswordComponent', () => { { provide: NotificationsService, useClass: NotificationsServiceStub }, { provide: ActivatedRoute, useValue: new ActivatedRouteStub() }, { provide: ThemeService, useValue: themeService }, + // The UI runs under a sub-path in production, e.g. http://dev-6.pc:8603/repository/ + { provide: APP_BASE_HREF, useValue: '/repository/' }, provideMockStore({ initialState }), ], schemas: [ @@ -204,12 +207,12 @@ describe('LogInPasswordComponent', () => { component.form.controls.password.setValue('password'); }); - it('redirects back to the redirectUrl page, reduced to an app-relative path', () => { + it('redirects back to the redirectUrl page, reduced to a router path', () => { setQueryParams({ redirectUrl: 'http://dev-6.pc:8603/repository/search' }); component.submit(); - expect(setRedirectUrlSpy).toHaveBeenCalledWith('/repository/search'); + expect(setRedirectUrlSpy).toHaveBeenCalledWith('/search'); expect(setRedirectUrlIfNotSetSpy).not.toHaveBeenCalled(); }); @@ -218,7 +221,7 @@ describe('LogInPasswordComponent', () => { component.submit(); - expect(setRedirectUrlSpy).toHaveBeenCalledWith('/repository/search?query=test'); + expect(setRedirectUrlSpy).toHaveBeenCalledWith('/search?query=test'); }); it('prefers a nested redirectUrl so the login page is not the redirect target', () => { @@ -228,7 +231,7 @@ describe('LogInPasswordComponent', () => { component.submit(); - expect(setRedirectUrlSpy).toHaveBeenCalledWith('/repository/items/1'); + expect(setRedirectUrlSpy).toHaveBeenCalledWith('/items/1'); }); it('returns to the page the auth guard put into the login URL, after the DiscoJuice local-login reload', () => { @@ -240,11 +243,19 @@ describe('LogInPasswordComponent', () => { }); it('passes through an already-relative redirectUrl unchanged', () => { - setQueryParams({ redirectUrl: '/repository/search' }); + setQueryParams({ redirectUrl: '/search' }); component.submit(); - expect(setRedirectUrlSpy).toHaveBeenCalledWith('/repository/search'); + expect(setRedirectUrlSpy).toHaveBeenCalledWith('/search'); + }); + + it('targets the plain login route when the login started on the login page itself, so it ends on the home page', () => { + setQueryParams({ redirectUrl: 'http://dev-6.pc:8603/repository/login' }); + + component.submit(); + + expect(setRedirectUrlSpy).toHaveBeenCalledWith('/login'); }); it('falls back to setRedirectUrlIfNotSet("/") when no redirectUrl query param is present', () => { diff --git a/src/app/shared/log-in/methods/password/log-in-password.component.ts b/src/app/shared/log-in/methods/password/log-in-password.component.ts index d34772641f0..dcd2d3f30bd 100644 --- a/src/app/shared/log-in/methods/password/log-in-password.component.ts +++ b/src/app/shared/log-in/methods/password/log-in-password.component.ts @@ -1,6 +1,7 @@ import { AsyncPipe, isPlatformBrowser, + Location, } from '@angular/common'; import { Component, @@ -166,6 +167,7 @@ export class LogInPasswordComponent implements OnInit, OnDestroy { protected configurationService: ConfigurationDataService, private notificationService: NotificationsService, private translateService: TranslateService, + private location: Location, ) { this.authMethod = injectedAuthMethodModel; } @@ -303,9 +305,12 @@ export class LogInPasswordComponent implements OnInit, OnDestroy { return this.toRelativePath(redirectUrl); } - /** Reduce a possibly-absolute URL to an app-relative path by dropping the scheme+host; relative values pass through. */ + /** + * Reduce a possibly-absolute URL to a router path by dropping the scheme+host and the base href (e.g. `/repository`). + * Without the base href a login page target is `/login`, which `AuthService.navigateToRedirectUrl` sends home. + */ private toRelativePath(url: string): string { - return url.replace(/^https?:\/\/[^/]+/i, ''); + return this.location.normalize(url.replace(/^https?:\/\/[^/]+/i, '')); } /** From da385d6684d4c03f1881fb137d0d78ecabd8e1a1 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 15:08:38 +0100 Subject: [PATCH 03/10] Keep the page query string in the DiscoJuice local-login URL aai.js now encodes the return URL it puts into /login?redirectUrl=, so a query string like ?configuration=workflow or a second param survives the reload. Co-Authored-By: Claude Opus 5.5 --- angular.json | 5 +++- src/aai/aai.js | 12 ++++++++- src/aai/aai.spec.ts | 26 +++++++++++++++++++ .../log-in-password.component.spec.ts | 8 ++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/aai/aai.spec.ts diff --git a/angular.json b/angular.json index 47d1ed346a1..e8e320ab930 100644 --- a/angular.json +++ b/angular.json @@ -45,7 +45,10 @@ { "glob": "**/*", "input": "src/aai", - "output": "/aai" + "output": "/aai", + "ignore": [ + "**/*.spec.ts" + ] } ], "styles": [ diff --git a/src/aai/aai.js b/src/aai/aai.js index 72a3650c813..bb9c2a59977 100644 --- a/src/aai/aai.js +++ b/src/aai/aai.js @@ -111,7 +111,7 @@ // jQuery('input#login').focus(); // Use cookie to toggle discojuice popup. setCookie('SHOW_DISCOJUICE_POPUP', false, 1) - window.location = window.location.origin + (namespace === '' ? namespace : '/' + namespace) + "/login?redirectUrl=" + window.location.href; + window.location = localLoginUrl(window.location.href); break; //case 'saml': default: @@ -135,6 +135,16 @@ } //if jQuery(selector) }; + // Exposed for tests. + this.localLoginUrl = localLoginUrl; + + /** + * Login page URL that returns to `pageUrl` after a local login. Encoded, so the query string of `pageUrl` stays in it. + */ + function localLoginUrl(pageUrl) { + return window.location.origin + formatNamespace(namespace) + '/login?redirectUrl=' + window.encodeURIComponent(pageUrl); + } + // Set a cookie function setCookie(name, value, daysToExpire) { var expirationDate = new Date(); diff --git a/src/aai/aai.spec.ts b/src/aai/aai.spec.ts new file mode 100644 index 00000000000..abf29ddbe19 --- /dev/null +++ b/src/aai/aai.spec.ts @@ -0,0 +1,26 @@ +import './aai.js'; + +import { DefaultUrlSerializer } from '@angular/router'; + +describe('aai.js', () => { + const aai = (window as any).aai; + + describe('localLoginUrl', () => { + const redirectUrlOf = (loginUrl: string): string => { + const url = new URL(loginUrl); + return new DefaultUrlSerializer().parse(url.pathname + url.search).queryParams.redirectUrl; + }; + + it('keeps the query string of the page the user came from', () => { + const page = 'http://localhost:4000/search?query=test&page=2'; + + expect(redirectUrlOf(aai.localLoginUrl(page))).toBe(page); + }); + + it('keeps the page stored by the auth guard when local login starts on the login page', () => { + const page = 'http://localhost:4000/login?redirectUrl=%2Fmydspace%3Fconfiguration%3Dworkflow'; + + expect(redirectUrlOf(aai.localLoginUrl(page))).toBe(page); + }); + }); +}); diff --git a/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts b/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts index 9a50eb2e9a8..d926848a5b1 100644 --- a/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts +++ b/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts @@ -242,6 +242,14 @@ describe('LogInPasswordComponent', () => { expect(setRedirectUrlSpy).toHaveBeenCalledWith('/mydspace'); }); + it('keeps the query string of the page the auth guard stored, when aai.js encodes the login URL', () => { + setQueryParams({ redirectUrl: 'http://dev-6.pc:8603/repository/login?redirectUrl=%2Fmydspace%3Fconfiguration%3Dworkflow' }); + + component.submit(); + + expect(setRedirectUrlSpy).toHaveBeenCalledWith('/mydspace?configuration=workflow'); + }); + it('passes through an already-relative redirectUrl unchanged', () => { setQueryParams({ redirectUrl: '/search' }); From 859498871032bb98395a4cacd05cd96a44d5b903 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 15:10:53 +0100 Subject: [PATCH 04/10] Keep the base href when an expired session goes to the login page The hard redirect used /login?expired=true, which drops the base href and gives a 404 when the UI runs under a sub-path like /repository/. Co-Authored-By: Claude Opus 5.5 --- src/app/core/auth/auth.service.spec.ts | 10 ++++++++++ src/app/core/auth/auth.service.ts | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/app/core/auth/auth.service.spec.ts b/src/app/core/auth/auth.service.spec.ts index 1d17f17bade..158f7e77fd0 100644 --- a/src/app/core/auth/auth.service.spec.ts +++ b/src/app/core/auth/auth.service.spec.ts @@ -426,6 +426,16 @@ describe('AuthService test', () => { expect(hardRedirectService.redirect).toHaveBeenCalledWith(jasmine.stringMatching(new RegExp('reload/[0-9]*(?!\\?)$'))); }); + it('should send an expired session to the login page under the base href', () => { + const browserLocation = { href: '' }; + (authService as any)._window = { nativeWindow: { location: browserLocation } }; + + authService.redirectToLoginWhenTokenExpired(); + + // The browser resolves the new href against , e.g. /repository/ + expect(new URL(browserLocation.href, 'http://localhost:4000/repository/').href).toBe('http://localhost:4000/repository/login?expired=true'); + }); + describe('impersonate', () => { const userId = 'testUserId'; diff --git a/src/app/core/auth/auth.service.ts b/src/app/core/auth/auth.service.ts index 4a155920865..edcbb0042bb 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -526,8 +526,9 @@ export class AuthService { public redirectToLoginWhenTokenExpired() { const redirectUrl = LOGIN_ROUTE + '?expired=true'; if (this._window.nativeWindow.location) { - // Hard redirect to login page, so that all state is definitely lost - this._window.nativeWindow.location.href = redirectUrl; + // Hard redirect to login page, so that all state is definitely lost. + // Relative to , like the `reload/` URL in navigateToRedirectUrl. + this._window.nativeWindow.location.href = '.' + redirectUrl; } else { this.router.navigateByUrl(redirectUrl); } From 535055e34e69b0773d8c7cc9d226ae4b868878c5 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 15:13:48 +0100 Subject: [PATCH 05/10] Keep a full return URL when Shibboleth login starts on the login page After local login the login page has a full URL in redirectUrl. aai.js put the namespace in front of it, so Shibboleth returned to a broken URL. Co-Authored-By: Claude Opus 5.5 --- src/aai/aai.js | 17 +++++++++++++---- src/aai/aai.spec.ts | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/aai/aai.js b/src/aai/aai.js index bb9c2a59977..9fa9824a441 100644 --- a/src/aai/aai.js +++ b/src/aai/aai.js @@ -49,10 +49,7 @@ if (redirectUrlFromLogin != null && redirectUrlFromLogin !== '') { // Redirect from the login page with retrieved redirect URL - var baseUrl = window.location.origin + formatNamespace(namespace); - var redirectPath = ensureLeadingSlash(redirectUrlFromLogin); - - redirectUrl = baseUrl + redirectPath; + redirectUrl = shibbolethReturnUrl(redirectUrlFromLogin); } // Encode the redirect URL @@ -137,6 +134,7 @@ // Exposed for tests. this.localLoginUrl = localLoginUrl; + this.shibbolethReturnUrl = shibbolethReturnUrl; /** * Login page URL that returns to `pageUrl` after a local login. Encoded, so the query string of `pageUrl` stays in it. @@ -145,6 +143,17 @@ return window.location.origin + formatNamespace(namespace) + '/login?redirectUrl=' + window.encodeURIComponent(pageUrl); } + /** + * Page to return to after a Shibboleth login, from the `redirectUrl` of the login page. + * A URL of this site (local login puts one there) is kept as it is, anything else is taken as a path in the app. + */ + function shibbolethReturnUrl(redirectUrlFromLogin) { + if (redirectUrlFromLogin.startsWith(window.location.origin + '/')) { + return redirectUrlFromLogin; + } + return window.location.origin + formatNamespace(namespace) + ensureLeadingSlash(redirectUrlFromLogin); + } + // Set a cookie function setCookie(name, value, daysToExpire) { var expirationDate = new Date(); diff --git a/src/aai/aai.spec.ts b/src/aai/aai.spec.ts index abf29ddbe19..e2be25e5701 100644 --- a/src/aai/aai.spec.ts +++ b/src/aai/aai.spec.ts @@ -23,4 +23,21 @@ describe('aai.js', () => { expect(redirectUrlOf(aai.localLoginUrl(page))).toBe(page); }); }); + + describe('shibbolethReturnUrl', () => { + it('keeps a URL of this site, as local login puts it into the login URL', () => { + const page = window.location.origin + '/items/1?query=test'; + + expect(aai.shibbolethReturnUrl(page)).toBe(page); + }); + + it('turns a path into a URL of this site', () => { + // The karma page has , so no namespace goes in front of the path. + expect(aai.shibbolethReturnUrl('/mydspace')).toBe(window.location.origin + '/mydspace'); + }); + + it('keeps the return on this site when the URL is of another site', () => { + expect(aai.shibbolethReturnUrl('https://example.org/items/1').startsWith(window.location.origin + '/')).toBeTrue(); + }); + }); }); From 7560dd81d743ad016f15f4da407fc3f28849e945 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 15:41:01 +0100 Subject: [PATCH 06/10] Keep the home page as the post-login target Location.normalize turns the home page URL into an empty string, so a stored page won over it. An empty path now means /. Co-Authored-By: Claude Opus 5.5 --- .../methods/password/log-in-password.component.spec.ts | 10 +++++++++- .../methods/password/log-in-password.component.ts | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts b/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts index d926848a5b1..ebc2b51422a 100644 --- a/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts +++ b/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts @@ -108,7 +108,7 @@ describe('LogInPasswordComponent', () => { { provide: NotificationsService, useClass: NotificationsServiceStub }, { provide: ActivatedRoute, useValue: new ActivatedRouteStub() }, { provide: ThemeService, useValue: themeService }, - // The UI runs under a sub-path in production, e.g. http://dev-6.pc:8603/repository/ + // The UI can run under a sub-path, e.g. /repository/ { provide: APP_BASE_HREF, useValue: '/repository/' }, provideMockStore({ initialState }), ], @@ -266,6 +266,14 @@ describe('LogInPasswordComponent', () => { expect(setRedirectUrlSpy).toHaveBeenCalledWith('/login'); }); + it('keeps the home page as the target, so a stored page does not win over it', () => { + setQueryParams({ redirectUrl: 'http://dev-6.pc:8603/repository/' }); + + component.submit(); + + expect(setRedirectUrlSpy).toHaveBeenCalledWith('/'); + }); + it('falls back to setRedirectUrlIfNotSet("/") when no redirectUrl query param is present', () => { setQueryParams({}); diff --git a/src/app/shared/log-in/methods/password/log-in-password.component.ts b/src/app/shared/log-in/methods/password/log-in-password.component.ts index dcd2d3f30bd..c33cca03d52 100644 --- a/src/app/shared/log-in/methods/password/log-in-password.component.ts +++ b/src/app/shared/log-in/methods/password/log-in-password.component.ts @@ -308,9 +308,10 @@ export class LogInPasswordComponent implements OnInit, OnDestroy { /** * Reduce a possibly-absolute URL to a router path by dropping the scheme+host and the base href (e.g. `/repository`). * Without the base href a login page target is `/login`, which `AuthService.navigateToRedirectUrl` sends home. + * The home page itself normalizes to '', so it becomes `/`. */ private toRelativePath(url: string): string { - return this.location.normalize(url.replace(/^https?:\/\/[^/]+/i, '')); + return this.location.normalize(url.replace(/^https?:\/\/[^/]+/i, '')) || '/'; } /** From e2c30420d38fd69ac9af820420a6c145f9ebd86a Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 15:42:02 +0100 Subject: [PATCH 07/10] Keep the UI namespace in the SSR redirect for an expired session The server answered with a 302 to /login?expired=true, which the browser resolves from the host root, not from the base href. Co-Authored-By: Claude Opus 5.5 --- src/app/core/auth/auth.service.ts | 2 +- src/app/core/auth/server-auth.service.spec.ts | 28 +++++++++++++++++++ src/app/core/auth/server-auth.service.ts | 8 +++++- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/app/core/auth/server-auth.service.spec.ts diff --git a/src/app/core/auth/auth.service.ts b/src/app/core/auth/auth.service.ts index edcbb0042bb..f812dec50ea 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -526,7 +526,7 @@ export class AuthService { public redirectToLoginWhenTokenExpired() { const redirectUrl = LOGIN_ROUTE + '?expired=true'; if (this._window.nativeWindow.location) { - // Hard redirect to login page, so that all state is definitely lost. + // Hard redirect to login page, so that all state is definitely lost // Relative to , like the `reload/` URL in navigateToRedirectUrl. this._window.nativeWindow.location.href = '.' + redirectUrl; } else { diff --git a/src/app/core/auth/server-auth.service.spec.ts b/src/app/core/auth/server-auth.service.spec.ts new file mode 100644 index 00000000000..578836f32f2 --- /dev/null +++ b/src/app/core/auth/server-auth.service.spec.ts @@ -0,0 +1,28 @@ +import { of } from 'rxjs'; + +import { AppConfig } from '../../../config/app-config.interface'; +import { ServerAuthService } from './server-auth.service'; + +describe('ServerAuthService', () => { + let response: { redirect: jasmine.Spy }; + + const createService = (nameSpace: string): ServerAuthService => { + response = { redirect: jasmine.createSpy('redirect') }; + const appConfig = { ui: { nameSpace } } as AppConfig; + // On the server there is no browser location, so the expired-session redirect is a 302. + const nativeWindowRef = { nativeWindow: {} } as any; + return new ServerAuthService({}, response, nativeWindowRef, null, null, null, null, null, of({}) as any, null, null, null, appConfig); + }; + + it('should send an expired session to the login page under the UI namespace', () => { + createService('/repository').redirectToLoginWhenTokenExpired(); + + expect(response.redirect).toHaveBeenCalledWith(302, '/repository/login?expired=true'); + }); + + it('should send an expired session to /login when the UI runs at the root', () => { + createService('/').redirectToLoginWhenTokenExpired(); + + expect(response.redirect).toHaveBeenCalledWith(302, '/login?expired=true'); + }); +}); diff --git a/src/app/core/auth/server-auth.service.ts b/src/app/core/auth/server-auth.service.ts index d287604c24d..68c6ed874f8 100644 --- a/src/app/core/auth/server-auth.service.ts +++ b/src/app/core/auth/server-auth.service.ts @@ -10,6 +10,10 @@ import { TranslateService } from '@ngx-translate/core'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; +import { + APP_CONFIG, + AppConfig, +} from '../../../config/app-config.interface'; import { REQUEST, RESPONSE, @@ -57,6 +61,7 @@ export class ServerAuthService extends AuthService { protected hardRedirectService: HardRedirectService, protected notificationService: NotificationsService, protected translateService: TranslateService, + @Inject(APP_CONFIG) protected appConfig: AppConfig, ) { super( _window, @@ -123,7 +128,8 @@ export class ServerAuthService extends AuthService { this._window.nativeWindow.location.href = redirectUrl; } else if (this.response) { if (!this.response._headerSent) { - this.response.redirect(302, redirectUrl); + // The browser resolves a 302 Location against the request path, not , so add the UI namespace. + this.response.redirect(302, this.appConfig.ui.nameSpace.replace(/\/$/, '') + redirectUrl); } } else { this.router.navigateByUrl(redirectUrl); From 68aa05a76f3b87d896a04a105c973a6d1d1b7584 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 15:56:12 +0100 Subject: [PATCH 08/10] Encode the whole Shibboleth target URL in aai.js Only the inner redirectUrl was encoded, so a return page like /search?query=corpus&page=2 reached the backend without page=2. Co-Authored-By: Claude Opus 5.5 --- src/aai/aai.js | 16 ++++++++++++---- src/aai/aai.spec.ts | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/aai/aai.js b/src/aai/aai.js index 9fa9824a441..12653c1e1dd 100644 --- a/src/aai/aai.js +++ b/src/aai/aai.js @@ -32,7 +32,6 @@ var targetUrl = ''; var opts = jQuery.extend({}, this.defaults, options), defaultCallback = function(e) { - targetUrl = opts.target + '?redirectUrl='; // E.g. Redirect to Item page var redirectUrl = window.location.href; @@ -52,9 +51,7 @@ redirectUrl = shibbolethReturnUrl(redirectUrlFromLogin); } - // Encode the redirect URL - targetUrl += window.encodeURIComponent(redirectUrl); - window.location = opts.host + opts.port + '/Shibboleth.sso/Login?SAMLDS=1&target=' + targetUrl + '&entityID=' + window.encodeURIComponent(e.entityID); + window.location = shibbolethLoginUrl(opts, redirectUrl, e.entityID); }; //console.log(opts); if(!opts.target){ @@ -135,6 +132,7 @@ // Exposed for tests. this.localLoginUrl = localLoginUrl; this.shibbolethReturnUrl = shibbolethReturnUrl; + this.shibbolethLoginUrl = shibbolethLoginUrl; /** * Login page URL that returns to `pageUrl` after a local login. Encoded, so the query string of `pageUrl` stays in it. @@ -154,6 +152,16 @@ return window.location.origin + formatNamespace(namespace) + ensureLeadingSlash(redirectUrlFromLogin); } + /** + * Shibboleth login URL whose `target` (the backend) returns to `redirectUrl`. + * `redirectUrl` is encoded inside `target` and then `target` as a whole, so the query string of `redirectUrl` reaches the backend. + */ + function shibbolethLoginUrl(opts, redirectUrl, entityID) { + var target = opts.target + '?redirectUrl=' + window.encodeURIComponent(redirectUrl); + return opts.host + opts.port + '/Shibboleth.sso/Login?SAMLDS=1&target=' + window.encodeURIComponent(target) + + '&entityID=' + window.encodeURIComponent(entityID); + } + // Set a cookie function setCookie(name, value, daysToExpire) { var expirationDate = new Date(); diff --git a/src/aai/aai.spec.ts b/src/aai/aai.spec.ts index e2be25e5701..2e69c84cd7c 100644 --- a/src/aai/aai.spec.ts +++ b/src/aai/aai.spec.ts @@ -40,4 +40,20 @@ describe('aai.js', () => { expect(aai.shibbolethReturnUrl('https://example.org/items/1').startsWith(window.location.origin + '/')).toBeTrue(); }); }); + + describe('shibbolethLoginUrl', () => { + const opts = { host: 'https://ui.example', port: '', target: 'https://ui.example/server/api/authn/shibboleth' }; + + it('gives the backend the whole redirectUrl, with its own query string', () => { + const page = 'https://ui.example/search?query=corpus&page=2'; + + const loginUrl = new URL(aai.shibbolethLoginUrl(opts, page, 'https://idp.example/idp')); + // The SP decodes `target` once and sends the user there; the backend then reads `redirectUrl` from it. + const target = new URL(loginUrl.searchParams.get('target')); + + expect(target.origin + target.pathname).toBe(opts.target); + expect(target.searchParams.get('redirectUrl')).toBe(page); + expect(loginUrl.searchParams.get('entityID')).toBe('https://idp.example/idp'); + }); + }); }); From c62ca69b39e936986331c5159f6ddc9f199f7cfd Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 15:58:07 +0100 Subject: [PATCH 09/10] Keep a stored URL with an access token out of the login URL A request-a-copy return URL with accessToken stays in the store only; the password login on the login page still returns to it. Co-Authored-By: Claude Opus 5.5 --- src/app/login-page/login-page.component.spec.ts | 6 ++++++ src/app/login-page/login-page.component.ts | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/app/login-page/login-page.component.spec.ts b/src/app/login-page/login-page.component.spec.ts index 5cbe8fb9241..fcab1189722 100644 --- a/src/app/login-page/login-page.component.spec.ts +++ b/src/app/login-page/login-page.component.spec.ts @@ -129,6 +129,12 @@ describe('LoginPageComponent', () => { expect(router.navigate).not.toHaveBeenCalled(); }); + it('keeps a stored URL with an access token out of the login URL', () => { + initWith('/bitstreams/123/download?accessToken=abc'); + + expect(router.navigate).not.toHaveBeenCalled(); + }); + it('leaves the URL alone when no page is stored', () => { initWith(undefined); diff --git a/src/app/login-page/login-page.component.ts b/src/app/login-page/login-page.component.ts index 1a9c3712b0e..d693159860b 100644 --- a/src/app/login-page/login-page.component.ts +++ b/src/app/login-page/login-page.component.ts @@ -120,13 +120,15 @@ export class LoginPageComponent implements OnDestroy, OnInit { /** * Puts the stored post-login target (e.g. set by the auth guard) into the `redirectUrl` query param. * The DiscoJuice scripts (aai.js) read it only from the URL: local login reloads the page, Shibboleth leaves the app. + * A URL with a request-a-copy `accessToken` stays only in the store, where the password login still reads it. */ private showRedirectUrlInQueryParams() { if (isNotEmpty(this.route.snapshot.queryParams?.redirectUrl)) { return; } this.subs.push(this.authService.getRedirectUrl().pipe(take(1)).subscribe((redirectUrl: string) => { - if (isNotEmpty(redirectUrl)) { + const hasAccessToken = new URLSearchParams(redirectUrl?.split('?')[1] ?? '').has('accessToken'); + if (isNotEmpty(redirectUrl) && !hasAccessToken) { void this.router.navigate([LOGIN_ROUTE], { queryParams: { redirectUrl }, queryParamsHandling: 'merge', From 338484bfefd4efa79309c4d868ed51c29b802a9d Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 19:01:53 +0100 Subject: [PATCH 10/10] Use a neutral host in the new login redirect specs Co-Authored-By: Claude Opus 5.5 --- .../methods/password/log-in-password.component.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts b/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts index ebc2b51422a..c2d473e9a48 100644 --- a/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts +++ b/src/app/shared/log-in/methods/password/log-in-password.component.spec.ts @@ -235,7 +235,7 @@ describe('LogInPasswordComponent', () => { }); it('returns to the page the auth guard put into the login URL, after the DiscoJuice local-login reload', () => { - setQueryParams({ redirectUrl: 'http://dev-6.pc:8603/repository/login?redirectUrl=/mydspace' }); + setQueryParams({ redirectUrl: 'http://localhost:4000/repository/login?redirectUrl=/mydspace' }); component.submit(); @@ -243,7 +243,7 @@ describe('LogInPasswordComponent', () => { }); it('keeps the query string of the page the auth guard stored, when aai.js encodes the login URL', () => { - setQueryParams({ redirectUrl: 'http://dev-6.pc:8603/repository/login?redirectUrl=%2Fmydspace%3Fconfiguration%3Dworkflow' }); + setQueryParams({ redirectUrl: 'http://localhost:4000/repository/login?redirectUrl=%2Fmydspace%3Fconfiguration%3Dworkflow' }); component.submit(); @@ -259,7 +259,7 @@ describe('LogInPasswordComponent', () => { }); it('targets the plain login route when the login started on the login page itself, so it ends on the home page', () => { - setQueryParams({ redirectUrl: 'http://dev-6.pc:8603/repository/login' }); + setQueryParams({ redirectUrl: 'http://localhost:4000/repository/login' }); component.submit(); @@ -267,7 +267,7 @@ describe('LogInPasswordComponent', () => { }); it('keeps the home page as the target, so a stored page does not win over it', () => { - setQueryParams({ redirectUrl: 'http://dev-6.pc:8603/repository/' }); + setQueryParams({ redirectUrl: 'http://localhost:4000/repository/' }); component.submit();