diff --git a/backend/src/app/rpc/mutations/profile.clj b/backend/src/app/rpc/mutations/profile.clj index 38a1cd182..963c36416 100644 --- a/backend/src/app/rpc/mutations/profile.clj +++ b/backend/src/app/rpc/mutations/profile.clj @@ -19,7 +19,6 @@ [app.rpc.queries.profile :as profile] [app.rpc.rlimit :as rlimit] [app.storage :as sto] - [app.util.async :as async] [app.util.services :as sv] [app.util.time :as dt] [buddy.hashers :as hashers] @@ -100,10 +99,15 @@ (sv/defmethod ::prepare-register-profile {:auth false} [{:keys [pool tokens] :as cfg} params] - (when-not (or (contains? :invitation-token params) - (contains? cf/flags :registration)) - (ex/raise :type :restriction - :code :registration-disabled)) + (when-not (contains? cf/flags :registration) + (if-not (contains? params :invitation-token) + (ex/raise :type :restriction + :code :registration-disabled) + (let [invitation (tokens :verify {:token (:invitation-token params) :iss :team-invitation})] + (when-not (= (:email params) (:member-email invitation)) + (ex/raise :type :restriction + :code :email-does-not-match-invitation + :hint "email should match the invitation"))))) (when-let [domains (cf/get :registration-domain-whitelist)] (when-not (email-domain-in-whitelist? domains (:email params)) @@ -130,6 +134,7 @@ :backend "penpot" :iss :prepared-register :exp (dt/in-future "48h")} + token (tokens :generate params)] {:token token})) @@ -150,7 +155,6 @@ [{:keys [conn tokens session] :as cfg} {:keys [token] :as params}] (let [claims (tokens :verify {:token token :iss :prepared-register}) params (merge params claims)] - (check-profile-existence! conn params) (let [is-active (or (:is-active params) @@ -159,10 +163,8 @@ (create-profile conn) (create-profile-relations conn) (decode-profile-row)) - invitation (when-let [token (:invitation-token params)] (tokens :verify {:token token :iss :team-invitation}))] - (cond ;; If invitation token comes in params, this is because the user comes from team-invitation process; ;; in this case, regenerate token and send back to the user a new invitation token (and mark current diff --git a/backend/test/app/services_profile_test.clj b/backend/test/app/services_profile_test.clj index fb0f4980e..c87e7cfa8 100644 --- a/backend/test/app/services_profile_test.clj +++ b/backend/test/app/services_profile_test.clj @@ -7,6 +7,7 @@ (ns app.services-profile-test (:require [app.common.uuid :as uuid] + [app.config :as cf] [app.db :as db] [app.rpc.mutations.profile :as profile] [app.test-helpers :as th] @@ -195,6 +196,56 @@ (t/is (nil? error)))) )) +(t/deftest prepare-and-register-with-invitation-and-disabled-registration-1 + (with-redefs [app.config/flags [:disable-registration]] + (let [tokens-fn (:app.tokens/tokens th/*system*) + itoken (tokens-fn :generate + {:iss :team-invitation + :exp (dt/in-future "48h") + :role :editor + :team-id uuid/zero + :member-email "user@example.com"}) + data {::th/type :prepare-register-profile + :invitation-token itoken + :email "user@example.com" + :password "foobar"} + + {:keys [result error] :as out} (th/mutation! data)] + (t/is (nil? error)) + (t/is (map? result)) + (t/is (string? (:token result))) + + (let [rtoken (:token result) + data {::th/type :register-profile + :token rtoken + :fullname "foobar"} + + {:keys [result error] :as out} (th/mutation! data)] + ;; (th/print-result! out) + (t/is (nil? error)) + (t/is (map? result)) + (t/is (string? (:invitation-token result))))))) + +(t/deftest prepare-and-register-with-invitation-and-disabled-registration-2 + (with-redefs [app.config/flags [:disable-registration]] + (let [tokens-fn (:app.tokens/tokens th/*system*) + itoken (tokens-fn :generate + {:iss :team-invitation + :exp (dt/in-future "48h") + :role :editor + :team-id uuid/zero + :member-email "user2@example.com"}) + + data {::th/type :prepare-register-profile + :invitation-token itoken + :email "user@example.com" + :password "foobar"} + {:keys [result error] :as out} (th/mutation! data)] + (t/is (th/ex-info? error)) + (t/is (= :restriction (th/ex-type error))) + (t/is (= :email-does-not-match-invitation (th/ex-code error)))))) + + (t/deftest prepare-register-with-registration-disabled (th/with-mocks {#'app.config/flags nil} (let [data {::th/type :prepare-register-profile diff --git a/backend/test/app/test_helpers.clj b/backend/test/app/test_helpers.clj index f51bccc86..e626f4dd1 100644 --- a/backend/test/app/test_helpers.clj +++ b/backend/test/app/test_helpers.clj @@ -313,6 +313,14 @@ [v] (instance? clojure.lang.ExceptionInfo v)) +(defn ex-type + [e] + (:type (ex-data e))) + +(defn ex-code + [e] + (:code (ex-data e))) + (defn ex-of-type? [e type] (let [data (ex-data e)]