2020-07-09 17:24:28 +02: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 12:17:56 +02:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Actor, as: ActorConverter
|
2021-06-27 13:15:24 +02:00
|
|
|
alias Mobilizon.Service.ErrorReporting.Sentry
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Service.HTTP.ActivityPub, as: ActivityPubClient
|
|
|
|
|
|
|
|
import Mobilizon.Federation.ActivityPub.Utils,
|
2021-11-16 15:46:23 +01:00
|
|
|
only: [maybe_date_fetch: 2, sign_fetch: 5, origin_check?: 2]
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
import Mobilizon.Service.Guards, only: [is_valid_string: 1]
|
|
|
|
|
|
|
|
@spec fetch(String.t(), Keyword.t()) ::
|
|
|
|
{:ok, map()}
|
2021-09-28 19:40:37 +02:00
|
|
|
| {:error,
|
|
|
|
:invalid_url | :http_gone | :http_error | :http_not_found | :content_not_json}
|
2020-07-09 17:24:28 +02:00
|
|
|
def fetch(url, options \\ []) do
|
|
|
|
on_behalf_of = Keyword.get(options, :on_behalf_of, Relay.get_actor())
|
2021-09-10 11:36:05 +02:00
|
|
|
date = Signature.generate_date_header()
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
headers =
|
|
|
|
[{:Accept, "application/activity+json"}]
|
|
|
|
|> maybe_date_fetch(date)
|
2021-11-16 15:46:23 +01:00
|
|
|
|> sign_fetch(on_behalf_of, url, date, options)
|
2020-08-27 11:53:24 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
client = ActivityPubClient.client(headers: headers)
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
if address_valid?(url) do
|
|
|
|
case ActivityPubClient.get(client, url) do
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, %Tesla.Env{body: data, status: code}} when code in 200..299 and is_map(data) ->
|
2021-09-10 11:36:05 +02:00
|
|
|
{:ok, data}
|
2021-04-09 14:01:40 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
{:ok, %Tesla.Env{status: 410}} ->
|
|
|
|
Logger.debug("Resource at #{url} is 410 Gone")
|
|
|
|
{:error, :http_gone}
|
|
|
|
|
|
|
|
{:ok, %Tesla.Env{status: 404}} ->
|
|
|
|
Logger.debug("Resource at #{url} is 404 Gone")
|
|
|
|
{:error, :http_not_found}
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, %Tesla.Env{body: data}} when is_binary(data) ->
|
|
|
|
{:error, :content_not_json}
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
{:ok, %Tesla.Env{} = res} ->
|
2021-11-13 18:45:01 +01:00
|
|
|
Logger.debug("Resource returned bad HTTP code #{inspect(res)}")
|
2021-09-28 19:40:37 +02:00
|
|
|
{:error, :http_error}
|
2021-11-13 18:45:01 +01:00
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2021-09-10 11:36:05 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, :invalid_url}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec fetch_and_create(String.t(), Keyword.t()) ::
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, map(), struct()} | {:error, atom()} | :error
|
2020-07-09 17:24:28 +02:00
|
|
|
def fetch_and_create(url, options \\ []) do
|
2021-09-28 19:40:37 +02:00
|
|
|
case fetch(url, options) do
|
|
|
|
{:ok, data} when is_map(data) ->
|
|
|
|
if origin_check?(url, data) do
|
|
|
|
case Transmogrifier.handle_incoming(%{
|
|
|
|
"type" => "Create",
|
|
|
|
"to" => data["to"],
|
|
|
|
"cc" => data["cc"],
|
|
|
|
"actor" => data["actor"] || data["attributedTo"],
|
|
|
|
"attributedTo" => data["attributedTo"] || data["actor"],
|
|
|
|
"object" => data
|
|
|
|
}) do
|
|
|
|
{:ok, entity, structure} ->
|
|
|
|
{:ok, entity, structure}
|
|
|
|
|
|
|
|
{:error, error} when is_atom(error) ->
|
|
|
|
{:error, error}
|
|
|
|
|
|
|
|
:error ->
|
|
|
|
{:error, :transmogrifier_error}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Logger.warn("Object origin check failed")
|
|
|
|
{:error, :object_origin_check_failed}
|
|
|
|
end
|
2021-09-10 11:36:05 +02:00
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec fetch_and_update(String.t(), Keyword.t()) ::
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, map(), struct()} | {:error, atom()}
|
2020-07-09 17:24:28 +02:00
|
|
|
def fetch_and_update(url, options \\ []) do
|
2021-09-28 19:40:37 +02:00
|
|
|
case fetch(url, options) do
|
|
|
|
{:ok, data} when is_map(data) ->
|
|
|
|
if origin_check(url, data) do
|
|
|
|
Transmogrifier.handle_incoming(%{
|
|
|
|
"type" => "Update",
|
|
|
|
"to" => data["to"],
|
|
|
|
"cc" => data["cc"],
|
|
|
|
"actor" => data["actor"] || data["attributedTo"],
|
|
|
|
"attributedTo" => data["attributedTo"] || data["actor"],
|
|
|
|
"object" => data
|
|
|
|
})
|
|
|
|
else
|
|
|
|
Logger.warn("Object origin check failed")
|
|
|
|
{:error, :object_origin_check_failed}
|
|
|
|
end
|
2020-08-27 11:53:24 +02:00
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
2021-04-09 10:52:40 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@type fetch_actor_errors ::
|
|
|
|
:json_decode_error | :actor_deleted | :http_error | :actor_not_allowed_type
|
2021-09-10 11:36:05 +02:00
|
|
|
|
2021-04-22 12:17:56 +02:00
|
|
|
@doc """
|
|
|
|
Fetching a remote actor's information through its AP ID
|
|
|
|
"""
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec fetch_and_prepare_actor_from_url(String.t()) ::
|
|
|
|
{:ok, map()} | {:error, fetch_actor_errors}
|
2021-11-13 18:45:01 +01:00
|
|
|
def fetch_and_prepare_actor_from_url(url, options \\ []) do
|
2021-04-22 12:17:56 +02:00
|
|
|
Logger.debug("Fetching and preparing actor from url")
|
|
|
|
Logger.debug(inspect(url))
|
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
case fetch(url, options) do
|
|
|
|
{:ok, data} ->
|
|
|
|
case ActorConverter.as_to_model_data(data) do
|
|
|
|
{:error, :actor_not_allowed_type} ->
|
|
|
|
{:error, :actor_not_allowed_type}
|
2021-09-10 11:36:05 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
map when is_map(map) ->
|
|
|
|
{:ok, map}
|
2021-09-10 11:36:05 +02:00
|
|
|
end
|
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
{:error, :http_gone} ->
|
2021-09-10 11:36:05 +02:00
|
|
|
Logger.info("Response HTTP 410")
|
|
|
|
{:error, :actor_deleted}
|
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
{:error, :http_error} ->
|
2021-09-10 11:36:05 +02:00
|
|
|
Logger.info("Non 200 HTTP Code")
|
|
|
|
{:error, :http_error}
|
|
|
|
|
|
|
|
{:error, error} ->
|
|
|
|
Logger.warn("Could not fetch actor at fetch #{url}, #{inspect(error)}")
|
|
|
|
{:error, :http_error}
|
|
|
|
end
|
2021-04-22 12:17:56 +02:00
|
|
|
end
|
|
|
|
|
2021-04-21 18:57:23 +02: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})
|
2021-08-10 20:32:50 +02:00
|
|
|
Logger.debug("Object origin check failed between #{inspect(url)} and #{inspect(data)}")
|
2021-04-21 18:57:23 +02:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec address_valid?(String.t()) :: boolean
|
|
|
|
defp address_valid?(address) do
|
2021-09-24 16:46:42 +02:00
|
|
|
%URI{host: host, scheme: scheme} = URI.parse(address)
|
|
|
|
is_valid_string(host) and is_valid_string(scheme)
|
2021-04-09 10:52:40 +02:00
|
|
|
end
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|