Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/app/core/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion src/app/core/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading