title: Put Clojure Ring middlewares in correct order
body: <p>I'm having trouble with my Clojure server middlewares. My app has the following requirements:</p>
  
  <ul>
  <li><p>Some routes should be accessible with no problems. Others require basic authentication, so I'd like to have an authentication function that sits in front of all the handler functions and makes sure the request is verified. I've been using the ring-basic-authentication handler for this, especially the instructions on <a href="https://github.com/remvee/ring-basic-authentication/wiki" rel="nofollow noreferrer">how to separate your public and private routes</a>.</p></li>
  <li><p>However, I'd also like the params sent in the <code>Authorization:</code> header to be available in the route controller. For this I've been using Compojure's <code>site</code> function in <code>compojure.handler</code>, which puts variables in the <code>:params</code> dictionary of the request (see for example <a href="https://stackoverflow.com/questions/6036733/missing-form-parameters-in-compojure-post-request">Missing form parameters in Compojure POST request</a>)</p></li>
  </ul>
  
  <p>However I can't seem to get both 401 authorization <strong>and</strong> the parameters to work at the same time. If I try this:</p>
  
  <pre><code>; this is a stripped down sample case:
  
  (defn authenticated?
    "authenticate the request"
    [service-name token]
    (:valid (model/valid-service-and-token service-name token)))
  
  (defroutes token-routes
    (POST "/api/:service-name/phone" request (add-phone request)))
  
  (defroutes public-routes
    controller/routes
    ; match anything in the static dir at resources/public
    (route/resources "/"))
  
  (defroutes authviasms-handler
    public-routes
    (auth/wrap-basic-authentication 
               controller/token-routes authenticated?))
  
  ;handler is compojure.handler
  (def application (handler/site authviasms-handler))
  
  (defn start [port]
    (ring/run-jetty (var application) {:port (or port 8000) :join? false}))
  </code></pre>
  
  <p>the authorization variables are accessible in the <code>authenticated?</code> function, but not in the routes. </p>
  
  <p>Obviously, this isn't a very general example, but I feel like I'm really spinning my wheels and just making changes at random to the middleware order and hoping things work. I'd appreciate some help both for my specific example, and learning more about how to wrap middlewares to make things execute correctly.</p>
  
  <p>Thanks,
  Kevin</p>