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

Replace oidc-client with oidc-client-ts #11

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ jobs:
timeout-minutes: 5
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup CI Environment
uses: yetanalytics/actions/setup-env@v0.0.4
uses: yetanalytics/action-setup-env@v2

- name: Cache Deps
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: |
~/.m2
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ jobs:
timeout-minutes: 5
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup CD Environment
uses: yetanalytics/actions/setup-env@v0.0.4
uses: yetanalytics/action-setup-env@v2

- name: Cache Deps
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: |
~/.m2
Expand All @@ -37,7 +37,7 @@ jobs:
run: echo version=${GITHUB_REF#refs\/tags\/v} >> $GITHUB_OUTPUT

- name: Build and deploy to Clojars
uses: yetanalytics/actions/deploy-clojars@v0.0.4
uses: yetanalytics/action-deploy-clojars@v2
with:
artifact-id: 're-oidc'
src-dirs: '["src/lib"]'
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pom.xml
.cpcache/
.rebel_readline_history
resources/public/cljs-out/
.calva/
.clj-kondo/.cache
.cljs_node_repl/
.lsp/
.DS_Store

10 changes: 5 additions & 5 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{:deps {org.clojure/clojure {:mvn/version "1.10.0"}
org.clojure/clojurescript {:mvn/version "1.10.773"}
reagent/reagent {:mvn/version "0.10.0"}
re-frame/re-frame {:mvn/version "1.3.0-rc2"}
cljsjs/oidc-client {:mvn/version "1.11.5-0"}}
{:deps {org.clojure/clojure {:mvn/version "1.10.0"}
org.clojure/clojurescript {:mvn/version "1.10.773"}
reagent/reagent {:mvn/version "0.10.0"}
re-frame/re-frame {:mvn/version "1.3.0-rc2"}
io.github.cljsjs/oidc-client-ts {:mvn/version "2.0.1-1"}}
:paths ["src/lib" "resources"]
:aliases {:dev {:extra-deps
{org.clojure/test.check {:mvn/version "1.1.1"}
Expand Down
136 changes: 70 additions & 66 deletions src/lib/com/yetanalytics/re_oidc.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns com.yetanalytics.re-oidc
(:require [cljsjs.oidc-client :refer [UserManager Log WebStorageStateStore]]
#_{:clj-kondo/ignore [:unused-referred-var]} ; For Log
(:require [cljsjs.oidc-client-ts :refer [UserManager Log WebStorageStateStore]]
[re-frame.core :as re-frame]
[clojure.spec.alpha :as s :include-macros true]
[com.yetanalytics.re-oidc.user :as user]
Expand Down Expand Up @@ -77,6 +78,17 @@
(doto (UserManager. (clj->js config))
(reg-events! lifecycle-callbacks))))

(defn- web-storage-state-store
[store]
(let [store* (case store
:local-storage
js/window.localStorage
:session-storage
js/window.sessionStorage
;; custom
store)]
(new WebStorageStateStore #js {:store store*})))

(re-frame/reg-fx
::init-fx
(fn [{:keys [config
Expand All @@ -87,26 +99,12 @@
:as init-input}]
(swap! user-manager
init!
(assoc
config
"stateStore"
(new WebStorageStateStore
#js {:store (case state-store
:local-storage
js/window.localStorage
:session-storage
js/window.sessionStorage
;; custom
state-store)})
"userStore"
(new WebStorageStateStore
#js {:store (case user-store
:local-storage
js/window.localStorage
:session-storage
js/window.sessionStorage
;; custom
user-store)}))
(assoc config
;; loadUserInfo is default true in original oidc-client lib,
;; false in new oidc-client-ts lib.
"loadUserInfo" true
"stateStore" (web-storage-state-store state-store)
"userStore" (web-storage-state-store user-store))
(select-keys init-input
[:on-user-loaded
:on-user-unloaded]))))
Expand Down Expand Up @@ -135,12 +133,9 @@
.getUser
(u/handle-promise
(cond-> (fn [?user]
(if-let [logged-in-user (and ?user
(not
(some-> ?user
.-expires_at
u/expired?))
?user)]
(if-let [logged-in-user
(and ?user
(not (some-> ?user .-expires_at u/expired?)))]
(do
(re-frame/dispatch [::user-loaded logged-in-user])
;; ensure any custom loaded callback is fired
Expand Down Expand Up @@ -168,13 +163,16 @@
(fn [{:keys [on-success
on-failure
query-string]}]
(let [on-failure (or on-failure
[::add-error ::signin-redirect-callback-fx])
um (get-user-manager)]
(-> um
(.signinRedirectCallback query-string)
(let [on-failure (or on-failure
[::add-error ::signin-redirect-callback-fx])
user-manager (get-user-manager)
;; We need a full URL, not query param string, here.
;; See: PRs #535 and #999 on oidc-client-ts.
url-string (str "http://127.0.0.1" query-string)]
(-> user-manager
(.signinRedirectCallback url-string)
(u/handle-promise on-success on-failure)
(.then #(.clearStaleState um))))))
(.then #(.clearStaleState user-manager))))))

(re-frame/reg-fx
::signout-redirect-fx
Expand All @@ -196,44 +194,50 @@
.signoutRedirectCallback
(u/handle-promise on-success on-failure)))))

(re-frame/reg-fx
::print-error-fx
(fn [js-error]
(js/console.error js-error)))

(defn add-error
"Add a thrown error to the list in the db"
[db [_ handler-id js-error]]
(update db
:errors
(fnil conj [])
(u/js-error->clj
handler-id
js-error)))
[{:keys [db]} [_ handler-id js-error]]
{:db (update db
:errors
(fnil conj [])
(u/js-error->clj
handler-id
js-error))
:fx [[::print-error-fx js-error]]})

(re-frame/reg-event-db
(re-frame/reg-event-fx
::add-error
add-error)

(defn user-loaded
"Load a user object from js into the db and set status to :loaded"
[db [_ js-user]]
(let [id-token (.-id_token js-user)
access-token (.-access_token js-user)
expires-at (.-expires_at js-user)
(let [id-token (.-id_token js-user)
access-token (.-access_token js-user)
expires-at (.-expires_at js-user)
refresh-token (.-refresh_token js-user)
token-type (.-token_type js-user)
state (.-state js-user)
token-type (.-token_type js-user)
state (.-state js-user)
session-state (.-session_state js-user)
scope (.-scope js-user)
profile (js->clj (.-profile js-user))]
scope (.-scope js-user)
profile (js->clj (.-profile js-user))]
(assoc db
::status :loaded
::user
{:id-token id-token
:access-token access-token
{:id-token id-token
:access-token access-token
:refresh-token refresh-token
:expires-at expires-at
:token-type token-type
:state state
:scope scope
:expires-at expires-at
:token-type token-type
:state state
:scope scope
:session-state session-state
:profile profile})))
:profile profile})))

(re-frame/reg-event-db
::user-loaded
Expand Down Expand Up @@ -361,26 +365,26 @@
(dissoc ::callback
::login-query-string))
:fx [[::init-fx
{:config (cond-> oidc-config
redirect-uri-absolution
u/absolve-redirect-uris)
:state-store state-store
:user-store user-store
:on-user-loaded on-user-loaded
{:config (cond-> oidc-config
redirect-uri-absolution
u/absolve-redirect-uris)
:state-store state-store
:user-store user-store
:on-user-loaded on-user-loaded
:on-user-unloaded on-user-unloaded}]
(case ?callback
:login [::signin-redirect-callback-fx
{:query-string ?qstring
:on-success on-login-success
:on-failure on-login-failure}]
:on-success on-login-success
:on-failure on-login-failure}]
:logout [::signout-redirect-callback-fx
{:on-success on-logout-success
:on-failure on-logout-failure}]
[::get-user-fx
;; We need to set the user, if present, no matter what
{:auto-login auto-login
:on-success on-get-user-success
:on-failure on-get-user-failure
{:auto-login auto-login
:on-success on-get-user-success
:on-failure on-get-user-failure
:on-user-loaded on-user-loaded}])]}))

(re-frame/reg-event-fx
Expand Down
2 changes: 1 addition & 1 deletion src/test/com/test_runner.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
[com.yetanalytics.re-oidc-test]
[figwheel.main.testing :refer [run-tests-async]]))

(defn -main [& args]
(defn -main [& _args]
(run-tests-async 5000))
12 changes: 6 additions & 6 deletions src/test/com/yetanalytics/re_oidc_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
:message "whoops!",
:handler :some-handler,
:ex-data {:type :com.yetanalytics.re-oidc-test/whoops}}]}
(add-error
{}
[nil
:some-handler
(ex-info "whoops!"
{:type ::whoops})])))))
(:db (add-error
{:db {}}
[nil
:some-handler
(ex-info "whoops!"
{:type ::whoops})]))))))

(deftest user-loaded-test
(testing "Loads the user from JS"
Expand Down
9 changes: 8 additions & 1 deletion test.cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

;; uncomment to launch tests in a headless environment
;; you will have to figure out the path to chrome on your system
:launch-js ["chromium" "--headless=new" "--disable-gpu" "--repl" :open-url]
:launch-js ["chromium"
"--headless=new"
"--disable-gpu"
"--no-sandbox"
"--disable-setuid-sandbox"
"--password-store=basic"
"--user-data-dir=/tmp/chromium"
"--repl" :open-url]
}
{:main com.test-runner}