Hello community,
I’m setting up Keycloak SSO across two realms with a React application, and I’m consistently getting an Invalid Request error with clientId=null during the Identity Brokering flow. Any insights would be greatly appreciated.
1. Architecture & Goal:
My setup involves a central identity realm (corporate-id ) and an application realm (comlog-dev ) for a React app (vendas-app ). The goal is seamless SSO from an onboarding-frontend (authenticated via NestJS to corporate-id ) to vendas-app-frontend .
- Keycloak Setup (Docker, PostgreSQL,
KC_LOG_LEVEL=DEBUG):corporate-idRealm (IdP): Containsusuario.testeinVendas_Gerentegroup. Usesgroups-scopeclient scope. Has a confidential clientbroker-comlog-devfor brokering andonboarding-api-backendfor direct access grants.comlog-devRealm (SP/Broker): Containsvendas-managerrole. Has a public clientvendas-app(Redirect URI:http://localhost:3002/*, Web Origin:http://localhost:3002, CORS Enabled). Has an Identity Providerlogin-centralizadopointing tocorporate-id(Client ID:broker-comlog-dev, matching secret). Critical IDP settings:Use PKCE: Off,Store tokens: On,Stored tokens readable: On,Disable nonce: Off,Default Scopes: groups-scope openid profile email. A mapper translatesgroups:Vendas_Gerente(fromcorporate-id) tovendas-app/vendas-manager(incomlog-dev).
- React Frontends:
onboarding-frontend(http://localhost:3000): Logs intocorporate-idvia NestJS. Has a button to redirect tovendas-app.vendas-app-frontend(http://localhost:3002): Useskeycloak-jswithonLoad: 'check-sso'.
2. Expected SSO Flow:
- User logs into
onboarding-frontendwithusuario.teste(session established incorporate-id). - User clicks “Go to Vendas App” on
onboarding-frontend. - Browser redirects to
comlog-devbroker endpoint (e.g.,http://localhost:8080/realms/comlog-dev/broker/login-centralizado/endpoint?client_id=vendas-app&redirect_uri=http%3A%2F%2Flocalhost%3A3002%2F&response_type=code&scope=openid%20profile%20email%20groups-scope&state=<random_state>). - Keycloak (as broker) should silently authenticate via
corporate-id, map roles, and redirect tovendas-app-frontend. vendas-app-frontendshould automatically log in the user.
3. The Problem:
After clicking “Go to Vendas App”, I’m redirected to a Keycloak error page saying “We are sorry… Invalid Request”.
onboarding-frontend works fine.
4. Keycloak Debug Logs:
The most relevant log line indicates:
DEBUG [org.keycloak.services.resources.IdentityBrokerService] Invalid request. Authorization code, clientId or tabId was null. Code=<auth_code>, clientId=null, tabID=null
WARN [org.keycloak.events] type="IDENTITY_PROVIDER_LOGIN_ERROR", ..., error="invalidRequestMessage"
ERROR [org.keycloak.services.resources.IdentityBrokerService] invalidRequestMessage
What I’ve verified:
- The URL generated by
onboarding-frontend(via debugger) is correct and containsresponse_type=codeandstate. Example:http://localhost:8080/realms/comlog-dev/broker/login-centralizado/endpoint?client_id=vendas-app&redirect_uri=http%3A%2F%2Flocalhost%3A3002%2F&response_type=code&scope=openid%20profile%20email%20groups-scope&state=krtsolhfdbepsvo64jc6i - Browser cache and site data for
localhosthave been cleared. - All client and IDP configurations (
Use PKCE: Off,Store tokens: On,Stored tokens readable: On,Disable nonce: Off, correct Redirect URIs/Web Origins) have been double-checked against the provided guide. - Keycloak is restarted via
docker compose downandup -dafter changes.
5. My Question:
Why are clientId and tabId null in the IdentityBrokerService when the user redirects back from the corporate-id to the comlog-dev broker, despite the initial request URL being correct? Is there a session/cookie configuration or header issue causing the context to be lost?
Any help or suggestions for debugging this would be greatly appreciated!
Thanks!