diff --git a/drafter/resources/drafter-base-config.edn b/drafter/resources/drafter-base-config.edn index bfccb92d2..76355a993 100644 --- a/drafter/resources/drafter-base-config.edn +++ b/drafter/resources/drafter-base-config.edn @@ -146,7 +146,8 @@ :drafter.feature.endpoint.show/handler {:repo #ig/ref :drafter/backend} :drafter.feature.endpoint.list/handler - {:drafter/backend #ig/ref :drafter/backend} + {:drafter/backend #ig/ref :drafter/backend + :wrap-authenticate #ig/ref :drafter.middleware/wrap-authenticate} [:drafter/routes :draftset/api] {:context "/v1" :routes [[:get "/users" #ig/ref :drafter.feature.users.list/get-users-handler] diff --git a/drafter/src/drafter/feature/endpoint/list.clj b/drafter/src/drafter/feature/endpoint/list.clj index f6277ac86..d7bbf667c 100644 --- a/drafter/src/drafter/feature/endpoint/list.clj +++ b/drafter/src/drafter/feature/endpoint/list.clj @@ -3,7 +3,7 @@ [drafter.endpoint :as ep] [drafter.feature.endpoint.public :as pub] [drafter.feature.draftset.list :as dsl] - [drafter.middleware :refer [include-endpoints-param]] + [drafter.middleware :as middleware] [drafter.routes.draftsets-api :refer [parse-union-with-live-handler]] [clojure.spec.alpha :as s] [ring.util.response :as ring])) @@ -29,11 +29,13 @@ (defn list-handler ":get /endpoints" - [backend] - (include-endpoints-param - (parse-union-with-live-handler - (fn [{user :identity {:keys [include union-with-live]} :params :as request}] - (ring/response (get-endpoints backend user include union-with-live)))))) + [backend wrap-authenticate] + (middleware/wrap-optionally-authenticate wrap-authenticate + (middleware/include-endpoints-param + (parse-union-with-live-handler + (fn [{user :identity {:keys [include union-with-live]} :params :as request}] + (ring/response (get-endpoints backend user include union-with-live))))))) -(defmethod ig/init-key ::handler [_ {:keys [drafter/backend] :as opts}] - (list-handler backend)) +(defmethod ig/init-key ::handler + [_ {:keys [drafter/backend wrap-authenticate] :as opts}] + (list-handler backend wrap-authenticate)) diff --git a/drafter/src/drafter/middleware.clj b/drafter/src/drafter/middleware.clj index 3722530bb..407bf6fcb 100644 --- a/drafter/src/drafter/middleware.clj +++ b/drafter/src/drafter/middleware.clj @@ -37,6 +37,17 @@ (handler request) (wrapped request)))))) +(defn wrap-optionally-authenticate + "Attempt to authenticate the request only if it has an authorization header. + For routes which don't require authentication, but can personalise results + if a user is logged in." + [wrap-authenticate handler] + (let [wrapped (wrap-authenticate handler)] + (fn [request] + (if (http/-get-header request "authorization") + (wrapped request) + (handler request))))) + (defn wrap-authorize [wrap-authenticate required-role handler] (wrap-authenticate (fn [request] diff --git a/drafter/test/resources/web.edn b/drafter/test/resources/web.edn index be5effb7a..84e5c04f6 100644 --- a/drafter/test/resources/web.edn +++ b/drafter/test/resources/web.edn @@ -121,7 +121,8 @@ :drafter.feature.endpoint.show/handler {:repo #ig/ref :drafter/backend} :drafter.feature.endpoint.list/handler - {:drafter/backend #ig/ref :drafter/backend} + {:drafter/backend #ig/ref :drafter/backend + :wrap-authenticate #ig/ref :drafter.middleware/wrap-authenticate} [:drafter/routes :draftset/api] {:context "/v1" :routes [[:get "/users" #ig/ref :drafter.feature.users.list/get-users-handler]