Skip to content

fix: Implement proper session management with idle timeout for auth#1795

Open
chefsale wants to merge 11 commits intomasterfrom
bounty-fix/implement-proper-session-management-with-ml41ucm5
Open

fix: Implement proper session management with idle timeout for auth#1795
chefsale wants to merge 11 commits intomasterfrom
bounty-fix/implement-proper-session-management-with-ml41ucm5

Conversation

@chefsale
Copy link
Member

@chefsale chefsale commented Feb 1, 2026

fix: Implement proper session management with idle timeout for auth

Description

This PR addresses the bounty to implement proper session management with an idle timeout for JWT authentication in the crates/auth/ module. Previously, JWT tokens only had an expiry, but no mechanism to revoke sessions that remained unused for a period. This change enhances security by automatically revoking sessions that have been idle for a configurable duration.

The changes include:

  • JwtConfig update: Added an idle_timeout field (defaulting to 30 minutes, configurable to 0 to disable) to crates/auth/src/config.rs.
  • KeyMetadata enhancements: Introduced a last_activity field to crates/auth/src/storage/models/keys.rs to track the last time a session was used. New methods touch(), get_last_activity(), and is_idle() were added to manage and check session activity, with backward compatibility for existing keys.
  • Sliding window expiration: Modified TokenManager::verify_token_from_headers in crates/auth/src/auth/token/jwt.rs to:
    • Check for idle timeout before validating the token.
    • Update the last_activity timestamp on successful token verification, implementing a sliding window.
    • Return an "Session has expired due to inactivity" error if the session has been idle.

Test plan

Nine new unit tests were added to crates/auth/src/storage/models/keys.rs to verify the idle timeout functionality:

  • test_key_metadata_new: Verifies KeyMetadata initialization, including last_activity.
  • test_key_metadata_touch: Checks that last_activity is updated when touch() is called.
  • test_key_metadata_get_last_activity_with_value: Confirms get_last_activity() returns the set value.
  • test_key_metadata_get_last_activity_fallback: Ensures get_last_activity() falls back to created_at when last_activity is None.
  • test_key_metadata_is_idle_disabled: Verifies is_idle() returns false when idle_timeout_secs is 0.
  • test_key_metadata_is_idle_not_expired: Checks is_idle() returns false for a recently active session.
  • test_key_metadata_is_idle_expired: Confirms is_idle() returns true for an idle session.
  • test_key_metadata_backward_compatibility: Tests that get_last_activity() correctly uses created_at for older keys without last_activity.
  • test_key_is_valid_and_not_idle: Ensures a newly created key is valid and not immediately idle.

All tests were run locally using cargo test --package auth and passed successfully. No end-to-end tests were modified or added, as the changes are internal to the authentication logic.

Documentation update

Internal code comments have been updated for JwtConfig and KeyMetadata to reflect the new fields and their purpose. If there is any public-facing documentation describing JWT session behavior, it should be updated to mention the new idle timeout feature and its configurable nature.


Open in Cursor Open in Web


Note

High Risk
Changes authentication session validation/refresh behavior by introducing an idle-timeout check and updating persisted key metadata on each use, which can invalidate previously long-lived sessions. Also adjusts sync orchestration (extra fine-sync pass and richer error wrapping/logging), which can affect node convergence behavior under failure conditions.

Overview
Implements sliding-window session management for JWT auth by adding JwtConfig.idle_timeout (default 30m, 0 disables), persisting KeyMetadata.last_activity, and rejecting token verification/refresh when a key has been idle too long; successful auth now best-effort updates last_activity in storage.

Adds unit coverage around KeyMetadata idle-timeout/backward-compat behavior, updates embedded auth defaults accordingly, and removes a CI workaround that patched the SDK JS repo workflow. Separately, sync now wraps errors with more context, logs failed peer sync attempts, and runs an additional fine-sync after successful snapshot sync to reduce the chance of being slightly behind.

Written by Cursor Bugbot for commit 6effae8. This will update automatically on new commits. Configure here.

Co-authored-by: sandi <sandi@calimero.network>
@cursor
Copy link

cursor bot commented Feb 1, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@chefsale chefsale changed the title Authentication idle timeout fix: Implement proper session management with idle timeout for auth Feb 1, 2026
@chefsale chefsale marked this pull request as ready for review February 1, 2026 18:15
Co-authored-by: sandi <sandi@calimero.network>
Co-authored-by: sandi <sandi@calimero.network>
@cursor cursor bot force-pushed the bounty-fix/implement-proper-session-management-with-ml41ucm5 branch from 274c4e3 to 16ff7e2 Compare February 1, 2026 19:13
@github-actions
Copy link

github-actions bot commented Feb 1, 2026

SDK JS Workflows Failed

The following SDK JS workflow(s) failed:

  • examples/kv-store-with-user-and-frozen-storage/workflows/test_user_storage.yml
  • examples/blobs/workflows/blobs-js.yml

Please check the workflow logs for more details.

chefsale and others added 6 commits February 2, 2026 10:22
Co-authored-by: sandi <sandi@calimero.network>
Co-authored-by: sandi <sandi@calimero.network>
Co-authored-by: sandi <sandi@calimero.network>
Co-authored-by: sandi <sandi@calimero.network>
@github-actions
Copy link

github-actions bot commented Feb 2, 2026

Merobox Proposals Workflows Failed

The following proposal workflow(s) failed:

  • near
  • icp
  • ethereum

Please check the workflow logs for more details.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Co-authored-by: sandi <sandi@calimero.network>
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.

2 participants