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
116 info = user.info || %{}
121 {:ok, pem} = Salmon.generate_rsa_pem()
122 info = Map.put(info, "keys", pem)
124 Ecto.Changeset.change(user, info: info)
125 |> User.update_and_set_cache()
129 defp get_magic_key(magic_key) do
130 "data:application/magic-public-key," <> magic_key = magic_key
133 MatchError -> {:error, "Missing magic key data."}
136 defp webfinger_from_xml(doc) do
137 with magic_key <- XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc),
138 {:ok, magic_key} <- get_magic_key(magic_key),
140 XML.string_from_xpath(
141 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
144 subject <- XML.string_from_xpath("//Subject", doc),
145 salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
147 XML.string_from_xpath(
148 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
152 XML.string_from_xpath(
153 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
157 "magic_key" => magic_key,
159 "subject" => subject,
161 "subscribe_address" => subscribe_address,
175 defp webfinger_from_json(doc) do
177 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
178 case {link["type"], link["rel"]} do
179 {"application/activity+json", "self"} ->
180 Map.put(data, "ap_id", link["href"])
182 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
183 Map.put(data, "ap_id", link["href"])
185 {_, "magic-public-key"} ->
186 "data:application/magic-public-key," <> magic_key = link["href"]
187 Map.put(data, "magic_key", magic_key)
189 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
190 Map.put(data, "topic", link["href"])
193 Map.put(data, "salmon", link["href"])
195 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
196 Map.put(data, "subscribe_address", link["template"])
199 Logger.debug("Unhandled type: #{inspect(link["type"])}")
207 def get_template_from_xml(body) do
208 xpath = "//Link[@rel='lrdd']/@template"
210 with doc when doc != :error <- XML.parse_document(body),
211 template when template != nil <- XML.string_from_xpath(xpath, doc) do
216 def find_lrdd_template(domain) do
217 with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <-
218 @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do
219 get_template_from_xml(body)
222 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
223 get_template_from_xml(body)
225 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
230 def finger(account) do
231 account = String.trim_leading(account, "@")
234 with [_name, domain] <- String.split(account, "@") do
238 URI.parse(account).host
242 case find_lrdd_template(domain) do
244 String.replace(template, "{uri}", URI.encode(account))
247 "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
253 [Accept: "application/xrd+xml,application/jrd+json"],
254 follow_redirect: true
256 {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response do
257 doc = XML.parse_document(body)
260 webfinger_from_xml(doc)
262 with {:ok, doc} <- Jason.decode(body) do
263 webfinger_from_json(doc)
270 Logger.debug(fn -> "Couldn't finger #{account}" end)
271 Logger.debug(fn -> inspect(e) end)