From 5424b0558b73aea7a0d67d8448e7844e914ffa48 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Mon, 9 May 2022 14:31:51 +0100 Subject: [PATCH 1/2] feat: permalink includes graph name and password --- src/cljs/athens/electron/boot.cljs | 5 +- src/cljs/athens/router.cljs | 50 +++++++++++-------- .../athens/self_hosted/presence/views.cljs | 6 +-- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/cljs/athens/electron/boot.cljs b/src/cljs/athens/electron/boot.cljs index 4228e6e9cc..3f255537c8 100644 --- a/src/cljs/athens/electron/boot.cljs +++ b/src/cljs/athens/electron/boot.cljs @@ -14,12 +14,13 @@ [(rf/inject-cofx :local-storage :athens/persist)] (fn [{:keys [local-storage]} [_ first-boot?]] (let [boot-tx (sentry/transaction-start "boot-sequence") - id-param (router/consume-graph-param) + param-db (when-let [graph-params (router/consume-graph-params)] + (apply utils/self-hosted-db graph-params)) init-app-db (cond-> ;; Init it from local storage. (wrap-span-no-new-tx "db/init-app-db" (db/init-app-db local-storage)) ;; Select the db in id-param if there. - id-param (db-picker/add-and-select (utils/self-hosted-db id-param id-param ""))) + param-db (db-picker/add-and-select param-db)) all-dbs (db-picker/all-dbs init-app-db) selected-db (db-picker/selected-db init-app-db) default-db (utils/get-default-db) diff --git a/src/cljs/athens/router.cljs b/src/cljs/athens/router.cljs index a1e362439d..c4683d41db 100644 --- a/src/cljs/athens/router.cljs +++ b/src/cljs/athens/router.cljs @@ -253,30 +253,38 @@ ;; Permalink param processing -(def graph-param-key "graph") +(def graph-name-param-key "graph-name") +(def graph-url-param-key "graph-url") +(def graph-password-param-key "graph-password") -(defn consume-graph-param - "Removes and returns the graph-id in the current URL, if any." +(defn consume-graph-params + "Removes and returns the graph params in the current URL, if any." [] ;; Note: don't use the reitit.frontend functions here, as the router ;; it not yet initialized during boot. - (let [url (js/URL. js/window.location) - graph-id (.. url -searchParams (get graph-param-key))] - ;; Replace history with a version without the graph param. - (.. url -searchParams (delete graph-param-key)) - (js/history.replaceState js/history.state nil url) - graph-id)) - - -(defn create-url-with-graph-param + (let [window-url (js/URL. js/window.location) + name (.. window-url -searchParams (get graph-name-param-key)) + url (.. window-url -searchParams (get graph-url-param-key)) + password (.. window-url -searchParams (get graph-password-param-key))] + (when url + ;; Replace history with a version without the graph params. + (.. window-url -searchParams (delete graph-name-param-key)) + (.. window-url -searchParams (delete graph-url-param-key)) + (.. window-url -searchParams (delete graph-password-param-key)) + (js/history.replaceState js/history.state nil window-url) + [(or name url) url password]))) + + +(defn create-url-with-graph-params "Create a URL containing graph-id." - [graph-id] - (let [url (js/URL. (if electron.utils/electron? - ;; Use live web client + page route on electron. - (str "https://web.athensresearch.org/" - js/window.location.hash) - js/window.location))] - (.. url -searchParams (set graph-param-key graph-id)) - (.toString url))) - + [name url password] + (let [created-url (js/URL. (if electron.utils/electron? + ;; Use live web client + page route on electron. + (str "https://web.athensresearch.org/" + js/window.location.hash) + js/window.location))] + (.. created-url -searchParams (set graph-name-param-key name)) + (.. created-url -searchParams (set graph-url-param-key url)) + (.. created-url -searchParams (set graph-password-param-key password)) + (.toString created-url))) diff --git a/src/cljs/athens/self_hosted/presence/views.cljs b/src/cljs/athens/self_hosted/presence/views.cljs index e384ac8a86..9102b7fcd2 100644 --- a/src/cljs/athens/self_hosted/presence/views.cljs +++ b/src/cljs/athens/self_hosted/presence/views.cljs @@ -32,9 +32,9 @@ (defn copy-permalink [] - (let [selected-db @(rf/subscribe [:db-picker/selected-db]) - url (router/create-url-with-graph-param (:id selected-db))] - (.. js/navigator -clipboard (writeText url)) + (let [{:keys [name url password]} @(rf/subscribe [:db-picker/selected-db]) + created-url (router/create-url-with-graph-params name url password)] + (.. js/navigator -clipboard (writeText created-url)) (util/toast (clj->js {:status "info" :position "top-right" :title "Copied permalink to clipboard"})))) From 6351d1d53c7ff10b94eabf5e7665fd6280ae14bb Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Mon, 9 May 2022 15:18:05 +0100 Subject: [PATCH 2/2] fix: don't show plaintext password on permalink It's still just base64, just not readable at a glance. --- src/cljs/athens/router.cljs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cljs/athens/router.cljs b/src/cljs/athens/router.cljs index c4683d41db..28e9700f55 100644 --- a/src/cljs/athens/router.cljs +++ b/src/cljs/athens/router.cljs @@ -266,7 +266,7 @@ (let [window-url (js/URL. js/window.location) name (.. window-url -searchParams (get graph-name-param-key)) url (.. window-url -searchParams (get graph-url-param-key)) - password (.. window-url -searchParams (get graph-password-param-key))] + password (js/atob (.. window-url -searchParams (get graph-password-param-key)))] (when url ;; Replace history with a version without the graph params. (.. window-url -searchParams (delete graph-name-param-key)) @@ -286,5 +286,5 @@ js/window.location))] (.. created-url -searchParams (set graph-name-param-key name)) (.. created-url -searchParams (set graph-url-param-key url)) - (.. created-url -searchParams (set graph-password-param-key password)) + (.. created-url -searchParams (set graph-password-param-key (js/btoa password))) (.toString created-url)))