diff --git a/.gitignore b/.gitignore index 8bd78af..d1a9e67 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ classes *.jar pom.xml +.lein-deps-sum diff --git a/project.clj b/project.clj index fb5e338..b649b82 100644 --- a/project.clj +++ b/project.clj @@ -1,9 +1,9 @@ -(defproject clj-oauth "1.2.10-SNAPSHOT" +(defproject org.clojars.adamwynne/clj-oauth "1.2.19" :description "OAuth support for Clojure" - :dependencies [[org.clojure/clojure "1.2.0"] - [com.twinql.clojure/clj-apache-http "2.3.1"] + :dependencies [[org.clojure/clojure "1.4.0"] + [org.clojars.adamwynne/clj-apache-http "2.3.2"] [org.apache.httpcomponents/httpclient "4.1"] [org.apache.httpcomponents/httpcore "4.1"] [org.apache.httpcomponents/httpmime "4.1"]] - :dev-dependencies [[swank-clojure "1.3.0-SNAPSHOT"]]) + :min-lein-version "2.0.0") diff --git a/src/oauth/client.clj b/src/oauth/client.clj index 42edf22..d283af5 100644 --- a/src/oauth/client.clj +++ b/src/oauth/client.clj @@ -10,13 +10,13 @@ (declare success-content authorization-header) -(defstruct #^{:doc "OAuth consumer"} consumer - :key - :secret - :request-uri - :access-uri - :authorize-uri - :signature-method) +(defrecord #^{:doc "OAuth consumer"} consumer + [key + secret + request-uri + access-uri + authorize-uri + signature-method]) (defn check-success-response [m] (let [code (:code m)] @@ -32,13 +32,12 @@ (defn make-consumer "Make a consumer struct map." [key secret request-uri access-uri authorize-uri signature-method] - (struct consumer - key - secret - request-uri - access-uri - authorize-uri - signature-method)) + (consumer. key + secret + request-uri + access-uri + authorize-uri + signature-method)) ;;; Parse form-encoded bodies from OAuth responses. (defmethod http/entity-as :urldecoded @@ -55,40 +54,33 @@ (defn request-token "Fetch request token for the consumer." - ([consumer] - (let [unsigned-params (sig/oauth-params consumer) - signature (sig/sign consumer - (sig/base-string "POST" - (:request-uri consumer) - unsigned-params)) - params (assoc unsigned-params - :oauth_signature signature)] - (success-content - (http/post (:request-uri consumer) - :headers {"Authorization" (authorization-header params)} - :parameters (http/map->params {:use-expect-continue false}) - :as :urldecoded)))) - ([consumer callback-uri] - (let [unsigned-params (assoc (sig/oauth-params consumer) - :oauth_callback callback-uri) - signature (sig/sign consumer - (sig/base-string "POST" - (:request-uri consumer) - unsigned-params)) - params (assoc unsigned-params - :oauth_signature signature)] - (success-content - (http/post (:request-uri consumer) - :headers {"Authorization" (authorization-header params)} - :parameters (http/map->params {:use-expect-continue false}) - :as :urldecoded))))) + [consumer & {:as args}] + + (let [extra-parameters (:parameters args) + query (:query args) + unsigned-params (merge (sig/oauth-params consumer) + extra-parameters) + signature (sig/sign consumer + (sig/base-string "POST" + (:request-uri consumer) + (merge unsigned-params + query))) + params (assoc unsigned-params + :oauth_signature signature)] + + (success-content + (http/post (:request-uri consumer) + :headers {"Authorization" (authorization-header params)} + :parameters (http/map->params {:use-expect-continue false}) + :query query + :as :urldecoded)))) (defn user-approval-uri "Builds the URI to the Service Provider where the User will be prompted to approve the Consumer's access to their account." - [consumer token] + [consumer token & {:as rest}] (.toString (http/resolve-uri (:authorize-uri consumer) - {:oauth_token token}))) + (merge rest {:oauth_token token})))) (defn access-token "Exchange a request token for an access token. diff --git a/test/oauth/signature_test.clj b/test/oauth/signature_test.clj index 003d4e4..f50cfda 100644 --- a/test/oauth/signature_test.clj +++ b/test/oauth/signature_test.clj @@ -242,11 +242,11 @@ url-form-encode (is (= (sig/url-form-encode {}) "")) (is (= (sig/url-form-encode {"hello" "there"}) "hello=there")) - (is (= (sig/url-form-encode {"hello" "there" "name" "Bill" }) "hello=there&name=Bill")) + (is (= (sig/url-form-encode {"hello" "there" "name" "Bill" }) "name=Bill&hello=there")) (is (= (sig/url-form-encode {:hello "there"}) "hello=there")) - (is (= (sig/url-form-encode {:hello "there" :name "Bill" }) "hello=there&name=Bill")) + (is (= (sig/url-form-encode {:hello "there" :name "Bill" }) "name=Bill&hello=there")) (is (= (sig/url-form-encode {:hello "there"}) "hello=there")) - (is (= (sig/url-form-encode {:hello "there" :name "Bill Smith" }) "hello=there&name=Bill%20Smith"))) + (is (= (sig/url-form-encode {:hello "there" :name "Bill Smith" }) "name=Bill%20Smith&hello=there")))