diff --git a/backend/src/app/rpc/commands/auth.clj b/backend/src/app/rpc/commands/auth.clj index 35326d8d6..76d41dc42 100644 --- a/backend/src/app/rpc/commands/auth.clj +++ b/backend/src/app/rpc/commands/auth.clj @@ -297,6 +297,27 @@ (assoc :default-team-id (:id team)) (assoc :default-project-id (:default-project-id team))))) +(defn send-email-verification! + [conn sprops profile] + (let [vtoken (tokens/generate sprops + {:iss :verify-email + :exp (dt/in-future "72h") + :profile-id (:id profile) + :email (:email profile)}) + ;; NOTE: this token is mainly used for possible complains + ;; identification on the sns webhook + ptoken (tokens/generate sprops + {:iss :profile-identity + :profile-id (:id profile) + :exp (dt/in-future {:days 30})})] + (eml/send! {::eml/conn conn + ::eml/factory eml/register + :public-uri (cf/get :public-uri) + :to (:email profile) + :name (:fullname profile) + :token vtoken + :extra-data ptoken}))) + (defn register-profile [{:keys [conn sprops session] :as cfg} {:keys [token] :as params}] (let [claims (tokens/verify sprops {:token token :iss :prepared-register}) @@ -342,23 +363,8 @@ ;; In all other cases, send a verification email. :else - (let [vtoken (tokens/generate sprops - {:iss :verify-email - :exp (dt/in-future "48h") - :profile-id (:id profile) - :email (:email profile)}) - ptoken (tokens/generate sprops - {:iss :profile-identity - :profile-id (:id profile) - :exp (dt/in-future {:days 30})})] - (eml/send! {::eml/conn conn - ::eml/factory eml/register - :public-uri (:public-uri cfg) - :to (:email profile) - :name (:fullname profile) - :token vtoken - :extra-data ptoken}) - + (do + (send-email-verification! conn sprops profile) (with-meta profile {::audit/replace-props (audit/profile->props profile) ::audit/profile-id (:id profile)})))))) diff --git a/backend/src/app/rpc/mutations/profile.clj b/backend/src/app/rpc/mutations/profile.clj index 95b607471..6daf3785f 100644 --- a/backend/src/app/rpc/mutations/profile.clj +++ b/backend/src/app/rpc/mutations/profile.clj @@ -169,8 +169,7 @@ params (assoc params :profile profile :email (str/lower email))] - (if (or (cf/get :smtp-enabled) - (contains? cf/flags :smtp)) + (if (contains? cf/flags :smtp) (request-email-change cfg params) (change-email-immediately cfg params))))) diff --git a/backend/src/app/srepl/main.clj b/backend/src/app/srepl/main.clj index 00a3101f9..56bb86a5f 100644 --- a/backend/src/app/srepl/main.clj +++ b/backend/src/app/srepl/main.clj @@ -10,9 +10,15 @@ (:require [app.common.logging :as l] [app.common.pprint :as p] + [app.common.spec :as us] + [app.db :as db] + [app.rpc.commands.auth :as cmd.auth] + [app.rpc.queries.profile :as profile] [app.srepl.fixes :as f] [app.srepl.helpers :as h] - [clojure.pprint :refer [pprint]])) + [app.util.time :as dt] + [clojure.pprint :refer [pprint]] + [cuerdas.core :as str])) (defn print-available-tasks [system] @@ -30,7 +36,53 @@ (defn send-test-email! [system destination] + (us/verify! + :expr (some? system) + :hint "system should be provided") + + (us/verify! + :expr (string? destination) + :hint "destination should be provided") + (let [handler (:app.emails/sendmail system)] (handler {:body "test email" :subject "test email" :to [destination]}))) + +(defn resend-email-verification-email! + [system email] + (us/verify! + :expr (some? system) + :hint "system should be provided") + + (let [sprops (:app.setup/props system) + pool (:app.db/pool system) + profile (profile/retrieve-profile-data-by-email pool email)] + + (cmd.auth/send-email-verification! pool sprops profile) + :email-sent)) + +(defn update-profile + "Update a limited set of profile attrs." + [system & {:keys [email id active? deleted?]}] + + (us/verify! + :expr (some? system) + :hint "system should be provided") + + (us/verify! + :expr (or (string? email) (uuid? id)) + :hint "email or id should be provided") + + (let [pool (:app.db/pool system) + params (cond-> {} + (true? active?) (assoc :is-active true) + (false? active?) (assoc :is-active false) + (true? deleted?) (assoc :deleted-at (dt/now))) + opts (cond-> {} + (some? email) (assoc :email (str/lower email)) + (some? id) (assoc :id id))] + + (some-> (db/update! pool :profile params opts) + (profile/decode-profile-row)))) + diff --git a/backend/test/app/services_profile_test.clj b/backend/test/app/services_profile_test.clj index e750ea6d2..984bdddc7 100644 --- a/backend/test/app/services_profile_test.clj +++ b/backend/test/app/services_profile_test.clj @@ -97,7 +97,7 @@ :profile-id (:id profile)} out (th/query! data)] - ;; (th/print-result! out) + #_(th/print-result! out) (t/is (nil? (:error out))) (let [result (:result out)] @@ -338,22 +338,21 @@ (t/deftest test-email-change-request-without-smtp - (with-mocks [email-send-mock {:target 'app.emails/send! :return nil} - cfg-get-mock {:target 'app.config/get - :return (th/mock-config-get-with - {:smtp-enabled false})}] - (let [profile (th/create-profile* 1) - pool (:app.db/pool th/*system*) - data {::th/type :request-email-change - :profile-id (:id profile) - :email "user1@example.com"}] + (with-mocks [email-send-mock {:target 'app.emails/send! :return nil}] + (with-redefs [app.config/flags #{}] + (let [profile (th/create-profile* 1) + pool (:app.db/pool th/*system*) + data {::th/type :request-email-change + :profile-id (:id profile) + :email "user1@example.com"}] - ;; without complaints - (let [out (th/mutation! data) - res (:result out)] - (t/is (= {:changed true} res)) - (let [mock (deref email-send-mock)] - (t/is (false? (:called? mock)))))))) + (let [out (th/mutation! data) + res (:result out)] + + ;; (th/print-result! out) + (t/is (= {:changed true} res)) + (let [mock (deref email-send-mock)] + (t/is (false? (:called? mock))))))))) (t/deftest test-request-profile-recovery