Skip to content

Let users set their session lifetime in account settings - #2612

Merged
four-random-common-words merged 4 commits into
masterfrom
2518-session-lifetime
Nov 18, 2026
Merged

four-random-common-words merged 4 commits into
masterfrom
2518-session-lifetime

Conversation

@four-random-common-words

@four-random-common-words four-random-common-words commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Issue: #2518
Frontend: hashtopolis/web-ui#859

Users can set their own session lifetime again, and it now decides how long their refresh token is valid. Until now every refresh token got the global maxSessionLength, and the user's sessionLifetime was only used by the old UI's login.

A refresh token now lives for the user's session lifetime, bounded to at least 2 hours and at most maxSessionLength. Since the token is rotated on every refresh, this bounds how long a session survives without the app renewing it (browser closed, computer asleep), not how long it may exist in total.

The minimum of 2 hours matches the access token lifetime: the frontend renews shortly before the access token expires, so a shorter refresh token would already be gone by then and end even active sessions. ACCESS_TOKEN_LIFETIME now refers to the same constant, and maxSessionLength is floored at 2 hours as well (also as a bound of the config setting).

PATCH /api/v2/helper/currentUser accepts sessionLifetime next to email, both optional; both are validated before anything is written, so a rejected attribute leaves the user unchanged. Creating a user validates the lifetime too, and new users (including the initial admin) start with 2 hours. A migration raises stored lifetimes below 2 hours to 2 hours.

A changed lifetime applies from the next refresh or login; the frontend forces a refresh right after saving. Out of range values are still answered with a 500, like the existing user PATCH. The OpenAPI spec still has no request body for the helper PATCH, which was already the case for email.

@four-random-common-words four-random-common-words changed the title Let users set their session lifetime Let users set their session lifetime in account settings Nov 4, 2026
@four-random-common-words
four-random-common-words marked this pull request as ready for review November 10, 2026 09:20
@four-random-common-words
four-random-common-words requested review from s3inlc and a balanced review from Copilot November 10, 2026 09:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A regression test is missing to verify that rotating an existing token uses the user's updated persisted lifetime for the successor's endValid.

1 open issue
What changed in this PR

Restores user-controlled session lifetimes by applying each account’s setting to refresh tokens and cookies, supporting the account-settings feature requested in #2518.

Changes:

  • Bounds refresh-token lifetimes between two hours and the configured maximum, applying changes on login or refresh.
  • Adds optional session-lifetime updates to the current-user endpoint, with validation before writes.
  • Updates account defaults, database migrations, API documentation, and regression tests.
File Description
src/​migrations/​postgres/​20261104120000_session-lifetime-minimum.sql Raises existing PostgreSQL lifetimes to two hours.
src/​migrations/​mysql/​20261104120000_session-lifetime-minimum.sql Raises existing MySQL lifetimes to two hours.
src/​inc/​utils/​UserUtils.php Validates lifetimes and updates new-user defaults.
src/​inc/​utils/​RefreshTokenUtils.php Applies bounded per-user lifetimes to refresh tokens.
src/​inc/​utils/​ConfigUtils.php Enforces a two-hour configuration minimum.
src/​inc/​utils/​AccountUtils.php Centralizes session-lifetime validation.
src/​inc/​startup/​setup.php Sets the initial administrator’s lifetime to two hours.
src/​inc/​apiv2/​helper/​CurrentUserHelperAPI.php Supports validated partial email and lifetime updates.
src/​inc/​apiv2/​auth/​token.routes.php Passes user context when issuing tokens and cookies.
src/​inc/​apiv2/​auth/​RefreshTokenCookie.php Uses the user’s bounded lifetime for cookies.
openapi.json Updates the current-user PATCH description.
ci/​phpunit/​inc/​utils/​UserUtilsTest.php Tests rejection of short lifetimes during creation.
ci/​phpunit/​inc/​utils/​RefreshTokenUtilsTest.php Tests lifetime bounds and updates token calls.
ci/​phpunit/​inc/​utils/​AccountUtilsTest.php Tests revised lifetime validation boundaries.
ci/​apiv2/​utils.py Updates shared user fixtures to the new minimum.
ci/​apiv2/​test_user.py Tests lifetime creation and update validation.
ci/​apiv2/​test_permissions.py Tests self-service lifetime updates and rejected partial updates.
ci/​apiv2/​test_attributes.py Updates attribute-test fixtures to valid lifetimes.

Review effort: Balanced

return [
"user" => $user,
"token" => self::issue($token->getUserId(), $token->getFamilyId()),
"token" => self::issue($user, $token->getFamilyId()),

@s3inlc s3inlc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reminder to update migrations date before merge

@four-random-common-words
four-random-common-words merged commit f995a6c into master Nov 18, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants