Table of Contents


This is a very rought write-up on how to use Keycloak SAML2 with a Matrix Synapse Homeserver for user authentication. Keep in mind that not all points outlined here may 100% work for you and “turning and changing” some parameters may be needed to get it working for your setup.


This blog post is especially made for Keycloak SAML 2.0 SSO with Matrix with the GitHub matrix-org/synapse - Complete the SAML2 implementation #5422, which is based on my draft PR GitHub matrix-org/synapse - SAML2 Improvements and redirect stuff #5316.

Matrix Syanspe

To get SAML 2.0 working you need to have a Matrix Synapse Homeserver running version at least version 1.1,0.

In addition to that you need to have the pysaml2 Python module installed and xmlsec1 must be installed on the Matrix Synapse homeserver too.

On Debian and CentOS (possibly all RHEL based OSes) the package is called xmlsec1.

Be sure to verify that the path to xmlsec1 is correctly configured in the upcoming /synapse/config/ section. To make sure you have the right path for the pysaml2 config, run which xmlsec1 and use the printed out path for the xmlsec_binary option.


The blog post assumes that the config for the Matrix Synapse Homeserver is located in /synapse/config/ directory, you can simply change this as long as you change it in all files and / or steps to do.


SAML2 Client

In Keycloak create a new SAML client and set the settings of that client as follows:

Keycloak Client Settings

Two mappers should be created:

// Concat First and Last name of user when non-empty names = [];

firstName = user.getFirstName(); if (firstName.length > 0) { names.push(firstName); } lastName = user.getLastName(); if (lastName.length > 0) { names.push(lastName); }

exports = names.join(” “);

    * `Single Value Attribute` - `On`
    * `Friendly Name` - `displayName`
    * `SAML Attribute Name` - `displayName`
    * `SAML Attribute NameFormat` - `Basic`

In the end it should look like this:

Keycloak Client Mappers List

