やりたいこと
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'`
アクセストークンを引数に渡した上で「PUT /admin/realms/{realm}/users/{id}/reset-password」を利用すると(valueは設定したいパスワードの値)
$ curl --location --request PUT 'http://localhost:8080/admin/realms/master/users/<user-id>/reset-password' --header 'Content-Type: application/json' --header "Authorization: Bearer $AT" --data-raw '{"type":"password", "value":"XXX"}'
標準出力はないまま、ユーザのパスワードが設定される。
以下、補足です。
補足
リクエストに含めるユーザIDとはユーザ情報を出力した際に「id」として格納されている値となります。
{ "id": "XX", "createdTimestamp": ..., "username": "test-user01", "enabled": true, "totp": false, "emailVerified": false, "disableableCredentialTypes": [], "requiredActions": [], "notBefore": 0,
尚、エラーハンドリングとしてPUTではなくPOSTを利用すると以下のエラーとなり
{"error":"RESTEASY003650: No resource method found for POST, return 405 with Allow header"}
URIに含めるidに誤りがあると以下のエラーとなりました。
{"error":"User not found"}
以上です。