feat(jans-fido) : fixed the ttl and session update issue#13829
feat(jans-fido) : fixed the ttl and session update issue#13829imran-ishaq wants to merge 1 commit intomainfrom
Conversation
Signed-off-by: imran <imranishaq7071@gmail.com>
📝 WalkthroughWalkthroughModified Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@jans-fido2/server/src/main/java/io/jans/fido2/service/persist/UserSessionIdService.java`:
- Around line 121-126: The current TTL refresh in UserSessionIdService computes
remainingTtl from entity.getExpirationDate() and calls
entity.setTtl(remainingTtl) but never updates the entity's expirationDate, so
active sessions don't extend; change the logic to recompute and set a new
expirationDate when TTL is refreshed (e.g., compute newExpiry = now +
configuredSessionWindow or use the shared auth-server expiration calculator if
available), then call entity.setExpirationDate(newExpiry) and set
entity.setTtl((int)((newExpiry.getTime() - System.currentTimeMillis())/1000));
also handle non-positive remainingTtl by replacing stale values rather than
leaving them unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 5dedb0df-6d0d-4ac4-af82-c2638b49fdee
📒 Files selected for processing (1)
jans-fido2/server/src/main/java/io/jans/fido2/service/persist/UserSessionIdService.java
| if (entity.getExpirationDate() != null) { | ||
| int remainingTtl = (int) ((entity.getExpirationDate().getTime() - System.currentTimeMillis()) / 1000); | ||
| if (remainingTtl > 0) { | ||
| entity.setTtl(remainingTtl); | ||
| } | ||
| } |
There was a problem hiding this comment.
TTL refresh logic still does not extend the session window
Line 121-126 derives ttl from the current expirationDate but never recalculates expirationDate itself, so expiry stays anchored to the old deadline. That does not satisfy the “active session extends lifetime” objective; also, when computed TTL is non-positive, stale TTL can remain unchanged.
Proposed fix
public void updateSessionId(SessionId entity) {
- entity.setLastUsedAt(new Date());
- if (entity.getExpirationDate() != null) {
- int remainingTtl = (int) ((entity.getExpirationDate().getTime() - System.currentTimeMillis()) / 1000);
- if (remainingTtl > 0) {
- entity.setTtl(remainingTtl);
- }
- }
+ final Date now = new Date();
+ entity.setLastUsedAt(now);
+
+ if (entity.getCreationDate() != null && entity.getExpirationDate() != null) {
+ final long lifetimeSeconds = Math.max(
+ 1L,
+ (entity.getExpirationDate().getTime() - entity.getCreationDate().getTime()) / 1000L
+ );
+ entity.setExpirationDate(new Date(now.getTime() + lifetimeSeconds * 1000L));
+ entity.setTtl((int) lifetimeSeconds);
+ }
persistenceEntryManager.merge(entity);
}If you already have a shared auth-server-style expiration calculator (state-aware), use that here to avoid drift.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In
`@jans-fido2/server/src/main/java/io/jans/fido2/service/persist/UserSessionIdService.java`
around lines 121 - 126, The current TTL refresh in UserSessionIdService computes
remainingTtl from entity.getExpirationDate() and calls
entity.setTtl(remainingTtl) but never updates the entity's expirationDate, so
active sessions don't extend; change the logic to recompute and set a new
expirationDate when TTL is refreshed (e.g., compute newExpiry = now +
configuredSessionWindow or use the shared auth-server expiration calculator if
available), then call entity.setExpirationDate(newExpiry) and set
entity.setTtl((int)((newExpiry.getTime() - System.currentTimeMillis())/1000));
also handle non-positive remainingTtl by replacing stale values rather than
leaving them unchanged.
Description
Implemented a robust and reliable mechanism, aligned with the approach used in the Jans Auth Server, to properly manage session Time-To-Live (TTL) and ensure accurate updates of active sessions.
Target issue
This enhancement resolves the issue in jans-fido2 where session TTL was not being updated correctly, and active session states were not consistently refreshed. The fix ensures that session lifecycles are maintained accurately, improving overall stability and consistency of session management.
closes #13818
Implementation Details
Test and Document the changes
Please check the below before submitting your PR. The PR will not be merged if there are no commits that start with
docs:to indicate documentation changes or if the below checklist is not selected.Summary by CodeRabbit