From 1b45f1096352ca011e05931b5e515c0ab813a7b9 Mon Sep 17 00:00:00 2001 From: Isaries Date: Sun, 15 Nov 2026 18:11:07 +0800 Subject: [PATCH] fix(SessionService): Disabling sign-in never signs anyone out, because the renewal answer is read as JSON /api/session/renew answers in plain text: "true", "false", or "requestLogout" when sign-in has been disabled. checkIfSessionIsActive() requests it as JSON, so "requestLogout" is a parse error and nobody who is already signed in is signed out when an administrator disables sign-in. The answer is now read as text and mapped to a session state. Signing out goes through logOut(), which does not save, so acting on "requestLogout" directly would discard a student's unsaved work, even though the session is still valid at that point and can save. Components can now register a save with registerExitSaver(); NodeComponent registers one that saves its unsaved components. When sign-in is disabled, the registered saves run, with at most ten seconds to finish, and the user is then signed out and sent to /login?loginDisabled=true. If the sign-out request fails, the next renewal tries again. --- src/app/services/sessionService.spec.ts | 132 ++++++++++++++++++-- src/assets/wise5/services/sessionService.ts | 80 ++++++++++-- src/assets/wise5/vle/node/node.component.ts | 8 ++ 3 files changed, 206 insertions(+), 14 deletions(-) diff --git a/src/app/services/sessionService.spec.ts b/src/app/services/sessionService.spec.ts index a588f886068..39f71c6eac0 100644 --- a/src/app/services/sessionService.spec.ts +++ b/src/app/services/sessionService.spec.ts @@ -1,9 +1,9 @@ import { TestBed, tick, fakeAsync } from '@angular/core/testing'; import { SessionService } from '../../assets/wise5/services/sessionService'; import { ConfigService } from '../../assets/wise5/services/configService'; -import { provideHttpClientTesting } from '@angular/common/http/testing'; -import { of } from 'rxjs'; -import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; +import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; +import { of, throwError } from 'rxjs'; +import { HttpClient, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; let service: SessionService; let configService: ConfigService; @@ -23,6 +23,8 @@ describe('SessionService', () => { checkMouseevent(); checkForLogout(); renewSession(); + serverAnswers(); + leaveClosedSite(); }); function calculateIntervals() { @@ -94,7 +96,7 @@ function checkForLogout() { describe('checkForLogout()', () => { it('should force logout when user is inactive for long enough', () => { spyOn(service, 'isInactiveLongEnoughToForceLogout').and.returnValue(true); - spyOn(service, 'checkIfSessionIsActive').and.returnValue(of(false)); + spyOn(service, 'checkSession').and.returnValue(of('ended')); const forceLogOutSpy = spyOn(service, 'forceLogOut'); service.checkForLogout(); expect(forceLogOutSpy).toHaveBeenCalled(); @@ -102,7 +104,7 @@ function checkForLogout() { it('should not force logout when user is inactive for long enough but session is active', () => { spyOn(service, 'isInactiveLongEnoughToForceLogout').and.returnValue(true); - spyOn(service, 'checkIfSessionIsActive').and.returnValue(of(true)); + spyOn(service, 'checkSession').and.returnValue(of('active')); const forceLogOutSpy = spyOn(service, 'forceLogOut'); service.checkForLogout(); expect(forceLogOutSpy).not.toHaveBeenCalled(); @@ -122,7 +124,7 @@ function checkForLogout() { function renewSession() { describe('renewSession()', () => { it('should renew the session', fakeAsync(() => { - spyOn(service, 'checkIfSessionIsActive').and.returnValue(of(true)); + spyOn(service, 'checkSession').and.returnValue(of('active')); const logOutSpy = spyOn(service, 'logOut'); service.renewSession(); tick(); @@ -130,7 +132,7 @@ function renewSession() { })); it('should log the user out when renew session fails', fakeAsync(() => { - spyOn(service, 'checkIfSessionIsActive').and.returnValue(of(false)); + spyOn(service, 'checkSession').and.returnValue(of('ended')); const logOutSpy = spyOn(service, 'logOut'); service.renewSession(); tick(); @@ -138,3 +140,119 @@ function renewSession() { })); }); } + +function serverAnswers() { + describe('the answer to a session renewal', () => { + let http: HttpTestingController; + let navigateSpy: jasmine.Spy; + + beforeEach(() => { + spyOn(configService, 'getConfigParam').and.returnValue('/api/session/renew'); + spyOn(configService, 'getSessionLogOutURL').and.returnValue('/api/logout'); + http = TestBed.inject(HttpTestingController); + navigateSpy = jasmine.createSpy('navigateTo'); + (service as any).navigateTo = navigateSpy; + }); + + it('should sign out when the session has ended', fakeAsync(() => { + const logOutSpy = spyOn(service, 'logOut'); + service.renewSession(); + http.expectOne('/api/session/renew').flush('false'); + tick(); + expect(logOutSpy).toHaveBeenCalled(); + })); + + it('should sign out to the sign-in page when sign-in has been disabled', fakeAsync(() => { + service.renewSession(); + http.expectOne('/api/session/renew').flush('requestLogout'); + tick(); + http.expectOne('/api/logout').flush({}); + expect(navigateSpy).toHaveBeenCalledWith('/login?loginDisabled=true'); + })); + + it('should keep the user signed in when the answer is empty', fakeAsync(() => { + const logOutSpy = spyOn(service, 'logOut'); + service.renewSession(); + http.expectOne('/api/session/renew').flush(''); + tick(); + expect(logOutSpy).not.toHaveBeenCalled(); + http.verify(); + })); + }); +} + +function leaveClosedSite() { + describe('when sign-in has been closed', () => { + let navigateSpy: jasmine.Spy; + let logOutRequest: jasmine.Spy; + + beforeEach(() => { + spyOn(service, 'checkSession').and.returnValue(of('closed')); + spyOn(configService, 'getSessionLogOutURL').and.returnValue('/api/logout'); + logOutRequest = spyOn(TestBed.inject(HttpClient), 'get').and.returnValue(of({})); + navigateSpy = spyOn(service, 'navigateTo'); + }); + + it('should save unsaved work before signing out', fakeAsync(() => { + let finishSave: () => void; + service.registerExitSaver(() => new Promise((resolve) => (finishSave = resolve))); + service.renewSession(); + tick(); + expect(logOutRequest).not.toHaveBeenCalled(); + finishSave(); + tick(); + expect(logOutRequest).toHaveBeenCalledWith('/api/logout'); + expect(navigateSpy).toHaveBeenCalledWith('/login?loginDisabled=true'); + })); + + it('should sign out anyway when a save never answers', fakeAsync(() => { + service.registerExitSaver(() => new Promise(() => {})); + service.renewSession(); + tick(9999); + expect(navigateSpy).not.toHaveBeenCalled(); + tick(1); + expect(navigateSpy).toHaveBeenCalledWith('/login?loginDisabled=true'); + })); + + it('should not save work whose component has gone', fakeAsync(() => { + const save = jasmine.createSpy('save').and.returnValue(Promise.resolve()); + const unregister = service.registerExitSaver(save); + unregister(); + service.renewSession(); + tick(); + expect(save).not.toHaveBeenCalled(); + expect(navigateSpy).toHaveBeenCalledWith('/login?loginDisabled=true'); + })); + + it('should sign out even when a save throws before it starts', fakeAsync(() => { + service.registerExitSaver(() => { + throw new TypeError('no node status'); + }); + service.renewSession(); + tick(); + expect(navigateSpy).toHaveBeenCalledWith('/login?loginDisabled=true'); + })); + + it('should try again at the next renewal when signing out fails', fakeAsync(() => { + logOutRequest.and.returnValues( + throwError(() => new Error('network')), + of({}) + ); + service.renewSession(); + tick(); + expect(navigateSpy).not.toHaveBeenCalled(); + service.renewSession(); + tick(); + expect(navigateSpy).toHaveBeenCalledWith('/login?loginDisabled=true'); + })); + + it('should leave an idle session the same way', fakeAsync(() => { + spyOn(service, 'isInactiveLongEnoughToForceLogout').and.returnValue(true); + const forceLogOutSpy = spyOn(service, 'forceLogOut'); + service.checkForLogout(); + tick(); + expect(forceLogOutSpy).not.toHaveBeenCalled(); + expect(navigateSpy).toHaveBeenCalledWith('/login?loginDisabled=true'); + })); + }); +} diff --git a/src/assets/wise5/services/sessionService.ts b/src/assets/wise5/services/sessionService.ts index e0b71b418b1..1a8e4aa8879 100644 --- a/src/assets/wise5/services/sessionService.ts +++ b/src/assets/wise5/services/sessionService.ts @@ -5,6 +5,10 @@ import { HttpClient } from '@angular/common/http'; import { ConfigService } from './configService'; import { map, Observable, Subject } from 'rxjs'; +export type SessionState = 'active' | 'ended' | 'closed' | 'unknown'; + +const EXIT_SAVE_TIMEOUT_MILLISECONDS = 10000; + @Injectable() export class SessionService { private warningVisible: boolean = false; @@ -20,6 +24,8 @@ export class SessionService { private logOutSource: Subject = new Subject(); public logOut$ = this.logOutSource.asObservable(); private sessionActive: boolean = true; + private exitSavers = new Set<() => Promise>(); + private leavingClosedSite: boolean = false; constructor( protected http: HttpClient, @@ -118,9 +124,13 @@ export class SessionService { checkForLogout(): void { if (this.isInactiveLongEnoughToForceLogout()) { - this.checkIfSessionIsActive().subscribe((isSessionActive) => { - this.sessionActive = isSessionActive; - if (!isSessionActive) { + this.checkSession().subscribe((state) => { + if (state === 'closed') { + this.leaveClosedSite(); + return; + } + this.sessionActive = state !== 'ended'; + if (state === 'ended') { this.forceLogOut(); } }); @@ -175,15 +185,71 @@ export class SessionService { this.renewSession(); } - checkIfSessionIsActive(): Observable { - return this.http.get(this.configService.getConfigParam('renewSessionURL')); + checkSession(): Observable { + return this.http + .get(this.configService.getConfigParam('renewSessionURL'), { responseType: 'text' }) + .pipe( + map((answer: string): SessionState => { + switch ((answer ?? '').trim()) { + case 'true': + return 'active'; + case 'false': + return 'ended'; + case 'requestLogout': + return 'closed'; + default: + return 'unknown'; + } + }) + ); } renewSession(): void { - this.checkIfSessionIsActive().subscribe((isSessionActive) => { - if (!isSessionActive) { + this.checkSession().subscribe((state) => { + if (state === 'closed') { + this.leaveClosedSite(); + } else if (state === 'ended') { this.logOut(); } }); } + + /** + * Registers work to save before the user is signed out because sign-in has been disabled. + * @return a function that unregisters it + */ + registerExitSaver(save: () => Promise): () => void { + this.exitSavers.add(save); + return () => this.exitSavers.delete(save); + } + + /** + * The session is still valid when sign-in is disabled, so unsaved work can be saved before + * signing out. If signing out fails, the next renewal tries again, because the sign-in page would + * send a user whose session is still valid back to where they were. + */ + private leaveClosedSite(): void { + if (this.leavingClosedSite) { + return; + } + this.leavingClosedSite = true; + const saves = Promise.allSettled( + [...this.exitSavers].map((save) => Promise.resolve().then(save)) + ); + let timer: ReturnType; + const timeout = new Promise( + (resolve) => (timer = setTimeout(resolve, EXIT_SAVE_TIMEOUT_MILLISECONDS)) + ); + Promise.race([saves, timeout]).then(() => { + clearTimeout(timer); + this.logOutWithoutHomeRedirect().subscribe({ + next: () => this.navigateTo('/login?loginDisabled=true'), + error: () => (this.leavingClosedSite = false) + }); + }); + } + + protected navigateTo(url: string): void { + window.location.href = url; + } } diff --git a/src/assets/wise5/vle/node/node.component.ts b/src/assets/wise5/vle/node/node.component.ts index e8c5260cb9b..21d0b04fe7a 100644 --- a/src/assets/wise5/vle/node/node.component.ts +++ b/src/assets/wise5/vle/node/node.component.ts @@ -467,6 +467,14 @@ export class NodeComponent implements OnInit { this.stopAutoSaveInterval(); }) ); + this.subscriptions.add( + this.sessionService.registerExitSaver(() => { + this.stopAutoSaveInterval(); + return this.dirtyComponentIds.length + ? this.createAndSaveComponentData(true) + : Promise.resolve(); + }) + ); } protected saveComponentState($event: any): Promise {