1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.WebFinger do
9 alias Pleroma.Web.Federator.Publisher
11 alias Pleroma.XmlBuilder
16 base_url = Web.base_url()
20 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
25 type: "application/xrd+xml",
26 template: "#{base_url}/.well-known/webfinger?resource={uri}"
30 |> XmlBuilder.to_doc()
33 def webfinger(resource, fmt) when fmt in ["XML", "JSON"] do
34 host = Pleroma.Web.Endpoint.host()
35 regex = ~r/(acct:)?(?<username>[a-z0-9A-Z_\.-]+)@#{host}/
37 with %{"username" => username} <- Regex.named_captures(regex, resource),
38 %User{} = user <- User.get_cached_by_nickname(username) do
39 {:ok, represent_user(user, fmt)}
42 with %User{} = user <- User.get_cached_by_ap_id(resource) do
43 {:ok, represent_user(user, fmt)}
46 {:error, "Couldn't find user"}
51 defp gather_links(%User{} = user) do
54 "rel" => "http://webfinger.net/rel/profile-page",
55 "type" => "text/html",
58 ] ++ Publisher.gather_webfinger_links(user)
61 defp gather_aliases(%User{} = user) do
62 [user.ap_id | user.also_known_as]
65 def represent_user(user, "JSON") do
66 {:ok, user} = User.ensure_keys_present(user)
69 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
70 "aliases" => gather_aliases(user),
71 "links" => gather_links(user)
75 def represent_user(user, "XML") do
76 {:ok, user} = User.ensure_keys_present(user)
81 |> Enum.map(&{:Alias, &1})
85 |> Enum.map(fn link -> {:Link, link} end)
89 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
91 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"}
94 |> XmlBuilder.to_doc()
97 defp webfinger_from_xml(doc) do
98 subject = XML.string_from_xpath("//Subject", doc)
101 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template}
102 |> XML.string_from_xpath(doc)
105 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href}
106 |> XML.string_from_xpath(doc)
109 "subject" => subject,
110 "subscribe_address" => subscribe_address,
117 defp webfinger_from_json(doc) do
119 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
120 case {link["type"], link["rel"]} do
121 {"application/activity+json", "self"} ->
122 Map.put(data, "ap_id", link["href"])
124 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
125 Map.put(data, "ap_id", link["href"])
127 {nil, "http://ostatus.org/schema/1.0/subscribe"} ->
128 Map.put(data, "subscribe_address", link["template"])
131 Logger.debug("Unhandled type: #{inspect(link["type"])}")
139 def get_template_from_xml(body) do
140 xpath = "//Link[@rel='lrdd']/@template"
142 with doc when doc != :error <- XML.parse_document(body),
143 template when template != nil <- XML.string_from_xpath(xpath, doc) do
148 def find_lrdd_template(domain) do
149 with {:ok, %{status: status, body: body}} when status in 200..299 <-
150 HTTP.get("http://#{domain}/.well-known/host-meta") do
151 get_template_from_xml(body)
154 with {:ok, %{body: body, status: status}} when status in 200..299 <-
155 HTTP.get("https://#{domain}/.well-known/host-meta") do
156 get_template_from_xml(body)
158 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
163 defp get_address_from_domain(domain, encoded_account) when is_binary(domain) do
164 case find_lrdd_template(domain) do
166 String.replace(template, "{uri}", encoded_account)
169 "https://#{domain}/.well-known/webfinger?resource=#{encoded_account}"
173 defp get_address_from_domain(_, _), do: nil
175 @spec finger(String.t()) :: {:ok, map()} | {:error, any()}
176 def finger(account) do
177 account = String.trim_leading(account, "@")
180 with [_name, domain] <- String.split(account, "@") do
184 URI.parse(account).host
187 encoded_account = URI.encode("acct:#{account}")
189 with address when is_binary(address) <- get_address_from_domain(domain, encoded_account),
193 [{"accept", "application/xrd+xml,application/jrd+json"}]
195 {:ok, %{status: status, body: body}} when status in 200..299 <- response do
196 doc = XML.parse_document(body)
199 webfinger_from_xml(doc)
201 with {:ok, doc} <- Jason.decode(body) do
202 webfinger_from_json(doc)
207 Logger.debug(fn -> "Couldn't finger #{account}" end)
208 Logger.debug(fn -> inspect(e) end)