Skip to content

Commit

Permalink
introduce wrap-optionally-authenticate for the list handler
Browse files Browse the repository at this point in the history
  • Loading branch information
callum-oakley committed May 31, 2022
1 parent d753a48 commit 2393496
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
3 changes: 2 additions & 1 deletion drafter/resources/drafter-base-config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
18 changes: 10 additions & 8 deletions drafter/src/drafter/feature/endpoint/list.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand All @@ -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))
11 changes: 11 additions & 0 deletions drafter/src/drafter/middleware.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion drafter/test/resources/web.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 2393496

Please sign in to comment.