1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.WebFinger do
6 @httpoison Application.get_env(:pleroma, :httpoison)
10 alias Pleroma.Web.Federator.Publisher
11 alias Pleroma.Web.Salmon
13 alias Pleroma.XmlBuilder
18 base_url = Web.base_url()
22 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
27 type: "application/xrd+xml",
28 template: "#{base_url}/.well-known/webfinger?resource={uri}"
32 |> XmlBuilder.to_doc()
35 def webfinger(resource, fmt) when fmt in ["XML", "JSON"] do
36 host = Pleroma.Web.Endpoint.host()
37 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
39 with %{"username" => username} <- Regex.named_captures(regex, resource),
40 %User{} = user <- User.get_cached_by_nickname(username) do
41 {:ok, represent_user(user, fmt)}
44 with %User{} = user <- User.get_cached_by_ap_id(resource) do
45 {:ok, represent_user(user, fmt)}
48 {:error, "Couldn't find user"}
53 defp gather_links(%User{} = user) do
56 "rel" => "http://webfinger.net/rel/profile-page",
57 "type" => "text/html",
60 ] ++ Publisher.gather_webfinger_links(user)
63 def represent_user(user, "JSON") do
64 {:ok, user} = ensure_keys_present(user)
67 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
68 "aliases" => [user.ap_id],
69 "links" => gather_links(user)
73 def represent_user(user, "XML") do
74 {:ok, user} = ensure_keys_present(user)
78 |> Enum.map(fn link -> {:Link, link} end)
82 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
84 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
88 |> XmlBuilder.to_doc()
91 # This seems a better fit in Salmon
92 def ensure_keys_present(user) do
98 {:ok, pem} = Salmon.generate_rsa_pem()
102 |> Pleroma.User.Info.set_keys(pem)
105 Ecto.Changeset.change(user)
106 |> Ecto.Changeset.put_embed(:info, info_cng)
108 User.update_and_set_cache(cng)
112 defp get_magic_key(magic_key) do
113 "data:application/magic-public-key," <> magic_key = magic_key
116 MatchError -> {:error, "Missing magic key data."}
119 defp webfinger_from_xml(doc) do
120 with magic_key <- XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc),
121 {:ok, magic_key} <- get_magic_key(magic_key),
123 XML.string_from_xpath(
124 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
127 subject <- XML.string_from_xpath("//Subject", doc),
128 salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
130 XML.string_from_xpath(
131 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
135 XML.string_from_xpath(
136 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
140 "magic_key" => magic_key,
142 "subject" => subject,
144 "subscribe_address" => subscribe_address,
158 defp webfinger_from_json(doc) do
160 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
161 case {link["type"], link["rel"]} do
162 {"application/activity+json", "self"} ->
163 Map.put(data, "ap_id", link["href"])
165 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
166 Map.put(data, "ap_id", link["href"])
168 {_, "magic-public-key"} ->
169 "data:application/magic-public-key," <> magic_key = link["href"]
170 Map.put(data, "magic_key", magic_key)
172 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
173 Map.put(data, "topic", link["href"])
176 Map.put(data, "salmon", link["href"])
178 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
179 Map.put(data, "subscribe_address", link["template"])
182 Logger.debug("Unhandled type: #{inspect(link["type"])}")
190 def get_template_from_xml(body) do
191 xpath = "//Link[@rel='lrdd']/@template"
193 with doc when doc != :error <- XML.parse_document(body),
194 template when template != nil <- XML.string_from_xpath(xpath, doc) do
199 def find_lrdd_template(domain) do
200 with {:ok, %{status: status, body: body}} when status in 200..299 <-
201 @httpoison.get("http://#{domain}/.well-known/host-meta", []) do
202 get_template_from_xml(body)
205 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
206 get_template_from_xml(body)
208 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
213 def finger(account) do
214 account = String.trim_leading(account, "@")
217 with [_name, domain] <- String.split(account, "@") do
221 URI.parse(account).host
225 case find_lrdd_template(domain) do
227 String.replace(template, "{uri}", URI.encode(account))
230 "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
236 Accept: "application/xrd+xml,application/jrd+json"
238 {:ok, %{status: status, body: body}} when status in 200..299 <- response do
239 doc = XML.parse_document(body)
242 webfinger_from_xml(doc)
244 with {:ok, doc} <- Jason.decode(body) do
245 webfinger_from_json(doc)
252 Logger.debug(fn -> "Couldn't finger #{account}" end)
253 Logger.debug(fn -> inspect(e) end)