Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ classes
*.jar
pom.xml

.lein-deps-sum
8 changes: 4 additions & 4 deletions project.clj
Original file line number Diff line number Diff line change
@@ -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")

78 changes: 35 additions & 43 deletions src/oauth/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions test/oauth/signature_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")))