Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass params submitted to launch-uri to authorization-uri #42

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 13 additions & 9 deletions src/ring/middleware/oauth2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[clj-time.core :as time]
[clojure.string :as str]
[crypto.random :as random]
[ring.middleware.params :refer [wrap-params]]
[ring.util.codec :as codec]
[ring.util.request :as req]
[ring.util.response :as resp]))
Expand All @@ -19,20 +20,23 @@
(defn- authorize-uri [profile request state]
(str (:authorize-uri profile)
(if (.contains ^String (:authorize-uri profile) "?") "&" "?")
(codec/form-encode {:response_type "code"
:client_id (:client-id profile)
:redirect_uri (redirect-uri profile request)
:scope (scopes profile)
:state state})))
(codec/form-encode
(merge (:query-params request)
{:response_type "code"
:client_id (:client-id profile)
:redirect_uri (redirect-uri profile request)
:scope (scopes profile)
:state state }))))

(defn- random-state []
(-> (random/base64 9) (str/replace "+" "-") (str/replace "/" "_")))

(defn- make-launch-handler [profile]
(fn [{:keys [session] :or {session {}} :as request}]
(let [state (random-state)]
(-> (resp/redirect (authorize-uri profile request state))
(assoc :session (assoc session ::state state))))))
(wrap-params
(fn [{:keys [session] :or {session {}} :as request}]
(let [state (random-state)]
(-> (resp/redirect (authorize-uri profile request state))
(assoc :session (assoc session ::state state)))))))

(defn- state-matches? [request]
(= (get-in request [:session ::state])
Expand Down
9 changes: 9 additions & 0 deletions test/ring/middleware/oauth2_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
location (get-in response [:headers "Location"])]
(is (.startsWith ^String location "https://example.com/oauth2/authorize?business_partner_id=XXXX&"))))

(deftest test-location-uri-with-dynamic-query
(let [profile test-profile
handler (wrap-oauth2 token-handler {:test profile})
response (handler (-> (mock/request :post "/oauth2/test?hd=tenant.com" {:hd "tenant.com"})
sbauch marked this conversation as resolved.
Show resolved Hide resolved
(mock/content-type "application/x-www-form-urlencoded")))
location (get-in response [:headers "Location"])]
(print ^String location)
(is (.startsWith ^String location "https://example.com/oauth2/authorize?hd=tenant.com&"))))

(def token-response
{:status 200
:headers {"Content-Type" "application/json"}
Expand Down