Keycloak and Matrix Synapse OIDC single sign-on

Connect a self-hosted Matrix Synapse homeserver to Keycloak using the oidc_providers block Synapse documents, with client secret authentication, attribute mapping and back-channel logout. This page covers the sequence, checks and failure points.

On this page

How Keycloak signs users into Synapse over OIDC

If you run or plan a self-hosted Matrix homeserver and want Keycloak to be the single sign-on source, you are working with a supported path. Synapse supports OpenID Connect providers through the oidc_providers setting, and its documentation includes a Keycloak example that uses an idp_id of keycloak, a confidential client authenticating with a client ID and secret, and the openid and profile scopes. In this model, a user clicks the Synapse login option, is redirected to Keycloak, authenticates there (including any MFA your realm enforces), and returns to Synapse with an identity token. Synapse then matches that identity to a Matrix user ID and either creates the account on first login or links it to an existing one, depending on how you configure mapping.

The integration does not replace everything Synapse does with identity. Synapse still owns Matrix user IDs, room membership, device sessions and server administration, so Synapse admin rights must be granted in Synapse, not inherited automatically from a Keycloak role unless you design a mapping for that. Local password accounts can remain enabled for break-glass access, and you should decide deliberately whether they stay on. Account deactivation in Keycloak also does not immediately remove a user's existing Synapse sessions unless back-channel logout is in place, which is covered separately below.

Because the design decisions (which realm and client to use, how claims map to Matrix IDs and display names, whether local logins stay enabled, and how admin access is granted) affect who can sign in and what they can do, DeployOpen scopes this integration in writing before work starts. Confirm with your own team which Synapse version and Keycloak release you run, and check both upstream documentation sets against your environment, since configuration keys and supported versions change between releases.

What you need before configuring Keycloak and Synapse

Four pieces must be in place before any OIDC settings are written: a Synapse release on a supported runtime, a Keycloak 26.x realm served over HTTPS, a stable server_name with a public base URL, and DNS and certificates for both hosts. Each card below is a check to complete and record, because a mismatch in any one of them shows up later as a failed login redirect or a federation problem.

Confirm the Synapse release line

The latest Synapse release in the supplied sources is v1.163.0, published October 14, 2026. Confirm the version your team will deploy against the Synapse GitHub releases page, and read the upgrade notes for every release between your current version and the target. Synapse 1.143.0 and later require PostgreSQL 14 or newer, and 1.142.0 and later require Python 3.10 or newer, so check both runtimes before you pick the release. Record the version, because the oidc_providers block is written against the configuration manual for that release.

Synapse releases

Check PostgreSQL and Python runtimes

Synapse documentation recommends PostgreSQL for production, with SQLite also supported. Synapse 1.143.0 and later require PostgreSQL 14 or newer, so confirm the database server version and that it is still within upstream support. Python 3.10 or newer is required from 1.142.0. Also confirm that a database backup and a tested restore exist before the first upgrade, because the upgrade notes warn that rolling back can be difficult after schema changes.

PostgreSQL version required
14+

Build a Keycloak 26.x realm over HTTPS

Create a dedicated realm for Matrix users and confirm the Keycloak server runs a 26.x release. Keycloak's production guide calls for TLS on all communication, so the realm's issuer URL must use HTTPS and resolve from the Synapse host. Decide which users may sign in and whether self-registration is allowed, since those choices affect every Matrix account Synapse creates from the token. Keep the Keycloak admin console on a separate hostname or context path from the public frontend URL.

Keycloak production guide

Fix server_name and the public base URL

The server_name setting fixes how users and rooms are identified, so an identifier such as @user:example.com is permanent once users exist. Choose it before any account is created, and do not change it later. Confirm the public base URL that clients and the OIDC redirect use, and make sure it matches the value registered in the Keycloak client. Delegation can serve user IDs from another host on port 443, so decide whether federation delegation is needed before publishing DNS records.

Synapse federation docs

Plan DNS and certificates for both hosts

