refactor(activitypub): handle failure finding public key in actor keys

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2023-12-06 08:25:02 +01:00
parent 3a55baeffd
commit 5b337f952a
2 changed files with 36 additions and 19 deletions

View File

@@ -112,19 +112,11 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
},
"discoverable" => actor.visibility == :public,
"openness" => actor.openness,
"manuallyApprovesFollowers" => actor.manually_approves_followers,
"publicKey" => %{
"id" => "#{actor.url}#main-key",
"owner" => actor.url,
"publicKeyPem" =>
if(is_nil(actor.domain) and not is_nil(actor.keys),
do: Utils.pem_to_public_key_pem(actor.keys),
else: actor.keys
)
}
"manuallyApprovesFollowers" => actor.manually_approves_followers
}
actor_data
|> add_keys(actor)
|> add_endpoints(actor)
|> maybe_add_members(actor)
|> maybe_add_avatar_picture(actor)
@@ -132,6 +124,28 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
|> maybe_add_physical_address(actor)
end
@spec add_keys(map(), ActorModel.t()) :: map()
defp add_keys(actor_data, %ActorModel{} = actor) do
keys =
if is_nil(actor.domain) and not is_nil(actor.keys) do
case Utils.pem_to_public_key_pem(actor.keys) do
{:error, :no_publickey_found} ->
raise "No publickey found in private keys"
public_key when is_binary(public_key) ->
public_key
end
else
actor.keys
end
Map.put(actor_data, "publicKey", %{
"id" => "#{actor.url}#main-key",
"owner" => actor.url,
"publicKeyPem" => keys
})
end
defp add_endpoints(%{"endpoints" => endpoints} = actor_data, %ActorModel{} = actor) do
new_endpoints = %{
"members" => actor.members_url,