Co-authored-by: Sharang Parnerkar <parnerkarsharang@gmail.com> Reviewed-on: #4
3.3 KiB
3.3 KiB
Keycloak Authentication
Compliance Scanner supports Keycloak for SSO authentication. When configured, all dashboard access requires signing in through Keycloak, and all API endpoints are protected.
How It Works
Dashboard (OAuth2/OIDC)
The dashboard implements a standard OAuth2 Authorization Code flow with PKCE:
- User visits the dashboard
- If not authenticated, a login page shows with a "Sign in with Keycloak" button
- User is redirected to Keycloak's login page
- After authentication, Keycloak redirects back with an authorization code
- The dashboard exchanges the code for tokens and creates a session
- All subsequent
/api/server function calls require a valid session
Agent API (JWT)
The agent API validates JWT Bearer tokens from Keycloak:
- Dashboard (or other clients) include the access token in requests:
Authorization: Bearer <token> - The agent fetches Keycloak's JWKS (JSON Web Key Set) to validate the token signature
- Token expiry and claims are verified
- The health endpoint (
/api/v1/health) is always public
If KEYCLOAK_URL and KEYCLOAK_REALM are not set on the agent, JWT validation is disabled and all endpoints are open.
Keycloak Setup
1. Create a Realm
In the Keycloak admin console:
- Create a new realm (e.g.
compliance) - Note the realm name — you'll need it for
KEYCLOAK_REALM
2. Create a Client
- Go to Clients > Create client
- Set:
- Client ID:
compliance-dashboard - Client type: OpenID Connect
- Client authentication: Off (public client)
- Client ID:
- Under Settings:
- Valid redirect URIs:
http://localhost:8080/auth/callback(adjust for your domain) - Valid post logout redirect URIs:
http://localhost:8080 - Web origins:
http://localhost:8080
- Valid redirect URIs:
3. Create Users
- Go to Users > Create user
- Set username, email, first name, last name
- Under Credentials, set a password
Environment Variables
# Keycloak server URL (no trailing slash)
KEYCLOAK_URL=http://localhost:8080
# Realm name
KEYCLOAK_REALM=compliance
# Client ID (must match the client created above)
KEYCLOAK_CLIENT_ID=compliance-dashboard
# OAuth callback URL (must match valid redirect URI in Keycloak)
REDIRECT_URI=http://localhost:8080/auth/callback
# Application root URL (used for post-logout redirect)
APP_URL=http://localhost:8080
Dashboard Features
When authenticated, the dashboard shows:
- User avatar in the sidebar (from Keycloak profile picture, or initials)
- User name from Keycloak profile
- Logout link that clears the session and redirects through Keycloak's logout flow
Session Configuration
Sessions use signed cookies with these defaults:
- Expiry: 24 hours of inactivity
- SameSite: Lax (required for Keycloak redirect flow)
- Secure: Disabled by default (enable behind HTTPS)
- Storage: In-memory (resets on server restart)
::: tip For production, consider persisting sessions to Redis or a database so they survive server restarts. :::
Running Without Keycloak
If no Keycloak variables are set:
- The dashboard serves without authentication (all pages accessible)
- The agent API accepts all requests without token validation
- A warning is logged:
Keycloak not configured - API endpoints are unprotected
This is suitable for local development and testing.