🎉 Add registration domain whitelist for emails

Signed-off-by: Andrey Miskov <amiskov@gmail.com>
This commit is contained in:
Andrey Miskov 2020-03-16 18:55:44 +03:00 committed by Andrey Antukh
parent 394d238f97
commit c663d54eb8
4 changed files with 26 additions and 0 deletions

View file

@ -34,6 +34,7 @@
:smtp-enabled false
:allow-demo-users true
:registration-enabled true
:registration-domain-whitelist ""
:debug-humanize-transit true
})
@ -58,6 +59,7 @@
(s/def ::smtp-enabled ::us/boolean)
(s/def ::allow-demo-users ::us/boolean)
(s/def ::registration-enabled ::us/boolean)
(s/def ::registration-domain-whitelist ::us/string)
(s/def ::debug-humanize-transit ::us/boolean)
(s/def ::config

View file

@ -10,6 +10,7 @@
(ns uxbox.services.mutations.profile
(:require
[clojure.spec.alpha :as s]
[clojure.string :as str]
[datoteka.core :as fs]
[promesa.core :as p]
[promesa.exec :as px]
@ -214,11 +215,23 @@
(s/def ::register-profile
(s/keys :req-un [::email ::password ::fullname]))
(defn email-domain-in-whitelist?
"Returns true if email's domain is in the given whitelist or if given whitelist is an empty string."
[whitelist email]
(if (str/blank? whitelist)
true
(let [domains (str/split whitelist #",\s*")
email-domain (second (str/split email #"@"))]
(contains? (set domains) email-domain))))
(sm/defmutation ::register-profile
[params]
(when-not (:registration-enabled cfg/config)
(ex/raise :type :restriction
:code :registration-disabled))
(when-not (email-domain-in-whitelist? (:registration-domain-whitelist cfg/config) (:email params))
(ex/raise :type :validation
:code ::email-domain-is-not-allowed))
(db/with-atomic [conn db/pool]
(check-profile-existence! conn params)
(-> (register-profile conn params)