2018-05-17 11:32:23 +02:00
|
|
|
defmodule Eventos.Service.WebFinger do
|
2018-06-14 18:15:27 +02:00
|
|
|
@moduledoc """
|
|
|
|
# WebFinger
|
|
|
|
|
|
|
|
Performs the WebFinger requests and responses (json only)
|
|
|
|
"""
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
alias Eventos.Actors
|
2018-05-17 11:32:23 +02:00
|
|
|
alias Eventos.Service.XmlBuilder
|
|
|
|
alias Eventos.Repo
|
|
|
|
require Jason
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
def host_meta do
|
|
|
|
base_url = EventosWeb.Endpoint.url()
|
|
|
|
|
|
|
|
{
|
|
|
|
: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
|
|
|
|
host = EventosWeb.Endpoint.host()
|
2018-05-18 09:56:21 +02:00
|
|
|
regex = ~r/(acct:)?(?<name>\w+)@#{host}/
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
with %{"name" => name} <- Regex.named_captures(regex, resource) do
|
|
|
|
user = Actors.get_local_actor_by_name(name)
|
2018-05-17 11:32:23 +02:00
|
|
|
{:ok, represent_user(user, "JSON")}
|
|
|
|
else
|
|
|
|
_e ->
|
2018-05-18 09:56:21 +02:00
|
|
|
with user when not is_nil(user) <- Actors.get_actor_by_url(resource) do
|
2018-05-17 11:32:23 +02:00
|
|
|
{:ok, represent_user(user, "JSON")}
|
|
|
|
else
|
|
|
|
_e ->
|
|
|
|
{:error, "Couldn't find user"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def represent_user(user, "JSON") do
|
|
|
|
%{
|
2018-05-18 09:56:21 +02:00
|
|
|
"subject" => "acct:#{user.preferred_username}@#{EventosWeb.Endpoint.host() <> ":4001"}",
|
2018-05-17 11:32:23 +02:00
|
|
|
"aliases" => [user.url],
|
|
|
|
"links" => [
|
2018-07-27 10:45:35 +02:00
|
|
|
%{"rel" => "self", "type" => "application/activity+json", "href" => user.url}
|
2018-05-17 11:32:23 +02: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 10:45:35 +02:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
_ ->
|
2018-07-27 10:45:35 +02:00
|
|
|
Logger.debug(fn ->
|
2018-06-14 18:15:27 +02:00
|
|
|
"Unhandled type: #{inspect(link["type"])}"
|
2018-07-27 10:45:35 +02:00
|
|
|
end)
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
data
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
{:ok, data}
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def finger(actor) do
|
|
|
|
actor = String.trim_leading(actor, "@")
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
domain =
|
2018-05-18 09:56:21 +02:00
|
|
|
with [_name, domain] <- String.split(actor, "@") do
|
2018-05-17 11:32:23 +02:00
|
|
|
domain
|
|
|
|
else
|
|
|
|
_e ->
|
2018-05-18 09:56:21 +02:00
|
|
|
URI.parse(actor).host
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
address = "http://#{domain}/.well-known/webfinger?resource=acct:#{actor}"
|
|
|
|
|
|
|
|
Logger.debug(inspect(address))
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
with {:ok, %HTTPoison.Response{} = response} <-
|
|
|
|
HTTPoison.get(
|
|
|
|
address,
|
|
|
|
[Accept: "application/json, application/activity+json, application/jrd+json"],
|
|
|
|
follow_redirect: true
|
|
|
|
),
|
2018-05-19 10:19:21 +02:00
|
|
|
%{status_code: status_code, body: body} when status_code in 200..299 <- response do
|
2018-07-27 10:45:35 +02:00
|
|
|
{:ok, doc} = Jason.decode(body)
|
|
|
|
webfinger_from_json(doc)
|
2018-05-17 11:32:23 +02:00
|
|
|
else
|
|
|
|
e ->
|
2018-05-18 09:56:21 +02:00
|
|
|
Logger.debug(fn -> "Couldn't finger #{actor}" end)
|
2018-05-17 11:32:23 +02:00
|
|
|
Logger.debug(fn -> inspect(e) end)
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|