ability to set and reset avatar, profile banner and backgroud in Mastodon API
[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 @httpoison Application.get_env(:pleroma, :httpoison)
7
8 alias Pleroma.User
9 alias Pleroma.Web
10 alias Pleroma.Web.Federator.Publisher
11 alias Pleroma.Web.XML
12 alias Pleroma.XmlBuilder
13 require Jason
14 require Logger
15
16 def host_meta do
17 base_url = Web.base_url()
18
19 {
20 :XRD,
21 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
22 {
23 :Link,
24 %{
25 rel: "lrdd",
26 type: "application/xrd+xml",
27 template: "#{base_url}/.well-known/webfinger?resource={uri}"
28 }
29 }
30 }
31 |> XmlBuilder.to_doc()
32 end
33
34 def webfinger(resource, fmt) when fmt in ["XML", "JSON"] do
35 host = Pleroma.Web.Endpoint.host()
36 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
37
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)}
41 else
42 _e ->
43 with %User{} = user <- User.get_cached_by_ap_id(resource) do
44 {:ok, represent_user(user, fmt)}
45 else
46 _e ->
47 {:error, "Couldn't find user"}
48 end
49 end
50 end
51
52 defp gather_links(%User{} = user) do
53 [
54 %{
55 "rel" => "http://webfinger.net/rel/profile-page",
56 "type" => "text/html",
57 "href" => user.ap_id
58 }
59 ] ++ Publisher.gather_webfinger_links(user)
60 end
61
62 def represent_user(user, "JSON") do
63 {:ok, user} = User.ensure_keys_present(user)
64
65 %{
66 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}",
67 "aliases" => [user.ap_id],
68 "links" => gather_links(user)
69 }
70 end
71
72 def represent_user(user, "XML") do
73 {:ok, user} = User.ensure_keys_present(user)
74
75 links =
76 gather_links(user)
77 |> Enum.map(fn link -> {:Link, link} end)
78
79 {
80 :XRD,
81 %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
82 [
83 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
84 {:Alias, user.ap_id}
85 ] ++ links
86 }
87 |> XmlBuilder.to_doc()
88 end
89
90 defp get_magic_key(magic_key) do
91 "data:application/magic-public-key," <> magic_key = magic_key
92 {:ok, magic_key}
93 rescue
94 MatchError -> {:error, "Missing magic key data."}
95 end
96
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),
100 topic <-
101 XML.string_from_xpath(
102 ~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href},
103 doc
104 ),
105 subject <- XML.string_from_xpath("//Subject", doc),
106 salmon <- XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc),
107 subscribe_address <-
108 XML.string_from_xpath(
109 ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template},
110 doc
111 ),
112 ap_id <-
113 XML.string_from_xpath(
114 ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
115 doc
116 ) do
117 data = %{
118 "magic_key" => magic_key,
119 "topic" => topic,
120 "subject" => subject,
121 "salmon" => salmon,
122 "subscribe_address" => subscribe_address,
123 "ap_id" => ap_id
124 }
125
126 {:ok, data}
127 else
128 {:error, e} ->
129 {:error, e}
130
131 e ->
132 {:error, e}
133 end
134 end
135
136 defp webfinger_from_json(doc) do
137 data =
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"])
142
143 {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} ->
144 Map.put(data, "ap_id", link["href"])
145
146 {_, "magic-public-key"} ->
147 "data:application/magic-public-key," <> magic_key = link["href"]
148 Map.put(data, "magic_key", magic_key)
149
150 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
151 Map.put(data, "topic", link["href"])
152
153 {_, "salmon"} ->
154 Map.put(data, "salmon", link["href"])
155
156 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
157 Map.put(data, "subscribe_address", link["template"])
158
159 _ ->
160 Logger.debug("Unhandled type: #{inspect(link["type"])}")
161 data
162 end
163 end)
164
165 {:ok, data}
166 end
167
168 def get_template_from_xml(body) do
169 xpath = "//Link[@rel='lrdd']/@template"
170
171 with doc when doc != :error <- XML.parse_document(body),
172 template when template != nil <- XML.string_from_xpath(xpath, doc) do
173 {:ok, template}
174 end
175 end
176
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)
181 else
182 _ ->
183 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
184 get_template_from_xml(body)
185 else
186 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
187 end
188 end
189 end
190
191 def finger(account) do
192 account = String.trim_leading(account, "@")
193
194 domain =
195 with [_name, domain] <- String.split(account, "@") do
196 domain
197 else
198 _e ->
199 URI.parse(account).host
200 end
201
202 address =
203 case find_lrdd_template(domain) do
204 {:ok, template} ->
205 String.replace(template, "{uri}", URI.encode(account))
206
207 _ ->
208 "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
209 end
210
211 with response <-
212 @httpoison.get(
213 address,
214 Accept: "application/xrd+xml,application/jrd+json"
215 ),
216 {:ok, %{status: status, body: body}} when status in 200..299 <- response do
217 doc = XML.parse_document(body)
218
219 if doc != :error do
220 webfinger_from_xml(doc)
221 else
222 with {:ok, doc} <- Jason.decode(body) do
223 webfinger_from_json(doc)
224 else
225 {:error, e} -> e
226 end
227 end
228 else
229 e ->
230 Logger.debug(fn -> "Couldn't finger #{account}" end)
231 Logger.debug(fn -> inspect(e) end)
232 {:error, e}
233 end
234 end
235 end