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)
9 alias Pleroma.XmlBuilder
12 alias Pleroma.Web.Salmon
13 alias Pleroma.Web.OStatus
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_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 def represent_user(user, "JSON") do
54 {:ok, user} = ensure_keys_present(user)
55 {:ok, _private, public} = Salmon.keys_from_pem(user.info.keys)
56 magic_key = Salmon.encode_key(public)
59 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
60 "aliases" => [user.ap_id],
63 "rel" => "http://schemas.google.com/g/2010#updates-from",
64 "type" => "application/atom+xml",
65 "href" => OStatus.feed_path(user)
68 "rel" => "http://webfinger.net/rel/profile-page",
69 "type" => "text/html",
72 %{"rel" => "salmon", "href" => OStatus.salmon_path(user)},
74 "rel" => "magic-public-key",
75 "href" => "data:application/magic-public-key,#{magic_key}"
77 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
80 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
84 "rel" => "http://ostatus.org/schema/1.0/subscribe",
85 "template" => OStatus.remote_follow_path()
91 def represent_user(user, "XML") do
92 {:ok, user} = ensure_keys_present(user)
93 {:ok, _private, public} = Salmon.keys_from_pem(user.info.keys)
94 magic_key = Salmon.encode_key(public)
98 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
100 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
101 {:Alias, user.ap_id},
104 rel: "http://schemas.google.com/g/2010#updates-from",
105 type: "application/atom+xml",
106 href: OStatus.feed_path(user)
109 %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
110 {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
112 %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}},
113 {:Link, %{rel: "self", type: "application/activity+json", href: user.ap_id}},
115 %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}}
118 |> XmlBuilder.to_doc()
121 # This seems a better fit in Salmon
122 def ensure_keys_present(user) do
128 {:ok, pem} = Salmon.generate_rsa_pem()
132 |> Pleroma.User.Info.set_keys(pem)
135 Ecto.Changeset.change(user)
136 |> Ecto.Changeset.put_embed(:info, info_cng)
138 User.update_and_set_cache(cng)
142 defp get_magic_key(magic_key) do
143 "data:application/magic-public-key," <> magic_key = magic_key
146 MatchError -> {:error, "Missing magic key data."}
149 defp webfinger_from_xml(doc) do
150 with magic_key <- XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc),
151 {:ok, magic_key} <- get_magic_key(magic_key),
153 XML.string_from_xpath(
154 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
157 subject <- XML.string_from_xpath("//Subject", doc),
158 salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
160 XML.string_from_xpath(
161 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
165 XML.string_from_xpath(
166 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
170 "magic_key" => magic_key,
172 "subject" => subject,
174 "subscribe_address" => subscribe_address,
188 defp webfinger_from_json(doc) do
190 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
191 case {link["type"], link["rel"]} do
192 {"application/activity+json", "self"} ->
193 Map.put(data, "ap_id", link["href"])
195 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
196 Map.put(data, "ap_id", link["href"])
198 {_, "magic-public-key"} ->
199 "data:application/magic-public-key," <> magic_key = link["href"]
200 Map.put(data, "magic_key", magic_key)
202 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
203 Map.put(data, "topic", link["href"])
206 Map.put(data, "salmon", link["href"])
208 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
209 Map.put(data, "subscribe_address", link["template"])
212 Logger.debug("Unhandled type: #{inspect(link["type"])}")
220 def get_template_from_xml(body) do
221 xpath = "//Link[@rel='lrdd']/@template"
223 with doc when doc != :error <- XML.parse_document(body),
224 template when template != nil <- XML.string_from_xpath(xpath, doc) do
229 def find_lrdd_template(domain) do
230 with {:ok, %{status: status, body: body}} when status in 200..299 <-
231 @httpoison.get("http://#{domain}/.well-known/host-meta", []) do
232 get_template_from_xml(body)
235 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
236 get_template_from_xml(body)
238 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
243 def finger(account) do
244 account = String.trim_leading(account, "@")
247 with [_name, domain] <- String.split(account, "@") do
251 URI.parse(account).host
255 case find_lrdd_template(domain) do
257 String.replace(template, "{uri}", URI.encode(account))
260 "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
266 Accept: "application/xrd+xml,application/jrd+json"
268 {:ok, %{status: status, body: body}} when status in 200..299 <- response do
269 doc = XML.parse_document(body)
272 webfinger_from_xml(doc)
274 with {:ok, doc} <- Jason.decode(body) do
275 webfinger_from_json(doc)
282 Logger.debug(fn -> "Couldn't finger #{account}" end)
283 Logger.debug(fn -> inspect(e) end)