2018-12-24 12:34:45 +00:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2018-12-27 10:24:04 +00:00
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/activity_pub/utils.ex
|
2018-12-24 12:34:45 +00:00
|
|
|
|
2020-01-22 01:14:42 +00:00
|
|
|
defmodule Mobilizon.Federation.ActivityPub.Utils do
|
2018-06-14 16:15:27 +00:00
|
|
|
@moduledoc """
|
2020-01-22 01:14:42 +00:00
|
|
|
Various ActivityPub related utils.
|
2018-06-14 16:15:27 +00:00
|
|
|
"""
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
alias Mobilizon.Actors
|
2018-10-11 15:37:39 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-12 23:35:03 +00:00
|
|
|
alias Mobilizon.Media.Picture
|
2020-01-22 01:14:42 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub.{Activity, Federator, Relay}
|
2020-01-22 21:40:40 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Mobilizon.Federation.HTTPSignatures
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2019-09-12 23:35:03 +00:00
|
|
|
require Logger
|
|
|
|
|
2019-09-04 16:24:31 +00:00
|
|
|
@actor_types ["Group", "Person", "Application"]
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
# Some implementations send the actor URI as the actor field, others send the entire actor object,
|
|
|
|
# so figure out what the actor's URI is based on what we have.
|
2019-07-30 14:40:59 +00:00
|
|
|
def get_url(%{"id" => id}), do: id
|
|
|
|
def get_url(id) when is_bitstring(id), do: id
|
2020-02-18 07:57:00 +00:00
|
|
|
def get_url(ids) when is_list(ids), do: get_url(hd(ids))
|
2019-07-30 14:40:59 +00:00
|
|
|
def get_url(_), do: nil
|
2018-11-12 08:05:31 +00:00
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
def make_json_ld_header do
|
|
|
|
%{
|
|
|
|
"@context" => [
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
2019-12-03 10:29:51 +00:00
|
|
|
"https://litepub.social/litepub/context.jsonld",
|
2018-05-17 09:32:23 +00:00
|
|
|
%{
|
2018-11-12 08:05:31 +00:00
|
|
|
"sc" => "http://schema.org#",
|
2019-12-03 10:29:51 +00:00
|
|
|
"ical" => "http://www.w3.org/2002/12/cal/ical#",
|
2019-12-16 10:46:19 +00:00
|
|
|
"pt" => "https://joinpeertube.org/ns#",
|
2018-05-17 09:32:23 +00:00
|
|
|
"Hashtag" => "as:Hashtag",
|
2018-11-12 08:05:31 +00:00
|
|
|
"category" => "sc:category",
|
2019-12-03 10:29:51 +00:00
|
|
|
"uuid" => "sc:identifier",
|
|
|
|
"maximumAttendeeCapacity" => "sc:maximumAttendeeCapacity",
|
2019-12-16 10:47:57 +00:00
|
|
|
"location" => %{
|
|
|
|
"@id" => "sc:location",
|
|
|
|
"@type" => "sc:Place"
|
|
|
|
},
|
|
|
|
"PostalAddress" => "sc:PostalAddress",
|
|
|
|
"address" => %{
|
|
|
|
"@id" => "sc:address",
|
|
|
|
"@type" => "sc:PostalAddress"
|
|
|
|
},
|
|
|
|
"addressCountry" => "sc:addressCountry",
|
|
|
|
"addressRegion" => "sc:addressRegion",
|
|
|
|
"postalCode" => "sc:postalCode",
|
|
|
|
"addressLocality" => "sc:addressLocality",
|
|
|
|
"streetAddress" => "sc:streetAddress",
|
2019-12-03 10:29:51 +00:00
|
|
|
"mz" => "https://joinmobilizon.org/ns#",
|
|
|
|
"repliesModerationOptionType" => %{
|
|
|
|
"@id" => "mz:repliesModerationOptionType",
|
|
|
|
"@type" => "rdfs:Class"
|
|
|
|
},
|
|
|
|
"repliesModerationOption" => %{
|
|
|
|
"@id" => "mz:repliesModerationOption",
|
|
|
|
"@type" => "mz:repliesModerationOptionType"
|
|
|
|
},
|
2019-12-16 10:46:19 +00:00
|
|
|
"commentsEnabled" => %{
|
|
|
|
"@type" => "sc:Boolean",
|
|
|
|
"@id" => "pt:commentsEnabled"
|
|
|
|
},
|
2019-12-03 10:29:51 +00:00
|
|
|
"joinModeType" => %{
|
|
|
|
"@id" => "mz:joinModeType",
|
|
|
|
"@type" => "rdfs:Class"
|
|
|
|
},
|
|
|
|
"joinMode" => %{
|
|
|
|
"@id" => "mz:joinMode",
|
|
|
|
"@type" => "mz:joinModeType"
|
2019-12-20 12:04:34 +00:00
|
|
|
},
|
|
|
|
"anonymousParticipationEnabled" => %{
|
|
|
|
"@id" => "mz:anonymousParticipationEnabled",
|
|
|
|
"@type" => "sc:Boolean"
|
2020-03-05 18:32:34 +00:00
|
|
|
},
|
|
|
|
"participationMessage" => %{
|
|
|
|
"@id" => "mz:participationMessage",
|
|
|
|
"@type" => "sc:Text"
|
2019-12-03 10:29:51 +00:00
|
|
|
}
|
2018-05-17 09:32:23 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_date do
|
2019-02-14 13:19:55 +00:00
|
|
|
DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Enqueues an activity for federation if it's local
|
|
|
|
"""
|
|
|
|
def maybe_federate(%Activity{local: true} = activity) do
|
2018-11-12 08:05:31 +00:00
|
|
|
Logger.debug("Maybe federate an activity")
|
|
|
|
|
2019-09-27 14:18:29 +00:00
|
|
|
if Mobilizon.Config.get!([:instance, :federating]) do
|
|
|
|
priority =
|
|
|
|
case activity.data["type"] do
|
|
|
|
"Delete" -> 10
|
|
|
|
"Create" -> 1
|
|
|
|
_ -> 5
|
|
|
|
end
|
|
|
|
|
|
|
|
Federator.enqueue(:publish, activity, priority)
|
|
|
|
end
|
2019-09-22 16:29:13 +00:00
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def maybe_federate(_), do: :ok
|
|
|
|
|
2018-11-12 08:05:31 +00:00
|
|
|
def remote_actors(%{data: %{"to" => to} = data}) do
|
|
|
|
to = to ++ (data["cc"] || [])
|
|
|
|
|
|
|
|
to
|
2018-11-12 17:17:53 +00:00
|
|
|
|> Enum.map(fn url -> Actors.get_actor_by_url(url) end)
|
|
|
|
|> Enum.map(fn {status, actor} ->
|
|
|
|
case status do
|
|
|
|
:ok ->
|
|
|
|
actor
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|> Enum.map(& &1)
|
2018-11-12 08:05:31 +00:00
|
|
|
|> Enum.filter(fn actor -> actor && !is_nil(actor.domain) end)
|
|
|
|
end
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
@doc """
|
|
|
|
Adds an id and a published data if they aren't there,
|
|
|
|
also adds it to an included object
|
|
|
|
"""
|
|
|
|
def lazy_put_activity_defaults(map) do
|
|
|
|
if is_map(map["object"]) do
|
2018-11-12 22:30:47 +00:00
|
|
|
object = lazy_put_object_defaults(map["object"])
|
2018-05-17 09:32:23 +00:00
|
|
|
%{map | "object" => object}
|
|
|
|
else
|
|
|
|
map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Adds an id and published date if they aren't there.
|
|
|
|
"""
|
2018-11-12 22:30:47 +00:00
|
|
|
def lazy_put_object_defaults(map) do
|
|
|
|
Map.put_new_lazy(map, "published", &make_date/0)
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def get_actor(%{"actor" => actor}) when is_binary(actor) do
|
|
|
|
actor
|
|
|
|
end
|
2018-11-12 08:05:31 +00:00
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def get_actor(%{"actor" => actor}) when is_list(actor) do
|
|
|
|
if is_binary(Enum.at(actor, 0)) do
|
|
|
|
Enum.at(actor, 0)
|
|
|
|
else
|
|
|
|
actor
|
|
|
|
|> Enum.find(fn %{"type" => type} -> type in ["Person", "Service", "Application"] end)
|
|
|
|
|> Map.get("id")
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def get_actor(%{"actor" => %{"id" => id}}) when is_bitstring(id) do
|
|
|
|
id
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def get_actor(%{"actor" => nil, "attributedTo" => actor}) when not is_nil(actor) do
|
|
|
|
get_actor(%{"actor" => actor})
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2019-07-23 11:49:22 +00:00
|
|
|
@doc """
|
2019-12-03 10:29:51 +00:00
|
|
|
Checks that an incoming AP object's actor matches the domain it came from.
|
2019-07-23 11:49:22 +00:00
|
|
|
"""
|
2020-02-18 07:57:00 +00:00
|
|
|
def origin_check?(id, %{"attributedTo" => actor} = params) do
|
|
|
|
params = params |> Map.put("actor", actor) |> Map.delete("attributedTo")
|
|
|
|
origin_check?(id, params)
|
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def origin_check?(id, %{"actor" => actor} = params) when not is_nil(actor) do
|
|
|
|
id_uri = URI.parse(id)
|
|
|
|
actor_uri = URI.parse(get_actor(params))
|
|
|
|
|
|
|
|
compare_uris?(actor_uri, id_uri)
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def origin_check?(_id, %{"actor" => nil}), do: false
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def origin_check?(_id, _data), do: false
|
2019-09-04 16:24:31 +00:00
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
defp compare_uris?(%URI{} = id_uri, %URI{} = other_uri), do: id_uri.host == other_uri.host
|
|
|
|
|
|
|
|
def origin_check_from_id?(id, other_id) when is_binary(other_id) do
|
|
|
|
id_uri = URI.parse(id)
|
|
|
|
other_uri = URI.parse(other_id)
|
|
|
|
|
|
|
|
compare_uris?(id_uri, other_uri)
|
2019-09-04 16:24:31 +00:00
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def origin_check_from_id?(id, %{"id" => other_id} = _params) when is_binary(other_id),
|
|
|
|
do: origin_check_from_id?(id, other_id)
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2019-05-31 15:58:03 +00:00
|
|
|
@doc """
|
|
|
|
Save picture data from %Plug.Upload{} and return AS Link data.
|
|
|
|
"""
|
2019-10-25 15:43:37 +00:00
|
|
|
def make_picture_data(%Plug.Upload{} = picture, opts) do
|
2020-01-26 20:36:50 +00:00
|
|
|
case Mobilizon.Web.Upload.store(picture, opts) do
|
2019-07-23 16:06:22 +00:00
|
|
|
{:ok, picture} ->
|
|
|
|
picture
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-31 15:58:03 +00:00
|
|
|
@doc """
|
2019-09-22 16:29:13 +00:00
|
|
|
Convert a picture model into an AS Link representation.
|
2019-05-31 15:58:03 +00:00
|
|
|
"""
|
2019-09-22 16:29:13 +00:00
|
|
|
def make_picture_data(%Picture{} = picture) do
|
|
|
|
Converter.Picture.model_to_as(picture)
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
|
2019-05-31 15:58:03 +00:00
|
|
|
@doc """
|
|
|
|
Save picture data from raw data and return AS Link data.
|
|
|
|
"""
|
2019-07-04 15:41:06 +00:00
|
|
|
def make_picture_data(picture) when is_map(picture) do
|
2019-06-03 15:13:47 +00:00
|
|
|
with {:ok, %{"url" => [%{"href" => url, "mediaType" => content_type}], "size" => size}} <-
|
2020-01-26 20:36:50 +00:00
|
|
|
Mobilizon.Web.Upload.store(picture.file),
|
2019-10-14 09:41:57 +00:00
|
|
|
{:picture_exists, nil} <- {:picture_exists, Mobilizon.Media.get_picture_by_url(url)},
|
2019-09-22 16:29:13 +00:00
|
|
|
{:ok, %Picture{file: _file} = picture} <-
|
2019-05-22 12:12:11 +00:00
|
|
|
Mobilizon.Media.create_picture(%{
|
|
|
|
"file" => %{
|
|
|
|
"url" => url,
|
2019-06-03 15:13:47 +00:00
|
|
|
"name" => picture.name,
|
|
|
|
"content_type" => content_type,
|
|
|
|
"size" => size
|
2019-05-31 15:58:03 +00:00
|
|
|
},
|
|
|
|
"actor_id" => picture.actor_id
|
2019-05-22 12:12:11 +00:00
|
|
|
}) do
|
2019-09-22 16:29:13 +00:00
|
|
|
Converter.Picture.model_to_as(picture)
|
2019-10-14 09:41:57 +00:00
|
|
|
else
|
|
|
|
{:picture_exists, %Picture{file: _file} = picture} ->
|
|
|
|
Converter.Picture.model_to_as(picture)
|
|
|
|
|
|
|
|
err ->
|
|
|
|
err
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_picture_data(nil), do: nil
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
@doc """
|
|
|
|
Make announce activity data for the given actor and object
|
|
|
|
"""
|
2019-07-30 14:40:59 +00:00
|
|
|
def make_announce_data(actor, object, activity_id, public \\ true)
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
def make_announce_data(
|
2020-02-18 07:57:00 +00:00
|
|
|
%Actor{} = actor,
|
2019-07-30 14:40:59 +00:00
|
|
|
%{"id" => url, "type" => type} = _object,
|
|
|
|
activity_id,
|
|
|
|
public
|
|
|
|
)
|
2019-09-04 16:24:31 +00:00
|
|
|
when type in @actor_types do
|
2020-02-18 07:57:00 +00:00
|
|
|
do_make_announce_data(actor, url, url, activity_id, public)
|
2019-07-30 14:40:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def make_announce_data(
|
2020-02-18 07:57:00 +00:00
|
|
|
%Actor{} = actor,
|
2019-07-30 14:40:59 +00:00
|
|
|
%{"id" => url, "type" => type, "actor" => object_actor_url} = _object,
|
|
|
|
activity_id,
|
|
|
|
public
|
|
|
|
)
|
2020-02-18 07:57:00 +00:00
|
|
|
when type in ["Note", "Event", "ResourceCollection", "Document"] do
|
2019-07-30 14:40:59 +00:00
|
|
|
do_make_announce_data(
|
2020-02-18 07:57:00 +00:00
|
|
|
actor,
|
2019-07-30 14:40:59 +00:00
|
|
|
object_actor_url,
|
|
|
|
url,
|
|
|
|
activity_id,
|
|
|
|
public
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_make_announce_data(
|
2020-02-18 07:57:00 +00:00
|
|
|
%Actor{type: actor_type} = actor,
|
2019-07-30 14:40:59 +00:00
|
|
|
object_actor_url,
|
|
|
|
object_url,
|
|
|
|
activity_id,
|
2019-08-14 15:45:11 +00:00
|
|
|
public
|
2019-07-30 14:40:59 +00:00
|
|
|
) do
|
|
|
|
{to, cc} =
|
|
|
|
if public do
|
2020-02-18 07:57:00 +00:00
|
|
|
{[actor.followers_url, object_actor_url],
|
2019-07-30 14:40:59 +00:00
|
|
|
["https://www.w3.org/ns/activitystreams#Public"]}
|
|
|
|
else
|
2020-02-18 07:57:00 +00:00
|
|
|
if actor_type == :Group do
|
|
|
|
{[actor.members_url], []}
|
|
|
|
else
|
|
|
|
{[actor.followers_url], []}
|
|
|
|
end
|
2019-07-30 14:40:59 +00:00
|
|
|
end
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
data = %{
|
|
|
|
"type" => "Announce",
|
2020-02-18 07:57:00 +00:00
|
|
|
"actor" => actor.url,
|
2019-07-30 14:40:59 +00:00
|
|
|
"object" => object_url,
|
|
|
|
"to" => to,
|
|
|
|
"cc" => cc
|
2018-12-14 16:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-07-30 14:40:59 +00:00
|
|
|
Make unannounce activity data for the given actor and object
|
2018-12-14 16:41:55 +00:00
|
|
|
"""
|
2019-07-30 14:40:59 +00:00
|
|
|
def make_unannounce_data(
|
|
|
|
%Actor{url: url} = actor,
|
|
|
|
activity,
|
2018-12-14 16:41:55 +00:00
|
|
|
activity_id
|
|
|
|
) do
|
|
|
|
data = %{
|
2019-07-30 14:40:59 +00:00
|
|
|
"type" => "Undo",
|
|
|
|
"actor" => url,
|
|
|
|
"object" => activity,
|
|
|
|
"to" => [actor.followers_url, actor.url],
|
2018-12-14 16:41:55 +00:00
|
|
|
"cc" => ["https://www.w3.org/ns/activitystreams#Public"]
|
2018-05-17 09:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
|
|
end
|
|
|
|
|
|
|
|
#### Unfollow-related helpers
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
@spec make_unfollow_data(Actor.t(), Actor.t(), map(), String.t()) :: map()
|
|
|
|
def make_unfollow_data(
|
|
|
|
%Actor{url: follower_url},
|
|
|
|
%Actor{url: followed_url},
|
|
|
|
follow_activity,
|
|
|
|
activity_id
|
|
|
|
) do
|
|
|
|
data = %{
|
2018-05-17 09:32:23 +00:00
|
|
|
"type" => "Undo",
|
2018-12-14 16:41:55 +00:00
|
|
|
"actor" => follower_url,
|
|
|
|
"to" => [followed_url],
|
|
|
|
"object" => follow_activity.data
|
2018-05-17 09:32:23 +00:00
|
|
|
}
|
2018-12-14 16:41:55 +00:00
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#### Create-related helpers
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
@doc """
|
|
|
|
Make create activity data
|
|
|
|
"""
|
|
|
|
@spec make_create_data(map(), map()) :: map()
|
2019-10-25 15:43:37 +00:00
|
|
|
def make_create_data(object, additional \\ %{}) do
|
2018-12-14 16:41:55 +00:00
|
|
|
Logger.debug("Making create data")
|
2019-10-25 15:43:37 +00:00
|
|
|
Logger.debug(inspect(object))
|
|
|
|
Logger.debug(inspect(additional))
|
2018-05-17 09:32:23 +00:00
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Create",
|
2019-10-25 15:43:37 +00:00
|
|
|
"to" => object["to"],
|
|
|
|
"cc" => object["cc"],
|
|
|
|
"actor" => object["actor"],
|
|
|
|
"object" => object,
|
|
|
|
"published" => make_date(),
|
|
|
|
"id" => object["id"] <> "/activity"
|
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Make update activity data
|
|
|
|
"""
|
|
|
|
@spec make_update_data(map(), map()) :: map()
|
|
|
|
def make_update_data(object, additional \\ %{}) do
|
|
|
|
Logger.debug("Making update data")
|
|
|
|
Logger.debug(inspect(object))
|
|
|
|
Logger.debug(inspect(additional))
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Update",
|
|
|
|
"to" => object["to"],
|
|
|
|
"cc" => object["cc"],
|
|
|
|
"actor" => object["actor"],
|
|
|
|
"object" => object,
|
|
|
|
"id" => object["id"] <> "/activity"
|
2018-05-17 09:32:23 +00:00
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
2018-06-14 15:25:55 +00:00
|
|
|
|
2019-10-25 15:43:37 +00:00
|
|
|
@doc """
|
|
|
|
Make accept join activity data
|
|
|
|
"""
|
|
|
|
@spec make_accept_join_data(map(), map()) :: map()
|
|
|
|
def make_accept_join_data(object, additional \\ %{}) do
|
|
|
|
%{
|
|
|
|
"type" => "Accept",
|
|
|
|
"to" => object["to"],
|
|
|
|
"cc" => object["cc"],
|
|
|
|
"object" => object,
|
|
|
|
"id" => object["id"] <> "/activity"
|
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
@doc """
|
|
|
|
Make add activity data
|
|
|
|
"""
|
|
|
|
@spec make_add_data(map(), map()) :: map()
|
|
|
|
def make_add_data(object, target, additional \\ %{}) do
|
|
|
|
Logger.debug("Making add data")
|
|
|
|
Logger.debug(inspect(object))
|
|
|
|
Logger.debug(inspect(additional))
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Add",
|
|
|
|
"to" => object["to"],
|
|
|
|
"cc" => object["cc"],
|
|
|
|
"actor" => object["actor"],
|
|
|
|
"object" => object,
|
|
|
|
"target" => Map.get(target, :url, target),
|
|
|
|
"id" => object["id"] <> "/add"
|
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Make move activity data
|
|
|
|
"""
|
|
|
|
@spec make_add_data(map(), map()) :: map()
|
|
|
|
def make_move_data(object, origin, target, additional \\ %{}) do
|
|
|
|
Logger.debug("Making move data")
|
|
|
|
Logger.debug(inspect(object))
|
|
|
|
Logger.debug(inspect(origin))
|
|
|
|
Logger.debug(inspect(target))
|
|
|
|
Logger.debug(inspect(additional))
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Move",
|
|
|
|
"to" => object["to"],
|
|
|
|
"cc" => object["cc"],
|
|
|
|
"actor" => object["actor"],
|
|
|
|
"object" => object,
|
|
|
|
"origin" => if(is_nil(origin), do: origin, else: Map.get(origin, :url, origin)),
|
|
|
|
"target" => if(is_nil(target), do: target, else: Map.get(target, :url, target)),
|
|
|
|
"id" => object["id"] <> "/move"
|
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
|
|
|
|
2018-06-14 15:25:55 +00:00
|
|
|
@doc """
|
|
|
|
Converts PEM encoded keys to a public key representation
|
|
|
|
"""
|
|
|
|
def pem_to_public_key(pem) do
|
2018-08-03 08:16:22 +00:00
|
|
|
[key_code] = :public_key.pem_decode(pem)
|
|
|
|
key = :public_key.pem_entry_decode(key_code)
|
2018-08-03 08:19:28 +00:00
|
|
|
|
2018-08-03 08:16:22 +00:00
|
|
|
case key do
|
|
|
|
{:RSAPrivateKey, _, modulus, exponent, _, _, _, _, _, _, _} ->
|
|
|
|
{:RSAPublicKey, modulus, exponent}
|
2018-08-03 08:19:28 +00:00
|
|
|
|
|
|
|
{:RSAPublicKey, modulus, exponent} ->
|
|
|
|
{:RSAPublicKey, modulus, exponent}
|
2018-08-03 08:16:22 +00:00
|
|
|
end
|
2018-06-14 15:25:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pem_to_public_key_pem(pem) do
|
|
|
|
public_key = pem_to_public_key(pem)
|
|
|
|
public_key = :public_key.pem_entry_encode(:RSAPublicKey, public_key)
|
|
|
|
:public_key.pem_encode([public_key])
|
|
|
|
end
|
2019-08-28 09:28:27 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
def make_signature(actor, id, date) do
|
2019-12-03 10:29:51 +00:00
|
|
|
uri = URI.parse(id)
|
|
|
|
|
|
|
|
signature =
|
2020-02-18 07:57:00 +00:00
|
|
|
actor
|
2020-01-22 01:14:42 +00:00
|
|
|
|> HTTPSignatures.Signature.sign(%{
|
2019-12-03 10:29:51 +00:00
|
|
|
"(request-target)": "get #{uri.path}",
|
|
|
|
host: uri.host,
|
|
|
|
date: date
|
|
|
|
})
|
2019-08-28 09:28:27 +00:00
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
[{:Signature, signature}]
|
2019-08-28 09:28:27 +00:00
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
@doc """
|
|
|
|
Sign a request with the instance Relay actor.
|
|
|
|
"""
|
|
|
|
@spec sign_fetch_relay(List.t(), String.t(), String.t()) :: List.t()
|
|
|
|
def sign_fetch_relay(headers, id, date) do
|
|
|
|
with %Actor{} = actor <- Relay.get_actor() do
|
|
|
|
sign_fetch(headers, actor, id, date)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Sign a request with an actor.
|
|
|
|
"""
|
|
|
|
@spec sign_fetch(List.t(), Actor.t(), String.t(), String.t()) :: List.t()
|
|
|
|
def sign_fetch(headers, actor, id, date) do
|
2019-12-03 10:29:51 +00:00
|
|
|
if Mobilizon.Config.get([:activitypub, :sign_object_fetches]) do
|
2020-02-18 07:57:00 +00:00
|
|
|
headers ++ make_signature(actor, id, date)
|
2019-12-03 10:29:51 +00:00
|
|
|
else
|
|
|
|
headers
|
|
|
|
end
|
2019-08-28 09:28:27 +00:00
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
@doc """
|
|
|
|
Add the Date header to the request if we sign object fetches
|
|
|
|
"""
|
|
|
|
@spec maybe_date_fetch(List.t(), String.t()) :: List.t()
|
2019-12-03 10:29:51 +00:00
|
|
|
def maybe_date_fetch(headers, date) do
|
|
|
|
if Mobilizon.Config.get([:activitypub, :sign_object_fetches]) do
|
|
|
|
headers ++ [{:Date, date}]
|
|
|
|
else
|
|
|
|
headers
|
|
|
|
end
|
2019-08-28 09:28:27 +00:00
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|