Compojure: Accessing Basic Authentication Elements for Route Processing

I am writing a web API using Compojure with basic authentication software. The main body of authentication looks something like this:

(defn authenticated? [id pass]
    (and (= id "blah")
         (= pass "blah"))

      

id and pass are passed using id method: pass @website. My problem is that I would like to access that ID and go further down where routes are processed with various GET, PUT, POST, etc. headers. But I can't figure out how to do it, and I didn't have time to walk.

; i'd like to access id and pass here
(defroutes routes
    (GET "/" [] {:status 200 :headers {} :body "hi!"})
    ...)

      

I guess the solution for the above is somehow "add" the id and go to some set of variables that can be accessed when routes are processed, but I have no idea how to add and access them.

Hopefully someone can point me in the right direction - thanks.

+3


source to share


2 answers


Assuming you say https://github.com/remvee/ring-basic-authentication , is it authenticated? fn can return a true value, which will be appended to the request in: basic-authentication. Something like (untested):



(defn authenticated? [id pass]
    (and (= id "gal") 
         (= pass "foo")
          {:user id :passwd pass}))

 (defroutes routes
     (GET "/" {the-user :basic-authentication} {:status 200 :headers {} :body (str "hi! Mr. " (:user the-user) " your password is " (:passwd the-user))})
     ...)

      

+1


source


Returning authenticated? the method is linked in the request map referenced by the key: basic authentication. Here's an example with a route that returns a user. However, you can return a card or any other object and access it via: basic authentication key.



(defn authenticated? [user password] user)

(defroutes require-auth-routes
   (GET "/return-user" request (.toString (:basic-authentication request)))

(def my-app
  (routes
    (-> require-auth-routes
        (wrap-basic-authentication authenticated?)))

      

0


source







All Articles