generate-keys-at-registration-time (#181)
authorfloatingghost <hannah@coffee-and-dreams.uk>
Wed, 24 Aug 2022 14:36:33 +0000 (14:36 +0000)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Wed, 24 Aug 2022 14:39:30 +0000 (15:39 +0100)
Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/181

lib/mix/pleroma.ex
lib/mix/tasks/pleroma/user.ex
lib/pleroma/user.ex
test/pleroma/user_test.exs

index f4a6bcf631ac8f05eb33abeecf8e1790279eadc8..6431f0a1c657028af41beff0e086ac54104e5477 100644 (file)
@@ -23,7 +23,15 @@ defmodule Mix.Pleroma do
     Pleroma.Config.Oban.warn()
     Pleroma.Application.limiters_setup()
     Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
-    Finch.start_link(name: MyFinch)
+
+    proxy_url = Pleroma.Config.get([:http, :proxy_url])
+    proxy = Pleroma.HTTP.AdapterHelper.format_proxy(proxy_url)
+
+    finch_config =
+      [:http, :adapter]
+      |> Pleroma.Config.get([])
+      |> Pleroma.HTTP.AdapterHelper.maybe_add_proxy_pool(proxy)
+      |> Keyword.put(:name, MyFinch)
 
     unless System.get_env("DEBUG") do
       Logger.remove_backend(:console)
@@ -45,6 +53,7 @@ defmodule Mix.Pleroma do
         Pleroma.Emoji,
         {Pleroma.Config.TransferTask, false},
         Pleroma.Web.Endpoint,
+        {Finch, finch_config},
         {Oban, oban_config},
         {Majic.Pool,
          [name: Pleroma.MajicPool, pool_size: Pleroma.Config.get([:majic_pool, :size], 2)]}
index d2d41665521744d11bcc212bcea243e32a2f1508..f420d68bbe2acc3bc11cc9df4af586b83cf7f2ea 100644 (file)
@@ -258,6 +258,25 @@ defmodule Mix.Tasks.Pleroma.User do
     end
   end
 
+  def run(["refetch_public_keys"]) do
+    start_pleroma()
+
+    Pleroma.User.Query.build(%{
+      external: true,
+      is_active: true
+    })
+    |> refetch_public_keys()
+  end
+
+  def run(["refetch_public_keys" | rest]) do
+    start_pleroma()
+
+    Pleroma.User.Query.build(%{
+      ap_id: rest
+    })
+    |> refetch_public_keys()
+  end
+
   def run(["invite" | rest]) do
     {options, [], []} =
       OptionParser.parse(rest,
@@ -519,6 +538,26 @@ defmodule Mix.Tasks.Pleroma.User do
     end
   end
 
+  defp refetch_public_keys(query) do
+    query
+    |> Pleroma.Repo.chunk_stream(50, :batches)
+    |> Stream.each(fn users ->
+      users
+      |> Enum.each(fn user ->
+        IO.puts("Re-Resolving: #{user.ap_id}")
+
+        with {:ok, user} <- Pleroma.User.fetch_by_ap_id(user.ap_id),
+             changeset <- Pleroma.User.update_changeset(user),
+             {:ok, _user} <- Pleroma.User.update_and_set_cache(changeset) do
+          :ok
+        else
+          error -> IO.puts("Could not resolve: #{user.ap_id}, #{inspect(error)}")
+        end
+      end)
+    end)
+    |> Stream.run()
+  end
+
   defp set_moderator(user, value) do
     {:ok, user} =
       user
index 2a1b5af94dad752970e084e1d63940bbfae22ec1..4383f8f536fd69036cbae00558f3fc58bd3b5bf7 100644 (file)
@@ -681,6 +681,7 @@ defmodule Pleroma.User do
     |> validate_exclusion(:nickname, Config.get([User, :restricted_nicknames]))
     |> validate_format(:nickname, local_nickname_regex())
     |> put_ap_id()
+    |> put_keys()
     |> unique_constraint(:ap_id)
     |> put_following_and_follower_and_featured_address()
   end
@@ -740,6 +741,7 @@ defmodule Pleroma.User do
     |> validate_length(:registration_reason, max: reason_limit)
     |> maybe_validate_required_email(opts[:external])
     |> put_password_hash
+    |> put_keys()
     |> put_ap_id()
     |> unique_constraint(:ap_id)
     |> put_following_and_follower_and_featured_address()
@@ -755,6 +757,11 @@ defmodule Pleroma.User do
     end
   end
 
+  def put_keys(changeset) do
+    {:ok, pem} = Keys.generate_rsa_pem()
+    put_change(changeset, :keys, pem)
+  end
+
   def put_ap_id(changeset) do
     ap_id = ap_id(%User{nickname: get_field(changeset, :nickname)})
     put_change(changeset, :ap_id, ap_id)
index 67136e95b45dbcb80507b2383d2eacd0206e121c..645622e43a7430f8ce219b1ce9f64b1b368b6af1 100644 (file)
@@ -620,13 +620,14 @@ defmodule Pleroma.UserTest do
       assert changeset.valid?
     end
 
-    test "it sets the password_hash and ap_id" do
+    test "it sets the password_hash, ap_id and PEM key" do
       changeset = User.register_changeset(%User{}, @full_user_data)
 
       assert changeset.valid?
 
       assert is_binary(changeset.changes[:password_hash])
       assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
+      assert is_binary(changeset.changes[:keys])
 
       assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
     end