Keycloak and Synapse usually run on separate hostnames, and both need valid certificates trusted by the other side. Synapse must reach the Keycloak discovery endpoint over HTTPS, so an internal certificate authority must be trusted by the Synapse host if one issues the Keycloak certificate. Check that DNS resolves each name from every network that needs it, including internal networks and any reverse proxy. Federation listens on port 8448 by default, so confirm that port is reachable or intentionally blocked.

Set Keycloak database and TLS baseline

Keycloak's supported production databases include PostgreSQL 14 to 18, and the default dev-file database is for development only. Confirm the Keycloak database is on a supported engine with backups, and that the Keycloak server runs with TLS on all communication behind its reverse proxy. Keycloak's high availability designs require a synchronously replicated database, so record the database architecture now. Decide who owns certificate renewal for Keycloak, since an expired certificate stops every Matrix sign-in at once.

Keycloak database guide

Configuring the Keycloak client and the oidc_providers block

This sequence creates a confidential Keycloak client for Synapse, wires its callback and back-channel logout endpoints, and points Synapse at Keycloak so that a test login succeeds. Each step ends with a check you can run before moving on.

  1. Confirm the Synapse public base URL and server name

    Before touching Keycloak, decide the public URL where Synapse's client API is reachable and the server_name that identifies your users, such as the domain in @user:example.com. The redirect URI and logout URL in Keycloak are built from this host, so a mismatch here causes every later step to fail. Record both values in your change record. The check is that the base URL resolves over HTTPS from a browser outside your network and returns a response from Synapse.

  2. Create a confidential client in the realm

    In the Keycloak admin console, select or create the realm that will hold your Matrix users, then create a client with client authentication turned on so it is confidential. Enable the standard authorization code flow and leave implicit and direct grant flows off unless you have a specific need for them. Keep the client ID simple, for example synapse, because it appears in configuration files. The check is that the client's Credentials tab shows a client secret and that the client appears in the realm's client list with the authorization code flow enabled.

  3. Set the Synapse callback and back-channel logout URLs

    In the client settings, add the Synapse OIDC callback URI as a valid redirect URI, built from your public base URL and the idp_id you will use in Synapse (keycloak). Then enter Synapse's back-channel logout URL in the client's back-channel logout settings and turn on session-required logout if your design needs it. Confirm the exact paths against the Synapse OpenID documentation for your Synapse version rather than copying them from older examples. The check is that the redirect URI and logout URL are saved and visible on the client's settings page.

  4. Store the client secret in a secrets store

    Copy the client secret from the Credentials tab and place it in the secrets store your team uses, such as a vault, a Kubernetes secret or your configuration management system's encrypted values. Do not paste it into a shared chat, a ticket or an unencrypted homeserver.yaml checked into version control. Decide who can rotate it and how Synapse will receive it at startup. The check is that the secret is retrievable by the account that runs Synapse and that the copy in your working notes has been removed.

  5. Add the oidc_providers entry to Synapse

    In the Synapse configuration, add an oidc_providers entry with idp_id set to keycloak, the Keycloak discovery issuer URL for your realm, the client ID, the secret supplied from your secrets store, and the openid and profile scopes. Map the Keycloak claims to Synapse's localpart and display name using the attribute mapping options in the Synapse OpenID documentation, and set backchannel_logout_enabled to true. Validate the YAML syntax before saving. The check is that the file parses without errors and that the entry lists the idp_id, scopes and back-channel setting you intended.

  6. Restart Synapse and check the discovery document

    Restart the Synapse process or container so it reads the new configuration. Then fetch the Keycloak OpenID discovery document for the realm from a host that can reach Keycloak, and confirm it returns valid JSON with the issuer and endpoint values you expect. Review the Synapse startup log for errors about the OIDC provider, which usually point to a wrong issuer URL, a certificate trust problem or a secret mismatch. The check is that Synapse starts cleanly and the discovery document loads from the Synapse host.

  7. Test a login and the back-channel logout

    Open an Element or other Matrix client pointed at your homeserver, choose the Keycloak sign-in option, and complete a login with a test user in the realm. Confirm that Synapse creates or matches the Matrix account with the expected localpart and display name. Then end the session from Keycloak's admin console and check that Synapse receives the back-channel logout and ends the matching session. The checks are a successful redirect back to Synapse, the expected Matrix user ID, and a logged-out session after the Keycloak logout.

