better wrap-fn

This commit is contained in:
Yann Esposito (Yogsototh) 2017-11-07 17:07:53 +01:00
parent b3c8160a55
commit cd5d9bbf4d
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646
3 changed files with 20 additions and 20 deletions

View file

@ -19,14 +19,13 @@ wrap your routes with it:
#+BEGIN_SRC clojure #+BEGIN_SRC clojure
(defn get-auth-from-api-key [token] (defn get-auth-from-api-key [token]
(when (= token "secret-api-key") (when (= token "secret-api-key")
{:user "user-01" {:user {:id "user-01" :name "username"}
:groups ["admin-id" "user-id"] :groups #{{:id "cisco" :name "Cisco"}}
:username "username" :roles #{:admin :user}
:group-names ["admin" "users"] :auth-type :api-key}))
:admin true
:auth-type :api-key}))
(def app (def app
(wrap-api-key-auth-fn handler get-auth-from-api-key)) ((wrap-api-key-auth-fn get-auth-from-api-key) handler))
#+END_SRC #+END_SRC
When configured like this all requests with the header: When configured like this all requests with the header:
@ -39,11 +38,10 @@ will be modified to be passed to the handler with the new key `:api-key-info`
containing: containing:
#+BEGIN_SRC clojure #+BEGIN_SRC clojure
{:user "user-01" {:user {:id "user-01" :name "username"}
:groups ["admin-id" "user-id"] :groups #{{:id "cisco" :name "Cisco"}}
:username "username" :roles #{:admin :user}
:group-names ["admin" "users"] :auth-type :api-key}
:admin true}
#+END_SRC #+END_SRC
If the header contain an Authorization header with an unknown `api-key` the If the header contain an Authorization header with an unknown `api-key` the

View file

@ -1,4 +1,4 @@
(defproject threatgrid/ring-api-key-middleware "0.1.1-SNAPSHOT" (defproject threatgrid/ring-api-key-middleware "0.1.2"
:description "A simple middleware to deal with API keys Authentication" :description "A simple middleware to deal with API keys Authentication"
:url "http://github.com/threatgrid/ring-api-key-middleware" :url "http://github.com/threatgrid/ring-api-key-middleware"
:license {:name "Eclipse Public License - v 1.0" :license {:name "Eclipse Public License - v 1.0"

View file

@ -18,10 +18,12 @@
(defn wrap-api-key-fn (defn wrap-api-key-fn
"I check " "I check "
[handler get-infos] [get-infos]
(fn [request] (fn
(if-let [api-key (get-api-key request)] [handler]
(if-let [infos (get-infos api-key)] (fn [request]
(handler (assoc request :api-key-infos infos)) (if-let [api-key (get-api-key request)]
(unauthorized "wrong access key")) (if-let [infos (get-infos api-key)]
(handler request)))) (handler (assoc request :api-key-infos infos))
(unauthorized "wrong access key"))
(handler request)))))