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

SQL LRS Testing #5

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions keycloak/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ services:
image: quay.io/keycloak/keycloak:16.1.0
environment:
KEYCLOAK_IMPORT: /tmp/test-realm.json
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: changeme123
configs:
- source: test_realm
target: /tmp/test-realm.json
Expand Down
10 changes: 5 additions & 5 deletions resources/public/oidc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"authority": "http://0.0.0.0:8080/auth/realms/test",
"client_id": "testapp_public",
"redirect_uri": "http://localhost:9500/login-callback",
"authority": "http://0.0.0.0:8081/auth/realms/test",
"client_id": "lrs_admin_ui",
"redirect_uri": "http://localhost:9500",
"response_type": "code",
"post_logout_redirect_uri": "http://localhost:9500/logout-callback",
"scope": "openid profile",
"post_logout_redirect_uri": "http://localhost:9500",
"scope": "openid profile lrs:all lrs:admin",
"automaticSilentRenew": true,
"monitorSession": false,
"filterProtocolClaims": false
Expand Down
93 changes: 82 additions & 11 deletions src/dev/com/yetanalytics/re_oidc/demo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
(def static-config
{:auto-login false
:on-login-success #(push-state "/")
:on-logout-success #(push-state "/")})
:on-logout-success #(push-state "/")
:on-get-user-success #(println "get user success" %)
:user-store :local-storage})

;; Init the demo's DB
(re-frame/reg-event-db
Expand All @@ -52,7 +54,7 @@
;; Initialize OIDC from the remote config
[::re-oidc/init
(assoc static-config
:user-store :local-storage

;; These config options are passed directly to the OIDC client
:oidc-config
config)]]]}))
Expand Down Expand Up @@ -93,6 +95,72 @@
(.error js/console "Failed to get token echo from api server, status:" status)
{}))

;; XAPI stuff
(re-frame/reg-event-fx
::get-statements!
(fn [{{status ::re-oidc/status
:as db} :db} _]
(if (= :loaded status)
(let [{{:keys [access-token]} ::re-oidc/user} db]
{:http-xhrio {:uri "http://0.0.0.0:8080/xapi/statements"
:method :get
:headers {"X-Experience-Api-Version" "1.0.3"
"Authorization" (format "Bearer %s" access-token)}
:response-format (ajax/json-response-format
{:keywords? true})
:on-success [::recv-get-statements]
:on-failure [::fail-get-statements]}})
(do
(.error js/console "Can't call XAPI if not logged in!")
{}))))

(re-frame/reg-event-db
::recv-get-statements
(fn [db [_ statement-response]]
(assoc db ::statement-response statement-response)))

(re-frame/reg-event-fx
::fail-get-statements
(fn [ctx [_ {:keys [status]}]]
(.error js/console "Failed to get statements, status:" status)
{}))

(re-frame/reg-event-fx
::post-statement!
(fn [{{status ::re-oidc/status
:as db} :db} _]
(if (= :loaded status)
(let [{{:keys [access-token]} ::re-oidc/user} db]
{:http-xhrio {:uri "http://0.0.0.0:8080/xapi/statements"
:method :post
:headers {"Content-Type" "application/json"
"X-Experience-Api-Version" "1.0.3"
"Authorization" (format "Bearer %s" access-token)}
:format (ajax/json-request-format)
:response-format (ajax/json-response-format
{:keywords? true})
:on-success [::recv-post-statement]
:on-failure [::fail-post-statement]
:params
{"actor" {"mbox" "mailto:[email protected]"}
"verb" {"id" "https://example.com/verbs/foo"}
"object" {"id" "https://example.com/activity"}}}})
(do
(.error js/console "Can't call XAPI if not logged in!")
{}))))

(re-frame/reg-event-db
::recv-post-statement
(fn [db [_ statement-post-response]]
(assoc db ::statement-post-response statement-post-response)))

(re-frame/reg-event-fx
::fail-post-statement
(fn [ctx [_ {:keys [status]}]]
(.error js/console "Failed to post statement, status:" status)
{}))


;; Compose init events for the demo db & getting remote config
(re-frame/reg-event-fx
::init!
Expand All @@ -115,15 +183,11 @@
(defn process-callbacks!
"Detect post login/logout callbacks and issue route dispatch to re-oidc."
[& _]
(case js/window.location.pathname
"/login-callback" (re-frame/dispatch
[::re-oidc/login-callback
static-config
js/window.location.search])
"/logout-callback" (re-frame/dispatch
[::re-oidc/logout-callback
static-config])
nil))
(when-let [search (not-empty js/window.location.search)]
(re-frame/dispatch
[::re-oidc/login-callback
static-config
search])))

