Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compojure: access basic-authentication vars in route processing

I am writing a web API using Compojure with basic-authentication middleware. The basic authentication part looks something like:

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

id and pass are passed in using the id:pass@website technique. My problem is, I would like to access this id and pass further on down where the routes are processed with the various GET, PUT, POST, etc. headings. But I can't figure out how to do this, and haven't had any luck googling around.

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

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

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

like image 816
yadda Avatar asked Mar 25 '26 14:03

yadda


1 Answers

Assuming you are talking about https://github.com/remvee/ring-basic-authentication, the authenticated? fn can return a truthly value that will be added to the request in the :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))})
     ...)
like image 171
DanLebrero Avatar answered Mar 27 '26 16:03

DanLebrero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!