diff --git a/src/active/clojure/openid.clj b/src/active/clojure/openid.clj index 39c43a2..3c058c8 100644 --- a/src/active/clojure/openid.clj +++ b/src/active/clojure/openid.clj @@ -80,7 +80,7 @@ (def access-token-lens (access-token-projection-lens :token :type :refresh-token :id-token - (lens/>> :expires (lens/xmap time-coerce/to-long time-coerce/from-long)) + (lens/>> :expires (lens/xmap time-coerce/from-long time-coerce/to-long)) :extra-data)) (define-record-type @@ -417,8 +417,37 @@ (def ^{:doc "The keyword the session lives in the in the request/response map."} state-session :session) (def ^{:doc "The keyword the authentication-state lives in the session map."} state-auth-state ::auth-state) +(defn- auth-state-edn + ;; possible states are: Authenticated, AuthenticationStartet, Unauthenticated and nil. + ([edn] + (cond + (= [::unauthenticated] edn) + (unauthenticated) + + (and (vector? edn) (= ::authenticated (first edn))) + (authenticated (:user-info (second edn))) + + (and (vector? edn) (= ::authentication-started (first edn))) + (authentication-started (:state-profile-map (second edn)) + (:original-uri (second edn))) + + :else nil)) + ([_ auth-state] + (cond + (unauthenticated? auth-state) + [::unauthenticated] + + (authenticated? auth-state) + [::authenticated {:user-info (authenticated-user-info auth-state)}] + + (authentication-started? auth-state) + [::authentication-started {:state-profile-map (authentication-started-state-profile-map auth-state) + :original-uri (authentication-started-original-uri auth-state)}] + + :else nil))) + (def state - (lens/>> state-session state-auth-state)) + (lens/>> state-session state-auth-state auth-state-edn)) (defn authenticated-request? [request] diff --git a/test/active/clojure/openid_test.clj b/test/active/clojure/openid_test.clj index fa145c9..e41cebe 100644 --- a/test/active/clojure/openid_test.clj +++ b/test/active/clojure/openid_test.clj @@ -2,3 +2,20 @@ (:require [active.clojure.openid :as openid] [clojure.test :as t])) +(t/deftest maybe-user-info-from-request + (let [req (fn [auth-state-edn] + {:session {:active.clojure.openid/auth-state auth-state-edn}})] + (t/testing "unauthenticated state" + (t/is (nil? (openid/maybe-user-info-from-request (req nil)))) + (t/is (nil? (openid/maybe-user-info-from-request (req [:active.clojure.openid/unauthenticated]))))) + + (t/testing "authenticated state" + (t/is (= "Charly" + (openid/user-info-name (openid/maybe-user-info-from-request (req [:active.clojure.openid/authenticated + {:user-info {:name "Charly"}}])))))) + + (t/testing "auth started state" + (t/is (nil? (openid/maybe-user-info-from-request (req [:active.clojure.openid/authentication-started + ;; Not sure what a profile-map looks like + {:state-profile-map {:foo :bar} + :original-uri "http://invalid.invalid/"}])))))))