Keycloak Integration Guide for PKI Manager
Source:
KEYCLOAK.md
This guide provides step-by-step instructions for configuring Keycloak as the OIDC provider for PKI Manager.
Table of Contents
Section titled “Table of Contents”- Overview
- Prerequisites
- Keycloak Configuration
- PKI Manager Configuration
- Testing the Integration
- Troubleshooting
- E2E Testing Configuration
Overview
Section titled “Overview”PKI Manager uses OpenID Connect (OIDC) for authentication. Keycloak serves as the identity provider, handling:
- User authentication via Authorization Code Flow with PKCE
- Token issuance (access tokens, refresh tokens, ID tokens)
- User session management
- Optional: Machine-to-machine (M2M) authentication via Client Credentials flow
Prerequisites
Section titled “Prerequisites”- Keycloak server (version 20+) accessible via HTTPS
- Admin access to Keycloak
- PKI Manager deployed and accessible
Keycloak Configuration
Section titled “Keycloak Configuration”Step 1: Access Keycloak Admin Console
Section titled “Step 1: Access Keycloak Admin Console”- Navigate to your Keycloak admin URL (e.g.,
https://your-keycloak-domain/admin) - Log in with administrator credentials

Step 2: Create or Select a Realm
Section titled “Step 2: Create or Select a Realm”Realms isolate users and applications. For PKI Manager, create a dedicated realm or use an existing one.
- Click the realm dropdown in the top-left sidebar
- Click Create realm or select an existing realm (e.g.,
pki-manager)


Step 3: Create the SPA Client (pki-web)
Section titled “Step 3: Create the SPA Client (pki-web)”The pki-web client is a public client for the frontend SPA application using PKCE.
3.1 Navigate to Clients
Section titled “3.1 Navigate to Clients”- In the sidebar, click Clients
- Click Create client

3.2 General Settings (Step 1)
Section titled “3.2 General Settings (Step 1)”Configure the basic client information:
| Field | Value |
|---|---|
| Client type | OpenID Connect |
| Client ID | pki-web |
| Name | PKI Manager Web |
| Description | Public SPA client for PKI Manager frontend with PKCE |

Click Next.
3.3 Capability Config (Step 2)
Section titled “3.3 Capability Config (Step 2)”Configure the client authentication and flows:
| Setting | Value | Reason |
|---|---|---|
| Client authentication | Off | Public client (SPA cannot securely store secrets) |
| Authorization | Off | Not needed for basic authentication |
| Standard flow | Enabled | Authorization Code Flow with PKCE |
| Direct access grants | Enabled | Optional: allows password grant for testing |
| Implicit flow | Disabled | Not recommended for SPAs |
| Service accounts roles | Disabled | Not applicable for public clients |

Click Next.
3.4 Login Settings (Step 3)
Section titled “3.4 Login Settings (Step 3)”Configure the redirect URIs and web origins:
| Field | Value | Description |
|---|---|---|
| Root URL | https://pki.example.com |
Your frontend URL |
| Home URL | https://pki.example.com |
Landing page after logout |
| Valid redirect URIs | https://pki.example.com/* |
Allowed redirect destinations |
| Valid post logout redirect URIs | https://pki.example.com/* |
Allowed post-logout destinations |
| Web origins | https://pki.example.com |
CORS allowed origins |

Click Save.
3.5 Client Created
Section titled “3.5 Client Created”The client is now created and enabled.

Step 4: Create a Test User
Section titled “Step 4: Create a Test User”Create a user to test the authentication flow.
4.1 Navigate to Users
Section titled “4.1 Navigate to Users”- In the sidebar, click Users
- Click Create new user

4.2 Fill User Details
Section titled “4.2 Fill User Details”| Field | Value |
|---|---|
| Username | testuser |
testuser@example.com |
|
| First name | Test |
| Last name | User |

Click Create.

4.3 Set Password
Section titled “4.3 Set Password”- Go to the Credentials tab
- Click Set password

- Enter password and confirmation
- Set Temporary to Off (user won’t be forced to change password)
- Click Save


Step 5: (Optional) Create M2M Client (pki-service)
Section titled “Step 5: (Optional) Create M2M Client (pki-service)”For machine-to-machine authentication (API access without user interaction):
-
Create a new client with:
- Client ID:
pki-service - Client authentication: On (confidential client)
- Service accounts roles: Enabled
- All other flows: Disabled
- Client ID:
-
After creation, go to Credentials tab to get the client secret
-
Add to backend
OIDC_AUDIENCE:pki-web,pki-service
PKI Manager Configuration
Section titled “PKI Manager Configuration”Backend Configuration
Section titled “Backend Configuration”Add these environment variables to your backend .env file:
# OIDC AuthenticationOIDC_ISSUER=https://your-keycloak-domain/realms/pki-managerOIDC_AUDIENCE=pki-web,pki-serviceOIDC_ROLES_CLAIM=realm_access.roles| Variable | Description |
|---|---|
OIDC_ISSUER |
Keycloak realm URL (must match iss claim in tokens) |
OIDC_AUDIENCE |
Expected aud claim(s), comma-separated |
OIDC_ROLES_CLAIM |
JSON path to roles in the JWT token |
Frontend Configuration
Section titled “Frontend Configuration”Option A: Build-time Configuration
Section titled “Option A: Build-time Configuration”Add to frontend .env before building:
VITE_OIDC_AUTHORITY=https://your-keycloak-domain/realms/pki-managerVITE_OIDC_CLIENT_ID=pki-webVITE_OIDC_SCOPE=openid profile emailOption B: Runtime Configuration (Docker)
Section titled “Option B: Runtime Configuration (Docker)”Mount a config.json file:
{ "apiUrl": "https://api.pki.example.com/trpc", "oidc": { "authority": "https://your-keycloak-domain/realms/pki-manager", "clientId": "pki-web", "scope": "openid profile email" }}Add volume mount in docker-compose.yml:
services: frontend: volumes: - ./config.json:/usr/share/nginx/html/config.json:roDocker Configuration
Section titled “Docker Configuration”Example .env for Docker deployment:
# Backend OIDCOIDC_ISSUER=https://your-keycloak-domain/realms/pki-managerOIDC_AUDIENCE=pki-web,pki-serviceOIDC_ROLES_CLAIM=realm_access.roles
# Frontend (if not using runtime config.json)VITE_OIDC_AUTHORITY=https://your-keycloak-domain/realms/pki-managerVITE_OIDC_CLIENT_ID=pki-webTesting the Integration
Section titled “Testing the Integration”-
Access PKI Manager: Navigate to
https://pki.example.com -
Login Redirect: You should be redirected to Keycloak login page
-
Authenticate: Enter test user credentials
-
Callback: After successful login, you’re redirected back to PKI Manager
-
Verify Token: Check browser DevTools → Network → look for
Authorization: Bearerheader in API calls
Troubleshooting
Section titled “Troubleshooting”“Invalid redirect URI” Error
Section titled ““Invalid redirect URI” Error”Cause: The redirect URI doesn’t match Keycloak’s allowed list.
Solution:
- Verify
Valid redirect URIsin Keycloak client settings - Ensure the frontend URL exactly matches (including protocol and trailing slashes)
“Invalid token issuer” Error
Section titled ““Invalid token issuer” Error”Cause: OIDC_ISSUER doesn’t match the iss claim in the token.
Solution:
- Check the exact Keycloak realm URL
- Ensure no trailing slash mismatch
- Format:
https://keycloak-domain/realms/realm-name
“CORS blocked” Error
Section titled ““CORS blocked” Error”Cause: Keycloak or backend not allowing the frontend origin.
Solution:
- Add frontend URL to Keycloak client’s
Web origins - Verify backend
FRONTEND_URLenvironment variable
“Token expired” or Silent Renewal Fails
Section titled ““Token expired” or Silent Renewal Fails”Cause: Refresh token expired or silent renewal iframe blocked.
Solution:
- Check Keycloak realm’s token lifespan settings
- Ensure
silent-renew.htmlis accessible - Verify third-party cookies aren’t blocked
User Gets “Access Denied” After Login
Section titled “User Gets “Access Denied” After Login”Cause: Token audience doesn’t include the expected client ID.
Solution:
- Verify
OIDC_AUDIENCEincludespki-web - Check Keycloak client’s audience mapper if customized
Quick Reference
Section titled “Quick Reference”Keycloak URLs
Section titled “Keycloak URLs”| Endpoint | URL Pattern |
|---|---|
| Admin Console | https://keycloak-domain/admin |
| Realm Settings | https://keycloak-domain/admin/master/console/#/realm-name |
| OIDC Discovery | https://keycloak-domain/realms/realm-name/.well-known/openid-configuration |
| Token Endpoint | https://keycloak-domain/realms/realm-name/protocol/openid-connect/token |
| UserInfo Endpoint | https://keycloak-domain/realms/realm-name/protocol/openid-connect/userinfo |
PKI Manager Environment Variables
Section titled “PKI Manager Environment Variables”| Variable | Example | Required |
|---|---|---|
OIDC_ISSUER |
https://keycloak.example.com/realms/pki |
Yes (backend) |
OIDC_AUDIENCE |
pki-web,pki-service |
Yes (backend) |
OIDC_ROLES_CLAIM |
realm_access.roles |
No (default shown) |
VITE_OIDC_AUTHORITY |
https://keycloak.example.com/realms/pki |
Yes (frontend) |
VITE_OIDC_CLIENT_ID |
pki-web |
Yes (frontend) |
VITE_OIDC_SCOPE |
openid profile email |
No (default shown) |
E2E Testing Configuration
Section titled “E2E Testing Configuration”PKI Manager includes a pre-configured Keycloak realm for E2E testing with RBAC verification.
E2E Test Users
Section titled “E2E Test Users”The E2E environment (docker/docker-compose.e2e.yml) includes pre-configured test users:
| Username | Password | Roles | Use Case |
|---|---|---|---|
testadmin |
Test123! |
admin |
Admin operations: create/revoke/delete CAs and certificates |
testuser |
Test123! |
user |
Regular user: view and issue certificates only |
E2E Keycloak Realm
Section titled “E2E Keycloak Realm”The pki-e2e realm is automatically imported when starting the E2E Docker environment:
- Realm:
pki-e2e - Client:
pki-web(public client with PKCE) - Roles:
admin,user - Direct Access Grants: Enabled (for automated testing)
- Brute Force Protection: Disabled (to prevent test lockouts)
Docker OIDC Networking
Section titled “Docker OIDC Networking”The E2E environment handles OIDC token validation in Docker with special configuration:
# Keycloak (v26+) - use full URL for consistent issuerenvironment: - KC_HOSTNAME=http://localhost:58180
# Backend - separate discovery URL for Docker networkingenvironment: - OIDC_ISSUER=http://localhost:58180/realms/pki-e2e - OIDC_DISCOVERY_BASE_URL=http://keycloak:8080/realms/pki-e2eWhy this is needed:
- Browser accesses Keycloak at
localhost:58180and tokens haveiss: http://localhost:58180/... - Backend container cannot reach
localhost:58180(it refers to the container itself) OIDC_DISCOVERY_BASE_URLtells the backend to fetch OIDC config from Docker network (keycloak:8080)- The backend validates tokens against
OIDC_ISSUER(what’s in tokens) while fetching from the discovery URL
Running E2E Tests
Section titled “Running E2E Tests”# Start the E2E environmentdocker compose -f docker/docker-compose.e2e.yml up -d
# Wait for Keycloak to be ready (~60 seconds)docker compose -f docker/docker-compose.e2e.yml ps
# Run RBAC testspnpm playwright test tests/e2e-rbac.spec.ts --reporter=list
# Cleanupdocker compose -f docker/docker-compose.e2e.yml down -vE2E Keycloak Access
Section titled “E2E Keycloak Access”| Service | URL | Credentials |
|---|---|---|
| Keycloak Console | http://localhost:58180 | admin / admin |
| OIDC Discovery | http://localhost:58180/realms/pki-e2e/.well-known/openid-configuration | - |
Production Test Users
Section titled “Production Test Users”For production testing, create equivalent users in your production Keycloak:
-
Create
testadminuser:- Username:
testadmin - Password:
Test123!(or stronger) - Realm roles:
admin
- Username:
-
Create
testuseruser:- Username:
testuser - Password:
Test123!(or stronger) - Realm roles:
user(noadminrole)
- Username:
Then run production tests:
E2E_TARGET=production pnpm playwright test tests/e2e-rbac.spec.tsSee Also
Section titled “See Also”- OIDC.md - How OIDC authentication works in PKI Manager (public clients, PKCE, token validation)
- DEPLOYMENT.md - Docker deployment and E2E testing instructions
- Keycloak Documentation
- OIDC Specification