Skip to content

Commit

Permalink
Minor refactoring for better interactive REPL behaviour
Browse files Browse the repository at this point in the history
Separate the defmethods from the implementations so that re-evaluating
middleware function forms does not require a reset.

This makes it easier to drop spy's etc into the code and do debugging.
  • Loading branch information
RickMoynihan committed Nov 17, 2021
1 parent 11fbe37 commit b2cb7a0
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/swirrl/auth0/middleware.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,36 @@
(assoc :swirrl.auth0/id-token token)))
request))

(defmethod ig/init-key :swirrl.auth0.middleware/bearer-token
[_ {{:keys [aud iss leeway] :or {leeway 0}} :auth0 :keys [auth0 jwk] :as opts}]
(fn [handler]
(fn [request]
(let [token (parse-header request "Bearer")]
(handler (access-token-request request token jwk iss aud leeway))))))
(defn wrap-bearer-token [handler {{:keys [aud iss leeway] :or {leeway 0}} :auth0 :keys [auth0 jwk] :as opts}]
(fn [request]
(let [token (parse-header request "Bearer")]
(handler (access-token-request request token jwk iss aud leeway)))))

(defmethod ig/init-key :swirrl.auth0.middleware/session-token
[_ {{:keys [iss aud leeway client-id] :or {leeway 0}} :auth0 :keys [jwk]}]
(fn [handler]
(fn [{{{:keys [access_token id_token]} :auth0} :session :as request}]
(-> request
(access-token-request access_token jwk iss aud leeway)
(id-token-request id_token jwk iss client-id leeway)
(handler)))))
(defmethod ig/init-key :swirrl.auth0.middleware/bearer-token [_ opts]
#(wrap-bearer-token % opts))

(defn wrap-session-token [handler {{:keys [iss aud leeway client-id] :or {leeway 0}} :auth0 :keys [jwk]}]
(fn [{{{:keys [access_token id_token]} :auth0} :session :as request}]
(-> request
(access-token-request access_token jwk iss aud leeway)
(id-token-request id_token jwk iss client-id leeway)
(handler))))

(defmethod ig/init-key :swirrl.auth0.middleware/session-token [_ opts]
#(wrap-session-token % opts))

(defn wrap-normalize-roles [handler {:keys [role-reader] :as opts}]
(fn [request]
(handler (update request :swirrl.auth0/access-token normalize-roles role-reader))))

(defmethod ig/init-key :swirrl.auth0.middleware/normalize-roles
[_ {:keys [role-reader] :as opts}]
(fn [handler]
(fn [request]
(handler (update request :swirrl.auth0/access-token normalize-roles role-reader)))))
[_ opts]
#(wrap-normalize-roles % opts))

(defn wrap-dev-token [handler opts]
(fn [request]
(handler (merge request opts))))

(defmethod ig/init-key :swirrl.auth0.middleware/dev-token
[_ opts]
(fn [handler]
(fn [request]
(handler (merge request opts)))))
#(wrap-dev-token % opts))

0 comments on commit b2cb7a0

Please sign in to comment.