Skip to content

Instantly share code, notes, and snippets.

@muratamuu
Last active December 24, 2022 00:49
Show Gist options
  • Save muratamuu/0c383a736b60269acb1648a18159f301 to your computer and use it in GitHub Desktop.
Save muratamuu/0c383a736b60269acb1648a18159f301 to your computer and use it in GitHub Desktop.
Keycloak APIを使ってユーザを作成する方法

参考情報

マニュアル

https://www.keycloak.org/docs-api/12.0/rest-api/index.html

エンドポイント確認

curl --insecure http://keycloak:8080/auth/realms/s1/.well-known/openid-configuration | jq

masterレルムの管理者ユーザのアクセストークン取得

export token=$(curl --insecure \
-d "client_id=admin-cli" \
-d "username=admin" \
-d "password=xxxxx" \
-d "grant_type=password" \
http://keycloak:8080/auth/realms/master/protocol/openid-connect/token | jq -r '.access_token')
echo $token

s1レルムに新規ユーザを追加

curl --insecure -X POST \
  -H "Authorization: Bearer $token" \
  -H "Content-Type: application/json; charset=UTF-8"
  -d '{"username":"hogehoge", "enabled": true, "email": "hogehoge@example.com"}' \
  http://keycloak:8080/auth/admin/realms/s1/users -v

追加したユーザの情報を取得

curl --insecure -X GET \
  -H "Authorization: Bearer $token" \
  http://keycloak:8080/auth/admin/realms/s1/users?username=hogehoge | jq

追加したユーザの情報、特にIDを取得

export userid=$(curl --insecure -X GET \
  -H "Authorization: Bearer $token" \
  http://keycloak:8080/auth/admin/realms/s1/users?username=hogehoge | jq -r .[].id)
echo $userid

指定したユーザのパスワードを設定

curl --insecure -X PUT \
  -H "Authorization: Bearer $token" \
  -H "Content-Type: application/json; charset=UTF-8" \
  -d '{"type": "password", "value": "hogefuga", "temporary": true}' \
  http://keycloak:8080/auth/admin/realms/s1/users/${userid}/reset-password -v

指定したユーザIDを削除

curl --insecure -X DELETE \
  -H "Authorization: Bearer $token" \
  http://keycloak:8080/auth/admin/realms/s1/users/${userid} -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment