0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-12 07:41:43 -05:00

🐛 Properly handle share-tokens on viewer.

This commit is contained in:
Andrey Antukh 2020-11-13 11:23:09 +01:00 committed by Alonso Torres
parent 51f9dfbc4f
commit 388d255243
5 changed files with 38 additions and 32 deletions

View file

@ -16,6 +16,12 @@
(fn [err & rest] (fn [err & rest]
(:type (ex-data err)))) (:type (ex-data err))))
(defmethod handle-exception :authorization
[err req]
{:status 403
:body (ex-data err)})
(defmethod handle-exception :validation (defmethod handle-exception :validation
[err req] [err req]
(let [header (get-in req [:headers "accept"]) (let [header (get-in req [:headers "accept"])

View file

@ -35,40 +35,39 @@
(s/def ::id ::us/uuid) (s/def ::id ::us/uuid)
(s/def ::file-id ::us/uuid) (s/def ::file-id ::us/uuid)
(s/def ::page-id ::us/uuid) (s/def ::page-id ::us/uuid)
(s/def ::share-token ::us/string) (s/def ::token ::us/string)
(s/def ::viewer-bundle (s/def ::viewer-bundle
(s/keys :req-un [::file-id ::page-id] (s/keys :req-un [::file-id ::page-id]
:opt-un [::profile-id ::share-token])) :opt-un [::profile-id ::token]))
(sq/defquery ::viewer-bundle (sq/defquery ::viewer-bundle
[{:keys [profile-id file-id page-id share-token] :as params}] [{:keys [profile-id file-id page-id token] :as params}]
(db/with-atomic [conn db/pool] (db/with-atomic [conn db/pool]
(let [file (files/retrieve-file conn file-id) (let [file (files/retrieve-file conn file-id)
project (retrieve-project conn (:project-id file)) project (retrieve-project conn (:project-id file))
page (get-in file [:data :pages-index page-id]) page (get-in file [:data :pages-index page-id])
file (merge (dissoc file :data)
file-library (select-keys (:data file) [:colors :media :typographies]) (select-keys (:data file) [:colors :media :typographies]))
bundle {:file (-> (dissoc file :data) libs (files/retrieve-file-libraries conn false file-id)
(merge file-library)) bundle {:file file
:page (get-in file [:data :pages-index page-id]) :page page
:project project} :project project
] :libraries libs}]
(if (string? share-token) (if (string? token)
(do (do
(check-shared-token! conn file-id page-id share-token) (check-shared-token! conn file-id page-id token)
(assoc bundle :share-token share-token)) (assoc bundle :token token))
(let [token (retrieve-shared-token conn file-id page-id)] (let [stoken (retrieve-shared-token conn file-id page-id)]
(files/check-edition-permissions! conn profile-id file-id) (files/check-read-permissions! conn profile-id file-id)
(assoc bundle :share-token token)))))) (assoc bundle :share-token (:token stoken)))))))
(defn check-shared-token! (defn check-shared-token!
[conn file-id page-id token] [conn file-id page-id token]
(let [sql "select exists(select 1 from file_share_token where file_id=? and page_id=? and token=?) as exists"] (let [sql "select exists(select 1 from file_share_token where file_id=? and page_id=? and token=?) as exists"]
(when-not (:exists (db/exec-one! conn [sql file-id page-id token])) (when-not (:exists (db/exec-one! conn [sql file-id page-id token]))
(ex/raise :type :validation (ex/raise :type :authorization
:code :not-authorized)))) :code :unauthorized-token))))
(defn retrieve-shared-token (defn retrieve-shared-token
[conn file-id page-id] [conn file-id page-id]

View file

@ -120,7 +120,8 @@
(reset! storage {}) (reset! storage {})
(i18n/set-default-locale!)))) (i18n/set-default-locale!))))
(def logout (defn logout
[]
(ptk/reify ::logout (ptk/reify ::logout
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]

View file

@ -71,13 +71,10 @@
(watch [_ state stream] (watch [_ state stream]
(let [params (cond-> {:page-id page-id (let [params (cond-> {:page-id page-id
:file-id file-id} :file-id file-id}
(string? token) (assoc :share-token token))] (string? token) (assoc :token token))]
(->> (rx/zip (rp/query :viewer-bundle params) (->> (rp/query :viewer-bundle params)
(rp/query :file-libraries {:file-id file-id}))
(rx/first) (rx/first)
(rx/map #(apply bundle-fetched %)) (rx/map bundle-fetched))))))
#_(rx/catch (fn [error-data]
(rx/of (rt/nav :not-found)))))))))
(defn- extract-frames (defn- extract-frames
[objects] [objects]
@ -89,7 +86,7 @@
(vec)))) (vec))))
(defn bundle-fetched (defn bundle-fetched
[{:keys [project file page share-token] :as bundle} libraries] [{:keys [project file page share-token token libraries] :as bundle}]
(us/verify ::bundle bundle) (us/verify ::bundle bundle)
(ptk/reify ::file-fetched (ptk/reify ::file-fetched
ptk/UpdateEvent ptk/UpdateEvent
@ -103,6 +100,7 @@
:file file :file file
:page page :page page
:frames frames :frames frames
:token token
:share-token share-token})))))) :share-token share-token}))))))
(def create-share-link (def create-share-link
@ -244,12 +242,12 @@
(let [page-id (get-in state [:viewer-local :page-id]) (let [page-id (get-in state [:viewer-local :page-id])
file-id (get-in state [:viewer-local :file-id]) file-id (get-in state [:viewer-local :file-id])
frames (get-in state [:viewer-data :frames]) frames (get-in state [:viewer-data :frames])
share-token (get-in state [:viewer-data :share-token]) token (get-in state [:viewer-data :token])
index (d/index-of-pred frames #(= (:id %) frame-id))] index (d/index-of-pred frames #(= (:id %) frame-id))]
(rx/of (rt/nav :viewer (rx/of (rt/nav :viewer
{:page-id page-id {:page-id page-id
:file-id file-id} :file-id file-id}
{:token share-token {:token token
:index index})))))) :index index}))))))
(defn set-current-frame [frame-id] (defn set-current-frame [frame-id]

View file

@ -178,7 +178,7 @@
(defmethod ptk/handle-error :validation (defmethod ptk/handle-error :validation
[error] [error]
(ts/schedule (ts/schedule
(st/emitf (dm/show {:content "Unexpected validation error." (st/emitf (dm/show {:content "Unexpected validation error (server side)."
:type :error :type :error
:timeout 5000}))) :timeout 5000})))
(when-let [explain (:explain error)] (when-let [explain (:explain error)]
@ -190,11 +190,13 @@
(defmethod ptk/handle-error :authentication (defmethod ptk/handle-error :authentication
[error] [error]
(ts/schedule 0 #(st/emit! logout))) (ts/schedule 0 #(st/emit! (logout))))
(defmethod ptk/handle-error :authorization (defmethod ptk/handle-error :authorization
[error] [error]
(ts/schedule 0 #(st/emit! logout))) (ts/schedule
(st/emitf (dm/show {:content "Not authorized to see this content."
:type :error}))))
(defmethod ptk/handle-error :assertion (defmethod ptk/handle-error :assertion
[{:keys [data stack message context] :as error}] [{:keys [data stack message context] :as error}]