やりたいこと
Red Hat build of Keycloak(Keycloakの商用版)にてAdmin API経由でログイン済みのユーザセッション数の合計を取得する。
環境情報
- Red Hat Enterprise Linux : 9.3 (Plow)
- rhbk-22.0.8
やり方
アクセストークンの取得を行い
$ AT=`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=xx&grant_type=password' | jq .access_token | sed -e 's/"//g'`
アクセストークンを引数に渡した上で「GET /{realm}/client-session-stats」を利用するとJSON形式で"active"にセッション数が表示されている。
$ curl -k -X GET http://localhost:8080/admin/realms/master/client-session-stats --header 'Content-Type: application/json' --header "Authorization: Bearer $AT" | jq . ... [ { "offline": "0", "clientId": "admin-cli", "active": "3", "id": "xx-xx-xx-xx-xx" } ]
以下、補足です。
補足
kcadm.shにて複数ユーザでログインを行うと上記のような結果となりました。
$ ./kcadm.sh config credentials --server http://localhost:8080 --realm master --user admin
試しに管理コンソールからGUIでログインをログインを行うとsecurity-admin-consoleというクライアントが追加で表示されました。
[{"offline":"0","clientId":"admin-cli","active":"1","id":"xx-xx-xx-xx-xx"},{"offline":"0","clientId":"security-admin-console","active":"1","id":"yy-yy-yy-yy-yy"}]
尚、ログイン済みのセッションはないものの存在しているクライアントに関する設定は下記にて表示できます。
$ ./kcadm.sh get clients | jq . ... { "id": "xx-xx-xx-xx-xx", "clientId": "admin-cli", "name": "${client_admin-cli}", "surrogateAuthRequired": false, "enabled": true, "alwaysDisplayInConsole": false, "clientAuthenticatorType": "client-secret", "redirectUris": [], "webOrigins": [], "notBefore": 0, "bearerOnly": false, "consentRequired": false, "standardFlowEnabled": false, "implicitFlowEnabled": false, "directAccessGrantsEnabled": true, "serviceAccountsEnabled": false, "publicClient": true, "frontchannelLogout": false, "protocol": "openid-connect", "attributes": { "post.logout.redirect.uris": "+" }, "authenticationFlowBindingOverrides": {}, "fullScopeAllowed": false, "nodeReRegistrationTimeout": 0, "defaultClientScopes": [ "web-origins", "acr", "profile", "roles", "email" ], "optionalClientScopes": [ "address", "phone", "offline_access", "microprofile-jwt" ], "access": { "view": true, "configure": true, "manage": true } }, ...
以上、ご参考になれば幸いです。