♻️ Refactor auth code

This commit is contained in:
Andrey Antukh 2022-06-30 15:11:41 +02:00
parent d021ac0226
commit 14d1cb90bd
30 changed files with 1306 additions and 960 deletions

View file

@ -80,10 +80,6 @@
(def default-theme "default")
(def default-language "en")
(def google-client-id (obj/get global "penpotGoogleClientID" nil))
(def gitlab-client-id (obj/get global "penpotGitlabClientID" nil))
(def github-client-id (obj/get global "penpotGithubClientID" nil))
(def oidc-client-id (obj/get global "penpotOIDCClientID" nil))
(def worker-uri (obj/get global "penpotWorkerURI" "/js/worker.js"))
(def translations (obj/get global "penpotTranslations"))
(def themes (obj/get global "penpotThemes"))
@ -100,14 +96,6 @@
(def terms-of-service-uri (obj/get global "penpotTermsOfServiceURI" nil))
(def privacy-policy-uri (obj/get global "penpotPrivacyPolicyURI" nil))
;; maintain for backward compatibility
(let [login-with-ldap (obj/get global "penpotLoginWithLDAP" false)
registration (obj/get global "penpotRegistrationEnabled" true)]
(when login-with-ldap
(swap! flags conj :login-with-ldap))
(when (false? registration)
(swap! flags disj :registration)))
(defn get-public-uri
[]
(let [uri (u/uri (or (obj/get global "penpotPublicURI")

View file

@ -145,7 +145,7 @@
ptk/WatchEvent
(watch [_ _ _]
(when (= status "ended")
(->> (rp/query! :exporter {:cmd :get-resource :blob? true :id resource-id})
(->> (rp/command! :export {:cmd :get-resource :blob? true :id resource-id})
(rx/delay 500)
(rx/map #(dom/trigger-download filename %)))))))
@ -165,9 +165,9 @@
:wait true}]
(rx/concat
(rx/of ::dwp/force-persist)
(->> (rp/query! :exporter params)
(->> (rp/command! :export params)
(rx/mapcat (fn [{:keys [id filename]}]
(->> (rp/query! :exporter {:cmd :get-resource :blob? true :id id})
(->> (rp/command! :export {:cmd :get-resource :blob? true :id id})
(rx/map (fn [data]
(dom/trigger-download filename data)
(clear-export-state uuid/zero))))))
@ -213,7 +213,7 @@
;; Launch the exportation process and stores the resource id
;; locally.
(->> (rp/query! :exporter params)
(->> (rp/command! :export params)
(rx/map (fn [{:keys [id] :as resource}]
(vreset! resource-id id)
(initialize-export-status exports cmd resource))))

View file

@ -206,7 +206,7 @@
;; the returned profile is an NOT authenticated profile, we
;; proceed to logout and show an error message.
(->> (rp/mutation :login (d/without-nils params))
(->> (rp/command :login-with-password (d/without-nils params))
(rx/merge-map (fn [data]
(rx/merge
(rx/of (fetch-profile))
@ -292,7 +292,7 @@
(ptk/reify ::logout
ptk/WatchEvent
(watch [_ _ _]
(->> (rp/mutation :logout)
(->> (rp/command :logout)
(rx/delay-at-least 300)
(rx/catch (constantly (rx/of 1)))
(rx/map #(logged-out params)))))))
@ -494,7 +494,7 @@
:or {on-error rx/throw
on-success identity}} (meta data)]
(->> (rp/mutation :request-profile-recovery data)
(->> (rp/command :request-profile-recovery data)
(rx/tap on-success)
(rx/catch on-error))))))
@ -513,7 +513,7 @@
(let [{:keys [on-error on-success]
:or {on-error rx/throw
on-success identity}} (meta data)]
(->> (rp/mutation :recover-profile data)
(->> (rp/command :recover-profile data)
(rx/tap on-success)
(rx/catch on-error))))))
@ -524,7 +524,7 @@
(ptk/reify ::create-demo-profile
ptk/WatchEvent
(watch [_ _ _]
(->> (rp/mutation :create-demo-profile {})
(->> (rp/command :create-demo-profile {})
(rx/map login)))))

View file

@ -73,10 +73,22 @@
(rx/map http/conditional-decode-transit)
(rx/mapcat handle-response)))
(defn- send-command!
"A simple helper for a common case of sending and receiving transit
data to the penpot mutation api."
[id params]
(->> (http/send! {:method :post
:uri (u/join base-uri "api/rpc/command/" (name id))
:credentials "include"
:body (http/transit-data params)})
(rx/map http/conditional-decode-transit)
(rx/mapcat handle-response)))
(defn- dispatch [& args] (first args))
(defmulti query dispatch)
(defmulti mutation dispatch)
(defmulti command dispatch)
(defmethod query :default
[id params]
@ -90,6 +102,10 @@
[id params]
(send-mutation! id params))
(defmethod command :default
[id params]
(send-command! id params))
(defn query!
([id] (query id {}))
([id params] (query id params)))
@ -98,7 +114,11 @@
([id] (mutation id {}))
([id params] (mutation id params)))
(defmethod mutation :login-with-oauth
(defn command!
([id] (command id {}))
([id params] (command id params)))
(defmethod command :login-with-oidc
[_ {:keys [provider] :as params}]
(let [uri (u/join base-uri "api/auth/oauth/" (d/name provider))
params (dissoc params :provider)]
@ -109,7 +129,7 @@
(rx/map http/conditional-decode-transit)
(rx/mapcat handle-response))))
(defmethod mutation :send-feedback
(defmethod command :send-feedback
[_ params]
(->> (http/send! {:method :post
:uri (u/join base-uri "api/feedback")
@ -128,7 +148,7 @@
(rx/map http/conditional-decode-transit)
(rx/mapcat handle-response)))
(defmethod query :exporter
(defmethod command :export
[_ params]
(let [default {:wait false :blob? false}]
(send-export (merge default params))))

View file

@ -23,10 +23,11 @@
[rumext.alpha :as mf]))
(def show-alt-login-buttons?
(or cf/google-client-id
cf/gitlab-client-id
cf/github-client-id
cf/oidc-client-id))
(some (partial contains? @cf/flags)
[:login-with-google
:login-with-github
:login-with-gitlab
:login-with-oidc]))
(s/def ::email ::us/email)
(s/def ::password ::us/not-empty-string)
@ -36,19 +37,27 @@
(s/keys :req-un [::email ::password]
:opt-un [::invitation-token]))
(defn- login-with-oauth
(defn- login-with-oidc
[event provider params]
(dom/prevent-default event)
(->> (rp/mutation! :login-with-oauth (assoc params :provider provider))
(->> (rp/command! :login-with-oidc (assoc params :provider provider))
(rx/subs (fn [{:keys [redirect-uri] :as rsp}]
(.replace js/location redirect-uri)))))
(.replace js/location redirect-uri))
(fn [{:keys [type code] :as error}]
(cond
(and (= type :restriction)
(= code :provider-not-configured))
(st/emit! (dm/error (tr "errors.auth-provider-not-configured")))
:else
(st/emit! (dm/error (tr "errors.generic"))))))))
(defn- login-with-ldap
[event params]
(dom/prevent-default event)
(dom/stop-propagation event)
(let [{:keys [on-error]} (meta params)]
(->> (rp/mutation! :login-with-ldap params)
(->> (rp/command! :login-with-ldap params)
(rx/subs (fn [profile]
(if-let [token (:invitation-token profile)]
(st/emit! (rt/nav :auth-verify-token {} {:token token}))
@ -56,11 +65,15 @@
(fn [{:keys [type code] :as error}]
(cond
(and (= type :restriction)
(= code :ldap-disabled))
(= code :ldap-not-initialized))
(st/emit! (dm/error (tr "errors.ldap-disabled")))
(fn? on-error)
(on-error error)))))))
(on-error error)
:else
(st/emit! (dm/error (tr "errors.generic")))))))))
(mf/defc login-form
[{:keys [params] :as props}]
@ -134,35 +147,35 @@
(mf/defc login-buttons
[{:keys [params] :as props}]
[:div.auth-buttons
(when cf/google-client-id
(when (contains? @cf/flags :login-with-google)
[:a.btn-primary.btn-large.btn-google-auth
{:on-click #(login-with-oauth % :google params)}
{:on-click #(login-with-oidc % :google params)}
[:span.logo i/brand-google]
(tr "auth.login-with-google-submit")])
(when cf/github-client-id
(when (contains? @cf/flags :login-with-github)
[:a.btn-primary.btn-large.btn-github-auth
{:on-click #(login-with-oauth % :github params)}
{:on-click #(login-with-oidc % :github params)}
[:span.logo i/brand-github]
(tr "auth.login-with-github-submit")])
(when cf/gitlab-client-id
(when (contains? @cf/flags :login-with-gitlab)
[:a.btn-primary.btn-large.btn-gitlab-auth
{:on-click #(login-with-oauth % :gitlab params)}
{:on-click #(login-with-oidc % :gitlab params)}
[:span.logo i/brand-gitlab]
(tr "auth.login-with-gitlab-submit")])
(when cf/oidc-client-id
(when (contains? @cf/flags :login-with-oidc)
[:a.btn-primary.btn-large.btn-github-auth
{:on-click #(login-with-oauth % :oidc params)}
{:on-click #(login-with-oidc % :oidc params)}
[:span.logo i/brand-openid]
(tr "auth.login-with-oidc-submit")])])
(mf/defc login-button-oidc
[{:keys [params] :as props}]
(when cf/oidc-client-id
(when (contains? @cf/flags :login-with-oidc)
[:div.link-entry.link-oidc
[:a {:on-click #(login-with-oauth % :oidc params)}
[:a {:on-click #(login-with-oidc % :oidc params)}
(tr "auth.login-with-oidc-submit")]]))
(mf/defc login-page

View file

@ -84,7 +84,7 @@
(fn [form _event]
(reset! submitted? true)
(let [cdata (:clean-data @form)]
(->> (rp/mutation :prepare-register-profile cdata)
(->> (rp/command :prepare-register-profile cdata)
(rx/map #(merge % params))
(rx/finalize #(reset! submitted? false))
(rx/subs (partial handle-prepare-register-success form)
@ -207,7 +207,7 @@
(fn [form _event]
(reset! submitted? true)
(let [params (:clean-data @form)]
(->> (rp/mutation :register-profile params)
(->> (rp/command :register-profile params)
(rx/finalize #(reset! submitted? false))
(rx/subs (partial handle-register-success form)
(partial handle-register-error form))))))

View file

@ -55,7 +55,7 @@
(fn [form _]
(reset! loading true)
(let [data (:clean-data @form)]
(->> (rp/mutation! :send-feedback data)
(->> (rp/command! :send-feedback data)
(rx/subs on-succes on-error)))))]
[:& fm/form {:class "feedback-form"