やりたいこと
Keycloak(RedHat SSO)上に存在するユーザをAdmin REST APIを利用して取得する。
尚、今回はデフォルトで存在するクライアント「admin-cli」を利用するケースで記載を行い、masterレルムを取得対象とする。
環境情報
- Linux(fedora)
- keycloak-21.1.2
やり方
Keycloakが動作しているサーバに対して以下のようにアクセストークンの発行を行なった上で
# curl --insecure -X POST http://localhost:8080/realms/master/protocol/openid-connect/token --user admin-cli:admin -H 'content-type: application/x-www-form-urlencoded' -d 'username=admin&password=<admin_password>&grant_type=password'
取得できる「access_token」を利用して「admin/realms/
# curl -k -X GET http://localhost:8080/admin/realms/master/users -H "Authorization: Bearer "$access_token | jq . ...
以下形式にてユーザ情報が取得できる。
[ { "id": "xx", "createdTimestamp": xx, "username": "admin", "enabled": true, "totp": false, "emailVerified": false, "disableableCredentialTypes": [], "requiredActions": [], "notBefore": 0, "access": { "manageGroupMembership": true, "view": true, "mapRoles": true, "impersonate": true, "manage": true } }, ...
以下、補足です。
補足
「admin-cli」はDirect access grantsが有効となっているのでこちらを利用してAdmin REST API を突けます。
初期状態ではClient authenticationが無効となっているため--user admin-cli:xx
部分のxxに相当する箇所は任意の文字列で構いません。(項目を消してしまうとインタラクティブに Enter host password for user 'admin-cli': とリクエストがあります)
同箇所でも認証を行いたい場合は、管理コンソールからClient authenticationを有効化した上でClient secretを入力します。
以上。