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
8 alias Pleroma.Web.Endpoint
9 alias Pleroma.Web.Federator.Publisher
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
72 {:ok, user} = User.ensure_keys_present(user)
75 "subject" => "acct:#{user.nickname}@#{domain()}",
76 "aliases" => gather_aliases(user),
77 "links" => gather_links(user)
81 def represent_user(user, "XML") do
82 {:ok, user} = User.ensure_keys_present(user)
87 |> Enum.map(&{:Alias, &1})
91 |> Enum.map(fn link -> {:Link, link} end)
95 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
97 {:Subject, "acct:#{user.nickname}@#{domain()}"}
100 |> XmlBuilder.to_doc()
104 Pleroma.Config.get([__MODULE__, :domain]) || Pleroma.Web.Endpoint.host()
107 defp webfinger_from_xml(body) do
108 with {:ok, doc} <- XML.parse_document(body) do
109 subject = XML.string_from_xpath("//Subject", doc)
112 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template}
113 |> XML.string_from_xpath(doc)
116 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href}
117 |> XML.string_from_xpath(doc)
120 "subject" => subject,
121 "subscribe_address" => subscribe_address,
129 defp webfinger_from_json(body) do
130 with {:ok, doc} <- Jason.decode(body) do
132 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
133 case {link["type"], link["rel"]} do
134 {"application/activity+json", "self"} ->
135 Map.put(data, "ap_id", link["href"])
137 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
138 Map.put(data, "ap_id", link["href"])
140 {nil, "http://ostatus.org/schema/1.0/subscribe"} ->
141 Map.put(data, "subscribe_address", link["template"])
144 Logger.debug("Unhandled type: #{inspect(link["type"])}")
153 def get_template_from_xml(body) do
154 xpath = "//Link[@rel='lrdd']/@template"
156 with {:ok, doc} <- XML.parse_document(body),
157 template when template != nil <- XML.string_from_xpath(xpath, doc) do
162 def find_lrdd_template(domain) do
163 # WebFinger is restricted to HTTPS - https://tools.ietf.org/html/rfc7033#section-9.1
164 meta_url = "https://#{domain}/.well-known/host-meta"
166 with {:ok, %{status: status, body: body}} when status in 200..299 <- HTTP.get(meta_url) do
167 get_template_from_xml(body)
170 Logger.warn("Can't find LRDD template in #{inspect(meta_url)}: #{inspect(error)}")
171 {:error, :lrdd_not_found}
175 defp get_address_from_domain(domain, encoded_account) when is_binary(domain) do
176 case find_lrdd_template(domain) do
178 String.replace(template, "{uri}", encoded_account)
181 "https://#{domain}/.well-known/webfinger?resource=#{encoded_account}"
185 defp get_address_from_domain(_, _), do: {:error, :webfinger_no_domain}
187 @spec finger(String.t()) :: {:ok, map()} | {:error, any()}
188 def finger(account) do
189 account = String.trim_leading(account, "@")
192 with [_name, domain] <- String.split(account, "@") do
196 URI.parse(account).host
199 encoded_account = URI.encode("acct:#{account}")
201 with address when is_binary(address) <- get_address_from_domain(domain, encoded_account),
202 {:ok, %{status: status, body: body, headers: headers}} when status in 200..299 <-
205 [{"accept", "application/xrd+xml,application/jrd+json"}]
207 case List.keyfind(headers, "content-type", 0) do
209 case Plug.Conn.Utils.media_type(content_type) do
210 {:ok, "application", subtype, _} when subtype in ~w(xrd+xml xml) ->
211 webfinger_from_xml(body)
213 {:ok, "application", subtype, _} when subtype in ~w(jrd+json json) ->
214 webfinger_from_json(body)
217 {:error, {:content_type, content_type}}
221 {:error, {:content_type, nil}}
225 Logger.debug("Couldn't finger #{account}: #{inspect(error)}")