fix: Implement proper session management with idle timeout for auth#1795
Open
fix: Implement proper session management with idle timeout for auth#1795
Conversation
Co-authored-by: sandi <sandi@calimero.network>
|
Cursor Agent can help with this pull request. Just |
Co-authored-by: sandi <sandi@calimero.network>
…ment-with-ml41ucm5
Co-authored-by: sandi <sandi@calimero.network>
274c4e3 to
16ff7e2
Compare
SDK JS Workflows FailedThe following SDK JS workflow(s) failed:
Please check the workflow logs for more details. |
…ment-with-ml41ucm5
Co-authored-by: sandi <sandi@calimero.network>
Co-authored-by: sandi <sandi@calimero.network>
Co-authored-by: sandi <sandi@calimero.network>
…ment-with-ml41ucm5
Co-authored-by: sandi <sandi@calimero.network>
Merobox Proposals Workflows FailedThe following proposal workflow(s) failed:
Please check the workflow logs for more details. |
There was a problem hiding this comment.
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>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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:
JwtConfigupdate: Added anidle_timeoutfield (defaulting to 30 minutes, configurable to 0 to disable) tocrates/auth/src/config.rs.KeyMetadataenhancements: Introduced alast_activityfield tocrates/auth/src/storage/models/keys.rsto track the last time a session was used. New methodstouch(),get_last_activity(), andis_idle()were added to manage and check session activity, with backward compatibility for existing keys.TokenManager::verify_token_from_headersincrates/auth/src/auth/token/jwt.rsto:last_activitytimestamp on successful token verification, implementing a sliding window.Test plan
Nine new unit tests were added to
crates/auth/src/storage/models/keys.rsto verify the idle timeout functionality:test_key_metadata_new: VerifiesKeyMetadatainitialization, includinglast_activity.test_key_metadata_touch: Checks thatlast_activityis updated whentouch()is called.test_key_metadata_get_last_activity_with_value: Confirmsget_last_activity()returns the set value.test_key_metadata_get_last_activity_fallback: Ensuresget_last_activity()falls back tocreated_atwhenlast_activityisNone.test_key_metadata_is_idle_disabled: Verifiesis_idle()returnsfalsewhenidle_timeout_secsis 0.test_key_metadata_is_idle_not_expired: Checksis_idle()returnsfalsefor a recently active session.test_key_metadata_is_idle_expired: Confirmsis_idle()returnstruefor an idle session.test_key_metadata_backward_compatibility: Tests thatget_last_activity()correctly usescreated_atfor older keys withoutlast_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 authand 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
JwtConfigandKeyMetadatato 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.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,0disables), persistingKeyMetadata.last_activity, and rejecting token verification/refresh when a key has been idle too long; successful auth now best-effort updateslast_activityin storage.Adds unit coverage around
KeyMetadataidle-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.