diff --git a/src/app/core/auth/auth.service.spec.ts b/src/app/core/auth/auth.service.spec.ts index 32ceee35d65..27aab85ca7a 100644 --- a/src/app/core/auth/auth.service.spec.ts +++ b/src/app/core/auth/auth.service.spec.ts @@ -13,7 +13,7 @@ import { CookieService } from '../services/cookie.service'; import { AuthRequestServiceStub } from '../../shared/testing/auth-request-service.stub'; import { AuthRequestService } from './auth-request.service'; import { AuthStatus } from './models/auth-status.model'; -import { AuthTokenInfo } from './models/auth-token-info.model'; +import { AuthTokenInfo, TOKENITEM } from './models/auth-token-info.model'; import { EPerson } from '../eperson/models/eperson.model'; import { EPersonMock } from '../../shared/testing/eperson.mock'; import { AppState } from '../../app.reducer'; @@ -369,6 +369,30 @@ describe('AuthService test', () => { expect(storage.set).toHaveBeenCalled(); }); + it('should store the token with a Secure, SameSite cookie when served over https', () => { + Object.defineProperty(window, 'nativeWindow', { + get: () => ({ location: { protocol: 'https:' } }), + configurable: true, + }); + authService.storeToken(token); + expect(storage.set).toHaveBeenCalledWith(TOKENITEM, token, jasmine.objectContaining({ + secure: true, + sameSite: 'lax', + })); + }); + + it('should store the token without the Secure flag when served over http', () => { + Object.defineProperty(window, 'nativeWindow', { + get: () => ({ location: { protocol: 'http:' } }), + configurable: true, + }); + authService.storeToken(token); + expect(storage.set).toHaveBeenCalledWith(TOKENITEM, token, jasmine.objectContaining({ + secure: false, + sameSite: 'lax', + })); + }); + it('should remove token from storage', () => { authService.removeToken(); expect(storage.remove).toHaveBeenCalled(); diff --git a/src/app/core/auth/auth.service.ts b/src/app/core/auth/auth.service.ts index 6d4ee2de4f6..936efac6d3d 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -456,7 +456,12 @@ export class AuthService { // Set the cookie expire date const expires = new Date(expireDate); - const options: CookieAttributes = {expires: expires}; + // Use Secure on HTTPS; Lax allows external-auth redirects via top-level GET navigation. + const options: CookieAttributes = { + expires: expires, + secure: this._window.nativeWindow.location?.protocol === 'https:', + sameSite: 'lax', + }; // Save cookie with the token return this.storage.set(TOKENITEM, token, options);