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)
10 alias Pleroma.Web.Federator.Publisher
12 alias Pleroma.XmlBuilder
17 base_url = Web.base_url()
21 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
26 type: "application/xrd+xml",
27 template: "#{base_url}/.well-known/webfinger?resource={uri}"
31 |> XmlBuilder.to_doc()
34 def webfinger(resource, fmt) when fmt in ["XML", "JSON"] do
35 host = Pleroma.Web.Endpoint.host()
36 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
38 with %{"username" => username} <- Regex.named_captures(regex, resource),
39 %User{} = user <- User.get_cached_by_nickname(username) do
40 {:ok, represent_user(user, fmt)}
43 with %User{} = user <- User.get_cached_by_ap_id(resource) do
44 {:ok, represent_user(user, fmt)}
47 {:error, "Couldn't find user"}
52 defp gather_links(%User{} = user) do
55 "rel" => "http://webfinger.net/rel/profile-page",
56 "type" => "text/html",
59 ] ++ Publisher.gather_webfinger_links(user)
62 def represent_user(user, "JSON") do
63 {:ok, user} = User.ensure_keys_present(user)
66 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
67 "aliases" => [user.ap_id],
68 "links" => gather_links(user)
72 def represent_user(user, "XML") do
73 {:ok, user} = User.ensure_keys_present(user)
77 |> Enum.map(fn link -> {:Link, link} end)
81 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
83 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
87 |> XmlBuilder.to_doc()
90 defp get_magic_key(magic_key) do
91 "data:application/magic-public-key," <> magic_key = magic_key
94 MatchError -> {:error, "Missing magic key data."}
97 defp webfinger_from_xml(doc) do
98 with magic_key <- XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc),
99 {:ok, magic_key} <- get_magic_key(magic_key),
101 XML.string_from_xpath(
102 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
105 subject <- XML.string_from_xpath("//Subject", doc),
106 salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
108 XML.string_from_xpath(
109 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
113 XML.string_from_xpath(
114 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
118 "magic_key" => magic_key,
120 "subject" => subject,
122 "subscribe_address" => subscribe_address,
136 defp webfinger_from_json(doc) do
138 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
139 case {link["type"], link["rel"]} do
140 {"application/activity+json", "self"} ->
141 Map.put(data, "ap_id", link["href"])
143 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
144 Map.put(data, "ap_id", link["href"])
146 {_, "magic-public-key"} ->
147 "data:application/magic-public-key," <> magic_key = link["href"]
148 Map.put(data, "magic_key", magic_key)
150 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
151 Map.put(data, "topic", link["href"])
154 Map.put(data, "salmon", link["href"])
156 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
157 Map.put(data, "subscribe_address", link["template"])
160 Logger.debug("Unhandled type: #{inspect(link["type"])}")
168 def get_template_from_xml(body) do
169 xpath = "//Link[@rel='lrdd']/@template"
171 with doc when doc != :error <- XML.parse_document(body),
172 template when template != nil <- XML.string_from_xpath(xpath, doc) do
177 def find_lrdd_template(domain) do
178 with {:ok, %{status: status, body: body}} when status in 200..299 <-
179 @httpoison.get("http://#{domain}/.well-known/host-meta", []) do
180 get_template_from_xml(body)
183 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
184 get_template_from_xml(body)
186 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
191 def finger(account) do
192 account = String.trim_leading(account, "@")
195 with [_name, domain] <- String.split(account, "@") do
199 URI.parse(account).host
203 case find_lrdd_template(domain) do
205 String.replace(template, "{uri}", URI.encode(account))
208 "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
214 Accept: "application/xrd+xml,application/jrd+json"
216 {:ok, %{status: status, body: body}} when status in 200..299 <- response do
217 doc = XML.parse_document(body)
220 webfinger_from_xml(doc)
222 with {:ok, doc} <- Jason.decode(body) do
223 webfinger_from_json(doc)
230 Logger.debug(fn -> "Couldn't finger #{account}" end)
231 Logger.debug(fn -> inspect(e) end)