Skip to content

Change data stored in session to raw edn. #9

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

Open
wants to merge 2 commits into
base: main
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
33 changes: 31 additions & 2 deletions src/active/clojure/openid.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
17 changes: 17 additions & 0 deletions test/active/clojure/openid_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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/"}])))))))