Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

redirect-on-auth? can be overloaded in ::friend/auth-config #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions src/cemerick/friend/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
first
val))

(defn first-non-nil [xs]
(first (drop-while nil? xs)))

(defn original-url
[{:keys [scheme server-name server-port uri query-string headers]}]
;; If your proxy doesn't send x-forwarded-proto headers, then you'll need to
Expand Down
32 changes: 16 additions & 16 deletions src/cemerick/friend/workflows.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[cemerick.friend.util :as util]
[ring.util.request :as req])
(:use [clojure.string :only (trim)]
[cemerick.friend.util :only (gets)])
[cemerick.friend.util :refer [gets first-non-nil]])
(:import org.apache.commons.codec.binary.Base64))

(defn http-basic-deny
Expand Down Expand Up @@ -78,19 +78,19 @@
request))))

(defn interactive-form
[& {:keys [login-uri credential-fn login-failure-handler redirect-on-auth?] :as form-config
:or {redirect-on-auth? true}}]
[& {:as form-config}]
(fn [{:keys [request-method params form-params] :as request}]
(when (and (= (gets :login-uri form-config (::friend/auth-config request)) (req/path-info request))
(= :post request-method))
(let [creds {:username (username form-params params)
:password (password form-params params)}
{:keys [username password]} creds]
(if-let [user-record (and username password
((gets :credential-fn form-config (::friend/auth-config request))
(with-meta creds {::friend/workflow :interactive-form})))]
(make-auth user-record
{::friend/workflow :interactive-form
::friend/redirect-on-auth? redirect-on-auth?})
((or (gets :login-failure-handler form-config (::friend/auth-config request)) #'interactive-login-redirect)
(update-in request [::friend/auth-config] merge form-config)))))))
(let [auth-config (request ::friend/auth-config)]
(when (and (= (gets :login-uri form-config auth-config) (req/path-info request))
(= :post request-method))
(let [creds {:username (username form-params params)
:password (password form-params params)}
{:keys [username password]} creds]
(if-let [user-record (and username password
((gets :credential-fn form-config auth-config)
(with-meta creds {::friend/workflow :interactive-form})))]
(make-auth user-record
{::friend/workflow :interactive-form
::friend/redirect-on-auth? (first-non-nil [(gets :redirect-on-auth? form-config auth-config) true])})
((or (gets :login-failure-handler form-config auth-config) #'interactive-login-redirect)
(update-in request [::friend/auth-config] merge form-config))))))))
28 changes: 27 additions & 1 deletion test/test_friend/interactive_form.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,30 @@
(is (= auth {:identity "Aladdin"}))
(is (= (meta auth) {::friend/workflow :interactive-form
:type ::friend/auth
::friend/redirect-on-auth? true})))))
::friend/redirect-on-auth? true})))

(testing "redirect-on-auth? can be specified when creating the workflow"
(let [form-handler (interactive-form :login-uri login-uri
:credential-fn (constantly {:identity "Aladdin"})
:redirect-on-auth? false)
auth (form-handler (assoc (request :post login-uri)
:params {:username "irrelevant but necessary"
:password "irrelevant but necessary"}))]
(is (= false (::friend/redirect-on-auth? (meta auth))))))

(testing "redirect-on-auth? is overloaded with global configuration when not specified when creating the workflow"
(let [form-handler (interactive-form :login-uri login-uri
:credential-fn (constantly {:identity "Aladdin"}))
auth (form-handler (assoc (request :post login-uri)
:params {:username "irrelevant but necessary"
:password "irrelevant but necessary"}
::friend/auth-config {:redirect-on-auth? false}))]
(is (= false (::friend/redirect-on-auth? (meta auth))))))

(testing "redirect-on-auth? defaults to true when it's never specified"
(let [form-handler (interactive-form :login-uri login-uri
:credential-fn (constantly {:identity "Aladdin"}))
auth (form-handler (assoc (request :post login-uri)
:params {:username "irrelevant but necessary"
:password "irrelevant but necessary"}))]
(is (= true (::friend/redirect-on-auth? (meta auth))))))))