Skip to content

fix: Set Keycloak token lifespans and add 401 retry with token refresh - #1100

Merged
cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/keycloak-token-expiry
Mar 25, 2026
Merged

cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/keycloak-token-expiry

Conversation

@cwiklik

@cwiklik cwiklik commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1009

Keycloak realm was created with default 5-minute access token lifespan, causing frequent "Signature has expired" errors. Users reported this after just hours (sometimes days) of use, with no way to recover without clearing browser cache or restarting all deployments.

Changes

1. Set token lifespans on realm bootstrap (kagenti/auth/ui-oauth-secret/auth_secret.py)

  • accessTokenLifespan: 1 hour (vs 5-minute Keycloak default)
  • ssoSessionIdleTimeout: 24 hours
  • ssoSessionMaxLifespan: 7 days

2. Add 401 retry with force-refreshed token (kagenti/ui-v2/src/services/api.ts, kagenti/ui-v2/src/contexts/AuthContext.tsx)

  • On 401 response, the API service calls keycloak.updateToken(-1) to force-refresh the token (bypassing cache)
  • Retries the failed request once with the fresh token
  • If retry also fails, throws the original error

Why both fixes?

Fix What it prevents
Longer token lifespan Reduces frequency of refreshes, fewer chances for expiry gaps
401 retry with refresh Catches remaining edge cases (backgrounded tabs, network blips, refresh timing gaps)

Test plan

  • Deploy to Kind cluster with auth enabled
  • Verify new realm gets 1-hour token lifespan: kubectl exec -it deployment/keycloak -n keycloak -- curl -s http://localhost:8080/realms/kagenti | jq .accessTokenLifespan (should return 3600)
  • Verify 401 retry: temporarily set short token lifespan, let token expire, confirm API call succeeds after automatic refresh
  • CI passes

Fixes rossoctl#1009 — Keycloak realm was created with default 5-minute access
token lifespan, causing "Signature has expired" errors. Two changes:

1. Set reasonable token lifespans on realm bootstrap:
   - accessTokenLifespan: 1 hour (vs 5 min default)
   - ssoSessionIdleTimeout: 24 hours
   - ssoSessionMaxLifespan: 7 days

2. Add automatic 401 retry with force-refreshed token in the UI API
   layer. On 401 response, the API service force-refreshes the Keycloak
   token (bypassing cache) and retries the request once. This makes
   the system resilient to token expiry edge cases (backgrounded tabs,
   network blips, timing gaps in the 30s refresh interval).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>

@Alan-Cha Alan-Cha 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.

Summary

This PR provides a robust two-layer solution to the token expiry issue (#1009):

  1. Preventive: Sets reasonable Keycloak token lifespans (1-hour access tokens vs 5-minute default)
  2. Defensive: Adds automatic 401 retry with force-refreshed tokens in the UI

The implementation is sound with proper error handling. The Python change correctly uses Keycloak's realm creation API with seconds-based timeout values. The TypeScript changes follow React patterns with proper useCallback dependencies.

Areas reviewed: Python (auth), Frontend (TypeScript/React), Security
Commits: 1 commit, properly signed-off with Assisted-By trailer
CI status: All checks passing (20/20)


Inline Feedback

✅ kagenti/auth/ui-oauth-secret/auth_secret.py (lines 282-286)

Praise: Good inline documentation with issue reference. These timeout values are well-reasoned and appropriate.

💡 kagenti/ui-v2/src/contexts/AuthContext.tsx (line 52-54)

Suggestion: The catch block swallows errors silently. Consider logging error details:

} catch (error) {
  console.error('Force token refresh failed:', error);
  return null;
}

💡 kagenti/ui-v2/src/services/api.ts (line 112)

Nit: Consider logging retry attempts for observability:

console.info('Retrying API request with refreshed token');
const retryResponse = await fetch(url, {

💡 kagenti/ui-v2/src/services/api.ts (line 122)

Nit: Retry JSON parsing could fail. Add error context:

return retryResponse.json().catch(() => ({ detail: 'Response parse failed' }));

No blocking issues. These are minor suggestions for improved observability and error context.

@cwiklik
cwiklik merged commit d0f3054 into rossoctl:main Mar 25, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Ready for Review!

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

🐛 Kagenti self certificates expire after a few days

2 participants