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
9 alias Pleroma.Web.Federator.Publisher
11 alias Pleroma.XmlBuilder
16 base_url = Web.base_url()
20 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
25 type: "application/xrd+xml",
26 template: "#{base_url}/.well-known/webfinger?resource={uri}"
30 |> XmlBuilder.to_doc()
33 def webfinger(resource, fmt) when fmt in ["XML", "JSON"] do
34 host = Pleroma.Web.Endpoint.host()
35 regex = ~r/(acct:)?(?<username>[a-z0-9A-Z_\.-]+)@#{host}/
37 with %{"username" => username} <- Regex.named_captures(regex, resource),
38 %User{} = user <- User.get_cached_by_nickname(username) do
39 {:ok, represent_user(user, fmt)}
42 with %User{} = user <- User.get_cached_by_ap_id(resource) do
43 {:ok, represent_user(user, fmt)}
46 {:error, "Couldn't find user"}
51 defp gather_links(%User{} = user) do
54 "rel" => "http://webfinger.net/rel/profile-page",
55 "type" => "text/html",
58 ] ++ Publisher.gather_webfinger_links(user)
61 def represent_user(user, "JSON") do
62 {:ok, user} = User.ensure_keys_present(user)
65 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
66 "aliases" => [user.ap_id],
67 "links" => gather_links(user)
71 def represent_user(user, "XML") do
72 {:ok, user} = User.ensure_keys_present(user)
76 |> Enum.map(fn link -> {:Link, link} end)
80 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
82 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
86 |> XmlBuilder.to_doc()
89 defp get_magic_key("data:application/magic-public-key," <> magic_key) do
93 defp get_magic_key(nil) do
94 Logger.debug("Undefined magic key.")
98 defp get_magic_key(_) do
99 {:error, "Missing magic key data."}
102 defp webfinger_from_xml(doc) do
103 with magic_key <- XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc),
104 {:ok, magic_key} <- get_magic_key(magic_key),
106 XML.string_from_xpath(
107 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
110 subject <- XML.string_from_xpath("//Subject", doc),
111 salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
113 XML.string_from_xpath(
114 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
118 XML.string_from_xpath(
119 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
123 "magic_key" => magic_key,
125 "subject" => subject,
127 "subscribe_address" => subscribe_address,
141 defp webfinger_from_json(doc) do
143 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
144 case {link["type"], link["rel"]} do
145 {"application/activity+json", "self"} ->
146 Map.put(data, "ap_id", link["href"])
148 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
149 Map.put(data, "ap_id", link["href"])
151 {_, "magic-public-key"} ->
152 "data:application/magic-public-key," <> magic_key = link["href"]
153 Map.put(data, "magic_key", magic_key)
155 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
156 Map.put(data, "topic", link["href"])
159 Map.put(data, "salmon", link["href"])
161 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
162 Map.put(data, "subscribe_address", link["template"])
165 Logger.debug("Unhandled type: #{inspect(link["type"])}")
173 def get_template_from_xml(body) do
174 xpath = "//Link[@rel='lrdd']/@template"
176 with doc when doc != :error <- XML.parse_document(body),
177 template when template != nil <- XML.string_from_xpath(xpath, doc) do
182 def find_lrdd_template(domain) do
183 with {:ok, %{status: status, body: body}} when status in 200..299 <-
184 HTTP.get("http://#{domain}/.well-known/host-meta", []) do
185 get_template_from_xml(body)
188 with {:ok, %{body: body}} <- HTTP.get("https://#{domain}/.well-known/host-meta", []) do
189 get_template_from_xml(body)
191 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
196 @spec finger(String.t()) :: {:ok, map()} | {:error, any()}
197 def finger(account) do
198 account = String.trim_leading(account, "@")
201 with [_name, domain] <- String.split(account, "@") do
205 URI.parse(account).host
209 case find_lrdd_template(domain) do
211 String.replace(template, "{uri}", URI.encode(account))
214 "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
220 Accept: "application/xrd+xml,application/jrd+json"
222 {:ok, %{status: status, body: body}} when status in 200..299 <- response do
223 doc = XML.parse_document(body)
226 webfinger_from_xml(doc)
228 with {:ok, doc} <- Jason.decode(body) do
229 webfinger_from_json(doc)
234 Logger.debug(fn -> "Couldn't finger #{account}" end)
235 Logger.debug(fn -> inspect(e) end)