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/web_finger/web_finger.ex
|
2018-12-24 12:34:45 +00:00
|
|
|
|
2020-01-22 21:40:40 +00:00
|
|
|
defmodule Mobilizon.Federation.WebFinger do
|
2018-06-14 16:15:27 +00:00
|
|
|
@moduledoc """
|
2020-01-22 01:14:42 +00:00
|
|
|
Performs the WebFinger requests and responses (JSON only).
|
2018-06-14 16:15:27 +00:00
|
|
|
"""
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2018-10-11 15:37:39 +00:00
|
|
|
alias Mobilizon.Actors
|
2018-11-28 09:49:16 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-04-22 10:17:56 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-01-22 21:40:40 +00:00
|
|
|
alias Mobilizon.Federation.WebFinger.XmlBuilder
|
2021-04-09 08:19:25 +00:00
|
|
|
alias Mobilizon.Service.HTTP.{HostMetaClient, WebfingerClient}
|
2020-01-28 18:18:33 +00:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2019-12-20 12:04:34 +00:00
|
|
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
2018-05-17 09:32:23 +00:00
|
|
|
require Jason
|
|
|
|
require Logger
|
2021-04-09 08:19:25 +00:00
|
|
|
import SweetXml
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
@doc """
|
|
|
|
Returns the Web Host Metadata (for `/.well-known/host-meta`) representation for the instance, following RFC6414.
|
|
|
|
"""
|
|
|
|
@spec host_meta :: String.t()
|
2018-05-17 09:32:23 +00:00
|
|
|
def host_meta do
|
2020-01-28 18:18:33 +00:00
|
|
|
base_url = Endpoint.url()
|
2021-04-09 08:19:25 +00:00
|
|
|
%URI{host: host} = URI.parse(base_url)
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
XmlBuilder.to_doc({
|
2018-05-17 09:32:23 +00:00
|
|
|
:XRD,
|
2021-04-09 08:19:25 +00:00
|
|
|
%{
|
|
|
|
xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0",
|
|
|
|
"xmlns:hm": "http://host-meta.net/ns/1.0"
|
|
|
|
},
|
|
|
|
[
|
|
|
|
{
|
|
|
|
:"hm:Host",
|
|
|
|
host
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:Link,
|
|
|
|
%{
|
|
|
|
rel: "lrdd",
|
|
|
|
type: "application/jrd+json",
|
|
|
|
template: "#{base_url}/.well-known/webfinger?resource={uri}"
|
|
|
|
}
|
2018-05-17 09:32:23 +00:00
|
|
|
}
|
2021-04-09 08:19:25 +00:00
|
|
|
]
|
2021-09-24 14:46:42 +00:00
|
|
|
})
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
@doc """
|
|
|
|
Returns the Webfinger representation for the instance, following RFC7033.
|
|
|
|
"""
|
|
|
|
@spec webfinger(String.t(), String.t()) :: {:ok, map} | {:error, :actor_not_found}
|
2018-05-17 09:32:23 +00:00
|
|
|
def webfinger(resource, "JSON") do
|
2020-01-28 18:18:33 +00:00
|
|
|
host = Endpoint.host()
|
2018-05-18 07:56:21 +00:00
|
|
|
regex = ~r/(acct:)?(?<name>\w+)@#{host}/
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2018-11-28 09:49:16 +00:00
|
|
|
with %{"name" => name} <- Regex.named_captures(regex, resource),
|
|
|
|
%Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
2018-11-27 17:42:56 +00:00
|
|
|
{:ok, represent_actor(actor, "JSON")}
|
2018-05-17 09:32:23 +00:00
|
|
|
else
|
|
|
|
_e ->
|
2021-04-22 10:17:56 +00:00
|
|
|
case ActivityPubActor.get_or_fetch_actor_by_url(resource) do
|
2019-07-23 16:06:22 +00:00
|
|
|
{:ok, %Actor{} = actor} when not is_nil(actor) ->
|
|
|
|
{:ok, represent_actor(actor, "JSON")}
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
_e ->
|
2021-09-10 09:36:05 +00:00
|
|
|
{:error, :actor_not_found}
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
@doc """
|
|
|
|
Return an `Mobilizon.Actors.Actor` Webfinger representation (as JSON)
|
|
|
|
"""
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec represent_actor(Actor.t(), String.t()) :: map()
|
2021-04-09 08:19:25 +00:00
|
|
|
def represent_actor(%Actor{} = actor, "JSON") do
|
|
|
|
links =
|
|
|
|
[
|
2018-11-27 17:42:56 +00:00
|
|
|
%{"rel" => "self", "type" => "application/activity+json", "href" => actor.url},
|
2019-12-20 12:04:34 +00:00
|
|
|
%{
|
|
|
|
"rel" => "http://ostatus.org/schema/1.0/subscribe",
|
|
|
|
"template" => "#{Routes.page_url(Endpoint, :interact, uri: nil)}{uri}"
|
2018-11-07 15:45:11 +00:00
|
|
|
}
|
2018-05-17 09:32:23 +00:00
|
|
|
]
|
2021-04-09 08:19:25 +00:00
|
|
|
|> maybe_add_avatar(actor)
|
|
|
|
|> maybe_add_profile_page(actor)
|
|
|
|
|
|
|
|
%{
|
|
|
|
"subject" => "acct:#{actor.preferred_username}@#{Endpoint.host()}",
|
|
|
|
"aliases" => [actor.url],
|
|
|
|
"links" => links
|
2018-05-17 09:32:23 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
@spec maybe_add_avatar(list(map()), Actor.t()) :: list(map())
|
2021-04-09 08:19:25 +00:00
|
|
|
defp maybe_add_avatar(data, %Actor{avatar: avatar}) when not is_nil(avatar) do
|
|
|
|
data ++
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"rel" => "http://webfinger.net/rel/avatar",
|
|
|
|
"type" => avatar.content_type,
|
|
|
|
"href" => avatar.url
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_avatar(data, _actor), do: data
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
@spec maybe_add_profile_page(list(map()), Actor.t()) :: list(map())
|
2021-04-09 08:19:25 +00:00
|
|
|
defp maybe_add_profile_page(data, %Actor{type: :Group, url: url}) do
|
|
|
|
data ++
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"rel" => "http://webfinger.net/rel/profile-page/",
|
|
|
|
"type" => "text/html",
|
|
|
|
"href" => url
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_profile_page(data, _actor), do: data
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
@type finger_errors ::
|
2021-12-29 13:58:38 +00:00
|
|
|
:host_not_found
|
|
|
|
| :address_invalid
|
|
|
|
| :http_error
|
|
|
|
| :webfinger_information_not_json
|
2022-04-20 09:35:22 +00:00
|
|
|
| :webfinger_information_not_valid
|
2021-12-29 13:58:38 +00:00
|
|
|
| :no_url_in_webfinger_data
|
2021-09-10 09:36:05 +00:00
|
|
|
|
2021-04-09 08:19:25 +00:00
|
|
|
@doc """
|
|
|
|
Finger an actor to retreive it's ActivityPub ID/URL
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
Fetches the Extensible Resource Descriptor endpoint `/.well-known/host-meta` to find the Webfinger endpoint (usually `/.well-known/webfinger?resource=`) and then performs a Webfinger query to get the ActivityPub ID associated to an actor.
|
2021-04-09 08:19:25 +00:00
|
|
|
"""
|
2021-09-10 09:36:05 +00:00
|
|
|
@spec finger(String.t()) ::
|
|
|
|
{:ok, String.t()}
|
|
|
|
| {:error, finger_errors}
|
2021-04-09 08:19:25 +00:00
|
|
|
def finger(actor) do
|
|
|
|
actor = String.trim_leading(actor, "@")
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
case validate_endpoint(actor) do
|
|
|
|
{:ok, address} ->
|
|
|
|
case fetch_webfinger_data(address) do
|
|
|
|
{:ok, %{"url" => url}} ->
|
|
|
|
{:ok, url}
|
|
|
|
|
2021-12-29 13:58:38 +00:00
|
|
|
{:ok, _} ->
|
|
|
|
Logger.debug("No URL found for actor from webfinger data")
|
|
|
|
{:error, :no_url_in_webfinger_data}
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
{:error, err} ->
|
|
|
|
Logger.debug("Couldn't process webfinger data for #{actor}")
|
2021-09-24 14:46:42 +00:00
|
|
|
{:error, err}
|
2021-09-10 09:36:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
Logger.debug("Couldn't find webfinger endpoint for #{actor}")
|
|
|
|
{:error, err}
|
2021-04-09 08:19:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
@spec fetch_webfinger_data(String.t()) ::
|
2022-04-20 09:35:22 +00:00
|
|
|
{:ok, map()}
|
|
|
|
| {:error,
|
|
|
|
:webfinger_information_not_json | :webfinger_information_not_valid | :http_error}
|
2021-09-10 09:36:05 +00:00
|
|
|
defp fetch_webfinger_data(address) do
|
2021-12-29 13:58:38 +00:00
|
|
|
Logger.debug("Calling WebfingerClient with #{inspect(address)}")
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
case WebfingerClient.get(address) do
|
|
|
|
{:ok, %{body: body, status: code}} when code in 200..299 ->
|
|
|
|
webfinger_from_json(body)
|
|
|
|
|
2021-12-29 13:58:38 +00:00
|
|
|
err ->
|
|
|
|
Logger.debug("Failed to fetch webfinger data #{inspect(err)}")
|
2021-09-10 09:36:05 +00:00
|
|
|
{:error, :http_error}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec validate_endpoint(String.t()) ::
|
|
|
|
{:ok, String.t()} | {:error, :address_invalid | :host_not_found}
|
|
|
|
defp validate_endpoint(actor) do
|
|
|
|
case apply_webfinger_endpoint(actor) do
|
|
|
|
address when is_binary(address) ->
|
|
|
|
if address_invalid(address) do
|
2021-12-29 13:58:38 +00:00
|
|
|
Logger.info("Webfinger endpoint seems to be an invalid URL #{inspect(address)}")
|
2021-09-10 09:36:05 +00:00
|
|
|
{:error, :address_invalid}
|
|
|
|
else
|
|
|
|
{:ok, address}
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
2021-12-29 13:58:38 +00:00
|
|
|
Logger.info("Host not found in actor address #{inspect(actor)}")
|
2021-09-10 09:36:05 +00:00
|
|
|
{:error, :host_not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
# Fetches the Extensible Resource Descriptor endpoint `/.well-known/host-meta`
|
|
|
|
# to find the Webfinger endpoint (usually `/.well-known/webfinger?resource=`)
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec find_webfinger_endpoint(String.t()) ::
|
|
|
|
{:ok, String.t()} | {:error, :link_not_found} | {:error, any()}
|
2021-09-10 09:36:05 +00:00
|
|
|
defp find_webfinger_endpoint(domain) when is_binary(domain) do
|
2021-12-29 13:58:38 +00:00
|
|
|
Logger.debug("Calling HostMetaClient for #{domain}")
|
2022-04-20 16:38:03 +00:00
|
|
|
prefix = if Application.fetch_env!(:mobilizon, :env) !== :dev, do: "https", else: "http"
|
2021-12-29 13:58:38 +00:00
|
|
|
|
2021-11-15 07:47:03 +00:00
|
|
|
with {:ok, %Tesla.Env{status: 200, body: body}} <-
|
2022-04-20 16:38:03 +00:00
|
|
|
HostMetaClient.get("#{prefix}://#{domain}/.well-known/host-meta"),
|
2021-06-10 09:40:54 +00:00
|
|
|
link_template when is_binary(link_template) <- find_link_from_template(body) do
|
2021-04-09 08:19:25 +00:00
|
|
|
{:ok, link_template}
|
2021-09-10 09:27:59 +00:00
|
|
|
else
|
2021-11-15 07:47:03 +00:00
|
|
|
{:ok, %Tesla.Env{status: 404}} -> {:error, :entity_not_found}
|
2021-12-29 13:58:38 +00:00
|
|
|
{:ok, %Tesla.Env{}} -> {:error, :http_error}
|
2021-09-10 09:27:59 +00:00
|
|
|
{:error, :link_not_found} -> {:error, :link_not_found}
|
|
|
|
{:error, error} -> {:error, error}
|
2021-04-09 08:19:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec apply_webfinger_endpoint(String.t()) :: String.t() | {:error, :host_not_found}
|
|
|
|
defp apply_webfinger_endpoint(actor) do
|
|
|
|
with {:ok, domain} <- domain_from_federated_actor(actor) do
|
|
|
|
case find_webfinger_endpoint(domain) do
|
|
|
|
{:ok, link_template} ->
|
2021-12-29 13:58:38 +00:00
|
|
|
Logger.debug("Using webfinger location provided by host-meta endpoint")
|
2021-04-09 08:19:25 +00:00
|
|
|
String.replace(link_template, "{uri}", "acct:#{actor}")
|
|
|
|
|
|
|
|
_ ->
|
2021-12-29 13:58:38 +00:00
|
|
|
Logger.debug("Using default webfinger location")
|
2022-04-20 16:38:03 +00:00
|
|
|
prefix = if Application.fetch_env!(:mobilizon, :env) !== :dev, do: "https", else: "http"
|
|
|
|
"#{prefix}://#{domain}/.well-known/webfinger?resource=acct:#{actor}"
|
2021-04-09 08:19:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec domain_from_federated_actor(String.t()) :: {:ok, String.t()} | {:error, :host_not_found}
|
|
|
|
defp domain_from_federated_actor(actor) do
|
|
|
|
case String.split(actor, "@") do
|
|
|
|
[_name, domain] ->
|
|
|
|
{:ok, domain}
|
|
|
|
|
|
|
|
_e ->
|
|
|
|
host = URI.parse(actor).host
|
|
|
|
if is_nil(host), do: {:error, :host_not_found}, else: {:ok, host}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec webfinger_from_json(map() | String.t()) ::
|
2022-04-20 09:35:22 +00:00
|
|
|
{:ok, map()}
|
|
|
|
| {:error, :webfinger_information_not_json | :webfinger_information_not_valid}
|
2021-04-09 08:19:25 +00:00
|
|
|
defp webfinger_from_json(doc) when is_map(doc) do
|
2022-04-20 09:35:22 +00:00
|
|
|
links = Map.get(doc, "links")
|
|
|
|
subject = Map.get(doc, "subject")
|
|
|
|
|
|
|
|
if !is_nil(links) && !is_nil(subject) do
|
|
|
|
data =
|
|
|
|
Enum.reduce(links, %{"subject" => subject}, fn link, data ->
|
|
|
|
case {link["type"], link["rel"]} do
|
|
|
|
{"application/activity+json", "self"} ->
|
|
|
|
Map.put(data, "url", link["href"])
|
|
|
|
|
|
|
|
{nil, _rel} ->
|
|
|
|
Logger.debug("No type declared for the following link #{inspect(link)}")
|
|
|
|
data
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Logger.debug(fn ->
|
|
|
|
"Unhandled type to finger: #{inspect(link)}"
|
|
|
|
end)
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
{:ok, data}
|
|
|
|
else
|
|
|
|
{:error, :webfinger_information_not_valid}
|
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2021-04-09 08:19:25 +00:00
|
|
|
defp webfinger_from_json(_doc), do: {:error, :webfinger_information_not_json}
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2021-04-09 08:19:25 +00:00
|
|
|
@spec find_link_from_template(String.t()) :: String.t() | {:error, :link_not_found}
|
|
|
|
defp find_link_from_template(doc) do
|
|
|
|
with res when res in [nil, ""] <-
|
|
|
|
xpath(doc, ~x"//Link[@rel=\"lrdd\"][@type=\"application/json\"]/@template"s),
|
|
|
|
res when res in [nil, ""] <- xpath(doc, ~x"//Link[@rel=\"lrdd\"]/@template"s),
|
|
|
|
do: {:error, :link_not_found}
|
2021-06-10 09:40:54 +00:00
|
|
|
catch
|
|
|
|
:exit, _e ->
|
|
|
|
{:error, :link_not_found}
|
2021-04-09 08:19:25 +00:00
|
|
|
end
|
2018-07-27 08:45:35 +00:00
|
|
|
|
2021-04-09 08:19:25 +00:00
|
|
|
@spec address_invalid(String.t()) :: false | {:error, :invalid_address}
|
|
|
|
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_address}
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|