Skip to content

Instantly share code, notes, and snippets.

@ruseel
Last active August 27, 2023 18:17
Show Gist options
  • Save ruseel/2b94622289b7e96e8d0c83ae01df29f0 to your computer and use it in GitHub Desktop.
Save ruseel/2b94622289b7e96e8d0c83ae01df29f0 to your computer and use it in GitHub Desktop.

Revisions

  1. ruseel revised this gist Feb 27, 2020. 1 changed file with 33 additions and 19 deletions.
    52 changes: 33 additions & 19 deletions keycloak-rest-api-sample.clj
    Original file line number Diff line number Diff line change
    @@ -1,40 +1,54 @@
    (def base-url "https://keyloack.of.myown.com")
    ;; this worked for me
    ;;

    (defn access-token []
    (->
    (client/post
    (str base-url "/auth/realms/callcenter/protocol/openid-connect/token")
    (str base-url "/auth/realms/" realm "/protocol/openid-connect/token")
    {:accept :json
    :form-params {"client_id" "admin-cli"
    "username" "admin-some-realm"
    "password" "some-password"
    "username" some-username
    "password" default-password
    "grant_type" "password"}})
    :body
    (json/read-str :key-fn keyword)
    :access_token))

    (access-token)

    (defn list-users []
    (->
    (client/get (str base-url "/auth/admin/realms/<REALM>/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)))


    ;;
    ;; https://www.keycloak.org/docs-api/9.0/rest-api/#_users_resource
    ;;
    ;; this is working on my keycloak
    ;;
    (defn create-user []
    (->
    (client/post (str base-url "/auth/admin/realms/<REALM>/users")
    (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 protected]"
    :username "a"})})))
    :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})})))
  2. ruseel revised this gist Feb 27, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion keycloak-rest-api-sample.clj
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,8 @@

    ;;
    ;; https://www.keycloak.org/docs-api/9.0/rest-api/#_users_resource
    ;; name `rep`
    ;;
    ;; this is working on my keycloak
    ;;
    (defn create-user []
    (->
  3. ruseel created this gist Feb 27, 2020.
    39 changes: 39 additions & 0 deletions keycloak-rest-api-sample.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    (def base-url "https://keyloack.of.myown.com")

    (defn access-token []
    (->
    (client/post
    (str base-url "/auth/realms/callcenter/protocol/openid-connect/token")
    {:accept :json
    :form-params {"client_id" "admin-cli"
    "username" "admin-some-realm"
    "password" "some-password"
    "grant_type" "password"}})
    :body
    (json/read-str :key-fn keyword)
    :access_token))

    (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)))


    ;;
    ;; https://www.keycloak.org/docs-api/9.0/rest-api/#_users_resource
    ;; name `rep`
    ;;
    (defn create-user []
    (->
    (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 protected]"
    :username "a"})})))