Skip to content

About

Actively synchronise the members of Discord roles to their linked Keycloak group

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Keycloak Groups to Discord Roles sync

This is a python application that projects Keycloak group membership onto Discord roles.

Keycloak is the system of record. Membership lives in Keycloak; the bot keeps Discord roles in sync to reflect it. For each linked group it adds the Discord role to everyone in the group and removes it from anyone who holds the role but isn't a member. Because Keycloak can't emit membership-change events, this runs at startup and then on a periodic poll (GROUP_SYNC_INTERVAL_MINUTES, default 5). Manually editing a linked Discord role is reverted to match Keycloak — to change who has access, change it in Keycloak (admin UI or the grant commands below).

For this to work you need to:

  1. Give each Keycloak user a discord_id custom user attribute holding their Discord user ID. The bot matches users to Discord by this attribute (searched via q=discord_id:<id>); a user with no discord_id is treated as unlinked. (Alternatively, populate it automatically with the Discord Keycloak Identity Provider.)
  2. Set up the Keycloak groups you'd like to project with the following attributes:
    • discord-role containing the ID of the role (requires dev mode for Discord to be enabled)
    • discord-guild containing the ID of the guild the role is in
  3. Create a Keycloak user with Admin API access
    • If you have fine-grained authz enabled, provide the account with the view-users & manage-users roles
  4. Create a Discord Application (here) & add the bot to your server.
    • Make sure it has the Server Members intent, otherwise it won't see role/member state
    • The bot needs the Manage Roles permission, and its highest role must sit above every role it manages, or it can't add/remove them
    • To use the grant commands (below), invite the bot with the applications.commands scope

Example config

      DISCORD_BOT_TOKEN: MZ1yGvKTjE0rY0cV8i47CjAa.uRHQPq.Xb1Mk2nEhe-4iUcrGOuegj57zMC
      KEYCLOAK_URL: https://keycloak.example.com
      KEYCLOAK_USERNAME: KeycloakUsername
      KEYCLOAK_PASSWORD: KeycloakPassword
      KEYCLOAK_REALM: Example-Corp
      # Only required if KeycloakUsername isn't an account under the Example-Corp realm
      KEYCLOAK_ADMIN_REALM: master
      # How often (minutes) to re-project Keycloak membership onto Discord roles (default 5)
      GROUP_SYNC_INTERVAL_MINUTES: 5
      # Path to the grant rules file (see below); omit to disable the grant commands
      GRANTS_CONFIG: /config/grants.yaml

Granting groups via slash commands

On top of the role sync, trusted users can add others to Keycloak groups directly, across any server the bot is in:

  • /grant <user> <group> — add a user to a group
  • /revoke <user> <group> — remove a user from a group
  • /grants — list the groups you're allowed to grant

Authorization is by Keycloak group membership: a user may grant a group only if they belong to one of its granted_by groups. Rules live in the YAML file pointed to by GRANTS_CONFIG:

grants:
  - group: "/infra/infra-developers"   # the grantable group's Keycloak path
    granted_by:                         # Keycloak group paths whose members may grant it
      - "/execs"
      - "/infra/infra-director"

A grantable group may also be role-synced (carry discord-role/discord-guild). Because Keycloak is the system of record, the two compose: /grant writes the user into the Keycloak group, and the projection then applies the linked Discord role (the command also applies it immediately so the user doesn't wait for the next poll). So /grant @bob /BEANS adds Bob to /BEANS and gives him the role tied to it; /revoke reverses both.

Both the grantable group and its granters are resolved against Keycloak at startup; typos or non-existent paths are logged and skipped rather than crashing the bot. See example-grants.yaml for a full sample (copy it to grants.yaml and point GRANTS_CONFIG at it).

About

Actively synchronise the members of Discord roles to their linked Keycloak group

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages