Skip to content

Keycloak Setup

This guide covers configuring Keycloak for use with ReArch when using AUTH_MODE=KEYCLOAK_FIREWALL. ReArch requires three Keycloak clients within a single realm.

Prerequisites:

  • A running Keycloak instance (e.g., https://auth.example.com)
  • Admin access to the Keycloak admin console
  • Your ReArch frontend URL (e.g., https://app.example.com)

Throughout this guide, replace example.com with your actual domain.

  1. Log in to the Keycloak admin console at https://auth.example.com/admin.
  2. Click the realm dropdown in the top-left corner and click Create realm.
  3. Set the Realm name to rearch and click Create.

After creating the realm, go to Realm settings and configure:

SettingValue
Display nameReArch
User registrationOff
Login with emailOn
Duplicate emailsOff
Reset passwordOn
Edit usernameOff
Brute force detectionOn

Go to Realm roles in the left sidebar and create two roles:

RoleDescription
userStandard user role. Assigned by default to all new users.
adminAdministrator role. Grants access to user management, settings, jobs, and analytics.

After creating both roles, go to Realm settings > Default roles and add user as a default role so every new user receives it automatically.

ReArch expects realm roles in the access token under a realm_roles claim.

  1. Go to Client scopes and click Create client scope.
  2. Set Name to roles, Protocol to OpenID Connect, and enable Include in token scope.
  3. Save the scope, then go to the Mappers tab.
  4. Click Add mapper > By configuration > User Realm Role and configure:
SettingValue
Namerealm roles
MultivaluedOn
Token Claim Namerealm_roles
Claim JSON TypeString
Add to ID tokenOn
Add to access tokenOn
Add to userinfoOn

ReArch uses three Keycloak clients:

A public client used by the React frontend for Authorization Code flow with PKCE.

General settings: Client ID rearch-app, Name ReArch Frontend.

Capability config:

SettingValue
Client authenticationOff
Standard flowChecked
All other flowsUnchecked

Login settings:

SettingValue
Root URLhttps://app.example.com
Valid redirect URIshttps://app.example.com/* and http://localhost:4200/*
Web originshttps://app.example.com and http://localhost:4200

After saving, go to the Advanced tab and set Proof Key for Code Exchange Code Challenge Method to S256.

Then go to Client scopes, add roles as a default scope, and add a User Realm Role mapper (same configuration as Step 3) on the rearch-app-dedicated scope.

A confidential client for service-to-service token validation.

General settings: Client ID rearch-backend, Name ReArch Backend.

Capability config:

SettingValue
Client authenticationOn
Service accounts rolesChecked
All other flowsUnchecked

After saving, go to the Credentials tab and copy the Client secret. Set it as the KEYCLOAK_BACKEND_CLIENT_SECRET environment variable.

Add the roles scope and realm roles mapper as with rearch-app.

A confidential client for Traefik forward-auth, protecting conversation containers.

General settings: Client ID rearch-proxy, Name ReArch OAuth2 Proxy.

Capability config:

SettingValue
Client authenticationOn
Standard flowChecked
All other flowsUnchecked

Login settings:

SettingValue
Root URLhttps://app.example.com
Valid redirect URIshttps://app.example.com/oauth2/callback and https://*.example.com/oauth2/callback
Web originshttps://app.example.com and https://*.example.com

Copy the Client secret and set it as OAUTH2_PROXY_CLIENT_SECRET.

Add the roles scope and realm roles mapper as with the other clients.

  1. Go to Users and click Add user.
  2. Set the email, first name, and last name. Toggle Email verified to On.
  3. Go to the Credentials tab and set a password.
  4. Go to the Role mapping tab, click Assign role, and assign both user and admin.

The backend syncs roles from the Keycloak token on every authenticated request. Role changes in Keycloak take effect on the user’s next API request.

VariableValue
AUTH_MODEKEYCLOAK_FIREWALL
KEYCLOAK_REALM_URLhttps://auth.example.com/realms/rearch
KEYCLOAK_CLIENT_IDrearch-backend
KEYCLOAK_CLIENT_SECRETClient secret from rearch-backend
VariableValue
KEYCLOAK_URLhttps://auth.example.com
KEYCLOAK_REALMrearch
KEYCLOAK_CLIENT_IDrearch-app
VariableValue
OAUTH2_PROXY_CLIENT_IDrearch-proxy
OAUTH2_PROXY_CLIENT_SECRETClient secret from rearch-proxy
OAUTH2_PROXY_OIDC_ISSUER_URLhttps://auth.example.com/realms/rearch
OAUTH2_PROXY_REDIRECT_URLhttps://app.example.com/oauth2/callback
OAUTH2_PROXY_COOKIE_SECRETRandom 32-byte base64 string (generate with openssl rand -base64 32)
OAUTH2_PROXY_COOKIE_DOMAINS.example.com
OAUTH2_PROXY_WHITELIST_DOMAINS.example.com
ClientTypeAuth FlowPurpose
rearch-appPublicAuthorization Code + PKCEFrontend SPA login
rearch-backendConfidentialService AccountToken validation
rearch-proxyConfidentialAuthorization CodeTraefik forward-auth for containers

ReArch recognizes two realm roles:

RoleAccess
userStandard access. Can create conversations, send messages, push changes.
adminFull access. Adds user management, LLM provider config, MCP servers, jobs dashboard, usage analytics, and application settings.

Roles are extracted from the Keycloak access token and synced to the application database on every authenticated request. Keycloak is the source of truth for role assignments.

Instead of configuring everything manually, use the provided realm export. When Keycloak starts with KC_IMPORT pointing to keycloak/realm-export.json, it auto-creates the realm, roles, scopes, and all three clients.

After the auto-import, you still need to:

  1. Replace placeholder client secrets — The export contains CHANGE_ME_BACKEND_SECRET and CHANGE_ME_PROXY_SECRET.
  2. Update redirect URIs and web origins to match your domain.
  3. Create your first user and assign the admin role.
  4. Set the environment variables with the real client secrets.

”Invalid redirect_uri” error during login

Section titled “”Invalid redirect_uri” error during login”

The redirect URI does not match the Keycloak client configuration. Verify that Valid redirect URIs for rearch-app includes https://app.example.com/* (with the trailing wildcard).

The frontend domain is not listed in the Web origins of rearch-app. Add https://app.example.com. For local development, also add http://localhost:4200.

The KEYCLOAK_CLIENT_ID does not match any client in the rearch realm. Check the spelling.

User has no admin access after role assignment

Section titled “User has no admin access after role assignment”

Roles are synced from the Keycloak token. The user needs to log out and back in (or make a new API request) for the updated token to be issued.

Verify that KEYCLOAK_REALM_URL is correct and that the backend can reach Keycloak’s JWKS endpoint at https://auth.example.com/realms/rearch/protocol/openid-connect/certs.

”Unauthorized” when accessing the frontend

Section titled “”Unauthorized” when accessing the frontend”

Remove forward-auth middleware from the frontend Traefik router. The SPA handles authentication via keycloak-js and must be publicly accessible so it can load before initiating the login flow. Forward-auth should only protect conversation container routes.

  • Always use HTTPS for all endpoints in production.
  • Rotate client secrets periodically and update the corresponding environment variables.
  • Use PKCE (S256) for the frontend client to prevent authorization code interception.
  • Keep rearch-backend restricted to service account flows only (no browser-based flows).
  • Enable brute force protection in the realm settings.
  • Set appropriate token lifespans based on your security requirements.