Merge branch 'develop' into feature/admin-api-user-statuses
[akkoma] / lib / pleroma / web / web_finger / web_finger.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 get_magic_key(magic_key) do
90 "data:application/magic-public-key," <> magic_key = magic_key
91 {:ok, magic_key}
92 rescue
93 MatchError -> {:error, "Missing magic key data."}
94 end
95
96 defp webfinger_from_xml(doc) do
97 with magic_key <- XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc),
98 {:ok, magic_key} <- get_magic_key(magic_key),
99 topic <-
100 XML.string_from_xpath(
101 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
102 doc
103 ),
104 subject <- XML.string_from_xpath("//Subject", doc),
105 salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
106 subscribe_address <-
107 XML.string_from_xpath(
108 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
109 doc
110 ),
111 ap_id <-
112 XML.string_from_xpath(
113 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
114 doc
115 ) do
116 data = %{
117 "magic_key" => magic_key,
118 "topic" => topic,
119 "subject" => subject,
120 "salmon" => salmon,
121 "subscribe_address" => subscribe_address,
122 "ap_id" => ap_id
123 }
124
125 {:ok, data}
126 else
127 {:error, e} ->
128 {:error, e}
129
130 e ->
131 {:error, e}
132 end
133 end
134
135 defp webfinger_from_json(doc) do
136 data =
137 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
138 case {link["type"], link["rel"]} do
139 {"application/activity+json", "self"} ->
140 Map.put(data, "ap_id", link["href"])
141
142 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
143 Map.put(data, "ap_id", link["href"])
144
145 {_, "magic-public-key"} ->
146 "data:application/magic-public-key," <> magic_key = link["href"]
147 Map.put(data, "magic_key", magic_key)
148
149 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
150 Map.put(data, "topic", link["href"])
151
152 {_, "salmon"} ->
153 Map.put(data, "salmon", link["href"])
154
155 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
156 Map.put(data, "subscribe_address", link["template"])
157
158 _ ->
159 Logger.debug("Unhandled type: #{inspect(link["type"])}")
160 data
161 end
162 end)
163
164 {:ok, data}
165 end
166
167 def get_template_from_xml(body) do
168 xpath = "//Link[@rel='lrdd']/@template"
169
170 with doc when doc != :error <- XML.parse_document(body),
171 template when template != nil <- XML.string_from_xpath(xpath, doc) do
172 {:ok, template}
173 end
174 end
175
176 def find_lrdd_template(domain) do
177 with {:ok, %{status: status, body: body}} when status in 200..299 <-
178 HTTP.get("http://#{domain}/.well-known/host-meta", []) do
179 get_template_from_xml(body)
180 else
181 _ ->
182 with {:ok, %{body: body}} <- HTTP.get("https://#{domain}/.well-known/host-meta", []) do
183 get_template_from_xml(body)
184 else
185 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
186 end
187 end
188 end
189
190 def finger(account) do
191 account = String.trim_leading(account, "@")
192
193 domain =
194 with [_name, domain] <- String.split(account, "@") do
195 domain
196 else
197 _e ->
198 URI.parse(account).host
199 end
200
201 address =
202 case find_lrdd_template(domain) do
203 {:ok, template} ->
204 String.replace(template, "{uri}", URI.encode(account))
205
206 _ ->
207 "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
208 end
209
210 with response <-
211 HTTP.get(
212 address,
213 Accept: "application/xrd+xml,application/jrd+json"
214 ),
215 {:ok, %{status: status, body: body}} when status in 200..299 <- response do
216 doc = XML.parse_document(body)
217
218 if doc != :error do
219 webfinger_from_xml(doc)
220 else
221 with {:ok, doc} <- Jason.decode(body) do
222 webfinger_from_json(doc)
223 else
224 {:error, e} -> e
225 end
226 end
227 else
228 e ->
229 Logger.debug(fn -> "Couldn't finger #{account}" end)
230 Logger.debug(fn -> inspect(e) end)
231 {:error, e}
232 end
233 end
234 end