This guide explains how to configure the products-web application to use Keycloak for authentication instead of Microsoft Entra ID.
The products-web application supports both Microsoft Entra ID and Keycloak as OAuth2 authentication providers. This flexibility allows you to integrate with your existing identity management infrastructure.
- Keycloak server (version 15+ recommended)
- Administrative access to your Keycloak realm
- Products-web application source code
- Python 3.12+ with required dependencies
- Log into your Keycloak Admin Console
- Navigate to your realm (or create a new one)
- Go to Clients → Create
- Configure the client:
Client ID: products-web-client
Client Protocol: openid-connect
Root URL: http://localhost:8501Configure the following settings for your client:
Access Type: confidential
Standard Flow: Enabled (Authorization Code Flow)
Valid Redirect URIs:
http://localhost:8501/oauth2callback
https://your-domain.com/oauth2callback # For production
Web Origins:
http://localhost:8501
https://your-domain.com # For production
Advanced Settings:
- Proof Key for Code Exchange Code Challenge Method:
S256(recommended)
- Go to the Credentials tab of your client
- Copy the Secret value - you'll need this for
CLIENT_SECRET
If you want to include additional user information in JWT tokens:
- Go to Client Scopes
- Add desired scopes like
profile,email - Configure mappers for additional claims if needed
Create a .env file based on .env.keycloak.example:
# Keycloak Client Configuration
CLIENT_ID=products-web-client
CLIENT_SECRET=your_actual_client_secret_here
# Keycloak Server Configuration
KEYCLOAK_SERVER_URL=https://keycloak.yourdomain.com
KEYCLOAK_REALM=your-realm-name
# OAuth Scopes
SCOPE=openid profile email
# Redirect URI
REDIRECT_URI=http://localhost:8501/oauth2callback
# Authentication Provider
AUTH_PROVIDER=keycloak
# ProductsAgent API Configuration
PRODUCTS_AGENT_URL=http://localhost:8000
# Logging
LOG_LEVEL=INFOFor advanced users who want to specify URLs manually:
# Manual URL configuration (instead of auto-construction)
AUTH_PROVIDER=keycloak
AUTHORIZE_URL=https://keycloak.yourdomain.com/realms/your-realm/protocol/openid-connect/auth
TOKEN_URL=https://keycloak.yourdomain.com/realms/your-realm/protocol/openid-connect/token
REFRESH_TOKEN_URL=https://keycloak.yourdomain.com/realms/your-realm/protocol/openid-connect/token
REVOKE_TOKEN_URL=https://keycloak.yourdomain.com/realms/your-realm/protocol/openid-connect/revokecd products-web
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt# Copy and modify the Keycloak example
cp .env.keycloak.example .env
# Edit .env with your actual Keycloak configuration
nano .envstreamlit run app.py- Navigate to
http://localhost:8501 - Click "Login with Keycloak"
- You'll be redirected to your Keycloak login page
- After successful authentication, you'll be returned to the application
- Ensure the redirect URI in your
.envexactly matches what's configured in Keycloak - Check for trailing slashes and protocol mismatches
- Verify
CLIENT_IDmatches the client ID in Keycloak - Ensure you're using the correct realm
- Double-check the
CLIENT_SECRETvalue - Ensure the client is set to "confidential" access type
- Verify the scopes in your
.envare configured in Keycloak - Common scopes:
openid,profile,email
LOG_LEVEL=DEBUGThe application shows configuration details in the "Configuration Details" expander on the authentication page.
Use browser developer tools to inspect:
- OAuth redirect flows
- Token responses
- API calls to Keycloak endpoints
To include custom user attributes in tokens:
- Create Custom Client Scope in Keycloak
- Add Protocol Mappers for your attributes
- Assign Scope to your client
- Update SCOPE environment variable
Example for custom user attributes:
SCOPE=openid profile email roles groups custom-attributesFor production deployments:
- Use HTTPS URLs in all configuration
- Configure SSL certificates in Keycloak
- Update redirect URIs to use HTTPS
- Set secure cookie options if needed
To support multiple Keycloak realms:
- Set up separate
.envfiles per realm - Use different client IDs for each realm
- Consider using realm-specific redirect URIs
- Use HTTPS for all URLs
- Set strong client secrets
- Configure appropriate token lifetimes
- Enable PKCE (Proof Key for Code Exchange)
- Review and minimize scope permissions
- Set up proper CORS policies
- Configure session timeout appropriately
- Enable audit logging in Keycloak
- Tokens are stored in Streamlit session state (server-side)
- JWT validation occurs on every API call
- Automatic token refresh is supported
- Logout clears all session data
| Feature | Keycloak | Microsoft Entra ID |
|---|---|---|
| Cost | Free (self-hosted) | Paid per user |
| Deployment | Self-managed | Cloud-managed |
| Customization | Highly customizable | Limited customization |
| Enterprise Features | Full-featured | Advanced enterprise features |
| Setup Complexity | Medium | Low |
| Token Standards | Full OAuth2/OIDC | Full OAuth2/OIDC |
- Check the main README.md for overall architecture
- See QUICKSTART.md for quick setup instructions
- Review products-agent README for backend configuration
If you encounter issues:
- Check the application logs (
LOG_LEVEL=DEBUG) - Verify Keycloak server logs
- Review network traffic in browser dev tools
- Consult Keycloak community forums
- Check the OAuth2 specification for protocol details