[#923] External User registration refactoring, password randomization.
authorIvan Tashkinov <ivant.business@gmail.com>
Mon, 18 Mar 2019 15:09:53 +0000 (18:09 +0300)
committerIvan Tashkinov <ivant.business@gmail.com>
Mon, 18 Mar 2019 15:09:53 +0000 (18:09 +0300)
lib/pleroma/user.ex
lib/pleroma/web/auth/pleroma_authenticator.ex

index bd742b2fd552d4243e08529ba1c6f407b609ef0b..5582168941c110bf65b1cb2491633d1d74b5c251 100644 (file)
@@ -209,35 +209,6 @@ defmodule Pleroma.User do
     update_and_set_cache(password_update_changeset(user, data))
   end
 
-  # TODO: FIXME (WIP):
-  def external_registration_changeset(struct, params \\ %{}) do
-    info_change = User.Info.confirmation_changeset(%User.Info{}, :confirmed)
-
-    changeset =
-      struct
-      |> cast(params, [:email, :nickname, :name, :bio])
-      |> unique_constraint(:email)
-      |> unique_constraint(:nickname)
-      |> validate_exclusion(:nickname, Pleroma.Config.get([Pleroma.User, :restricted_nicknames]))
-      |> validate_format(:email, @email_regex)
-      |> validate_length(:bio, max: 1000)
-      |> put_change(:info, info_change)
-
-    if changeset.valid? do
-      nickname = changeset.changes[:nickname]
-      ap_id = (nickname && User.ap_id(%User{nickname: nickname})) || nil
-      followers = User.ap_followers(%User{nickname: ap_id})
-
-      changeset
-      |> put_change(:ap_id, ap_id)
-      |> unique_constraint(:ap_id)
-      |> put_change(:following, [followers])
-      |> put_change(:follower_address, followers)
-    else
-      changeset
-    end
-  end
-
   def register_changeset(struct, params \\ %{}, opts \\ []) do
     confirmation_status =
       if opts[:confirmed] || !Pleroma.Config.get([:instance, :account_activation_required]) do
@@ -251,7 +222,7 @@ defmodule Pleroma.User do
     changeset =
       struct
       |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
-      |> validate_required([:email, :name, :nickname, :password, :password_confirmation])
+      |> validate_required([:name, :nickname, :password, :password_confirmation])
       |> validate_confirmation(:password)
       |> unique_constraint(:email)
       |> unique_constraint(:nickname)
@@ -262,6 +233,13 @@ defmodule Pleroma.User do
       |> validate_length(:name, min: 1, max: 100)
       |> put_change(:info, info_change)
 
+    changeset =
+      if opts[:external] do
+        changeset
+      else
+        validate_required(changeset, [:email])
+      end
+
     if changeset.valid? do
       hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
       ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
index 2d4399490abe2889ce68fd45f603b530f2719691..36ecd05608d67456b0cc3cfac3dee71379a9ac6e 100644 (file)
@@ -54,20 +54,26 @@ defmodule Pleroma.Web.Auth.PleromaAuthenticator do
       # Note: generating a random numeric suffix to nickname in case this nickname is already taken
       nickname =
         if nickname && User.get_by_nickname(nickname) do
-          "#{nickname}_#{:os.system_time()}"
+          "#{nickname}#{:os.system_time()}"
         else
           nickname
         end
 
+      random_password = :crypto.strong_rand_bytes(64) |> Base.encode64()
+
       with {:ok, new_user} <-
-             User.external_registration_changeset(
+             User.register_changeset(
                %User{},
                %{
                  name: info.name,
                  bio: info.description,
                  email: email,
-                 nickname: nickname
-               }
+                 nickname: nickname,
+                 password: random_password,
+                 password_confirmation: random_password
+               },
+               external: true,
+               confirmed: true
              )
              |> Repo.insert(),
            {:ok, _} <-