Clean up unused aliases and imports
[akkoma] / lib / pleroma / web / web_finger / web_finger.ex
1 defmodule Pleroma.Web.WebFinger do
2 @httpoison Application.get_env(:pleroma, :httpoison)
3
4 alias Pleroma.{User, XmlBuilder}
5 alias Pleroma.Web
6 alias Pleroma.Web.{XML, Salmon, OStatus}
7 require Jason
8 require Logger
9
10 def host_meta do
11 base_url = Web.base_url()
12
13 {
14 :XRD,
15 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
16 {
17 :Link,
18 %{
19 rel: "lrdd",
20 type: "application/xrd+xml",
21 template: "#{base_url}/.well-known/webfinger?resource={uri}"
22 }
23 }
24 }
25 |> XmlBuilder.to_doc()
26 end
27
28 def webfinger(resource, "JSON") do
29 host = Pleroma.Web.Endpoint.host()
30 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
31
32 with %{"username" => username} <- Regex.named_captures(regex, resource) do
33 user = User.get_by_nickname(username)
34 {:ok, represent_user(user, "JSON")}
35 else
36 _e ->
37 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
38 {:ok, represent_user(user, "JSON")}
39 else
40 _e ->
41 {:error, "Couldn't find user"}
42 end
43 end
44 end
45
46 def webfinger(resource, "XML") do
47 host = Pleroma.Web.Endpoint.host()
48 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
49
50 with %{"username" => username} <- Regex.named_captures(regex, resource) do
51 user = User.get_by_nickname(username)
52 {:ok, represent_user(user, "XML")}
53 else
54 _e ->
55 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
56 {:ok, represent_user(user, "XML")}
57 else
58 _e ->
59 {:error, "Couldn't find user"}
60 end
61 end
62 end
63
64 def represent_user(user, "JSON") do
65 {:ok, user} = ensure_keys_present(user)
66 {:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
67 magic_key = Salmon.encode_key(public)
68
69 %{
70 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
71 "aliases" => [user.ap_id],
72 "links" => [
73 %{
74 "rel" => "http://schemas.google.com/g/2010#updates-from",
75 "type" => "application/atom+xml",
76 "href" => OStatus.feed_path(user)
77 },
78 %{
79 "rel" => "http://webfinger.net/rel/profile-page",
80 "type" => "text/html",
81 "href" => user.ap_id
82 },
83 %{"rel" => "salmon", "href" => OStatus.salmon_path(user)},
84 %{
85 "rel" => "magic-public-key",
86 "href" => "data:application/magic-public-key,#{magic_key}"
87 },
88 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
89 %{
90 "rel" => "http://ostatus.org/schema/1.0/subscribe",
91 "template" => OStatus.remote_follow_path()
92 }
93 ]
94 }
95 end
96
97 def represent_user(user, "XML") do
98 {:ok, user} = ensure_keys_present(user)
99 {:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
100 magic_key = Salmon.encode_key(public)
101
102 {
103 :XRD,
104 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
105 [
106 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
107 {:Alias, user.ap_id},
108 {:Link,
109 %{
110 rel: "http://schemas.google.com/g/2010#updates-from",
111 type: "application/atom+xml",
112 href: OStatus.feed_path(user)
113 }},
114 {:Link,
115 %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
116 {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
117 {:Link,
118 %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}},
119 {:Link, %{rel: "self", type: "application/activity+json", href: user.ap_id}},
120 {:Link,
121 %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}}
122 ]
123 }
124 |> XmlBuilder.to_doc()
125 end
126
127 # This seems a better fit in Salmon
128 def ensure_keys_present(user) do
129 info = user.info || %{}
130
131 if info["keys"] do
132 {:ok, user}
133 else
134 {:ok, pem} = Salmon.generate_rsa_pem()
135 info = Map.put(info, "keys", pem)
136
137 Ecto.Changeset.change(user, info: info)
138 |> User.update_and_set_cache()
139 end
140 end
141
142 defp webfinger_from_xml(doc) do
143 magic_key = XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc)
144 "data:application/magic-public-key," <> magic_key = magic_key
145
146 topic =
147 XML.string_from_xpath(
148 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
149 doc
150 )
151
152 subject = XML.string_from_xpath("//Subject", doc)
153 salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
154
155 subscribe_address =
156 XML.string_from_xpath(
157 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
158 doc
159 )
160
161 ap_id =
162 XML.string_from_xpath(
163 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
164 doc
165 )
166
167 data = %{
168 "magic_key" => magic_key,
169 "topic" => topic,
170 "subject" => subject,
171 "salmon" => salmon,
172 "subscribe_address" => subscribe_address,
173 "ap_id" => ap_id
174 }
175
176 {:ok, data}
177 end
178
179 defp webfinger_from_json(doc) do
180 data =
181 Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn link, data ->
182 case {link["type"], link["rel"]} do
183 {"application/activity+json", "self"} ->
184 Map.put(data, "ap_id", link["href"])
185
186 {_, "magic-public-key"} ->
187 "data:application/magic-public-key," <> magic_key = link["href"]
188 Map.put(data, "magic_key", magic_key)
189
190 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
191 Map.put(data, "topic", link["href"])
192
193 {_, "salmon"} ->
194 Map.put(data, "salmon", link["href"])
195
196 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
197 Map.put(data, "subscribe_address", link["template"])
198
199 _ ->
200 Logger.debug("Unhandled type: #{inspect(link["type"])}")
201 data
202 end
203 end)
204
205 {:ok, data}
206 end
207
208 def get_template_from_xml(body) do
209 xpath = "//Link[@rel='lrdd' and @type='application/xrd+xml']/@template"
210
211 with doc when doc != :error <- XML.parse_document(body),
212 template when template != nil <- XML.string_from_xpath(xpath, doc) do
213 {:ok, template}
214 end
215 end
216
217 def find_lrdd_template(domain) do
218 with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <-
219 @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do
220 get_template_from_xml(body)
221 else
222 _ ->
223 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
224 get_template_from_xml(body)
225 else
226 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
227 end
228 end
229 end
230
231 def finger(account) do
232 account = String.trim_leading(account, "@")
233
234 domain =
235 with [_name, domain] <- String.split(account, "@") do
236 domain
237 else
238 _e ->
239 URI.parse(account).host
240 end
241
242 case find_lrdd_template(domain) do
243 {:ok, template} ->
244 address = String.replace(template, "{uri}", URI.encode(account))
245
246 _ ->
247 address = "http://#{domain}/.well-known/webfinger?resource=acct:#{account}"
248 end
249
250 with response <-
251 @httpoison.get(
252 address,
253 [Accept: "application/xrd+xml,application/jrd+json"],
254 follow_redirect: true
255 ),
256 {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response do
257 doc = XML.parse_document(body)
258
259 if doc != :error do
260 webfinger_from_xml(doc)
261 else
262 {:ok, doc} = Jason.decode(body)
263 webfinger_from_json(doc)
264 end
265 else
266 e ->
267 Logger.debug(fn -> "Couldn't finger #{account}" end)
268 Logger.debug(fn -> inspect(e) end)
269 {:error, e}
270 end
271 end
272 end