|
1 | | -# matrix-keycloak-sync |
| 1 | +# matrix-keycloak-sync |
| 2 | + |
| 3 | +A script that synchronizes users from a Keycloak group with user accounts in a Matrix chat (Synapse). |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The script fetches the list of users from a specific Keycloak group and compares them against the user state (enabled/disabled) in the Matrix/Synapse database. User activation and deactivation in Matrix is controlled by Keycloak group membership. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## User Sync Logic |
| 14 | + |
| 15 | +| User in Keycloak group | User in Synapse DB | Action | |
| 16 | +|---|---|---| |
| 17 | +| ✅ Yes | ✅ Yes (active) | No action needed | |
| 18 | +| ✅ Yes | ✅ Yes (disabled) | **Activate** via `mas-cli` | |
| 19 | +| ✅ Yes | ❌ No | **Skip** — will be created on first login | |
| 20 | +| ❌ No | ✅ Yes (active) | **Disable** via `mas-cli` | |
| 21 | +| ❌ No | ✅ Yes (disabled) | No action needed | |
| 22 | + |
| 23 | +> **Note:** If a user is present in the Keycloak group but does not yet exist in the Synapse database, they are skipped during this sync run. The user account will be automatically created in Synapse upon their first login via Keycloak SSO. |
| 24 | +
|
| 25 | +--- |
| 26 | + |
| 27 | +## Components |
| 28 | + |
| 29 | +### Keycloak |
| 30 | +- Source of truth for user access. |
| 31 | +- Users added to a designated group are considered **active** in Matrix. |
| 32 | +- Users removed from the group will be **disabled** in Matrix on the next sync run. |
| 33 | + |
| 34 | +### Synapse API |
| 35 | +- Used **read-only** to fetch the current list of users and their `deactivated` status. |
| 36 | +- Requires an admin token (see [Authentication](#authentication) below). |
| 37 | + |
| 38 | +### mas-cli (Matrix Authentication Service CLI) |
| 39 | +- Used to **activate and deactivate** users. |
| 40 | +- Requires a `mas-cli.yaml` config file. |
| 41 | +- Communicates **directly with the database** — bypasses Synapse API for write operations. |
| 42 | + |
| 43 | +> **Why mas-cli instead of Synapse API for writes?** |
| 44 | +> Disabling a user directly in the Synapse database (without going through MAS) is insufficient — MAS will still allow the user to log in even if the `is_disabled = true` flag is set in the Synapse DB. All deactivation must go through `mas-cli` to be effective. |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Authentication |
| 49 | + |
| 50 | +### Synapse Admin Token |
| 51 | + |
| 52 | +Generate an admin-scoped token via `mas-cli`. It is recommended to create a **dedicated non-admin Matrix user** for management purposes and grant it admin privileges via token only: |
| 53 | + |
| 54 | +```bash |
| 55 | +mas-cli manage issue-compatibility-token \ |
| 56 | + --yes-i-want-to-grant-synapse-admin-privileges \ |
| 57 | + <your_matrix_username_for_managing_users> |
| 58 | +``` |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## Configuration |
| 63 | + |
| 64 | +### `mas-cli.yaml` |
| 65 | + |
| 66 | +Required for `mas-cli` to connect to the database. Example path: `/etc/mas-cli/mas-cli.yaml`. |
| 67 | + |
| 68 | +### Environment variables / config for the script |
| 69 | + |
| 70 | +| Variable | Description | |
| 71 | +|---|---| |
| 72 | +| `KC_URL` | Base URL of the Keycloak instance | |
| 73 | +| `KC_REALM` | Keycloak realm name | |
| 74 | +| `KC_GROUP_NAME` | Name or ID of the group to sync | |
| 75 | +| `KC_CLIENT_ID` | Client ID | |
| 76 | +| `KC_ADMIN_USER` | User in keycloak in master realm (with restriction permission in specific realm for getting user list)| |
| 77 | +| `KC_ADMIN_PASS` | User pass in keycloak in master realm | |
| 78 | +| `MATRIX_URL` | Base URL of the Synapse homeserver | |
| 79 | +| `MATRIX_ADMIN_TOKEN` | Admin token for Synapse API | |
| 80 | +| `MATRIX_SERVER_NAME` | Matrix server name | |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +## Alternative: MAS Admin API |
| 85 | + |
| 86 | +It is also possible to manage users via the [MAS Admin API](https://matrix-org.github.io/matrix-authentication-service/topics/admin-api.html#enabling-the-api) instead of `mas-cli`. However, this approach has additional overhead: |
| 87 | + |
| 88 | +- The API must be explicitly enabled in MAS configuration. |
| 89 | +- A separate API token must be generated (not the one generated via the MAS CLI). |
| 90 | +- The API endpoint is **exposed publicly by default** and requires additional hardening. |
| 91 | + |
| 92 | +For these reasons, using `mas-cli` directly is the recommended approach for this script. |
0 commit comments