ZTF Keycloak Integration Tutorial¶
This tutorial demonstrates how to integrate ZTF (Zero Trust Fabric) Keycloak authentication into a React application. This example shows a complete passwordless authentication flow using the Authorization Code Flow with PKCE (Proof Key for Code Exchange).
Tutorial 1 Code Repository: The complete working code for this tutorial is available at https://github.com/viascience/ztf-tutorial. Clone this repository to follow along with the tutorial and test the implementation.
Overview¶
This application demonstrates:
- Passwordless authentication using Keycloak
- JWT token management and automatic refresh
- Secure logout functionality
- CORS configuration for production deployment
- Docker containerization with nginx
Prerequisites¶
- Node.js (version 18 or higher)
- npm or yarn
- Access to a ZTF Keycloak instance (use the default configuration provided in this tutorial or contact VIA to get a production setup for your application)
- Basic understanding of React and OAuth 2.0/OpenID Connect
Keycloak Configuration¶
Server Details¶
- Keycloak URL:
https://auth.solvewithvia.com/auth - Realm:
ztf_demo - Client ID:
localhost-app(pre-configured for local development)
Client Configuration in Keycloak¶
The localhost-app client is pre-configured for development.
Installation & Setup¶
1. Get the Tutorial Code¶
First, clone the tutorial repository containing the complete working code:
2. Dependencies¶
All dependencies are automatically installed during the Docker build process. The project uses these essential packages (as defined in package.json):
keycloak-js: ^25.0.6 - Keycloak JavaScript adapterreact: ^18.2.0 - React frameworkreact-dom: ^18.2.0 - React DOM rendererhttp-proxy-middleware: ^2.0.6 - Development CORS proxy
2. Configure Keycloak Client¶
Update the Keycloak configuration in src/App.tsx:
Note for Production: For production deployments, use environment variables instead of hardcoded values to configure Keycloak settings.
Application Structure¶
Core Files¶
src/App.tsx- Main application component with Keycloak integrationsrc/setupProxy.js- Development CORS configurationnginx.conf- Production CORS configurationpublic/silent-check-sso.html- Silent SSO check for iframe-based authenticationDockerfile- Container configuration for production deployment
Key Components¶
1. Keycloak Initialization (src/App.tsx)¶
Key Configuration Options:
onLoad: "login-required"- Automatically redirect to login if not authenticatedpkceMethod: "S256"- Use PKCE for secure authenticationuseNonce: true- Enabled for better security with nonce validationcheckLoginIframe: false- Disabled for better compatibility with modern browsers that block cross-origin iframe communication and CSP restrictions
2. Token Management (src/App.tsx)¶
The application includes automatic token refresh:
3. Logout Functionality (src/App.tsx)¶
Development Setup¶
1. Build and Run with Docker¶
To run the ZTF integration tutorial locally, ensure you're in the cloned repository directory and use the following Docker commands:
The application will start on http://localhost:80 and be ready for ZTF Keycloak authentication.
2. Build and Run with Docker Compose¶
For simplified deployment and management, use Docker Compose:
The application will start on http://localhost:80 and be ready for ZTF Keycloak authentication.
3. CORS Configuration for Development¶
The src/setupProxy.js file configures CORS headers for development:
Production Deployment¶
1. Docker Build¶
2. Docker Run¶
3. Production CORS Configuration¶
The nginx.conf file includes production-ready CORS configuration:
Authentication Flow¶
1. Initial Load¶
- Application initializes Keycloak client
- Checks for existing authentication state
- If not authenticated, redirects to Keycloak login page
2. Login Process¶
- User is redirected to ZTF Keycloak login page
- User authenticates (passwordless flow)
- Keycloak redirects back with authorization code
- Application exchanges code for JWT using PKCE
3. Token Management¶
- Application stores JWT
- Automatically refreshes tokens every 5 minutes if they expire within 350 seconds
- Displays current token information and user details
4. Logout Process¶
- Clears local token state
- Redirects to Keycloak logout endpoint
- Keycloak performs global logout and redirects back to application
User Information Access¶
Once authenticated, you can access user information from the JWT token:
API Integration¶
To make authenticated API calls, include the JWT token in the Authorization header:
Error Handling¶
The application includes comprehensive error handling:
1. Initialization Errors¶
- Network connectivity issues
- Invalid client configuration
- Keycloak server unavailability
2. Authentication Errors¶
- Invalid credentials
- Expired sessions
- Token exchange failures
3. Token Refresh Errors¶
- Network failures during refresh
- Invalid refresh tokens
- Server-side token revocation
Troubleshooting¶
Common Issues¶
- "Invalid nonce" errors: If you encounter nonce validation errors, ensure your keycloak-js version matches your Keycloak deployment version. Current configuration uses
useNonce: truewith Keycloak 25.0.6 - CORS errors: Check that your domain is added to Keycloak client's Web Origins
- Redirect URI mismatch: Verify Valid Redirect URIs in Keycloak client configuration
- Token refresh failures: Check network connectivity and Keycloak server status
Debug Mode¶
Enable detailed logging by setting enableLogging: true in Keycloak initialization. Check browser console for detailed authentication flow information.
Security Considerations¶
- PKCE: Always use PKCE for public clients (single-page applications)
- Token Storage: Tokens are stored in memory, not localStorage, for better security
- HTTPS: Always use HTTPS in production for secure token transmission
- CORS: Configure restrictive CORS policies in production
- Token Validation: Implement proper token validation on your backend services
Support¶
For issues related to:
- ZTF Keycloak configuration: Contact ZTF support team or visit the GitHub Issues page
- Application integration: Refer to Keycloak.js documentation
- This tutorial: Create an issue in the project repository