Permalink
Browse files

initial import

  • Loading branch information...
0 parents commit 12597ed4f6d99f45179f268a73d5bb4c2ce97dc6 @DerGuteMoritz committed Jun 26, 2011
Showing with 73 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +13 −0 project.clj
  3. +39 −0 src/clj_oauth2/client.clj
  4. +16 −0 test/clj_oauth2/test/client.clj
@@ -0,0 +1,5 @@
+pom.xml
+*jar
+/lib/
+/classes/
+.lein-deps-sum
@@ -0,0 +1,13 @@
+(defproject clj-oauth2 "0.0.1-SNAPSHOT"
+ :description "clj-http and ring middlewares for OAuth 2.0"
+ :dependencies [[org.clojure/clojure "1.2.1"]
+ [org.clojure/clojure-contrib "1.2.0"]
+ [clj-http "0.1.2"]]
+
+ :dev-dependencies [[org.clojure/clojure "1.2.1"]
+ [org.clojure/clojure-contrib "1.2.0"]
+ [clj-http "0.1.2"]
+ [com.stuartsierra/lazytest "1.1.2"]
+ [lein-autotest "1.2.0"]]
+
+ :repositories {"stuartsierra-releases" "http://stuartsierra.com/maven2"})
@@ -0,0 +1,39 @@
+(ns clj-oauth2.client
+ (:use [clj-http.client :only [wrap-request]]
+ [clojure.contrib.json :only [read-json]]
+ [clojure.contrib.java-utils])
+ (:require [clj-http.client :as http]
+ [clojure.string :as str])
+ (:import (java.net URL URLEncoder URLDecoder)))
+
+
+;; taken from https://github.com/marktriggs/clojure-http-client/blob/master/src/clojure/http/client.clj
+;; modified to use ASCII instead of UTF-8
+(defn url-encode
+ "Wrapper around java.net.URLEncoder returning an ASCII URL encoded
+ representation of argument, either a string or map."
+ [arg]
+ (if (map? arg)
+ (str/join \& (map #(str/join \= (map url-encode %)) arg))
+ (URLEncoder/encode (as-str arg) "ASCII")))
+
+(defn url-decode [str]
+ (URLDecoder/decode str "ASCII"))
+
+(defn form-url-decode [str]
+ (into {}
+ (map (fn [p] (vector (keyword (first p)) (second p)))
+ (map (fn [s] (map url-decode (str/split s #"=")))
+ (str/split str #"&")))))
+
+(defn parse-uri [uri]
+ (let [uri (http/parse-url uri)]
+ (assoc uri :query-params (form-url-decode (:query-string uri)))))
+
+(defn make-request [endpoint]
+ (let [uri (as-url (:authorization-uri endpoint))
+ query-str (. uri getQuery)
+ query (if query-str (form-url-decode query-str) {})
+ query (assoc query :response_type "code")]
+ ;; (. uri setQuery (url-encode query))
+ {:uri (. (URL. uri (str "?" (url-encode query))) toString)}))
@@ -0,0 +1,16 @@
+(ns clj-oauth2.test.client
+ (:use [clj-oauth2.client])
+ (:use [lazytest.describe]))
+
+(def endpoint
+ {:authorization-uri "http://example.com/auth"
+ :access-token-uri "http://example.com/token"
+ :client-id "foo"
+ :client-secret "bar"
+ :redirect-uri "http://my.host/cb"})
+
+(describe "grant-type authorization-code"
+ (given [req (make-request (assoc endpoint :grant-type 'authorization-code))
+ uri (parse-uri (:uri req))]
+ (it "constructs a uri for the authorization redirect"
+ (= (get-in uri [:query-params :response_type]) "code"))))

0 comments on commit 12597ed

Please sign in to comment.