From d54863e79bc5396c97aaa9412fa54b96ca3801b0 Mon Sep 17 00:00:00 2001 From: Moritz Heidkamp Date: Sat, 10 Sep 2011 17:37:46 +0200 Subject: [PATCH] export wrap-oauth2 to allow building custom middleware stacks and respect the :throw-exceptions option --- src/clj_oauth2/OAuth2Exception.clj | 10 +++++++--- src/clj_oauth2/client.clj | 21 +++++++++++++-------- test/clj_oauth2/test/client.clj | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/clj_oauth2/OAuth2Exception.clj b/src/clj_oauth2/OAuth2Exception.clj index bc4946f..1d2bd98 100644 --- a/src/clj_oauth2/OAuth2Exception.clj +++ b/src/clj_oauth2/OAuth2Exception.clj @@ -4,10 +4,14 @@ :implements [clojure.lang.IDeref] :init init :state state - :constructors {[String String] [String]})) + :constructors {[String String] [String] + [String] [String]})) -(defn -init [message type] - [[message] [message type]]) +(defn -init + ([message type] + [[message] [message type]]) + ([message] + [[message] [message false]])) (defn -deref [this] diff --git a/src/clj_oauth2/client.clj b/src/clj_oauth2/client.clj index c2e4b2e..6b137a9 100644 --- a/src/clj_oauth2/client.clj +++ b/src/clj_oauth2/client.clj @@ -71,14 +71,19 @@ (request-access-token endpoint code))) -(defn request [{:keys [oauth2] :as req}] - (let [{:keys [access-token query-param]} oauth2] - (when-not query-param - (raise :type :argument-error - :message ":query-param missing")) - (http/request (assoc-in req - [:query-params query-param] - access-token)))) +(defn wrap-oauth2 [client] + (fn [{:keys [oauth2] :as req}] + (let [{:keys [access-token query-param throw-exceptions]} oauth2] + (if (and query-param access-token) + (client (assoc-in (dissoc req :query-param :access-token) + [:query-params query-param] + access-token)) + (if throw-exceptions + (throw (OAuth2Exception. "Missing :oauth2 params")) + (client req)))))) + +(defn request [req] + ((wrap-oauth2 http/request) req)) (defmacro def-request-shortcut-fn [method] (let [method-key (keyword method)] diff --git a/test/clj_oauth2/test/client.clj b/test/clj_oauth2/test/client.clj index e7a379d..bc3beb7 100644 --- a/test/clj_oauth2/test/client.clj +++ b/test/clj_oauth2/test/client.clj @@ -176,7 +176,7 @@ (it "should deny access to protected resource given an invalid access token" (= "nope" (:body (request {:method :get - :oauth2 {:query-param :access_token} + :oauth2 (assoc access-token :access-token "nope") :url "http://localhost:18080/some-resource" :throw-exceptions false}))))