defmodule Pleroma.Web.WebFinger do
@httpoison Application.get_env(:pleroma, :httpoison)
- alias Pleroma.{Repo, User, XmlBuilder}
+ alias Pleroma.{User, XmlBuilder}
alias Pleroma.Web
alias Pleroma.Web.{XML, Salmon, OStatus}
require Jason
"href" => "data:application/magic-public-key,#{magic_key}"
},
%{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
+ %{
+ "rel" => "self",
+ "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
+ "href" => user.ap_id
+ },
%{
"rel" => "http://ostatus.org/schema/1.0/subscribe",
"template" => OStatus.remote_follow_path()
end
end
- defp webfinger_from_xml(doc) do
- magic_key = XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc)
+ defp get_magic_key(magic_key) do
"data:application/magic-public-key," <> magic_key = magic_key
+ {:ok, magic_key}
+ rescue
+ MatchError -> {:error, "Missing magic key data."}
+ end
- topic =
- XML.string_from_xpath(
- ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
- doc
- )
-
- subject = XML.string_from_xpath("//Subject", doc)
- salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
-
- subscribe_address =
- XML.string_from_xpath(
- ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
- doc
- )
-
- ap_id =
- XML.string_from_xpath(
- ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
- doc
- )
-
- data = %{
- "magic_key" => magic_key,
- "topic" => topic,
- "subject" => subject,
- "salmon" => salmon,
- "subscribe_address" => subscribe_address,
- "ap_id" => ap_id
- }
+ defp webfinger_from_xml(doc) do
+ with magic_key <- XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc),
+ {:ok, magic_key} <- get_magic_key(magic_key),
+ topic <-
+ XML.string_from_xpath(
+ ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
+ doc
+ ),
+ subject <- XML.string_from_xpath("//Subject", doc),
+ salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
+ subscribe_address <-
+ XML.string_from_xpath(
+ ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
+ doc
+ ),
+ ap_id <-
+ XML.string_from_xpath(
+ ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
+ doc
+ ) do
+ data = %{
+ "magic_key" => magic_key,
+ "topic" => topic,
+ "subject" => subject,
+ "salmon" => salmon,
+ "subscribe_address" => subscribe_address,
+ "ap_id" => ap_id
+ }
- {:ok, data}
+ {:ok, data}
+ else
+ {:error, e} ->
+ {:error, e}
+
+ e ->
+ {:error, e}
+ end
end
defp webfinger_from_json(doc) do
{"application/activity+json", "self"} ->
Map.put(data, "ap_id", link["href"])
+ {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
+ Map.put(data, "ap_id", link["href"])
+
{_, "magic-public-key"} ->
"data:application/magic-public-key," <> magic_key = link["href"]
Map.put(data, "magic_key", magic_key)
end
def get_template_from_xml(body) do
- xpath = "//Link[@rel='lrdd' and @type='application/xrd+xml']/@template"
+ xpath = "//Link[@rel='lrdd']/@template"
with doc when doc != :error <- XML.parse_document(body),
template when template != nil <- XML.string_from_xpath(xpath, doc) do
URI.parse(account).host
end
- case find_lrdd_template(domain) do
- {:ok, template} ->
- address = String.replace(template, "{uri}", URI.encode(account))
+ address =
+ case find_lrdd_template(domain) do
+ {:ok, template} ->
+ String.replace(template, "{uri}", URI.encode(account))
- _ ->
- address = "http://#{domain}/.well-known/webfinger?resource=acct:#{account}"
- end
+ _ ->
+ "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
+ end
with response <-
@httpoison.get(
if doc != :error do
webfinger_from_xml(doc)
else
- {:ok, doc} = Jason.decode(body)
- webfinger_from_json(doc)
+ with {:ok, doc} <- Jason.decode(body) do
+ webfinger_from_json(doc)
+ else
+ {:error, e} -> e
+ end
end
else
e ->