|
| 1 | +# KeycloakService Refactoring: Migration from keycloak-js to oidc-client-ts |
| 2 | + |
| 3 | +## Overview |
| 4 | +The KeycloakService has been successfully refactored to use `oidc-client-ts` instead of `keycloak-js`. This modern OIDC library provides better TypeScript support, more flexibility, and follows standard OIDC protocols. |
| 5 | + |
| 6 | +## Key Changes Made |
| 7 | + |
| 8 | +### 1. Package Dependencies |
| 9 | +- **Removed**: `keycloak-js` (v25.0.4) |
| 10 | +- **Added**: `oidc-client-ts` (v3.0.1) |
| 11 | + |
| 12 | +### 2. Import Changes |
| 13 | +```typescript |
| 14 | +// Before (keycloak-js) |
| 15 | +import Keycloak, { |
| 16 | + KeycloakInitOptions, |
| 17 | + KeycloakTokenParsed, |
| 18 | + KeycloakConfig, |
| 19 | +} from "keycloak-js"; |
| 20 | + |
| 21 | +// After (oidc-client-ts) |
| 22 | +import { |
| 23 | + UserManager, |
| 24 | + User, |
| 25 | + UserManagerSettings, |
| 26 | + WebStorageStateStore, |
| 27 | + Log |
| 28 | +} from "oidc-client-ts"; |
| 29 | +``` |
| 30 | + |
| 31 | +### 3. Configuration Changes |
| 32 | +The service now uses `UserManagerSettings` instead of Keycloak-specific configuration: |
| 33 | + |
| 34 | +```typescript |
| 35 | +// Before |
| 36 | +private _keycloakConfig: KeycloakConfig |
| 37 | +private kc: Keycloak |
| 38 | + |
| 39 | +// After |
| 40 | +private _userManagerConfig: UserManagerSettings |
| 41 | +private userManager: UserManager |
| 42 | +``` |
| 43 | + |
| 44 | +### 4. Authentication Flow Updates |
| 45 | + |
| 46 | +#### Configuration |
| 47 | +- Authority URL format: `${url}/realms/${realm}` (compatible with Keycloak) |
| 48 | +- Response type: `code` (PKCE flow) |
| 49 | +- Scopes: `"openid profile email offline_access"` |
| 50 | +- Automatic silent renewal enabled |
| 51 | +- Web storage for state management |
| 52 | + |
| 53 | +#### Event Handlers |
| 54 | +Added comprehensive event handling for: |
| 55 | +- Access token expiring/expired |
| 56 | +- Silent renew errors |
| 57 | +- User loaded/unloaded events |
| 58 | + |
| 59 | +### 5. Method Refactoring |
| 60 | + |
| 61 | +#### Core Methods |
| 62 | +- `initKeycloak()` → `initKeycloak()` (maintained same interface) |
| 63 | +- `login()` → `signinRedirect()` |
| 64 | +- `logout()` → `signoutRedirect()` |
| 65 | +- `updateToken()` → `signinSilent()` |
| 66 | + |
| 67 | +#### New Methods Added |
| 68 | +- `handleCallback()` - Handle authentication redirect callback |
| 69 | +- `extractUserRoles()` - Extract roles from OIDC token claims |
| 70 | +- `setupEventHandlers()` - Configure OIDC event listeners |
| 71 | +- `handleUserLoaded()`/`handleUserUnloaded()` - User state management |
| 72 | + |
| 73 | +### 6. Token Management |
| 74 | +- Automatic token refresh via `automaticSilentRenew` |
| 75 | +- Improved token expiration handling |
| 76 | +- Better offline mode support |
| 77 | +- Consistent storage management |
| 78 | + |
| 79 | +### 7. Role Extraction |
| 80 | +The service now supports multiple role claim formats: |
| 81 | +- `resource_access.{client-id}.roles` (Keycloak style) |
| 82 | +- `realm_access.roles` (Keycloak realm roles) |
| 83 | +- `roles` (direct roles claim) |
| 84 | +- `groups` (groups as roles) |
| 85 | + |
| 86 | +### 8. Static Files Created |
| 87 | +- `public/silent-check-sso.html` - Silent authentication iframe |
| 88 | +- `public/callback.html` - Authentication callback handler |
| 89 | + |
| 90 | +## Benefits of Migration |
| 91 | + |
| 92 | +1. **Better TypeScript Support**: Full type safety and IntelliSense |
| 93 | +2. **Standard Compliance**: Follows OIDC/OAuth2 standards |
| 94 | +3. **Improved Error Handling**: Better error reporting and recovery |
| 95 | +4. **Automatic Token Management**: Built-in silent renewal |
| 96 | +5. **Flexibility**: Works with any OIDC-compliant provider |
| 97 | +6. **Modern Architecture**: Event-driven design pattern |
| 98 | +7. **Better Testing**: Easier to mock and test |
| 99 | + |
| 100 | +## Breaking Changes |
| 101 | + |
| 102 | +### API Changes |
| 103 | +- `userLogout()` is now async and returns Promise<void> |
| 104 | +- `initKeycloak()` callback parameter now expects boolean instead of any |
| 105 | +- `isAuthenticated()` now checks token expiration status |
| 106 | +- Added new methods: `getUser()`, `handleCallback()` |
| 107 | + |
| 108 | +### Configuration Changes |
| 109 | +- Redirect URIs must be configured to point to `/callback` |
| 110 | +- Silent check SSO URI points to `/silent-check-sso.html` |
| 111 | +- Client must support PKCE flow |
| 112 | + |
| 113 | +## Migration Checklist |
| 114 | + |
| 115 | +- [x] Update package dependencies |
| 116 | +- [x] Refactor service implementation |
| 117 | +- [x] Update imports and types |
| 118 | +- [x] Create static HTML files |
| 119 | +- [x] Test build process |
| 120 | +- [ ] Update application routes to handle `/callback` path |
| 121 | +- [ ] Test authentication flow end-to-end |
| 122 | +- [ ] Update any consuming components that rely on changed APIs |
| 123 | + |
| 124 | +## Next Steps |
| 125 | + |
| 126 | +1. **Route Configuration**: Ensure your application handles the `/callback` route |
| 127 | +2. **Testing**: Thoroughly test the authentication flow |
| 128 | +3. **Error Handling**: Verify error scenarios work as expected |
| 129 | +4. **Documentation**: Update any API documentation for consuming applications |
| 130 | + |
| 131 | +## Compatibility Notes |
| 132 | + |
| 133 | +The refactored service maintains backward compatibility for most public methods while providing enhanced functionality and better error handling. The offline mode support and role extraction have been improved to work with various OIDC token formats. |
0 commit comments