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
|
2020-06-15 09:13:20 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2020-01-22 21:40:40 +00:00
|
|
|
alias Mobilizon.Federation.WebFinger.XmlBuilder
|
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
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
@http_options [
|
2020-07-30 15:57:20 +00:00
|
|
|
adapter: [
|
|
|
|
follow_redirect: true,
|
|
|
|
ssl: [{:versions, [:"tlsv1.2"]}]
|
|
|
|
]
|
2020-02-18 07:57:00 +00:00
|
|
|
]
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
def host_meta do
|
2020-01-28 18:18:33 +00:00
|
|
|
base_url = Endpoint.url()
|
2018-05-17 09:32:23 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
:XRD,
|
|
|
|
%{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
|
|
|
|
{
|
|
|
|
:Link,
|
|
|
|
%{
|
|
|
|
rel: "lrdd",
|
|
|
|
type: "application/xrd+xml",
|
|
|
|
template: "#{base_url}/.well-known/webfinger?resource={uri}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|> XmlBuilder.to_doc()
|
|
|
|
end
|
|
|
|
|
|
|
|
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 ->
|
2020-06-15 09:13:20 +00:00
|
|
|
case ActivityPub.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 ->
|
2018-11-27 17:42:56 +00:00
|
|
|
{:error, "Couldn't find actor"}
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-27 17:42:56 +00:00
|
|
|
@spec represent_actor(Actor.t()) :: struct()
|
|
|
|
def represent_actor(actor), do: represent_actor(actor, "JSON")
|
|
|
|
|
2019-02-22 15:11:57 +00:00
|
|
|
@spec represent_actor(Actor.t(), String.t()) :: struct()
|
2018-11-27 17:42:56 +00:00
|
|
|
def represent_actor(actor, "JSON") do
|
2018-05-17 09:32:23 +00:00
|
|
|
%{
|
2020-01-28 18:18:33 +00:00
|
|
|
"subject" => "acct:#{actor.preferred_username}@#{Endpoint.host()}",
|
2018-11-27 17:42:56 +00:00
|
|
|
"aliases" => [actor.url],
|
2018-05-17 09:32:23 +00:00
|
|
|
"links" => [
|
2018-11-27 17:42:56 +00:00
|
|
|
%{"rel" => "self", "type" => "application/activity+json", "href" => actor.url},
|
2018-11-07 15:45:11 +00:00
|
|
|
%{
|
|
|
|
"rel" => "https://webfinger.net/rel/profile-page/",
|
|
|
|
"type" => "text/html",
|
2018-11-27 17:42:56 +00:00
|
|
|
"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
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp webfinger_from_json(doc) do
|
|
|
|
data =
|
|
|
|
Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
|
|
|
|
case {link["type"], link["rel"]} do
|
|
|
|
{"application/activity+json", "self"} ->
|
|
|
|
Map.put(data, "url", link["href"])
|
2018-07-27 08:45:35 +00:00
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
_ ->
|
2018-07-27 08:45:35 +00:00
|
|
|
Logger.debug(fn ->
|
2020-10-15 15:19:15 +00:00
|
|
|
"Unhandled type to finger: #{inspect(link["type"])}"
|
2018-07-27 08:45:35 +00:00
|
|
|
end)
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
data
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
{:ok, data}
|
|
|
|
end
|
|
|
|
|
2018-05-18 07:56:21 +00:00
|
|
|
def finger(actor) do
|
|
|
|
actor = String.trim_leading(actor, "@")
|
2018-05-17 09:32:23 +00:00
|
|
|
|
|
|
|
domain =
|
2019-07-23 16:06:22 +00:00
|
|
|
case String.split(actor, "@") do
|
|
|
|
[_name, domain] ->
|
|
|
|
domain
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
_e ->
|
2018-05-18 07:56:21 +00:00
|
|
|
URI.parse(actor).host
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2018-07-27 08:45:35 +00:00
|
|
|
address = "http://#{domain}/.well-known/webfinger?resource=acct:#{actor}"
|
|
|
|
|
|
|
|
Logger.debug(inspect(address))
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2018-11-07 15:19:44 +00:00
|
|
|
with false <- is_nil(domain),
|
2020-07-09 15:24:28 +00:00
|
|
|
{:ok, %{} = response} <-
|
|
|
|
Tesla.get(
|
2018-07-27 08:45:35 +00:00
|
|
|
address,
|
2020-07-09 15:24:28 +00:00
|
|
|
headers: [
|
|
|
|
{"accept", "application/json, application/activity+json, application/jrd+json"}
|
|
|
|
],
|
|
|
|
opts: @http_options
|
2018-07-27 08:45:35 +00:00
|
|
|
),
|
2020-07-09 15:24:28 +00:00
|
|
|
%{status: status, body: body} when status in 200..299 <- response,
|
2018-11-07 15:30:55 +00:00
|
|
|
{:ok, doc} <- Jason.decode(body) do
|
2018-07-27 08:45:35 +00:00
|
|
|
webfinger_from_json(doc)
|
2018-05-17 09:32:23 +00:00
|
|
|
else
|
|
|
|
e ->
|
2018-05-18 07:56:21 +00:00
|
|
|
Logger.debug(fn -> "Couldn't finger #{actor}" end)
|
2018-05-17 09:32:23 +00:00
|
|
|
Logger.debug(fn -> inspect(e) end)
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|