> **INFO** > > Thanks to [this comment]( for pointing to the correct attributes to use for Matrix Synapse code to pick'em up! ### SAML 2.0 Identity Provider Metadata file Now download the Keycloak "SAML 2.0 Identity Provider Metadata" file. You can get it when you login to the "Keycloak Admin Console" and then click the "SAML 2.0 Identity Provider Metadata" link in the `General` tab (selected by default) at the `Endpoints` list.

Keycloak Realm Settings

> **NOTE** > > Should you not have this button / link in the `Endpoints` list, update your Keycloak instance to version `6.0.1` or higher! > > If you have a very good reason to not keep your Keycloak uptodate, you can try to get the file from `https://YOUR_KEYCLOAK_URL/auth/realms/YOUR_REALM/protocol/saml/descriptor`. > (Replace the placeholders according to your setup) ## Files ### Replacements Be sure to replace the following strings with your value: * `` - Your Matrix homeserver address. * `matrix-example-com` - The name of the SAML2 Keycloak client you chose during the client creation. ### `/synapse/config/key.pem` and `/synapse/config/cert.pem` Certificate and key from Keycloak Client "SAML Keys" tab page. If there is no certificate and key shown, press the `Generate new keys` button to generate them. Click the `Export` button, set the following options before clicking the `Download` button: * Archive Format: `PKCS12` * Key Alias: The name of your Keycloak Client. * Key Password: A password which is used later to "decrypt" the PKCS12 cert + key store. * Realm Certificate Alias: `master` * Store password: Either another password or the same as for `Key Password`. You should get a file named `keystore.p12` after pressing the `Download` button. Now run the following sequence of commands to extract the key and cert in PEM format (this assumes the file is named `keystore.p12` and the password chosen is `example123`): ```shell $ export KEYSTORE_PW="example123" $ openssl pkcs12 -in keystore.p12 -password "pass:${KEYSTORE_PW}" -nocerts -nodes | openssl rsa -out key.pem writing RSA key $ openssl pkcs12 -in keystore.p12 -password "pass:${KEYSTORE_PW}" -nodes | openssl x509 -out cert.pem

Two files, key.pem and cert.pem, are now generated in your current directory and now just need to be placed in the /synapse/config/ directory (full paths see section title) on the Matrix Synapse host(s).



If you have already downloaded the “SAML 2.0 Identity Provider Metadata” file as mentioned in the Keycloak - SAML 2.0 Identity Provider Metadata file section, you can just use and copy it to /synapse/config/idp.xml on the Matrix Synapse Homeserver.

You can get it when you login to the “Keycloak Admin Console” and then click the “SAML 2.0 Identity Provider Metadata” link in the General tab (selected by default) at the Endpoints list.

Keycloak Realm Settings


Should you not have this button / link in the Endpoints list, update your Keycloak instance to version 6.0.1 or higher!

If you have a very good reason to not keep your Keycloak uptodate, you can try to get the file from https://YOUR_KEYCLOAK_URL/auth/realms/YOUR_REALM/protocol/saml/descriptor. (Replace the placeholders according to your setup)

Be sure to copy the downloaded file to /synapse/config/idp.xml on the Matrix Synapse Homeserver.

Example Keycloak SAML 2.0 Identity Provider Metadata file:

<?xml version="1.0" encoding="UTF-8"?>
  ~ Copyright 2016 Red Hat, Inc. and/or its affiliates
  ~ and other contributors as indicated by the @author tags.
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.

<EntitiesDescriptor Name="urn:keycloak" xmlns="urn:oasis:names:tc:SAML:2.0:metadata"
	<EntityDescriptor entityID="https://__YOUR_KEYCLOAK_URL__/auth/realms/master">
		<IDPSSODescriptor WantAuthnRequestsSigned="true"
                        <KeyDescriptor use="signing">

					Location="https://__YOUR_KEYCLOAK_URL__/auth/realms/master/protocol/saml" />
					Location="https://__YOUR_KEYCLOAK_URL__/auth/realms/master/protocol/saml" />
			<SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
				Location="https://__YOUR_KEYCLOAK_URL__/auth/realms/master/protocol/saml" />
				Location="https://__YOUR_KEYCLOAK_URL__/auth/realms/master/protocol/saml" />
				Location="https://__YOUR_KEYCLOAK_URL__/auth/realms/master/protocol/saml" />


This is the pysaml2 config file. It configures pysaml2 to talk with the Keycloak server and do SAML2 authentication.

Create this file with the following content: (Don’t forget to replace the placeholders, see Replacements section)

import saml2
from saml2.saml import NAME_FORMAT_URI

BASE = ""

    "entityid": "matrix-example-com",
    "description": "Matrix Server",
    "service": {
        "sp": {
            "name": "matrix-login",
            "endpoints": {
                "single_sign_on_service": [
                    (BASE + "_matrix/saml2/authn_response", saml2.BINDING_HTTP_POST),
                "assertion_consumer_service": [
                    (BASE + "_matrix/saml2/authn_response", saml2.BINDING_HTTP_POST),
                #"single_logout_service": [
                #    (BASE + "_matrix/saml2/logout", saml2.BINDING_HTTP_POST),
            "required_attributes": ["uid",],
            "optional_attributes": ["displayName"],
            "sign_assertion": True,
            "sign_response": True,
    "debug": 0,
    "key_file": "/synapse/config/key.pem",
    "cert_file": "/synapse/config/cert.pem",
    "encryption_keypairs": [
            "key_file": "/synapse/config/key.pem",
            "cert_file": "/synapse/config/cert.pem",
    "attribute_map_dir": "/synapse/saml2_attribute_maps/",
    "metadata": {
        "local": ["/synapse/config/idp.xml"]
    # If you want to have organization and contact_person for the pysaml2 config
    #"organization": {
    #    "name": "Example AB",
    #    "display_name": [("Example AB", "se"), ("Example Co.", "en")],
    #    "url": "",
    #"contact_person": [{
    #    "given_name": "Example",
    #    "sur_name": "Example",
    #    "email_address": ["[email protected]"],
    #    "contact_type": "technical",
    #    },
    # Make sure to have xmlsec1 installed on your host(s)!
    "xmlsec_binary": "/usr/bin/xmlsec1",
    "name_form": NAME_FORMAT_URI,


This file is your way to map attributes coming from the SSO (/ IDP) service.

MAP = {
    "identifier": "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",
    "fro": {
        'uid': 'uid',
        'displayName': 'displayName',
    "to": {
        'uid': 'uid',
        'displayName': 'displayName',


fro in the above file is not a typo, see pysaml2 Documentation - “Configuration of pySAML2 entities” - attribute_map_dir.

Your Matrix Synapse Homeserver Config YAML file

Add or change the following lines to your Matrix Synapse Homeserver config (make sure you don’t duplicate the lines as that may lead to weird server behavior and / or issues):

# Enable SAML2 for registration and login. Uses pysaml2
# config_path:      Path to the configuration file
# idp_redirect_url: Identity provider URL which will redirect
#                   the user back to /login/saml2 with proper info.
# See pysaml2 docs for format of config.
  enabled: true
  config_path: "/synapse/config/"
  idp_redirect_url: "https://__KEYCLOAK_SERVER__/auth/realms/__KEYCLOAK_REALM__/protocol/saml"


That should be it, now when you go to your Riot Webapp (and chose your Matrix Homeserver) it should give you a button to login through your (SAML2) SSO.

Riot Webapp SSO Login

If it does not work, make sure your Matrix Synapse Homeserver has the required pysaml2 module installed and check your Synapse Homeserver logs for errors and / or warnings.

Have Fun!