How Synapse maps Keycloak claims to Matrix user IDs and profiles

Synapse builds each Matrix user ID from claims in the Keycloak token, and the mapping is set under the user_mapping_provider block inside each oidc_providers entry. The subject claim identifies the external account, the localpart template turns a claim such as preferred_username into the part of the ID before the colon, and the display name template reads a claim such as name to set the profile name. Keycloak sends sub as a standard OIDC claim, and the Keycloak client's mappers decide what else appears in the token. Before go-live, confirm the template keys and their defaults for your Synapse version in the Synapse configuration manual, then test the full mapping with a pilot user.

The server_name setting fixes the domain part of every user ID, so a user is written as @alice:example.com and the same value appears in room IDs and federation. Synapse uses it from the first start, so decide it before any account exists. If your public name differs from the host that serves Synapse, delegation must be configured to match, and that decision belongs with the federation design. Changing server_name later does not rename existing accounts cleanly, so treat it as a permanent identifier and confirm the consequences for your version in the upgrade notes before any change.

Choose a claim that is stable and unique for the life of the account. The subject identifier is the safest anchor because Synapse uses it to link the Keycloak identity to a Matrix account. A username can be renamed, and a freed username can later be reused by someone else, so an account tied to preferred_username alone can attach to the wrong person. Changing the subject claim, the subject template or the localpart rule after users exist can make Synapse treat returning users as new, which creates duplicate accounts and leaves their rooms and history behind. Fix the mapping in a written decision, and if a change is unavoidable, plan a reconciliation of accounts with a database backup taken first.

Keycloak claims and Synapse profile fields

This table compares each standard OIDC claim Keycloak can send with the Synapse user attribute it can feed, and notes the default behavior and the decision your team must make. Rows marked for confirmation depend on your Synapse version and the configuration manual, so check them before go-live.

Keycloak claim or scopeSynapse attribute it feedsDefault behavior and decision
sub (subject identifier)External ID for the identity linkKeycloak issues a stable subject per user. Changing it later creates a new Matrix account, so confirm the mapping and do not reassign subject values between users.
preferred_usernameLocalpart of the Matrix user ID (default mapping)Synapse builds the MXID from this claim by default. Decide whether usernames are stable, lowercase, and unique across the realm before users sign in. Confirm the template in your version's manual.
nameDisplay nameRead through the profile mapping. Decide whether users may edit display names in Synapse or only in Keycloak. Confirm the mapping key in your configuration manual.
emailEmail address on the Synapse accountRequires the email scope in Keycloak. Decide whether Synapse should trust this claim for email binding and whether Keycloak verifies addresses. Confirm whether your version uses it for account matching.
openid and profile scopesMakes sub, name and preferred_username availableThe Synapse Keycloak example requests these scopes. Add email only if your mapping uses it. Confirm which scopes your version requests by default.
Custom mapper or group claimNot mapped to a profile field by defaultSynapse does not map groups to room or admin rights through this table. Decide whether group-based access is handled elsewhere. Confirm before adding any custom claim.

Back-channel logout from Keycloak to Synapse

With back-channel logout enabled, Keycloak sends a logout request from its server to Synapse when the user's Keycloak session ends, for example after an administrator signs the user out. Synapse then ends the login session it created from that OIDC login. The browser is not part of this exchange, so the signal does not depend on the user returning to a page. The setting lives in the Synapse oidc_providers entry for the keycloak provider, and the Keycloak client must carry Synapse's back-channel logout URL.

  • Synapse setting: Set backchannel_logout_enabled: true on the Keycloak provider in oidc_providers.
  • Client field: Enter Synapse's back-channel logout URL in the Keycloak client's back-channel logout settings, copying the exact value from the Synapse OpenID documentation for your Synapse version.
  • Network path: Keycloak must reach that URL over HTTPS on your internal network, so check firewall rules and reverse proxy paths between the two services.
  • Session matching: Confirm that the identifiers in the logout token match the session Synapse created at login, using a test user.
  • Evidence: Record a Keycloak logout and the matching Synapse log entry, so the control can be shown later.

