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, "JSON") do
29 host = Pleroma.Web.Endpoint.host()
30 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
32 with %{"username" => username} <- Regex.named_captures(regex, resource) do
33 user = User.get_by_nickname(username)
34 {:ok, represent_user(user, "JSON")}
37 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
38 {:ok, represent_user(user, "JSON")}
41 {:error, "Couldn't find user"}
46 def webfinger(resource, "XML") do
47 host = Pleroma.Web.Endpoint.host()
48 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
50 with %{"username" => username} <- Regex.named_captures(regex, resource) do
51 user = User.get_by_nickname(username)
52 {:ok, represent_user(user, "XML")}
55 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
56 {:ok, represent_user(user, "XML")}
59 {:error, "Couldn't find user"}
64 def represent_user(user, "JSON") do
65 {:ok, user} = ensure_keys_present(user)
66 {:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
67 magic_key = Salmon.encode_key(public)
70 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
71 "aliases" => [user.ap_id],
74 "rel" => "http://schemas.google.com/g/2010#updates-from",
75 "type" => "application/atom+xml",
76 "href" => OStatus.feed_path(user)
79 "rel" => "http://webfinger.net/rel/profile-page",
80 "type" => "text/html",
83 %{"rel" => "salmon", "href" => OStatus.salmon_path(user)},
85 "rel" => "magic-public-key",
86 "href" => "data:application/magic-public-key,#{magic_key}"
88 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
91 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
95 "rel" => "http://ostatus.org/schema/1.0/subscribe",
96 "template" => OStatus.remote_follow_path()
102 def represent_user(user, "XML") do
103 {:ok, user} = ensure_keys_present(user)
104 {:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
105 magic_key = Salmon.encode_key(public)
109 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
111 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
112 {:Alias, user.ap_id},
115 rel: "http://schemas.google.com/g/2010#updates-from",
116 type: "application/atom+xml",
117 href: OStatus.feed_path(user)
120 %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
121 {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
123 %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}},
124 {:Link, %{rel: "self", type: "application/activity+json", href: user.ap_id}},
126 %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}}
129 |> XmlBuilder.to_doc()
132 # This seems a better fit in Salmon
133 def ensure_keys_present(user) do
134 info = user.info || %{}
139 {:ok, pem} = Salmon.generate_rsa_pem()
140 info = Map.put(info, "keys", pem)
142 Ecto.Changeset.change(user, info: info)
143 |> User.update_and_set_cache()
147 defp get_magic_key(magic_key) do
148 "data:application/magic-public-key," <> magic_key = magic_key
151 MatchError -> {:error, "Missing magic key data."}
154 defp webfinger_from_xml(doc) do
155 with magic_key <- XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc),
156 {:ok, magic_key} <- get_magic_key(magic_key),
158 XML.string_from_xpath(
159 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
162 subject <- XML.string_from_xpath("//Subject", doc),
163 salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
165 XML.string_from_xpath(
166 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
170 XML.string_from_xpath(
171 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
175 "magic_key" => magic_key,
177 "subject" => subject,
179 "subscribe_address" => subscribe_address,
193 defp webfinger_from_json(doc) do
195 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
196 case {link["type"], link["rel"]} do
197 {"application/activity+json", "self"} ->
198 Map.put(data, "ap_id", link["href"])
200 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
201 Map.put(data, "ap_id", link["href"])
203 {_, "magic-public-key"} ->
204 "data:application/magic-public-key," <> magic_key = link["href"]
205 Map.put(data, "magic_key", magic_key)
207 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
208 Map.put(data, "topic", link["href"])
211 Map.put(data, "salmon", link["href"])
213 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
214 Map.put(data, "subscribe_address", link["template"])
217 Logger.debug("Unhandled type: #{inspect(link["type"])}")
225 def get_template_from_xml(body) do
226 xpath = "//Link[@rel='lrdd']/@template"
228 with doc when doc != :error <- XML.parse_document(body),
229 template when template != nil <- XML.string_from_xpath(xpath, doc) do
234 def find_lrdd_template(domain) do
235 with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <-
236 @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do
237 get_template_from_xml(body)
240 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
241 get_template_from_xml(body)
243 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
248 def finger(account) do
249 account = String.trim_leading(account, "@")
252 with [_name, domain] <- String.split(account, "@") do
256 URI.parse(account).host
260 case find_lrdd_template(domain) do
262 String.replace(template, "{uri}", URI.encode(account))
265 "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
271 [Accept: "application/xrd+xml,application/jrd+json"],
272 follow_redirect: true
274 {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response do
275 doc = XML.parse_document(body)
278 webfinger_from_xml(doc)
280 with {:ok, doc} <- Jason.decode(body) do
281 webfinger_from_json(doc)
288 Logger.debug(fn -> "Couldn't finger #{account}" end)
289 Logger.debug(fn -> inspect(e) end)