           (println (format "Tried to promote user: %s" user-id))
           (lib/internal-server-error))))))
 
-
 (defn do-demote-user-from-admin
   [{:keys [api-root] :as keycloak} tenant author-claims user-id]
   (if (= (get author-claims "sub") user-id)

(defmethod ig/pre-init-spec :akvo.lumen.component.keycloak/data [_]
  ::data)

(defn new-connection-manager
  ([] (new-connection-manager {}))
  ([opts]
   (let [default-values {:timeout 1000 :threads 2 :insecure? false :default-per-route 2}]
     (http.conn-mgr/make-reusable-conn-manager (merge default-values (select-keys opts [:timeout :threads :insecure? :default-per-route]))))))


;;   all timeouts are in milliseconds!!
;;  :timeout - Time that connections are left open before automatically closing
;;  :connection-timeout - int the default connect timeout value for connection

(defmethod ig/init-key :akvo.lumen.component.keycloak/keycloak  [_ {:keys [credentials data max-user-ids-cache] :as opts}]
  (log/info "Starting keycloak")
  (let [{:keys [issuer openid-config api-root] :as this} (init-keycloak (assoc data :credentials credentials :max-user-ids-cache max-user-ids-cache))
        connection-manager (new-connection-manager {:timeout (* 30 1000) :threads 10 :default-per-route 10})
        openid-config (fetch-openid-configuration issuer {})]
    (log/info "Successfully got openid-config from provider.")
    (assoc this
           :connection-manager connection-manager
           :http-timeout http-timeout
           :openid-config openid-config)))

(defmethod ig/halt-key! :akvo.lumen.component.keycloak/keycloak  [_ opts]

  (allowed-paths [this email]
    (allowed-paths this email)))

(defn- init-keycloak [{:keys [credentials url realm max-user-ids-cache]}]
  (map->KeycloakAgent {:api-root (format "%s/admin/realms/%s" url realm)
                       :credentials credentials
                       :issuer (format "%s/realms/%s" url realm)
                       :user-id-cache (atom (cache/lru-cache-factory {} :threshold max-user-ids-cache))}))

(defmethod ig/init-key :akvo.lumen.component.keycloak/data  [_ {:keys [url realm] :as opts}]
  (try