Confirm with a test user that a Keycloak logout ends the Synapse session before relying on this control.

Securing the Synapse and Keycloak trust relationship

Synapse trusts Keycloak to vouch for every user who signs in, and Keycloak trusts the redirect and logout endpoints Synapse exposes. The controls below protect that trust from the client secret to the federation listener, and each one should be checked in your own environment before go-live.

Client secret storage and rotation

The Synapse configuration uses a confidential client authenticated with a client ID and secret, so the secret is the credential that proves Synapse is the party exchanging codes for tokens. Keep it out of the homeserver.yaml file in version control, supply it from a secrets store or environment at deploy time, and restrict who can read it on the host. Agree a rotation procedure in advance that updates the Keycloak client and the Synapse config together, then test it in a non-production realm. Confirm the exact configuration keys in the Synapse configuration manual.

Configuration manual

TLS on every endpoint

The browser redirects, the token exchange, the discovery document and the back-channel logout call all cross the network, and the Keycloak production guide calls for TLS on all communication. Serve the Synapse client listener and the Keycloak hostname over HTTPS with certificates the browser and Synapse both trust, and make sure the issuer URL in the oidc_providers block matches the Keycloak hostname exactly. Check that Synapse can validate the Keycloak certificate chain from its own host, since a trust failure often appears only as a failed login.

Keycloak production guide

Separate admin console hostname

The Keycloak production guide recommends exposing the admin console and Administration REST API on a different hostname or context path from public frontend URLs. Apply that split here so the login pages that users reach are not the same surface that administers the realm and the Synapse client. Restrict the admin hostname to administrator networks or a VPN, and confirm that the reverse proxy blocks admin paths on the public name. Reviewers will ask for this boundary in an access review.

Exact redirect URI matching

The Keycloak client should list only the callback URL Synapse actually uses, for example the path ending in /_synapse/client/oidc/callback on your public homeserver name. Avoid wildcard redirect URIs and avoid registering development or test hostnames on the production client. An overly broad redirect list lets an attacker who controls another listed URL receive authorization codes. Review the list after every change to server_name, delegation or reverse proxy routing, because those changes often add hostnames.

Local Synapse admin for recovery

If Keycloak is unreachable, misconfigured or its client secret is rotated incorrectly, users cannot sign in through SSO, and an administrator may be locked out of the homeserver. Keep at least one local Synapse admin account with a password stored in your break-glass process, and confirm that you can log in with it before enabling SSO-only access. Document who may use it, when, and how the use is logged and reviewed. Confirm how your Synapse version handles password login alongside oidc_providers in the upgrade notes.

Firewalling the federation listener

Synapse federates on port 8448 by default, and the configuration manual recommends firewalling the federation listener in addition to the federation_domain_whitelist setting. Decide whether this homeserver federates at all before go-live. If it does not, set the whitelist to an empty list and block port 8448 at the network edge. If it does, allow only the peers you have agreed to and confirm that delegation sends traffic to the port you expect. Reviewers commonly check this with an external scan.

Synapse federation docs

How a Matrix login flows through Keycloak

The login starts in the browser, which the Matrix client opens toward Keycloak for the authorization code redirect. Keycloak returns the code to Synapse's callback, and Synapse exchanges it at the token endpoint using the client secret, then reads the userinfo or ID token to map attributes to a Matrix user. Check that the redirect URI, the confidential client setting and the back-channel logout URL all match what Synapse is configured to expose, and that the database host stays on the internal side of the boundary.

