Skip to content

Commit

Permalink
export wrap-oauth2 to allow building custom middleware stacks and res…
Browse files Browse the repository at this point in the history
…pect the :throw-exceptions option
  • Loading branch information
DerGuteMoritz committed Sep 10, 2011
1 parent de98222 commit d54863e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/clj_oauth2/OAuth2Exception.clj
Expand Up @@ -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]
Expand Down
21 changes: 13 additions & 8 deletions src/clj_oauth2/client.clj
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion test/clj_oauth2/test/client.clj
Expand Up @@ -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}))))

Expand Down

0 comments on commit d54863e

Please sign in to comment.