From 92ba2802fb0d71a0bbca676ecc3af40c0a27db53 Mon Sep 17 00:00:00 2001
From: floatingghost
Date: Wed, 24 Aug 2022 14:36:33 +0000
Subject: [PATCH] generate-keys-at-registration-time (#181)
Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/181
---
lib/mix/pleroma.ex | 11 +++++-
lib/mix/tasks/pleroma/user.ex | 39 +++++++++++++++++++
lib/pleroma/user.ex | 7 ++++
test/pleroma/user_test.exs | 3 +-
.../article_note_page_validator_test.exs | 2 +-
5 files changed, 59 insertions(+), 3 deletions(-)
diff --git a/lib/mix/pleroma.ex b/lib/mix/pleroma.ex
index f4a6bcf63..6431f0a1c 100644
--- a/lib/mix/pleroma.ex
+++ b/lib/mix/pleroma.ex
@@ -23,7 +23,15 @@ def start_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 @@ def start_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)]}
diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex
index d2d416655..f420d68bb 100644
--- a/lib/mix/tasks/pleroma/user.ex
+++ b/lib/mix/tasks/pleroma/user.ex
@@ -258,6 +258,25 @@ def run(["untag", nickname | tags]) 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 @@ def run(["fix_follow_state", local_user, remote_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
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 2a1b5af94..4383f8f53 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -681,6 +681,7 @@ def register_changeset_ldap(struct, params = %{password: password})
|> 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 @@ def register_changeset(struct, params \\ %{}, opts \\ []) 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 @@ def maybe_validate_required_email(changeset, _) 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)
diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs
index 67136e95b..645622e43 100644
--- a/test/pleroma/user_test.exs
+++ b/test/pleroma/user_test.exs
@@ -620,13 +620,14 @@ test "it blocks blacklisted email domains" 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
diff --git a/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs
index c766414a6..1d73d6765 100644
--- a/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs
+++ b/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs
@@ -115,7 +115,7 @@ test "a misskey MFM status with a content field should work and be linked", _ do
assert content =~ "@oops_not_a_mention"
assert content =~
- "mfm goes here
aaa"
+ "mfm goes here aaa"
end
test "a misskey MFM status with a _misskey_content field should work and be linked", _ do