Repository navigation
fix: Set Keycloak token lifespans and add 401 retry with token refresh - #1100
Conversation
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
left a comment
There was a problem hiding this comment.
Summary
This PR provides a robust two-layer solution to the token expiry issue (#1009):
- Preventive: Sets reasonable Keycloak token lifespans (1-hour access tokens vs 5-minute default)
- 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.
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 hoursssoSessionMaxLifespan: 7 days2. Add 401 retry with force-refreshed token (
kagenti/ui-v2/src/services/api.ts,kagenti/ui-v2/src/contexts/AuthContext.tsx)keycloak.updateToken(-1)to force-refresh the token (bypassing cache)Why both fixes?
Test plan
kubectl exec -it deployment/keycloak -n keycloak -- curl -s http://localhost:8080/realms/kagenti | jq .accessTokenLifespan(should return 3600)