feat(auth): admin-configurable session lifetime ceiling (#1706)

The 24h session cap from the M-2 audit finding was hard-coded, so the
  "Remember Me" checkbox could only control storage location, never
  duration. Add session_max_hours setting (default 24, max 720) honoured
  at all four token-issuance sites: plain login, 2FA TOTP/email, 2FA
  backup, OIDC.

  - backend/app/core/auth.py: SESSION_MAX_HOURS_HARD_CEILING + resolver
    that clamps to [1h, 720h] and falls back to 24h on missing/blank/
    unparseable. DB errors propagate — the login transaction must abort
    on a broken DB rather than silently extend or shrink the lifetime.
  - backend/app/api/routes/auth.py, mfa.py: all four sites read the
    resolved value instead of ACCESS_TOKEN_EXPIRE_MINUTES directly.
  - backend/app/schemas/settings.py, routes/settings.py: schema field
    with ge=1 le=720 + int coercion in _build_settings_response.
  - frontend/src/pages/SettingsPage.tsx: half-width card at top of
    Settings -> Users left column with 24h/7d/30d presets, custom input,
    and a yellow warning when value > 24h.
  - frontend/src/i18n/locales/*.ts: 8 new keys per locale, real
    translations in all 11 (en/de/es/fr/it/ja/ko/pt-BR/tr/zh-CN/zh-TW).
  - backend/tests/integration/test_session_policy.py: 15 tests across
    resolver clamping, login JWT exp end-to-end, settings API round-trip.

  Already-issued tokens keep their original expiry; the new setting only
  affects future logins.
This commit is contained in:
maziggy
2026-06-16 12:00:27 +02:00
parent b97f25d819
commit 2940fbdcf7
22 changed files with 544 additions and 69 deletions
+4 -3
View File
@@ -15,7 +15,6 @@ from sqlalchemy.orm import selectinload
from backend.app.api.routes.settings import get_external_login_url
from backend.app.core.auth import (
ACCESS_TOKEN_EXPIRE_MINUTES,
ALGORITHM,
SECRET_KEY,
Permission,
@@ -31,6 +30,7 @@ from backend.app.core.auth import (
get_user_by_email,
get_user_by_username,
is_jti_revoked,
resolve_session_max_minutes,
revoke_jti,
security,
)
@@ -495,8 +495,9 @@ async def login(raw_request: Request, request: LoginRequest, response: Response,
two_fa_methods=methods,
)
# No 2FA — issue full token immediately
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
# No 2FA — issue full token immediately. Session lifetime honours the
# admin-configurable ceiling (#1706); resolver clamps to [1h, 720h].
access_token_expires = timedelta(minutes=await resolve_session_max_minutes(db))
access_token = create_access_token(data={"sub": user.username}, expires_delta=access_token_expires)
return LoginResponse(
+4 -4
View File
@@ -41,13 +41,13 @@ from sqlalchemy.orm import selectinload, undefer
from backend.app.api.routes._oidc_helpers import assert_safe_public_https_url
from backend.app.api.routes.settings import get_setting, set_setting
from backend.app.core.auth import (
ACCESS_TOKEN_EXPIRE_MINUTES,
RequirePermissionIfAuthEnabled,
create_access_token,
get_current_active_user,
get_user_by_email,
get_user_by_username,
is_auth_enabled,
resolve_session_max_minutes,
verify_password,
)
from backend.app.core.database import get_db
@@ -1242,7 +1242,7 @@ async def verify_2fa(
access_token = create_access_token(
data={"sub": user.username},
expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
expires_delta=timedelta(minutes=await resolve_session_max_minutes(db)),
)
result = await db.execute(select(User).where(User.id == user.id).options(selectinload(User.groups)))
user = result.scalar_one()
@@ -1258,7 +1258,7 @@ async def verify_2fa(
access_token = create_access_token(
data={"sub": user.username},
expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
expires_delta=timedelta(minutes=await resolve_session_max_minutes(db)),
)
# Reload with groups for permission calculation
@@ -2146,7 +2146,7 @@ async def oidc_exchange(
access_token = create_access_token(
data={"sub": user.username},
expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
expires_delta=timedelta(minutes=await resolve_session_max_minutes(db)),
)
return LoginResponse(
+1
View File
@@ -159,6 +159,7 @@ async def _build_settings_response(db: AsyncSession, is_api_key: bool = False) -
"stagger_group_size",
"stagger_interval_minutes",
"forecast_global_lead_time_days",
"session_max_hours",
]:
settings_dict[setting.key] = int(setting.value)
elif setting.key == "default_printer_id":