1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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)
8 alias Pleroma.{User, XmlBuilder}
10 alias Pleroma.Web.{XML, Salmon, OStatus}
15 base_url = Web.base_url()
19 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
24 type: "application/xrd+xml",
25 template: "#{base_url}/.well-known/webfinger?resource={uri}"
29 |> XmlBuilder.to_doc()
32 def webfinger(resource, fmt) when fmt in ["XML", "JSON"] do
33 host = Pleroma.Web.Endpoint.host()
34 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
36 with %{"username" => username} <- Regex.named_captures(regex, resource),
37 %User{} = user <- User.get_by_nickname(username) do
38 {:ok, represent_user(user, fmt)}
41 with %User{} = user <- User.get_cached_by_ap_id(resource) do
42 {:ok, represent_user(user, fmt)}
45 {:error, "Couldn't find user"}
50 def represent_user(user, "JSON") do
51 {:ok, user} = ensure_keys_present(user)
52 {:ok, _private, public} = Salmon.keys_from_pem(user.info.keys)
53 magic_key = Salmon.encode_key(public)
56 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
57 "aliases" => [user.ap_id],
60 "rel" => "http://schemas.google.com/g/2010#updates-from",
61 "type" => "application/atom+xml",
62 "href" => OStatus.feed_path(user)
65 "rel" => "http://webfinger.net/rel/profile-page",
66 "type" => "text/html",
69 %{"rel" => "salmon", "href" => OStatus.salmon_path(user)},
71 "rel" => "magic-public-key",
72 "href" => "data:application/magic-public-key,#{magic_key}"
74 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
77 "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
81 "rel" => "http://ostatus.org/schema/1.0/subscribe",
82 "template" => OStatus.remote_follow_path()
88 def represent_user(user, "XML") do
89 {:ok, user} = ensure_keys_present(user)
90 {:ok, _private, public} = Salmon.keys_from_pem(user.info.keys)
91 magic_key = Salmon.encode_key(public)
95 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
97 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
101 rel: "http://schemas.google.com/g/2010#updates-from",
102 type: "application/atom+xml",
103 href: OStatus.feed_path(user)
106 %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
107 {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
109 %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}},
110 {:Link, %{rel: "self", type: "application/activity+json", href: user.ap_id}},
112 %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}}
115 |> XmlBuilder.to_doc()
118 # This seems a better fit in Salmon
119 def ensure_keys_present(user) do
125 {:ok, pem} = Salmon.generate_rsa_pem()
129 |> Pleroma.User.Info.set_keys(pem)
132 Ecto.Changeset.change(user)
133 |> Ecto.Changeset.put_embed(:info, info_cng)
135 User.update_and_set_cache(cng)
139 defp get_magic_key(magic_key) do
140 "data:application/magic-public-key," <> magic_key = magic_key
143 MatchError -> {:error, "Missing magic key data."}
146 defp webfinger_from_xml(doc) do
147 with magic_key <- XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc),
148 {:ok, magic_key} <- get_magic_key(magic_key),
150 XML.string_from_xpath(
151 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
154 subject <- XML.string_from_xpath("//Subject", doc),
155 salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
157 XML.string_from_xpath(
158 ~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},
167 "magic_key" => magic_key,
169 "subject" => subject,
171 "subscribe_address" => subscribe_address,
185 defp webfinger_from_json(doc) do
187 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
188 case {link["type"], link["rel"]} do
189 {"application/activity+json", "self"} ->
190 Map.put(data, "ap_id", link["href"])
192 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
193 Map.put(data, "ap_id", link["href"])
195 {_, "magic-public-key"} ->
196 "data:application/magic-public-key," <> magic_key = link["href"]
197 Map.put(data, "magic_key", magic_key)
199 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
200 Map.put(data, "topic", link["href"])
203 Map.put(data, "salmon", link["href"])
205 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
206 Map.put(data, "subscribe_address", link["template"])
209 Logger.debug("Unhandled type: #{inspect(link["type"])}")
217 def get_template_from_xml(body) do
218 xpath = "//Link[@rel='lrdd']/@template"
220 with doc when doc != :error <- XML.parse_document(body),
221 template when template != nil <- XML.string_from_xpath(xpath, doc) do
226 def find_lrdd_template(domain) do
227 with {:ok, %{status: status, body: body}} when status in 200..299 <-
228 @httpoison.get("http://#{domain}/.well-known/host-meta", []) do
229 get_template_from_xml(body)
232 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
233 get_template_from_xml(body)
235 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
240 def finger(account) do
241 account = String.trim_leading(account, "@")
244 with [_name, domain] <- String.split(account, "@") do
248 URI.parse(account).host
252 case find_lrdd_template(domain) do
254 String.replace(template, "{uri}", URI.encode(account))
257 "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
263 Accept: "application/xrd+xml,application/jrd+json"
265 {:ok, %{status: status, body: body}} when status in 200..299 <- response do
266 doc = XML.parse_document(body)
269 webfinger_from_xml(doc)
271 with {:ok, doc} <- Jason.decode(body) do
272 webfinger_from_json(doc)
279 Logger.debug(fn -> "Couldn't finger #{account}" end)
280 Logger.debug(fn -> inspect(e) end)