Skip to content

Интеграция Mattermost и Keycloak

Mattermost нужно указать какие токены вы хотите получать - oauth2 или oidc

Поэтому всё что требовалось это сказать ему - запрашивай oidc-токены(т.е указать scope openid), а не обычные oauth2-ые Если не указать openid при передаче в scope, то токен будет выпущен согласно протоколу OAuth2 и при обращении к /userinfo - выкинет ошибку 403, потому что этот эндпоинт определён в стандарте OIDC и работает по его правилам, т.е. только с токенами выпущенными согласно протоколу OIDC, а не OAuth2

в 19 версии /userinfo был доступен с токенами oauth2, после разрабы kc это исправили для соответствия стандарту oidc

скоуп openid вроде и так по дефолту уже есть во всех oidc клиентах в него попадает всё, что лежит в assigned default client scopes в клиенте

Интеграция с Keycloak

Настройка keycloak:

ClientID : mattermost
Client authentication: On
Enabled (ON)
Client Protocol (openid-connect)
Authentication flow: Standard Flow only
Valid redirect URI: https://mattermost.truedev.ru/signup/gitlab/complete 

Clients - Clients List - mattermost:
    Client scopes - mattermost-dedicated:
        Add mapper - by configuration: 
            User attribute:
                Mapper type: User attribute
                Name:        ldap_mattermost_id_mapper
                User attribute: uidNumber
                Token Claim Name: id
                Claim Json Type: long 
                only Add to userinfo: ON 
            User property:
                Mapper type: User property
                Name:        username
                Property: username
                Token Claim Name: username
                Claim Json Type: string 
                only Add to userinfo: ON 

Get Realm Endpoints: curl https:// | jq

  "issuer": "https://keycloak.domain.ru/realms/<REALM_NAME>",
  "authorization_endpoint": "https://keycloak.domain.ru/realms/<REALM_NAME>/protocol/openid-connect/auth",
  "token_endpoint": "https://keycloak.domain.ru/realms/<REALM_NAME>/protocol/openid-connect/token",
  "userinfo_endpoint": "https://keycloak.domain.ru/realms/<REALM_NAME>/protocol/openid-connect/userinfo",

Create Client Scope

Client scopes - mattermost:
    Add mapper - by configuration: 
        User attribute:
            mattermostid
        User property:
            username

Mattermost Settings Mattermost Mappers ClientScope Mapper uidNumber ClientScope Mapper username

Create Client

Clients - Clients List - Create client - mattermost: Mattermost Client Alt text Alt text

Настройка mattermost:

Authentication -

Authentication:
    Signup:
        Enable Account Creation: true 
    Gitlab:
        Enable authentication with gitlab: true
        Application ID: mattermost
        Application Secret Key: ...
        User API Endpoint:
        Auth Endpoint:
        Token Endpoint:
    Plugins - Calls - Test mode: Off
docker exec -u 0 mattermost-mattermost-1 bash -c 'apt update && apt install vim -y'
docker exec -it mattermost-mattermost-1 bash 

vim config/config.json

"GitLabSettings": {
    "Enable": true,
    "Secret": "<secret>",
    "Id": "mattermost",
    "Scope": "",
    "AuthEndpoint": "https://keycloak.domain.ru/realms/truedev/protocol/openid-connect/auth",
    "TokenEndpoint": "https://keycloak.domain.ru/realms/truedev/protocol/openid-connect/token",
    "UserApiEndpoint": "https://keycloak.domain.ru/realms/truedev/protocol/openid-connect/userinfo"
},

change in GitLabSettings: AuthEndpoint TokenEndpoint UserApiEndpoint and in ServiceSettings: "EnableLocalMode": true and you can use command mmctl --local config edit docker compose down && docker compose up -d

Alt text


DEBUG

  1. CURL

curl -X "POST" "https://hostname/realms/master/protocol/openid-connect/token" \
     -H 'Content-Type: application/x-www-form-urlencoded' \
     -H 'Accept: application/json' \
     --data-urlencode "grant_type=password" \
     --data-urlencode "client_id=mattermost openid" \
     --data-urlencode "client_secret=blabla" \
     --data-urlencode "scope=mattermost" \
     --data-urlencode "username=dev" \
     --data-urlencode "password=passs"
Alt text Alt text

Копируем Access Token и вставляет после Bearer Alt text

  1. using Evaluate Alt text

Email notifications

...

Ошибка Alt text Alt text Alt text Alt text

  • https://github.com/mattermost/mattermost/issues/15606#issuecomment-1431648625