1 defmodule Pleroma.Web.WebFinger do
2 @httpoison Application.get_env(:pleroma, :httpoison)
4 alias Pleroma.{User, XmlBuilder}
6 alias Pleroma.Web.{XML, Salmon, OStatus}
11 base_url = Web.base_url()
15 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
20 type: "application/xrd+xml",
21 template: "#{base_url}/.well-known/webfinger?resource={uri}"
25 |> XmlBuilder.to_doc()
28 def webfinger(resource, fmt) when fmt in ["XML", "JSON"] do
29 host = Pleroma.Web.Endpoint.host()
30 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
32 with %{"username" => username} <- Regex.named_captures(regex, resource),
33 %User{} = user <- User.get_by_nickname(username) do
34 {:ok, represent_user(user, fmt)}
37 with %User{} = user <- User.get_cached_by_ap_id(resource) do
38 {:ok, represent_user(user, fmt)}
41 {:error, "Couldn't find user"}
46 def represent_user(user, "JSON") do
47 {:ok, user} = ensure_keys_present(user)
48 {:ok, _private, public} = Salmon.keys_from_pem(user.info.keys)
49 magic_key = Salmon.encode_key(public)
52 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
53 "aliases" => [user.ap_id],
56 "rel" => "http://schemas.google.com/g/2010#updates-from",
57 "type" => "application/atom+xml",
58 "href" => OStatus.feed_path(user)
61 "rel" => "http://webfinger.net/rel/profile-page",
62 "type" => "text/html",
65 %{"rel" => "salmon", "href" => OStatus.salmon_path(user)},
67 "rel" => "magic-public-key",
68 "href" => "data:application/magic-public-key,#{magic_key}"
70 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
73 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
77 "rel" => "http://ostatus.org/schema/1.0/subscribe",
78 "template" => OStatus.remote_follow_path()
84 def represent_user(user, "XML") do
85 {:ok, user} = ensure_keys_present(user)
86 {:ok, _private, public} = Salmon.keys_from_pem(user.info.keys)
87 magic_key = Salmon.encode_key(public)
91 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
93 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
97 rel: "http://schemas.google.com/g/2010#updates-from",
98 type: "application/atom+xml",
99 href: OStatus.feed_path(user)
102 %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
103 {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
105 %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}},
106 {:Link, %{rel: "self", type: "application/activity+json", href: user.ap_id}},
108 %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}}
111 |> XmlBuilder.to_doc()
114 # This seems a better fit in Salmon
115 def ensure_keys_present(user) do
121 {:ok, pem} = Salmon.generate_rsa_pem()
125 |> Pleroma.User.Info.set_keys(pem)
128 Ecto.Changeset.change(user)
129 |> Ecto.Changeset.put_embed(:info, info_cng)
131 User.update_and_set_cache(cng)
135 defp get_magic_key(magic_key) do
136 "data:application/magic-public-key," <> magic_key = magic_key
139 MatchError -> {:error, "Missing magic key data."}
142 defp webfinger_from_xml(doc) do
143 with magic_key <- XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc),
144 {:ok, magic_key} <- get_magic_key(magic_key),
146 XML.string_from_xpath(
147 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
150 subject <- XML.string_from_xpath("//Subject", doc),
151 salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
153 XML.string_from_xpath(
154 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
158 XML.string_from_xpath(
159 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
163 "magic_key" => magic_key,
165 "subject" => subject,
167 "subscribe_address" => subscribe_address,
181 defp webfinger_from_json(doc) do
183 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
184 case {link["type"], link["rel"]} do
185 {"application/activity+json", "self"} ->
186 Map.put(data, "ap_id", link["href"])
188 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
189 Map.put(data, "ap_id", link["href"])
191 {_, "magic-public-key"} ->
192 "data:application/magic-public-key," <> magic_key = link["href"]
193 Map.put(data, "magic_key", magic_key)
195 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
196 Map.put(data, "topic", link["href"])
199 Map.put(data, "salmon", link["href"])
201 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
202 Map.put(data, "subscribe_address", link["template"])
205 Logger.debug("Unhandled type: #{inspect(link["type"])}")
213 def get_template_from_xml(body) do
214 xpath = "//Link[@rel='lrdd']/@template"
216 with doc when doc != :error <- XML.parse_document(body),
217 template when template != nil <- XML.string_from_xpath(xpath, doc) do
222 def find_lrdd_template(domain) do
223 with {:ok, %{status: status, body: body}} when status in 200..299 <-
224 @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do
225 get_template_from_xml(body)
228 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
229 get_template_from_xml(body)
231 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
236 def finger(account) do
237 account = String.trim_leading(account, "@")
240 with [_name, domain] <- String.split(account, "@") do
244 URI.parse(account).host
248 case find_lrdd_template(domain) do
250 String.replace(template, "{uri}", URI.encode(account))
253 "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
259 [Accept: "application/xrd+xml,application/jrd+json"],
260 follow_redirect: true
262 {:ok, %{status: status, body: body}} when status in 200..299 <- response do
263 doc = XML.parse_document(body)
266 webfinger_from_xml(doc)
268 with {:ok, doc} <- Jason.decode(body) do
269 webfinger_from_json(doc)
276 Logger.debug(fn -> "Couldn't finger #{account}" end)
277 Logger.debug(fn -> inspect(e) end)