2020-07-09 15:24:28 +00:00
|
|
|
defmodule Mobilizon.Federation.ActivityPub.Fetcher do
|
|
|
|
@moduledoc """
|
|
|
|
Module to handle direct URL ActivityPub fetches to remote content
|
|
|
|
|
|
|
|
If you need to first get cached data, see `Mobilizon.Federation.ActivityPub.fetch_object_from_url/2`
|
|
|
|
"""
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
alias Mobilizon.Federation.HTTPSignatures.Signature
|
|
|
|
alias Mobilizon.Federation.ActivityPub.{Relay, Transmogrifier}
|
2021-04-22 10:17:56 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Actor, as: ActorConverter
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Service.HTTP.ActivityPub, as: ActivityPubClient
|
|
|
|
|
|
|
|
import Mobilizon.Federation.ActivityPub.Utils,
|
|
|
|
only: [maybe_date_fetch: 2, sign_fetch: 4, origin_check?: 2]
|
|
|
|
|
|
|
|
@spec fetch(String.t(), Keyword.t()) :: {:ok, map()}
|
|
|
|
def fetch(url, options \\ []) do
|
|
|
|
on_behalf_of = Keyword.get(options, :on_behalf_of, Relay.get_actor())
|
|
|
|
|
2021-04-09 08:52:40 +00:00
|
|
|
with false <- address_invalid(url),
|
|
|
|
date <- Signature.generate_date_header(),
|
2020-07-09 15:24:28 +00:00
|
|
|
headers <-
|
|
|
|
[{:Accept, "application/activity+json"}]
|
|
|
|
|> maybe_date_fetch(date)
|
|
|
|
|> sign_fetch(on_behalf_of, url, date),
|
|
|
|
client <-
|
|
|
|
ActivityPubClient.client(headers: headers),
|
|
|
|
{:ok, %Tesla.Env{body: data, status: code}} when code in 200..299 <-
|
|
|
|
ActivityPubClient.get(client, url) do
|
|
|
|
{:ok, data}
|
2020-08-27 09:53:24 +00:00
|
|
|
else
|
|
|
|
{:ok, %Tesla.Env{status: 410}} ->
|
2021-04-21 16:57:23 +00:00
|
|
|
Logger.debug("Resource at #{url} is 410 Gone")
|
2020-08-27 09:53:24 +00:00
|
|
|
{:error, "Gone"}
|
|
|
|
|
2020-10-19 07:32:37 +00:00
|
|
|
{:ok, %Tesla.Env{status: 404}} ->
|
2021-04-21 16:57:23 +00:00
|
|
|
Logger.debug("Resource at #{url} is 404 Gone")
|
2020-10-19 07:32:37 +00:00
|
|
|
{:error, "Not found"}
|
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
{:ok, %Tesla.Env{} = res} ->
|
|
|
|
{:error, res}
|
2021-04-09 12:01:40 +00:00
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec fetch_and_create(String.t(), Keyword.t()) :: {:ok, map(), struct()}
|
|
|
|
def fetch_and_create(url, options \\ []) do
|
|
|
|
with {:ok, data} when is_map(data) <- fetch(url, options),
|
|
|
|
{:origin_check, true} <- {:origin_check, origin_check?(url, data)},
|
|
|
|
params <- %{
|
|
|
|
"type" => "Create",
|
|
|
|
"to" => data["to"],
|
|
|
|
"cc" => data["cc"],
|
2020-08-14 09:32:23 +00:00
|
|
|
"actor" => data["actor"] || data["attributedTo"],
|
|
|
|
"attributedTo" => data["attributedTo"] || data["actor"],
|
2020-07-09 15:24:28 +00:00
|
|
|
"object" => data
|
|
|
|
} do
|
|
|
|
Transmogrifier.handle_incoming(params)
|
|
|
|
else
|
|
|
|
{:origin_check, false} ->
|
|
|
|
Logger.warn("Object origin check failed")
|
|
|
|
{:error, "Object origin check failed"}
|
2020-08-27 09:53:24 +00:00
|
|
|
|
2021-03-16 15:50:43 +00:00
|
|
|
# Returned content is not JSON
|
|
|
|
{:ok, data} when is_binary(data) ->
|
|
|
|
{:error, "Failed to parse content as JSON"}
|
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec fetch_and_update(String.t(), Keyword.t()) :: {:ok, map(), struct()}
|
|
|
|
def fetch_and_update(url, options \\ []) do
|
|
|
|
with {:ok, data} when is_map(data) <- fetch(url, options),
|
2021-04-21 16:57:23 +00:00
|
|
|
{:origin_check, true} <- {:origin_check, origin_check(url, data)},
|
2020-07-09 15:24:28 +00:00
|
|
|
params <- %{
|
|
|
|
"type" => "Update",
|
|
|
|
"to" => data["to"],
|
|
|
|
"cc" => data["cc"],
|
2020-08-14 09:32:23 +00:00
|
|
|
"actor" => data["actor"] || data["attributedTo"],
|
|
|
|
"attributedTo" => data["attributedTo"] || data["actor"],
|
2020-07-09 15:24:28 +00:00
|
|
|
"object" => data
|
|
|
|
} do
|
|
|
|
Transmogrifier.handle_incoming(params)
|
|
|
|
else
|
|
|
|
{:origin_check, false} ->
|
|
|
|
{:error, "Object origin check failed"}
|
2020-08-27 09:53:24 +00:00
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-09 08:52:40 +00:00
|
|
|
|
2021-04-22 10:17:56 +00:00
|
|
|
@doc """
|
|
|
|
Fetching a remote actor's information through its AP ID
|
|
|
|
"""
|
|
|
|
@spec fetch_and_prepare_actor_from_url(String.t()) :: {:ok, map()} | {:error, atom()} | any()
|
|
|
|
def fetch_and_prepare_actor_from_url(url) do
|
|
|
|
Logger.debug("Fetching and preparing actor from url")
|
|
|
|
Logger.debug(inspect(url))
|
|
|
|
|
|
|
|
res =
|
|
|
|
with {:ok, %{status: 200, body: body}} <-
|
|
|
|
Tesla.get(url,
|
|
|
|
headers: [{"Accept", "application/activity+json"}],
|
|
|
|
follow_redirect: true
|
|
|
|
),
|
|
|
|
:ok <- Logger.debug("response okay, now decoding json"),
|
|
|
|
{:ok, data} <- Jason.decode(body) do
|
|
|
|
Logger.debug("Got activity+json response at actor's endpoint, now converting data")
|
|
|
|
{:ok, ActorConverter.as_to_model_data(data)}
|
|
|
|
else
|
|
|
|
# Actor is gone, probably deleted
|
|
|
|
{:ok, %{status: 410}} ->
|
|
|
|
Logger.info("Response HTTP 410")
|
|
|
|
{:error, :actor_deleted}
|
|
|
|
|
2021-04-23 07:25:57 +00:00
|
|
|
{:ok, %Tesla.Env{}} ->
|
|
|
|
Logger.info("Non 200 HTTP Code")
|
|
|
|
{:error, :http_error}
|
|
|
|
|
2021-04-22 10:17:56 +00:00
|
|
|
{:error, e} ->
|
|
|
|
Logger.warn("Could not decode actor at fetch #{url}, #{inspect(e)}")
|
|
|
|
{:error, e}
|
|
|
|
|
|
|
|
e ->
|
|
|
|
Logger.warn("Could not decode actor at fetch #{url}, #{inspect(e)}")
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2021-04-21 16:57:23 +00:00
|
|
|
@spec origin_check(String.t(), map()) :: boolean()
|
|
|
|
defp origin_check(url, data) do
|
|
|
|
if origin_check?(url, data) do
|
|
|
|
true
|
|
|
|
else
|
|
|
|
Sentry.capture_message("Object origin check failed", extra: %{url: url, data: data})
|
|
|
|
Logger.debug("Object origin check failed")
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-09 08:52:40 +00:00
|
|
|
@spec address_invalid(String.t()) :: false | {:error, :invalid_url}
|
|
|
|
defp address_invalid(address) do
|
|
|
|
with %URI{host: host, scheme: scheme} <- URI.parse(address),
|
|
|
|
true <- is_nil(host) or is_nil(scheme) do
|
|
|
|
{:error, :invalid_url}
|
|
|
|
end
|
|
|
|
end
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|