Skip to content

Commit fd73f43

Browse files
committed
Merge remote-tracking branch 'pelle/oauth'
2 parents e2406ff + efef3c5 commit fd73f43

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ More example requests:
117117
(client/get "http://site.com/protected" {:basic-auth ["user" "pass"]})
118118
(client/get "http://site.com/protected" {:basic-auth "user:pass"})
119119

120+
;; OAuth 2
121+
(client/get "http://site.com/protected" {:oauth-token "secret-token" })
122+
120123
;; Query parameters
121124
(client/get "http://site.com/search" {:query-params {"q" "foo, bar"}})
122125

src/clj_http/client.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@
306306
(basic-auth-value basic-auth))))
307307
(client req))))
308308

309+
(defn wrap-oauth [client]
310+
(fn [req]
311+
(if-let [oauth-token (:oauth-token req)]
312+
(client (-> req (dissoc :oauth-token)
313+
(assoc-in [:headers "Authorization"]
314+
(str "Bearer " oauth-token))))
315+
(client req))))
316+
317+
309318
(defn parse-user-info [user-info]
310319
(when user-info
311320
(str/split user-info #":")))
@@ -380,6 +389,7 @@
380389
(-> request
381390
wrap-query-params
382391
wrap-basic-auth
392+
wrap-oauth
383393
wrap-user-info
384394
wrap-url
385395
wrap-redirects

test/clj_http/test/client.clj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,17 @@
279279
(is-passed client/wrap-basic-auth
280280
{:uri "/foo"}))
281281

282+
(deftest apply-on-oauth
283+
(is-applied client/wrap-oauth
284+
{:oauth-token "my-token"}
285+
{:headers {"Authorization"
286+
"Bearer my-token"}}))
287+
288+
(deftest pass-on-no-oauth
289+
(is-passed client/wrap-oauth
290+
{:uri "/foo"}))
291+
292+
282293
(deftest apply-on-method
283294
(let [m-client (client/wrap-method identity)
284295
echo (m-client {:key :val :method :post})]

0 commit comments

Comments
 (0)