1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.WebFinger do
8 alias Pleroma.Web.ActivityPub.Publisher
9 alias Pleroma.Web.Endpoint
11 alias Pleroma.XmlBuilder
16 base_url = Endpoint.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()
37 if webfinger_domain = Pleroma.Config.get([__MODULE__, :domain]) do
38 ~r/(acct:)?(?<username>[a-z0-9A-Z_\.-]+)@(#{host}|#{webfinger_domain})/
40 ~r/(acct:)?(?<username>[a-z0-9A-Z_\.-]+)@#{host}/
43 with %{"username" => username} <- Regex.named_captures(regex, resource),
44 %User{} = user <- User.get_cached_by_nickname(username) do
45 {:ok, represent_user(user, fmt)}
48 with %User{} = user <- User.get_cached_by_ap_id(resource) do
49 {:ok, represent_user(user, fmt)}
52 {:error, "Couldn't find user"}
57 defp gather_links(%User{} = user) do
60 "rel" => "http://webfinger.net/rel/profile-page",
61 "type" => "text/html",
64 ] ++ Publisher.gather_webfinger_links(user)
67 defp gather_aliases(%User{} = user) do
68 [user.ap_id | user.also_known_as]
71 def represent_user(user, "JSON") do
73 "subject" => "acct:#{user.nickname}@#{host()}",
74 "aliases" => gather_aliases(user),
75 "links" => gather_links(user)
79 def represent_user(user, "XML") do
83 |> Enum.map(&{:Alias, &1})
87 |> Enum.map(fn link -> {:Link, link} end)
91 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
93 {:Subject, "acct:#{user.nickname}@#{host()}"}
96 |> XmlBuilder.to_doc()
100 Pleroma.Config.get([__MODULE__, :domain]) || Pleroma.Web.Endpoint.host()
103 defp webfinger_from_xml(body) do
104 with {:ok, doc} <- XML.parse_document(body) do
105 subject = XML.string_from_xpath("//Subject", doc)
108 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template}
109 |> XML.string_from_xpath(doc)
112 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href}
113 |> XML.string_from_xpath(doc)
116 "subject" => subject,
117 "subscribe_address" => subscribe_address,
125 defp webfinger_from_json(body) do
126 with {:ok, doc} <- Jason.decode(body) do
128 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
129 case {link["type"], link["rel"]} do
130 {"application/activity+json", "self"} ->
131 Map.put(data, "ap_id", link["href"])
133 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
134 Map.put(data, "ap_id", link["href"])
136 {nil, "http://ostatus.org/schema/1.0/subscribe"} ->
137 Map.put(data, "subscribe_address", link["template"])
140 Logger.debug("Unhandled type: #{inspect(link["type"])}")
149 def get_template_from_xml(body) do
150 xpath = "//Link[@rel='lrdd']/@template"
152 with {:ok, doc} <- XML.parse_document(body),
153 template when template != nil <- XML.string_from_xpath(xpath, doc) do
158 def find_lrdd_template(domain) do
159 # WebFinger is restricted to HTTPS - https://tools.ietf.org/html/rfc7033#section-9.1
160 meta_url = "https://#{domain}/.well-known/host-meta"
162 with {:ok, %{status: status, body: body}} when status in 200..299 <- HTTP.get(meta_url) do
163 get_template_from_xml(body)
166 Logger.warning("Can't find LRDD template in #{inspect(meta_url)}: #{inspect(error)}")
167 {:error, :lrdd_not_found}
171 defp get_address_from_domain(domain, encoded_account) when is_binary(domain) do
172 case find_lrdd_template(domain) do
174 String.replace(template, "{uri}", encoded_account)
177 "https://#{domain}/.well-known/webfinger?resource=#{encoded_account}"
181 defp get_address_from_domain(_, _), do: {:error, :webfinger_no_domain}
183 @spec finger(String.t()) :: {:ok, map()} | {:error, any()}
184 def finger(account) do
185 account = String.trim_leading(account, "@")
188 with [_name, domain] <- String.split(account, "@") do
192 URI.parse(account).host
195 encoded_account = URI.encode("acct:#{account}")
197 with address when is_binary(address) <- get_address_from_domain(domain, encoded_account),
198 {:ok, %{status: status, body: body, headers: headers}} when status in 200..299 <-
201 [{"accept", "application/xrd+xml,application/jrd+json"}]
203 case List.keyfind(headers, "content-type", 0) do
205 case Plug.Conn.Utils.media_type(content_type) do
206 {:ok, "application", subtype, _} when subtype in ~w(xrd+xml xml) ->
207 webfinger_from_xml(body)
209 {:ok, "application", subtype, _} when subtype in ~w(jrd+json json) ->
210 webfinger_from_json(body)
213 {:error, {:content_type, content_type}}
217 {:error, {:content_type, nil}}
221 Logger.debug("Couldn't finger #{account}: #{inspect(error)}")