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},
90 "rel" => "http://ostatus.org/schema/1.0/subscribe",
91 "template" => OStatus.remote_follow_path()
97 def represent_user(user, "XML") do
98 {:ok, user} = ensure_keys_present(user)
99 {:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
100 magic_key = Salmon.encode_key(public)
104 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
106 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
107 {:Alias, user.ap_id},
110 rel: "http://schemas.google.com/g/2010#updates-from",
111 type: "application/atom+xml",
112 href: OStatus.feed_path(user)
115 %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
116 {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
118 %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}},
119 {:Link, %{rel: "self", type: "application/activity+json", href: user.ap_id}},
121 %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}}
124 |> XmlBuilder.to_doc()
127 # This seems a better fit in Salmon
128 def ensure_keys_present(user) do
129 info = user.info || %{}
134 {:ok, pem} = Salmon.generate_rsa_pem()
135 info = Map.put(info, "keys", pem)
137 Ecto.Changeset.change(user, info: info)
138 |> User.update_and_set_cache()
142 defp webfinger_from_xml(doc) do
143 magic_key = XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc)
144 "data:application/magic-public-key," <> magic_key = magic_key
147 XML.string_from_xpath(
148 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
152 subject = XML.string_from_xpath("//Subject", doc)
153 salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
156 XML.string_from_xpath(
157 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
162 XML.string_from_xpath(
163 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
168 "magic_key" => magic_key,
170 "subject" => subject,
172 "subscribe_address" => subscribe_address,
179 defp webfinger_from_json(doc) do
181 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
182 case {link["type"], link["rel"]} do
183 {"application/activity+json", "self"} ->
184 Map.put(data, "ap_id", link["href"])
186 {_, "magic-public-key"} ->
187 "data:application/magic-public-key," <> magic_key = link["href"]
188 Map.put(data, "magic_key", magic_key)
190 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
191 Map.put(data, "topic", link["href"])
194 Map.put(data, "salmon", link["href"])
196 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
197 Map.put(data, "subscribe_address", link["template"])
200 Logger.debug("Unhandled type: #{inspect(link["type"])}")
208 def get_template_from_xml(body) do
209 xpath = "//Link[@rel='lrdd' and @type='application/xrd+xml']/@template"
211 with doc when doc != :error <- XML.parse_document(body),
212 template when template != nil <- XML.string_from_xpath(xpath, doc) do
217 def find_lrdd_template(domain) do
218 with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <-
219 @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do
220 get_template_from_xml(body)
223 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
224 get_template_from_xml(body)
226 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
231 def finger(account) do
232 account = String.trim_leading(account, "@")
235 with [_name, domain] <- String.split(account, "@") do
239 URI.parse(account).host
242 case find_lrdd_template(domain) do
244 address = String.replace(template, "{uri}", URI.encode(account))
247 address = "http://#{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 {:ok, doc} = Jason.decode(body)
263 webfinger_from_json(doc)
267 Logger.debug(fn -> "Couldn't finger #{account}" end)
268 Logger.debug(fn -> inspect(e) end)