authorization

This commit is contained in:
Tienson Qin
2026-01-09 03:01:10 +08:00
parent 582b741b69
commit b9d6f472c3
13 changed files with 340 additions and 176 deletions

View File

@@ -232,7 +232,7 @@
(p/catch (fs/unlink! repo file-path {}) (constantly nil))))
(defn new-task--rtc-upload-asset
[repo aes-key asset-block-uuid-str asset-type checksum put-url]
[repo aes-key asset-block-uuid-str asset-type checksum put-url & {:keys [extra-headers]}]
(assert (and asset-type checksum))
(m/sp
(let [asset-file (try (c.m/<? (<read-asset repo asset-block-uuid-str asset-type))
@@ -244,8 +244,10 @@
(ldb/write-transit-str
(c.m/<? (crypt/<encrypt-uint8array aes-key asset-file))))
*progress-flow (atom nil)
http-task (http/put put-url {:headers {"x-amz-meta-checksum" checksum
"x-amz-meta-type" asset-type}
headers (merge extra-headers
{"x-amz-meta-checksum" checksum
"x-amz-meta-type" asset-type})
http-task (http/put put-url {:headers headers
:body asset-file*
:with-credentials? false
:*progress-flow *progress-flow})]
@@ -262,11 +264,12 @@
{:type :rtc.exception/upload-asset-failed :data (dissoc r :body)})))))))
(defn new-task--rtc-download-asset
[repo aes-key asset-block-uuid-str asset-type get-url]
[repo aes-key asset-block-uuid-str asset-type get-url & {:keys [extra-headers]}]
(m/sp
(let [*progress-flow (atom nil)
http-task (http/get get-url {:with-credentials? false
:response-type :array-buffer
:headers extra-headers
:*progress-flow *progress-flow})
progress-canceler
(c.m/run-task :download-asset-progress
@@ -290,7 +293,7 @@
(catch js/SyntaxError _
body)
(catch :default e
;; if decrypt failed, write origin-body
;; if decrypt failed, write origin-body
(if (= "decrypt-uint8array" (ex-message e))
body
(throw e)))))]
@@ -313,16 +316,16 @@
(<get-asset-file-metadata repo asset-block-id asset-type))
(def-thread-api :thread-api/rtc-upload-asset
[repo exported-aes-key asset-block-uuid-str asset-type checksum put-url]
[repo exported-aes-key asset-block-uuid-str asset-type checksum put-url & {:as opts}]
(m/sp
(let [aes-key (when exported-aes-key (c.m/<? (crypt/<import-aes-key exported-aes-key)))]
(m/? (new-task--rtc-upload-asset repo aes-key asset-block-uuid-str asset-type checksum put-url)))))
(m/? (new-task--rtc-upload-asset repo aes-key asset-block-uuid-str asset-type checksum put-url opts)))))
(def-thread-api :thread-api/rtc-download-asset
[repo exported-aes-key asset-block-uuid-str asset-type get-url]
[repo exported-aes-key asset-block-uuid-str asset-type get-url & {:as opts}]
(m/sp
(let [aes-key (when exported-aes-key (c.m/<? (crypt/<import-aes-key exported-aes-key)))]
(m/? (new-task--rtc-download-asset repo aes-key asset-block-uuid-str asset-type get-url)))))
(m/? (new-task--rtc-download-asset repo aes-key asset-block-uuid-str asset-type get-url opts)))))
(comment
;; read asset

View File

@@ -4,6 +4,7 @@
[frontend.config :as config]
[frontend.db :as db]
[frontend.handler.repo :as repo-handler]
[frontend.handler.user :as user-handler]
[frontend.state :as state]
[lambdaisland.glogi :as log]
[logseq.db :as ldb]
@@ -29,9 +30,18 @@
(def ^:private snapshot-rows-limit 2000)
(defn- auth-headers []
(when-let [token (state/get-auth-id-token)]
{"authorization" (str "Bearer " token)}))
(defn- with-auth-headers [opts]
(if-let [auth (auth-headers)]
(assoc opts :headers (merge (or (:headers opts) {}) auth))
opts))
(defn- fetch-json
[url opts]
(p/let [resp (js/fetch url (clj->js opts))
(p/let [resp (js/fetch url (clj->js (with-auth-headers opts)))
text (.text resp)
data (when (seq text) (js/JSON.parse text))]
(if (.-ok resp)
@@ -60,7 +70,8 @@
(let [schema-version (some-> (ldb/get-graph-schema-version (db/get-db)) :major str)
base (http-base)]
(if base
(p/let [result (fetch-json (str base "/graphs")
(p/let [_ (js/Promise. user-handler/task--ensure-id&access-token)
result (fetch-json (str base "/graphs")
{:method "POST"
:headers {"content-type" "application/json"}
:body (js/JSON.stringify
@@ -83,7 +94,8 @@
[graph-uuid _schema-version]
(let [base (http-base)]
(if (and graph-uuid base)
(fetch-json (str base "/graphs/" graph-uuid) {:method "DELETE"})
(p/let [_ (js/Promise. user-handler/task--ensure-id&access-token)]
(fetch-json (str base "/graphs/" graph-uuid) {:method "DELETE"}))
(p/rejected (ex-info "worker-sync missing graph id"
{:type :worker-sync/invalid-graph
:graph-uuid graph-uuid
@@ -94,7 +106,8 @@
(state/set-state! :rtc/downloading-graph-uuid graph-uuid)
(let [base (http-base)]
(-> (if (and graph-uuid base)
(p/let [graph (str config/db-version-prefix graph-name)]
(p/let [_ (js/Promise. user-handler/task--ensure-id&access-token)
graph (str config/db-version-prefix graph-name)]
(p/loop [after -1 ; root addr is 0
first-batch? true]
(p/let [resp (fetch-json (str base "/sync/" graph-uuid "/snapshot/rows"
@@ -125,6 +138,7 @@
(if-not base
(p/resolved [])
(-> (p/let [_ (state/set-state! :rtc/loading-graphs? true)
_ (js/Promise. user-handler/task--ensure-id&access-token)
resp (fetch-json (str base "/graphs") {:method "GET"})
graphs (js->clj (aget resp "graphs") :keywordize-keys true)
result (mapv (fn [graph]