Last active
August 27, 2023 18:17
-
-
Save ruseel/2b94622289b7e96e8d0c83ae01df29f0 to your computer and use it in GitHub Desktop.
Keycloak Admin REST API Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; this worked for me | |
;; | |
(defn access-token [] | |
(-> | |
(client/post | |
(str base-url "/auth/realms/" realm "/protocol/openid-connect/token") | |
{:accept :json | |
:form-params {"client_id" "admin-cli" | |
"username" some-username | |
"password" default-password | |
"grant_type" "password"}}) | |
:body | |
(json/read-str :key-fn keyword) | |
:access_token)) | |
(defn list-users [] | |
(-> | |
(client/get (str base-url "/auth/admin/realms/" realm "/users") | |
{:headers {"Authorization" | |
(clojure.string/join " " ["bearer" (access-token)])}}) | |
:body | |
(json/read-str :key-fn keyword))) | |
(defn create-user [{:keys [email id name]}] | |
(-> | |
(client/post (str base-url "/auth/admin/realms/" realm "/users") | |
{:headers {"Authorization" | |
(clojure.string/join " " ["bearer" (access-token)])} | |
:content-type "application/json" | |
:body (json/write-str | |
{:email email | |
:username id})}))) | |
(defn update-user [{:keys [id] :as m}] | |
(client/put (str base-url "/auth/admin/realms/" realm "/users/" id) | |
{:headers {"Authorization" | |
(clojure.string/join " " ["bearer" (access-token)])} | |
:content-type "application/json" | |
:body (json/write-str m)})) | |
(defn user-enable [m] | |
(update-user (assoc m :enabled true))) | |
(defn reset-password [{:keys [id password]}] | |
(-> | |
(client/put (str base-url "/auth/admin/realms/" realm "/users/" id "/reset-password") | |
{:headers {"Authorization" | |
(clojure.string/join " " ["bearer" (access-token)])} | |
:content-type "application/json" | |
:body (json/write-str | |
{"type" "password" | |
"value" password | |
"temporary" false})}))) |
You are the best! Thank you very much for sharing this document with us. I was all afternoon behind the API call to change the password, your code for me, it worked perfectly! <3
@gustavofveloso Thanks. you made me a day.
Thank you so much !! Was very helpful!
Do You have in Java way?
Did you find a way for java?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do You have in Java way?