Sign-in and back-channel logout between the Matrix client, Synapse and Keycloak.

Why Synapse SSO logins fail after setup

This table lists the failures teams most often hit after wiring Synapse to Keycloak, with the likely cause and the fix for each. Check the rows in order: most login errors trace back to a mismatched URL, secret or scope before anything deeper is involved.

FailureLikely causeFix
Redirect URI mismatchThe Keycloak client's valid redirect URI does not exactly match Synapse's public URL plus the /_synapse/client/oidc/callback path with the idp_id.Set the valid redirect URI in the Keycloak client to the exact https address Synapse serves, including the idp_id segment, and retest from a clean browser session.
Wrong issuer or discovery URLThe issuer in oidc_providers points at the wrong realm, a bare Keycloak host, or an address with a trailing mismatch, so discovery fails.Set the issuer to the Keycloak realm URL and confirm the .well-known/openid-configuration document loads from the Synapse host.
Client secret mismatchThe secret in homeserver.yaml differs from the one in the Keycloak client credentials tab, often after regeneration or a copy with extra whitespace.Regenerate or copy the secret from Keycloak, update the client_secret setting, and restart Synapse so the new value loads.
Missing openid scopeThe scopes list omits openid, so Keycloak returns no ID token and Synapse cannot read the subject.Include openid and profile in the scopes list, matching the Synapse Keycloak example, and check the scope is allowed on the Keycloak client.
TLS certificate trustSynapse does not trust the certificate chain of the Keycloak endpoint, often because the chain uses a private or internal CA.Install the internal CA into the Synapse container or host trust store and confirm the Synapse host can reach Keycloak over HTTPS without errors.
Duplicate accounts after a mapping changeA changed user_mapping or localpart template produces new Matrix IDs for existing users, so the same person gets a second account.Decide the localpart template before go-live, test it with a pilot user, and document how existing accounts are reconciled before changing the mapping.
Back-channel logout not reaching Synapsebackchannel_logout_enabled is not set to true, or the Synapse back-channel logout URL is missing or wrong in the Keycloak client.Set backchannel_logout_enabled to true, enter Synapse's back-channel logout URL in the Keycloak client, then confirm logout events arrive in Synapse logs.

What teams ask before connecting Synapse to Keycloak

What drives the effort of connecting Synapse to Keycloak?

The main drivers are the number of Keycloak realms that will sign in to Synapse, how many accounts already exist locally in Synapse, your federation policy, and the worker layout of your Synapse deployment. One realm with a clean user base and a single homeserver is a small change to the oidc_providers block. Multiple realms, existing password accounts that must map to SSO identities, and a worker layout that routes login traffic to specific processes each add design and testing work. Federation policy matters because federation_domain_whitelist and firewalled federation listeners change who can reach your server after sign-in is working. Ask for each of these to be written into the scope before you agree on a price.

Can our team connect Synapse to Keycloak alone using the documentation?

Often yes, if you have someone comfortable with both products. The Synapse OpenID documentation includes a Keycloak example with an idp_id of keycloak, a confidential client using client ID and secret authentication, and the openid and profile scopes. The Synapse configuration manual covers backchannel_logout_enabled, and the Keycloak server administration guide covers client setup. The gaps usually appear in attribute mapping, how existing local accounts behave after SSO is turned on, and testing back-channel logout end to end. Teams that have not run Keycloak in production often find the production guide's TLS and hostname requirements take longer than the Synapse change itself.

What do we need from our identity and platform teams?

Plan for three contributions. Your identity team owns the Keycloak realm, the client definition, the user attributes and the group or role claims that Synapse will map. Your platform team owns the TLS certificates, the reverse proxy path rules, DNS for the Synapse and Keycloak hostnames, and the database and Redis services Synapse depends on. Your Synapse operators own the homeserver configuration, the restart plan and the test accounts. Name one person in each group who can approve changes, and confirm who will rotate the client secret, because it lives in both Keycloak and the Synapse configuration file.

How long does the work take?

The duration depends on scope, and no fixed figure is supplied for this integration. A single realm with one confidential client and no migration of existing accounts is the smallest case. Add realms, account migration, worker changes, back-channel logout testing and a staging rehearsal, and the schedule grows with each item. Ask any provider to break the timeline into design, build, staging test, production cutover and handover, and to state which items depend on your change windows and approvals. A fixed-scope agreement fixes the work list before the timeline is set.

How do upgrades of Synapse or Keycloak affect the integration?

Each product can change the integration on its own schedule, so plan for both. The Synapse upgrade notes warn that rolling back can be difficult because schema changes differ between releases, and that Synapse drops support for Python and PostgreSQL versions once they reach upstream end of life. Keycloak plans four minor releases a year, and its 26.8 upgrade notes include mandatory signature validation of back-channel logout tokens from external providers and enforced SMTP StartTLS. Test the client, scopes, mappers and logout flow in staging against both the new Synapse and Keycloak versions before each production upgrade. Keep the Keycloak operator on manual approval mode if you run it on Kubernetes.

What breaks most often after sign-in is switched on?

The most common failures are mismatched redirect URIs, a client secret that was rotated in one system but not the other, and user attributes that Synapse does not receive because a mapper or scope is missing. Users with existing local Synapse accounts can end up with a second identity if the mapping is not designed first. Back-channel logout fails when the Synapse logout URL is not entered in the Keycloak client, or when the reverse proxy blocks the request. Check each item in a staging environment with test users from every realm before you turn SSO on for real accounts.

What does a fixed-scope quote from DeployOpen cover?

A fixed-scope engagement covers the scope, deliverables and timeline agreed in writing before work starts. For this integration, expect the written scope to list the realms and clients in scope, the Synapse oidc_providers configuration, the attribute mapping rules, the back-channel logout setup and test, the staging and production steps, and handover documentation. It should also state what is excluded, such as account migration, worker redesign or changes to upstream identity providers, and what you must provide, such as hostnames, certificates and test users. Ask for each of these items to be listed, because the quote follows from that list. DeployOpen does not publish a price list.

Does the Synapse change affect federation or who can reach our server?

It can. SSO changes how users sign in, but federation is a separate policy. Synapse federates on port 8448 by default, and server_name determines how user IDs and rooms are identified. The federation_domain_whitelist setting restricts federation to listed domains, and an empty list disables it. The Synapse documentation recommends firewalling the federation listener as well. Confirm your federation policy before cutover, because a sign-in change made without that check can leave the federation setup different from what your security team approved.

How DeployOpen scopes and delivers the Synapse SSO integration

DeployOpen plans and builds Keycloak and Matrix Synapse OIDC single sign-on inside infrastructure you control, with the Keycloak client, the oidc_providers block, attribute mapping and back-channel logout agreed in writing before work starts. Engagements are fixed-scope, and the handover includes documentation your own team can run.

How we scope

  • We confirm your Synapse version, database, and whether you run a single homeserver or workers behind Redis
  • We agree which Keycloak realm, client and user attributes Synapse will consume before any configuration starts
  • We record in the written scope whether back-channel logout, federation and SAML or CAS are in or out

How we build

  • We configure a confidential Keycloak client with client ID and secret authentication and the openid and profile scopes
  • We set the oidc_providers entry with idp_id keycloak, claim mapping and the back-channel logout setting, then test sign-in end to end
  • We verify TLS, redirect URIs and the back-channel logout URL against your public and admin hostnames

How we hand over

  • You receive the realm, client and Synapse configuration with the secret stored outside the config file
  • You receive test cases for first login, attribute mapping changes, and logout propagation from Keycloak to Synapse
  • You receive a runbook covering secret rotation, upgrade checks against Synapse release notes, and rollback limits

How we support

  • Ongoing support, upgrades and continuous testing are available as a retainer after handover
  • We retest the sign-in flow after each Synapse or Keycloak upgrade you schedule
  • We review changes to Keycloak client settings and Synapse configuration against the agreed scope

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.