(defn hello-world []
[:div
Expand All @@ -141,6 +205,13 @@
[:button "Loading..."]]
:loaded
[:div
[:div
[:button {:on-click #(re-frame/dispatch [::get-statements!])}
"Get Statements"]
[:button {:on-click #(re-frame/dispatch [::post-statement!])}
"Post Statement"]
]

[:button
{:on-click #(re-frame/dispatch [::echo-token!])}
"Echo Token"]
Expand Down
32 changes: 23 additions & 9 deletions src/lib/com/yetanalytics/re_oidc.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@

(defn reg-events!
"Register event callbacks to re-frame on the OIDC UserManager"
[^UserManager user-manager]
[^UserManager user-manager
{:keys [on-user-loaded
on-user-unloaded]}]
(doto user-manager.events
(.addUserLoaded
(u/dispatch-cb [::user-loaded]))
(cond-> (u/dispatch-cb [::user-loaded])
on-user-loaded
(juxt on-user-loaded)))
(.addUserUnloaded
(u/dispatch-cb [::user-unloaded]))
(cond-> (u/dispatch-cb [::user-unloaded])
on-user-unloaded
(juxt on-user-unloaded)))
;; We set automaticSilentRenew to true and these are done for us
#_(.addAccessTokenExpiring
(u/dispatch-cb [::access-token-expiring]))
Expand All @@ -64,20 +70,21 @@
(u/dispatch-cb [::user-session-changed]))))

(defn init!
"Initialize the OIDC UserManager from config. Idempotent"
[user-manager config]
"Initialize the OIDC UserManager from config + calllbacks. Idempotent"
[user-manager config lifecycle-callbacks]
(if user-manager
user-manager
(doto (UserManager. (clj->js config))
reg-events!)))
(reg-events! lifecycle-callbacks))))

(re-frame/reg-fx
::init-fx
(fn [{:keys [config
state-store
user-store]
:or {state-store :local-storage
user-store :session-storage}}]
user-store :session-storage}
:as init-input}]
(swap! user-manager
init!
(assoc
Expand All @@ -99,7 +106,10 @@
:session-storage
js/window.sessionStorage
;; custom
user-store)})))))
user-store)}))
(select-keys init-input
[:on-user-loaded
:on-user-unloaded]))))

(defn- throw-not-initialized!
[]
Expand Down Expand Up @@ -329,6 +339,8 @@
on-logout-failure
on-get-user-success
on-get-user-failure
on-user-loaded
on-user-unloaded
state-store
user-store
redirect-uri-absolution]
Expand All @@ -347,7 +359,9 @@
redirect-uri-absolution
u/absolve-redirect-uris)
:state-store state-store
:user-store user-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
Expand Down
28 changes: 20 additions & 8 deletions src/test/com/yetanalytics/re_oidc_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@
:fx [[::re-oidc/init-fx
{:config {}
:state-store :local-storage
:user-store :session-storage}]
:user-store :session-storage
:on-user-loaded [::loaded]
:on-user-unloaded [::unloaded]}]
[::re-oidc/signin-redirect-callback-fx
{:query-string "?foo=bar",
:on-success [::success],
Expand All @@ -144,13 +146,17 @@
[nil
{:oidc-config {}
:on-login-success [::success]
:on-login-failure [::failure]}]))))
:on-login-failure [::failure]
:on-user-loaded [::loaded]
:on-user-unloaded [::unloaded]}]))))
(testing "with logout callback"
(is (= {:db {::re-oidc/status :init},
:fx [[::re-oidc/init-fx
{:config {}
:state-store :local-storage
:user-store :session-storage}]
:user-store :session-storage
:on-user-loaded [::loaded]
:on-user-unloaded [::unloaded]}]
[::re-oidc/signout-redirect-callback-fx
{:on-success [::success],
:on-failure [::failure]}]]}
Expand All @@ -159,20 +165,26 @@
[nil
{:oidc-config {}
:on-logout-success [::success]
:on-logout-failure [::failure]}]))))
:on-logout-failure [::failure]
:on-user-loaded [::loaded]
:on-user-unloaded [::unloaded]}]))))
(testing "with no callback"
(is (= {:db {::re-oidc/status :init},
:fx [[::re-oidc/init-fx
{:config {}
:state-store :local-storage
:user-store :session-storage}]
:user-store :session-storage
:on-user-loaded [::loaded]
:on-user-unloaded [::unloaded]}]
[::re-oidc/get-user-fx
{:auto-login false,
:on-success [::success],
{:auto-login false
:on-success [::success]
:on-failure [::failure]}]]}
(init
{:db {}}
[nil
{:oidc-config {}
:on-get-user-success [::success]
:on-get-user-failure [::failure]}])))))
:on-get-user-failure [::failure]
:on-user-loaded [::loaded]
:on-user-unloaded [::unloaded]}])))))