🎉 Add generic oauth2/openid-connect authentication subsystem.

This commit is contained in:
Andrey Antukh 2021-04-13 08:57:08 +02:00 committed by Andrés Moya
parent 9e5923004f
commit 63b95e71a7
17 changed files with 368 additions and 620 deletions

View file

@ -69,6 +69,7 @@
(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 login-with-ldap (obj/get global "penpotLoginWithLDAP" false))
(def registration-enabled (obj/get global "penpotRegistrationEnabled" true))
(def worker-uri (obj/get global "penpotWorkerURI" "/js/worker.js"))

View file

@ -6,6 +6,7 @@
(ns app.main.repo
(:require
[app.common.data :as d]
[beicon.core :as rx]
[lambdaisland.uri :as u]
[cuerdas.core :as str]
@ -84,23 +85,10 @@
([id] (mutation id {}))
([id params] (mutation id params)))
(defmethod mutation :login-with-google
[id params]
(let [uri (u/join base-uri "api/oauth/google")]
(->> (http/send! {:method :post :uri uri :query params})
(rx/map http/conditional-decode-transit)
(rx/mapcat handle-response))))
(defmethod mutation :login-with-gitlab
[id params]
(let [uri (u/join base-uri "api/oauth/gitlab")]
(->> (http/send! {:method :post :uri uri :query params})
(rx/map http/conditional-decode-transit)
(rx/mapcat handle-response))))
(defmethod mutation :login-with-github
[id params]
(let [uri (u/join base-uri "api/oauth/github")]
(defmethod mutation :login-with-oauth
[id {:keys [provider] :as params}]
(let [uri (u/join base-uri "api/auth/oauth/" (d/name provider))
params (dissoc params :provider)]
(->> (http/send! {:method :post :uri uri :query params})
(rx/map http/conditional-decode-transit)
(rx/mapcat handle-response))))

View file

@ -29,26 +29,10 @@
(s/def ::login-form
(s/keys :req-un [::email ::password]))
(defn- login-with-google
[event params]
(defn- login-with-oauth
[event provider params]
(dom/prevent-default event)
(->> (rp/mutation! :login-with-google params)
(rx/subs (fn [{:keys [redirect-uri] :as rsp}]
(.replace js/location redirect-uri))
(fn [{:keys [type] :as error}]
(st/emit! (dm/error (tr "errors.google-auth-not-enabled")))))))
(defn- login-with-gitlab
[event params]
(dom/prevent-default event)
(->> (rp/mutation! :login-with-gitlab params)
(rx/subs (fn [{:keys [redirect-uri] :as rsp}]
(.replace js/location redirect-uri)))))
(defn- login-with-github
[event params]
(dom/prevent-default event)
(->> (rp/mutation! :login-with-github params)
(->> (rp/mutation! :login-with-oauth (assoc params :provider provider))
(rx/subs (fn [{:keys [redirect-uri] :as rsp}]
(.replace js/location redirect-uri)))))
@ -127,6 +111,33 @@
{:label (tr "auth.login-with-ldap-submit")
:on-click on-submit-ldap}])]]))
(mf/defc login-buttons
[{:keys [params] :as props}]
[:div.auth-buttons
(when cfg/google-client-id
[:a.btn-ocean.btn-large.btn-google-auth
{:on-click #(login-with-oauth % :google params)}
(tr "auth.login-with-google-submit")])
(when cfg/gitlab-client-id
[:a.btn-ocean.btn-large.btn-gitlab-auth
{:on-click #(login-with-oauth % :gitlab params)}
[:img.logo
{:src "/images/icons/brand-gitlab.svg"}]
(tr "auth.login-with-gitlab-submit")])
(when cfg/github-client-id
[:a.btn-ocean.btn-large.btn-github-auth
{:on-click #(login-with-oauth % :github params)}
[:img.logo
{:src "/images/icons/brand-github.svg"}]
(tr "auth.login-with-github-submit")])
(when cfg/oidc-client-id
[:a.btn-ocean.btn-large.btn-github-auth
{:on-click #(login-with-oauth % :oidc params)}
(tr "auth.login-with-oidc-submit")])])
(mf/defc login-page
[{:keys [params] :as props}]
[:div.generic-form.login-form
@ -149,24 +160,7 @@
:tab-index "6"}
(tr "auth.register-submit")]])]
(when cfg/google-client-id
[:a.btn-ocean.btn-large.btn-google-auth
{:on-click #(login-with-google % params)}
"Login with Google"])
(when cfg/gitlab-client-id
[:a.btn-ocean.btn-large.btn-gitlab-auth
{:on-click #(login-with-gitlab % params)}
[:img.logo
{:src "/images/icons/brand-gitlab.svg"}]
(tr "auth.login-with-gitlab-submit")])
(when cfg/github-client-id
[:a.btn-ocean.btn-large.btn-github-auth
{:on-click #(login-with-github % params)}
[:img.logo
{:src "/images/icons/brand-github.svg"}]
(tr "auth.login-with-github-submit")])
[:& login-buttons {:params params}]
(when cfg/allow-demo-users
[:div.links.demo

View file

@ -137,7 +137,6 @@
[:div.notification-text-email (:email params "")]
[:div.notification-text (tr "auth.check-your-email")]])
(mf/defc register-page
[{:keys [params] :as props}]
[:div.form-container
@ -161,24 +160,9 @@
[:span (tr "auth.create-demo-profile") " "]
[:a {:on-click #(st/emit! da/create-demo-profile)
:tab-index "5"}
(tr "auth.create-demo-account")]])]
(tr "auth.create-demo-account")]])
(when cfg/google-client-id
[:a.btn-ocean.btn-large.btn-google-auth
{:on-click #(login/login-with-google % params)}
"Login with Google"])
[:& login/login-buttons {:params params}]]])
(when cfg/gitlab-client-id
[:a.btn-ocean.btn-large.btn-gitlab-auth
{:on-click #(login/login-with-gitlab % params)}
[:img.logo
{:src "/images/icons/brand-gitlab.svg"}]
(tr "auth.login-with-gitlab-submit")])
(when cfg/github-client-id
[:a.btn-ocean.btn-large.btn-github-auth
{:on-click #(login/login-with-github % params)}
[:img.logo
{:src "/images/icons/brand-github.svg"}]
(tr "auth.login-with-github-submit")])])