Skip to content

Instantly share code, notes, and snippets.

@ruseel
Last active August 27, 2023 18:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruseel/2b94622289b7e96e8d0c83ae01df29f0 to your computer and use it in GitHub Desktop.
Save ruseel/2b94622289b7e96e8d0c83ae01df29f0 to your computer and use it in GitHub Desktop.
Keycloak Admin REST API Sample
;; 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})})))
@hendisantika
Copy link

Do You have in Java way?

@gustavofveloso
Copy link

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

@ruseel
Copy link
Author

ruseel commented Mar 15, 2022

@gustavofveloso Thanks. you made me a day.

@Xitija
Copy link

Xitija commented Dec 7, 2022

Thank you so much !! Was very helpful!

@Albimuca5
Copy link

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