CS › Web-Network

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.

How cross-domain SSO works with 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 Python requests.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.com and service-b.io) cannot share cookies under browser rules.
  • It widens the blast radius: Every subdomain under .example.com receives 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:

  1. Signing in to App 1: An unauthenticated request hits App 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 to App 1’s callback URL with a one-time authorization code. App 1 exchanges that code over a back-channel request, validates the returned token, and creates its own local session.
  2. Entering App 2 without re-authenticating: When the user visits App 2 on a different domain, App 2 sees no local session and redirects the browser to the same IdP /auth endpoint. 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 fresh code for App 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 at https://127.0.0.1:18443), realm demo-realm
  • Clients: app1-client (http://app1.example.test/callback) and app2-client (http://app2.example.test/callback), both enforcing PKCE S256
  • Client simulator: Python requests.Session tracking cookies and redirects with REQUESTS_CA_BUNDLE pointing 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:

  1. Every cookie belongs strictly to the IdP origin: After signing in to app1-client, the cookie jar holds KEYCLOAK_IDENTITY (HttpOnly, the actual SSO session proof) and KEYCLOAK_SESSION (Max-Age=36000, non-HttpOnly, used by OIDC Session Management). Both live on 127.0.0.1 under Path=/realms/demo-realm/. Neither app1.example.test nor app2.example.test received any cookie.
  2. Passwordless code issuance with matching session_state: Sending the same cookie jar to app2-client’s /auth endpoint returned 302 Found straight to http://app2.example.test/callback with a new code and no login prompt. The session_state query parameter matched app1-client’s login response, confirming both apps share one IdP session.
  3. Control test without IdP cookies: Calling app2-client’s /auth endpoint from a fresh, empty cookie jar returned 200 OK with the HTML login form.

Test scope and limits: Keycloak 26.8.2 marks its session cookies Secure; SameSite=None by default. Modern browsers treat http://localhost and http://127.0.0.1 as secure contexts, but strict RFC 6265 clients like Python requests refuse to send Secure cookies 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 between id_token and access_token in 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 Envoy ext_authz. If the auth service approves the request, the proxy forwards it upstream with identity headers like Remote-User, Remote-Groups, and Remote-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.
CriterionOIDC (OpenID Connect 1.0)SAML 2.0Reverse Proxy Forward Auth
Proof formatJSON / JWT (id_token, access_token)Signed/encrypted XML AssertionProxy-injected HTTP headers (Remote-User, etc.)
Who verifiesApp verifies directly via IdP JWKSApp (SP) verifies via IdP X.509 certReverse proxy delegates each request to auth service
App changesRequired (OIDC client library/config)Required (SAML SP library/metadata)None (or header-login config only)
API scope delegationNative via OAuth 2.0 Access TokensNo built-in API authorization modelPoor fit; tied to browser proxy sessions
SPA & mobile fitStrong (PKCE + BFF patterns)Weak (browser XML POST binding)Weak (requires proxied browser traffic)
Best fitNew web/mobile apps, APIs, microservicesB2B enterprise SaaS, legacy portalsInternal 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

  1. Proxy bypass and header stripping in Forward Auth: Configure your reverse proxy to strip or overwrite any client-supplied Remote-User or Remote-Groups headers before forwarding, and keep upstream containers on an internal network with no public host ports.
  2. 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.
  3. Unified login with fragmented logout: Clearing App 1’s local session leaves KEYCLOAK_IDENTITY active on the IdP. Visiting App 2—or clicking login on App 1 again—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 Secure IdP 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.

References

OpenID Connect Core 1.0 incorporating errata set 2
The ngx_http_auth_request_module module (1.5.4+) implements client authorization based on the result of a subrequest. If the subrequest returns a 2xx response code, the access is allowed. If it returns 401 or 403, the access is denied with the corresponding error code. Any other response code returned by the subrequest is considered an error.
The page you’re looking for does not exist.
This post is licensed under CC BY 4.0 by the author.