-
Notifications
You must be signed in to change notification settings - Fork 1
VIA-942: Enforce Session Expiry Reliably After 10 Minutes of Inactivity #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liming-cheung-nhs
wants to merge
11
commits into
main
Choose a base branch
from
VIA-942/set-signout-cookie
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e9ffc8c
TEMPCOMMIT - Prevent getSession from returning a token if signOut was…
donna-belsey-nhs 296e908
set signout cookie flag to session id
liming-cheung-nhs de5a981
add info on new cookie for user
liming-cheung-nhs 1f86212
add cookie table test
liming-cheung-nhs b8a2c56
update cookie info to reflect final design
liming-cheung-nhs 209c48c
update cookie info to reflect final design 2
liming-cheung-nhs 2f54fb7
replace sanitize-data with missing dependency mocks
liming-cheung-nhs 2523676
tidy up tests
liming-cheung-nhs acce62c
wrap userLogout action - logout and set cookie - in request context
liming-cheung-nhs bd12691
copilot comments
liming-cheung-nhs ddf571b
move signout back to client side to match the UI handling as is
liming-cheung-nhs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import setSignOutFlagCookie from "@src/utils/auth/setSignOutFlagCookie"; | ||
| import { SESSION_ID_COOKIE_NAME, SIGNOUT_FLAG_COOKIE_NAME } from "@src/utils/constants"; | ||
|
|
||
| const mockSessionId = "session-id-123"; | ||
| const setCookie = jest.fn(); | ||
|
|
||
| jest.mock("@src/utils/requestScopedStorageWrapper", () => ({ | ||
| requestScopedStorageWrapper: jest.fn((fn) => fn()), | ||
| })); | ||
| jest.mock("@src/utils/config", () => ({ | ||
| __esModule: true, | ||
| default: { | ||
| MAX_SESSION_AGE_MINUTES: Promise.resolve(2), | ||
| }, | ||
| })); | ||
| jest.mock("@src/utils/logger", () => ({ | ||
| mockWarn: jest.fn(), | ||
| logger: { | ||
| child: jest.fn(() => { | ||
| const mockedLoggerModule = jest.requireMock("@src/utils/logger") as { mockWarn: jest.Mock }; | ||
| return { warn: mockedLoggerModule.mockWarn }; | ||
| }), | ||
| }, | ||
| })); | ||
|
|
||
| let mockGetCookie = (name: string) => { | ||
| if (name === SESSION_ID_COOKIE_NAME) { | ||
| return { value: mockSessionId }; | ||
| } | ||
| return undefined; | ||
| }; | ||
|
|
||
| jest.mock("next/headers", () => ({ | ||
| cookies: jest.fn(() => ({ | ||
| get: jest.fn((name) => mockGetCookie(name)), | ||
| set: setCookie, | ||
| })), | ||
| headers: jest.fn(), | ||
| })); | ||
|
|
||
| describe("setSignOutFlagCookie", () => { | ||
| const getLoggerWarnMock = (): jest.Mock => { | ||
| const mockedLoggerModule = jest.requireMock("@src/utils/logger") as { mockWarn: jest.Mock }; | ||
| return mockedLoggerModule.mockWarn; | ||
| }; | ||
|
|
||
| it("should set signout cookie with the current session id", async () => { | ||
| mockGetCookie = (name: string) => { | ||
| if (name === SESSION_ID_COOKIE_NAME) { | ||
| return { value: mockSessionId }; | ||
| } | ||
| return undefined; | ||
| }; | ||
| setCookie.mockClear(); | ||
| await setSignOutFlagCookie(); | ||
| const expectedCookieTimeoutSeconds = 30; | ||
| expect(setCookie).toHaveBeenCalledWith(SIGNOUT_FLAG_COOKIE_NAME, mockSessionId, { | ||
| maxAge: expectedCookieTimeoutSeconds, | ||
| secure: true, | ||
| httpOnly: true, | ||
| sameSite: "lax", | ||
| path: "/", | ||
| }); | ||
| }); | ||
|
|
||
| it("should not set signout cookie if session id is missing", async () => { | ||
| mockGetCookie = () => { | ||
| return undefined; | ||
| }; | ||
| setCookie.mockClear(); | ||
| const warnMock = getLoggerWarnMock(); | ||
| warnMock.mockClear(); | ||
|
|
||
| await setSignOutFlagCookie(); | ||
|
|
||
| expect(setCookie).not.toHaveBeenCalled(); | ||
| expect(warnMock).toHaveBeenCalledWith("Session ID missing, skipping signout cookie"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| "use server"; | ||
|
|
||
| import { SESSION_ID_COOKIE_NAME, SIGNOUT_FLAG_COOKIE_NAME } from "@src/utils/constants"; | ||
| import { logger } from "@src/utils/logger"; | ||
| import { requestScopedStorageWrapper } from "@src/utils/requestScopedStorageWrapper"; | ||
| import { cookies } from "next/headers"; | ||
|
|
||
| const log = logger.child({ module: "utils-auth-setSignOutFlagCookie" }); | ||
|
|
||
| const setSignOutFlagCookie = async () => { | ||
| return requestScopedStorageWrapper(setSignOutFlagCookieAction); | ||
| }; | ||
|
|
||
| const setSignOutFlagCookieAction = async () => { | ||
| const SIGN_OUT_FLAG_COOKIE_MAX_AGE_SECONDS = 30; | ||
|
liming-cheung-nhs marked this conversation as resolved.
|
||
| const cookieStore = await cookies(); | ||
| const currentSessionId = cookieStore.get(SESSION_ID_COOKIE_NAME)?.value ?? ""; | ||
| if (!currentSessionId) { | ||
| log.warn("Session ID missing, skipping signout cookie"); | ||
| return; | ||
| } | ||
| cookieStore.set(SIGNOUT_FLAG_COOKIE_NAME, currentSessionId, { | ||
| secure: true, | ||
| httpOnly: true, | ||
| sameSite: "lax", | ||
|
liming-cheung-nhs marked this conversation as resolved.
|
||
| path: "/", | ||
| maxAge: SIGN_OUT_FLAG_COOKIE_MAX_AGE_SECONDS, | ||
| }); | ||
|
liming-cheung-nhs marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| export default setSignOutFlagCookie; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to split the logout flow back again because the two steps need different runtimes.
The signout cookie must be set on the server so it can use request cookies and requestScopedStorageWrapper.
The actual signOut() stays on the client because when both steps were moved to the server, the logout response no longer preserved the signout cookie reliably, so the logout flow stopped working properly. Keeping signOut() on the client preserves the cookie step and still gives the expected immediate logout and redirect.