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

Simple su implementation #72

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
14 changes: 14 additions & 0 deletions src/cemerick/friend.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [cemerick.friend.util :as util]
[clojure.set :as set])
(:use (ring.util [response :as response :only (redirect)])
(ring.middleware [session :as session :only (wrap-session)])
[slingshot.slingshot :only (throw+ try+)]
[clojure.core.incubator :only (-?>)])
(:refer-clojure :exclude (identity)))
Expand Down Expand Up @@ -122,6 +123,19 @@ any funny-business related to the dynamic binding of `*identity*`."
identity-or-ring-map)]
(-> identity :authentications (get (:current identity))))))

(defn switch-user
"Simply updates current authentication. Receives a ring
handler and a function that returns a user when it receives the
request.

This function assumes that you somehow already have several
authentications."
[handler current-auth-fn]
(fn [request]
(if-let [response ((wrap-session handler) request)]
(assoc-in response [:session ::identity :current]
(current-auth-fn request)))))

(def ^{:doc "Returns true only if the provided request/response has no identity.
Equivalent to (complement current-authentication)."}
anonymous? (complement current-authentication))
Expand Down
13 changes: 12 additions & 1 deletion test/test_friend/functional.clj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@
(is (= (page-bodies "/")) (http/get (url "/logout")))
(is (= (page-bodies "/login") (:body (http/get (url "/admin")))))))

; su
(deftest alternate-users
(binding [clj-http.core/*cookie-store* (clj-http.cookies/cookie-store)]
(http/post (url "/login") {:form-params {:username "root" :password "admin_password"}})
(is (= "root" (:body (http/get (url "/echo-identity")))))
(http/post (url "/login") {:form-params {:username "jane" :password "user_password"}})
(is (= "jane" (:body (http/get (url "/echo-identity")))))
(http/post (url "/switch-user") {:form-params {:user "root"}})
(is (= "root" (:body (http/get (url "/echo-identity")))))
(http/post (url "/switch-user") {:form-params {:user "jane"}})
(is (= "jane" (:body (http/get (url "/echo-identity")))))))

;;;; TODO
; requires-scheme
; su
9 changes: 9 additions & 0 deletions test/test_friend/mock_app.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,20 @@
(GET "/login" request (page-bodies (:uri request)))
(GET "/free-api" request (api-call 99))
(friend/logout (ANY "/logout" request (resp/redirect "/")))
(friend/switch-user
(POST "/switch-user" request (resp/response "User switched"))
#(-> % :params :user))

(GET "/echo-roles" request (friend/authenticated
(-> (friend/current-authentication request)
(select-keys [:roles])
json-response)))

(GET "/echo-identity" request (-> request
:session
:cemerick.friend/identity
:current
resp/response))

;;;;; session integrity
(GET "/session-value" request
Expand Down