Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / web / web_finger.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.WebFinger do
6 alias Pleroma.HTTP
7 alias Pleroma.User
8 alias Pleroma.Web
9 alias Pleroma.Web.Federator.Publisher
10 alias Pleroma.Web.XML
11 alias Pleroma.XmlBuilder
12 require Jason
13 require Logger
14
15 def host_meta do
16 base_url = Web.base_url()
17
18 {
19 :XRD,
20 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
21 {
22 :Link,
23 %{
24 rel: "lrdd",
25 type: "application/xrd+xml",
26 template: "#{base_url}/.well-known/webfinger?resource={uri}"
27 }
28 }
29 }
30 |> XmlBuilder.to_doc()
31 end
32
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}/
36
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)}
40 else
41 _e ->
42 with %User{} = user <- User.get_cached_by_ap_id(resource) do
43 {:ok, represent_user(user, fmt)}
44 else
45 _e ->
46 {:error, "Couldn't find user"}
47 end
48 end
49 end
50
51 defp gather_links(%User{} = user) do
52 [
53 %{
54 "rel" => "http://webfinger.net/rel/profile-page",
55 "type" => "text/html",
56 "href" => user.ap_id
57 }
58 ] ++ Publisher.gather_webfinger_links(user)
59 end
60
61 def represent_user(user, "JSON") do
62 {:ok, user} = User.ensure_keys_present(user)
63
64 %{
65 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
66 "aliases" => [user.ap_id],
67 "links" => gather_links(user)
68 }
69 end
70
71 def represent_user(user, "XML") do
72 {:ok, user} = User.ensure_keys_present(user)
73
74 links =
75 gather_links(user)
76 |> Enum.map(fn link -> {:Link, link} end)
77
78 {
79 :XRD,
80 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
81 [
82 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
83 {:Alias, user.ap_id}
84 ] ++ links
85 }
86 |> XmlBuilder.to_doc()
87 end
88
89 defp webfinger_from_xml(doc) do
90 subject = XML.string_from_xpath("//Subject", doc)
91
92 subscribe_address =
93 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template}
94 |> XML.string_from_xpath(doc)
95
96 ap_id =
97 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href}
98 |> XML.string_from_xpath(doc)
99
100 data = %{
101 "subject" => subject,
102 "subscribe_address" => subscribe_address,
103 "ap_id" => ap_id
104 }
105
106 {:ok, data}
107 end
108
109 defp webfinger_from_json(doc) do
110 data =
111 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
112 case {link["type"], link["rel"]} do
113 {"application/activity+json", "self"} ->
114 Map.put(data, "ap_id", link["href"])
115
116 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
117 Map.put(data, "ap_id", link["href"])
118
119 {nil, "http://ostatus.org/schema/1.0/subscribe"} ->
120 Map.put(data, "subscribe_address", link["template"])
121
122 _ ->
123 Logger.debug("Unhandled type: #{inspect(link["type"])}")
124 data
125 end
126 end)
127
128 {:ok, data}
129 end
130
131 def get_template_from_xml(body) do
132 xpath = "//Link[@rel='lrdd']/@template"
133
134 with doc when doc != :error <- XML.parse_document(body),
135 template when template != nil <- XML.string_from_xpath(xpath, doc) do
136 {:ok, template}
137 end
138 end
139
140 def find_lrdd_template(domain) do
141 with {:ok, %{status: status, body: body}} when status in 200..299 <-
142 HTTP.get("http://#{domain}/.well-known/host-meta") do
143 get_template_from_xml(body)
144 else
145 _ ->
146 with {:ok, %{body: body, status: status}} when status in 200..299 <-
147 HTTP.get("https://#{domain}/.well-known/host-meta") do
148 get_template_from_xml(body)
149 else
150 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
151 end
152 end
153 end
154
155 defp get_address_from_domain(domain, encoded_account) when is_binary(domain) do
156 case find_lrdd_template(domain) do
157 {:ok, template} ->
158 String.replace(template, "{uri}", encoded_account)
159
160 _ ->
161 "https://#{domain}/.well-known/webfinger?resource=#{encoded_account}"
162 end
163 end
164
165 defp get_address_from_domain(_, _), do: nil
166
167 @spec finger(String.t()) :: {:ok, map()} | {:error, any()}
168 def finger(account) do
169 account = String.trim_leading(account, "@")
170
171 domain =
172 with [_name, domain] <- String.split(account, "@") do
173 domain
174 else
175 _e ->
176 URI.parse(account).host
177 end
178
179 encoded_account = URI.encode("acct:#{account}")
180
181 with address when is_binary(address) <- get_address_from_domain(domain, encoded_account),
182 response <-
183 HTTP.get(
184 address,
185 [{"accept", "application/xrd+xml,application/jrd+json"}]
186 ),
187 {:ok, %{status: status, body: body}} when status in 200..299 <- response do
188 doc = XML.parse_document(body)
189
190 if doc != :error do
191 webfinger_from_xml(doc)
192 else
193 with {:ok, doc} <- Jason.decode(body) do
194 webfinger_from_json(doc)
195 end
196 end
197 else
198 e ->
199 Logger.debug(fn -> "Couldn't finger #{account}" end)
200 Logger.debug(fn -> inspect(e) end)
201 {:error, e}
202 end
203 end
204 end