How cross-domain SSO works with OIDC, SAML 2.0, and Forward Auth
Learn how cross-domain SSO uses IdP first-party cookies and redirects, verified on Keycloak 26.8.2, and how to choose between OIDC, SAML 2.0, and Forward Auth.
Browsers do not share cookies across different domains. Cross-domain SSO works without shared app cookies by keeping a first-party session cookie on a central Identity Provider (IdP) domain, using top-level browser redirects, and exchanging signed tokens or assertions.
The HTTP sequence and cookie headers below were verified locally against
quay.io/keycloak/keycloak:26.8.2(start-dev --import-realm, self-signed TLS) using Pythonrequests.Session.
Why cross-domain SSO cannot rely on shared app cookies
When you run multiple web services—say app1.example.test and app2.example.test—you want users who sign in to the first app to enter the second without typing their password again. A common first assumption is that App 2 simply reads the session cookie created by App 1.
Browsers block that by design. The Same-Origin Policy (SOP) and cookie domain scoping prevent one origin from reading or attaching cookies owned by another domain—part of the same browser boundary model I covered in HTTP idempotency and CORS preflight mechanics.
If every app lives under the same parent domain, you can set a wildcard cookie like Domain=.example.com. That shortcut breaks down in two ways:
- It fails across separate root domains: Completely distinct domains (
service-a.comandservice-b.io) cannot share cookies under browser rules. - It widens the blast radius: Every subdomain under
.example.comreceives the same session cookie. One XSS bug on a legacy marketing page or static subdomain exposes sessions for every sibling app.
Production cross-domain SSO avoids sharing cookies between applications. Instead, it centralizes the login session on a single IdP (Identity Provider) domain.
How IdP first-party sessions and redirects work
Cross-domain SSO relies on top-level navigation redirects to the IdP. When the browser navigates directly to the IdP’s origin (auth.example.test), it automatically attaches the IdP’s own first-party session cookie.
sequenceDiagram
participant B as Browser
participant A1 as App 1
participant A2 as App 2
participant IdP as IdP (Keycloak)
B->>A1: 1. Protected page
A1-->>B: 2. 302 IdP /auth
B->>IdP: 3. Submit login
IdP-->>B: 4. IdP cookie + 302 code
B->>A1: 5. /callback?code
A1->>IdP: 6. Token + local session
B->>A2: 7. Open App 2
A2-->>B: 8. 302 IdP /auth
B->>IdP: 9. /auth (IdP cookie)
IdP-->>B: 10. 302 code (no login)
B->>A2: 11. /callback?code
A2->>IdP: 12. Token + local session
The flow splits into two steps:
- Signing in to
App 1: An unauthenticated request hitsApp 1, which redirects the browser to the IdP’s authorization endpoint (/auth). After the user submits credentials, the IdP sets a session cookie scoped strictly to the IdP domain and redirects back toApp 1’s callback URL with a one-time authorizationcode.App 1exchanges thatcodeover a back-channel request, validates the returned token, and creates its own local session. - Entering
App 2without re-authenticating: When the user visitsApp 2on a different domain,App 2sees no local session and redirects the browser to the same IdP/authendpoint. Because this is a top-level redirect to the IdP’s origin, the browser sends the IdP’s first-party session cookie automatically. The IdP recognizes the active session, skips the login form, and immediately issues a freshcodeforApp 2.
App 1 and App 2 never see each other’s cookies. Each app manages its own local session, while the IdP’s first-party cookie bridges authentication across domains.
Verifying the SSO sequence on Keycloak 26.8.2
To inspect the exact HTTP status codes and Set-Cookie headers behind this flow, I ran an isolated Keycloak 26.8.2 container locally.
- Runtime:
quay.io/keycloak/keycloak:26.8.2(start-dev --import-realm, self-signed TLS athttps://127.0.0.1:18443), realmdemo-realm - Clients:
app1-client(http://app1.example.test/callback) andapp2-client(http://app2.example.test/callback), both enforcing PKCES256 - Client simulator: Python
requests.Sessiontracking cookies and redirects withREQUESTS_CA_BUNDLEpointing to the local CA
Here is the masked output recorded across the app1 login, the app2 silent SSO redirect, and an unauthenticated control request:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
"step2_app1_login_post": {
"status": 302,
"location_origin_path": "http://app1.example.test/callback",
"location_query_keys": ["code", "iss", "session_state", "state"],
"code_issued": true,
"set_cookies": [
"KC_RESTART=; Version=1; Path=/realms/demo-realm/; Max-Age=0",
"KEYCLOAK_IDENTITY=eyJhbGci...<masked:580chars>; Version=1; Path=/realms/demo-realm/; Secure; HttpOnly; SameSite=None",
"KEYCLOAK_SESSION=5TsKbKId...<masked:64chars>; Version=1; Path=/realms/demo-realm/; Max-Age=36000; Secure; SameSite=None"
]
},
"cookie_jar_after_app1_login": [
{ "domain": "127.0.0.1", "path": "/realms/demo-realm/", "name": "AUTH_SESSION_ID", "secure": true, "httponly": true, "samesite": "None" },
{ "domain": "127.0.0.1", "path": "/realms/demo-realm/", "name": "KEYCLOAK_IDENTITY", "secure": true, "httponly": true, "samesite": "None" },
{ "domain": "127.0.0.1", "path": "/realms/demo-realm/", "name": "KEYCLOAK_SESSION", "secure": true, "httponly": false, "samesite": "None" }
],
"step3_app2_silent_sso": {
"status": 302,
"password_prompted": false,
"location_origin_path": "http://app2.example.test/callback",
"location_query_keys": ["code", "iss", "session_state", "state"],
"code_issued": true,
"same_session_state": true
},
"step4_app2_without_idp_cookie": {
"status": 200,
"login_form_found": true
}
}
Three observations stand out:
- Every cookie belongs strictly to the IdP origin: After signing in to
app1-client, the cookie jar holdsKEYCLOAK_IDENTITY(HttpOnly, the actual SSO session proof) andKEYCLOAK_SESSION(Max-Age=36000, non-HttpOnly, used by OIDC Session Management). Both live on127.0.0.1underPath=/realms/demo-realm/. Neitherapp1.example.testnorapp2.example.testreceived any cookie. - Passwordless
codeissuance with matchingsession_state: Sending the same cookie jar toapp2-client’s/authendpoint returned302 Foundstraight tohttp://app2.example.test/callbackwith a newcodeand no login prompt. Thesession_statequery parameter matchedapp1-client’s login response, confirming both apps share one IdP session. - Control test without IdP cookies: Calling
app2-client’s/authendpoint from a fresh, empty cookie jar returned200 OKwith the HTML login form.
Test scope and limits: Keycloak 26.8.2 marks its session cookies
Secure; SameSite=Noneby default. Modern browsers treathttp://localhostandhttp://127.0.0.1as secure contexts, but strict RFC 6265 clients like Pythonrequestsrefuse to sendSecurecookies over plain HTTP. Using a local TLS certificate reproduces real HTTPS behavior. This test covers top-level redirects in a single container; browser third-party cookie blocking on hidden iframe session checks is outside this run’s scope.
Comparing OIDC, SAML 2.0, and Forward Auth
Once the IdP confirms the user’s session, it has to hand that identity to the application. Most teams choose among OIDC, SAML 2.0, and Reverse Proxy Forward Auth.
flowchart TD
Req["Incoming Request"] --> Branch{"Trust Boundary"}
Branch -->|"Signed JWT"| OIDC["OIDC (App verifies JWKS)"]
Branch -->|"Signed XML"| SAML["SAML 2.0 (XML signature)"]
Branch -->|"Subrequest"| FA["Forward Auth (Proxy header)"]
The main difference between them is the trust boundary:
- OIDC (OpenID Connect 1.0) layers authentication on top of OAuth 2.0. The app receives a signed JSON Web Token (
id_token) and verifies its signature directly against the IdP’s public keys (jwks_uri). As in my Gnuboard5 OIDC SSO plugin implementation, integrating OIDC inside the app gives you cryptographic verification and clean integration with API access tokens. I cover the boundary betweenid_tokenandaccess_tokenin OAuth 2.0 vs. OIDC token roles and misuse. - SAML 2.0 is an OASIS XML-based federation protocol. The IdP delivers a signed (and optionally encrypted) XML assertion to the Service Provider (SP), typically through a browser HTTP-POST binding. It remains standard in B2B enterprise SaaS and legacy corporate directories (Microsoft Entra ID, Okta, AD FS), but XML canonicalization is complex and SAML fits native mobile apps or REST API delegation poorly.
- Forward Auth delegates every incoming request at the reverse proxy layer—using Nginx
ngx_http_auth_request_module, Traefik ForwardAuth, or Envoyext_authz. If the auth service approves the request, the proxy forwards it upstream with identity headers likeRemote-User,Remote-Groups, andRemote-Email. As shown in my Authelia and Nginx Proxy Manager Forward Auth guide, it protects legacy tools or self-hosted dashboards that lack built-in SSO without touching their source code.
| Criterion | OIDC (OpenID Connect 1.0) | SAML 2.0 | Reverse Proxy Forward Auth |
|---|---|---|---|
| Proof format | JSON / JWT (id_token, access_token) | Signed/encrypted XML Assertion | Proxy-injected HTTP headers (Remote-User, etc.) |
| Who verifies | App verifies directly via IdP JWKS | App (SP) verifies via IdP X.509 cert | Reverse proxy delegates each request to auth service |
| App changes | Required (OIDC client library/config) | Required (SAML SP library/metadata) | None (or header-login config only) |
| API scope delegation | Native via OAuth 2.0 Access Tokens | No built-in API authorization model | Poor fit; tied to browser proxy sessions |
| SPA & mobile fit | Strong (PKCE + BFF patterns) | Weak (browser XML POST binding) | Weak (requires proxied browser traffic) |
| Best fit | New web/mobile apps, APIs, microservices | B2B enterprise SaaS, legacy portals | Internal dashboards, self-hosted tools without SSO |
Choosing the right mechanism and when not to use each
For new web applications, mobile clients, or API backends you own, OIDC is the cleanest default. If enterprise B2B customers require connecting their corporate Okta or Entra ID via SAML, you will need SAML 2.0. If you must put authentication in front of an internal admin UI or third-party container that doesn’t support OIDC, Forward Auth gets you there fastest.
Filtering out where each option breaks down prevents costly mistakes:
- When not to use Forward Auth: Never use it if callers can bypass the reverse proxy and reach the upstream app directly over an internal network or exposed port. Because the upstream app trusts plain HTTP headers instead of cryptographic signatures, anyone who bypasses the proxy can forge
Remote-User: admin. Forward Auth also adds a proxy-to-auth-service check on uncached requests, making it a poor fit for high-throughput API gateways or fine-grained OAuth 2.0 API delegation. - When not to use SAML 2.0: Skip SAML on greenfield services unless an enterprise customer or legacy IdP mandates it. XML parsing and canonicalization bugs have a long history of XML Signature Wrapping vulnerabilities, and manual X.509 certificate rotation carries more operational overhead than OIDC’s automatic JWKS key discovery.
- When not to use OIDC: Don’t force a custom OIDC adapter into a closed-source internal tool that only supports header auth—Forward Auth is simpler and easier to maintain there. Likewise, if a B2B contract requires SAML 2.0 federation, OIDC alone won’t satisfy the customer.
Production pitfalls and pre-deployment checklist
Watch out for these production edge cases
- Proxy bypass and header stripping in Forward Auth: Configure your reverse proxy to strip or overwrite any client-supplied
Remote-UserorRemote-Groupsheaders before forwarding, and keep upstream containers on an internal network with no public host ports.- Third-party cookie blocking vs. top-level redirects: Cross-domain SSO login uses top-level redirects (first-party context), so third-party cookie blocking does not break standard sign-in. However, hidden
<iframe>session polling (KEYCLOAK_SESSION) and OIDC Front-Channel Logout can fail in browsers that block third-party cookies.- Unified login with fragmented logout: Clearing
App 1’s local session leavesKEYCLOAK_IDENTITYactive on the IdP. VisitingApp 2—or clicking login onApp 1again—silently signs the user right back in unless you also implement RP-Initiated Logout and Back-Channel Logout.
Before rolling out cross-domain SSO, run through these four checks:
- HTTPS everywhere: Both the IdP and every client app serve over HTTPS so
SecureIdP cookies work consistently across browsers. - Matched trust boundary: Apps that handle sensitive data or cross-network traffic verify signed tokens (OIDC/SAML) rather than trusting proxy headers.
- No proxy bypass (Forward Auth): Upstream ports are isolated from direct access, and incoming identity headers from clients are stripped at the edge.
- Agreed logout behavior: You’ve decided up front whether local app logout is enough or whether multi-app Single Logout (SLO) is required.