Merge branch 'update-mastofe/glitch-soc-2019-02-10' into 'develop'
[akkoma] / lib / pleroma / web / websub / websub.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.Websub do
6 alias Ecto.Changeset
7 alias Pleroma.Instances
8 alias Pleroma.Repo
9 alias Pleroma.Web.Websub.WebsubServerSubscription
10 alias Pleroma.Web.Websub.WebsubClientSubscription
11 alias Pleroma.Web.OStatus.FeedRepresenter
12 alias Pleroma.Web.XML
13 alias Pleroma.Web.Endpoint
14 alias Pleroma.Web.OStatus
15 alias Pleroma.Web.Router.Helpers
16 require Logger
17
18 import Ecto.Query
19
20 @httpoison Application.get_env(:pleroma, :httpoison)
21
22 def verify(subscription, getter \\ &@httpoison.get/3) do
23 challenge = Base.encode16(:crypto.strong_rand_bytes(8))
24 lease_seconds = NaiveDateTime.diff(subscription.valid_until, subscription.updated_at)
25 lease_seconds = lease_seconds |> to_string
26
27 params = %{
28 "hub.challenge": challenge,
29 "hub.lease_seconds": lease_seconds,
30 "hub.topic": subscription.topic,
31 "hub.mode": "subscribe"
32 }
33
34 url = hd(String.split(subscription.callback, "?"))
35 query = URI.parse(subscription.callback).query || ""
36 params = Map.merge(params, URI.decode_query(query))
37
38 with {:ok, response} <- getter.(url, [], params: params),
39 ^challenge <- response.body do
40 changeset = Changeset.change(subscription, %{state: "active"})
41 Repo.update(changeset)
42 else
43 e ->
44 Logger.debug("Couldn't verify subscription")
45 Logger.debug(inspect(e))
46 {:error, subscription}
47 end
48 end
49
50 @supported_activities [
51 "Create",
52 "Follow",
53 "Like",
54 "Announce",
55 "Undo",
56 "Delete"
57 ]
58 def publish(topic, user, %{data: %{"type" => type}} = activity)
59 when type in @supported_activities do
60 response =
61 user
62 |> FeedRepresenter.to_simple_form([activity], [user])
63 |> :xmerl.export_simple(:xmerl_xml)
64 |> to_string
65
66 query =
67 from(
68 sub in WebsubServerSubscription,
69 where: sub.topic == ^topic and sub.state == "active",
70 where: fragment("? > (NOW() at time zone 'UTC')", sub.valid_until)
71 )
72
73 subscriptions = Repo.all(query)
74
75 callbacks = Enum.map(subscriptions, & &1.callback)
76 reachable_callbacks_metadata = Instances.filter_reachable(callbacks)
77 reachable_callbacks = Map.keys(reachable_callbacks_metadata)
78
79 subscriptions
80 |> Enum.filter(&(&1.callback in reachable_callbacks))
81 |> Enum.each(fn sub ->
82 data = %{
83 xml: response,
84 topic: topic,
85 callback: sub.callback,
86 secret: sub.secret,
87 unreachable_since: reachable_callbacks_metadata[sub.callback]
88 }
89
90 Pleroma.Web.Federator.enqueue(:publish_single_websub, data)
91 end)
92 end
93
94 def publish(_, _, _), do: ""
95
96 def sign(secret, doc) do
97 :crypto.hmac(:sha, secret, to_string(doc)) |> Base.encode16() |> String.downcase()
98 end
99
100 def incoming_subscription_request(user, %{"hub.mode" => "subscribe"} = params) do
101 with {:ok, topic} <- valid_topic(params, user),
102 {:ok, lease_time} <- lease_time(params),
103 secret <- params["hub.secret"],
104 callback <- params["hub.callback"] do
105 subscription = get_subscription(topic, callback)
106
107 data = %{
108 state: subscription.state || "requested",
109 topic: topic,
110 secret: secret,
111 callback: callback
112 }
113
114 change = Changeset.change(subscription, data)
115 websub = Repo.insert_or_update!(change)
116
117 change =
118 Changeset.change(websub, %{valid_until: NaiveDateTime.add(websub.updated_at, lease_time)})
119
120 websub = Repo.update!(change)
121
122 Pleroma.Web.Federator.enqueue(:verify_websub, websub)
123
124 {:ok, websub}
125 else
126 {:error, reason} ->
127 Logger.debug("Couldn't create subscription")
128 Logger.debug(inspect(reason))
129
130 {:error, reason}
131 end
132 end
133
134 def incoming_subscription_request(user, params) do
135 Logger.info("Unhandled WebSub request for #{user.nickname}: #{inspect(params)}")
136
137 {:error, "Invalid WebSub request"}
138 end
139
140 defp get_subscription(topic, callback) do
141 Repo.get_by(WebsubServerSubscription, topic: topic, callback: callback) ||
142 %WebsubServerSubscription{}
143 end
144
145 # Temp hack for mastodon.
146 defp lease_time(%{"hub.lease_seconds" => ""}) do
147 # three days
148 {:ok, 60 * 60 * 24 * 3}
149 end
150
151 defp lease_time(%{"hub.lease_seconds" => lease_seconds}) do
152 {:ok, String.to_integer(lease_seconds)}
153 end
154
155 defp lease_time(_) do
156 # three days
157 {:ok, 60 * 60 * 24 * 3}
158 end
159
160 defp valid_topic(%{"hub.topic" => topic}, user) do
161 if topic == OStatus.feed_path(user) do
162 {:ok, OStatus.feed_path(user)}
163 else
164 {:error, "Wrong topic requested, expected #{OStatus.feed_path(user)}, got #{topic}"}
165 end
166 end
167
168 def subscribe(subscriber, subscribed, requester \\ &request_subscription/1) do
169 topic = subscribed.info.topic
170 # FIXME: Race condition, use transactions
171 {:ok, subscription} =
172 with subscription when not is_nil(subscription) <-
173 Repo.get_by(WebsubClientSubscription, topic: topic) do
174 subscribers = [subscriber.ap_id | subscription.subscribers] |> Enum.uniq()
175 change = Ecto.Changeset.change(subscription, %{subscribers: subscribers})
176 Repo.update(change)
177 else
178 _e ->
179 subscription = %WebsubClientSubscription{
180 topic: topic,
181 hub: subscribed.info.hub,
182 subscribers: [subscriber.ap_id],
183 state: "requested",
184 secret: :crypto.strong_rand_bytes(8) |> Base.url_encode64(),
185 user: subscribed
186 }
187
188 Repo.insert(subscription)
189 end
190
191 requester.(subscription)
192 end
193
194 def gather_feed_data(topic, getter \\ &@httpoison.get/1) do
195 with {:ok, response} <- getter.(topic),
196 status when status in 200..299 <- response.status,
197 body <- response.body,
198 doc <- XML.parse_document(body),
199 uri when not is_nil(uri) <- XML.string_from_xpath("/feed/author[1]/uri", doc),
200 hub when not is_nil(hub) <- XML.string_from_xpath(~S{/feed/link[@rel="hub"]/@href}, doc) do
201 name = XML.string_from_xpath("/feed/author[1]/name", doc)
202 preferredUsername = XML.string_from_xpath("/feed/author[1]/poco:preferredUsername", doc)
203 displayName = XML.string_from_xpath("/feed/author[1]/poco:displayName", doc)
204 avatar = OStatus.make_avatar_object(doc)
205 bio = XML.string_from_xpath("/feed/author[1]/summary", doc)
206
207 {:ok,
208 %{
209 "uri" => uri,
210 "hub" => hub,
211 "nickname" => preferredUsername || name,
212 "name" => displayName || name,
213 "host" => URI.parse(uri).host,
214 "avatar" => avatar,
215 "bio" => bio
216 }}
217 else
218 e ->
219 {:error, e}
220 end
221 end
222
223 def request_subscription(websub, poster \\ &@httpoison.post/3, timeout \\ 10_000) do
224 data = [
225 "hub.mode": "subscribe",
226 "hub.topic": websub.topic,
227 "hub.secret": websub.secret,
228 "hub.callback": Helpers.websub_url(Endpoint, :websub_subscription_confirmation, websub.id)
229 ]
230
231 # This checks once a second if we are confirmed yet
232 websub_checker = fn ->
233 helper = fn helper ->
234 :timer.sleep(1000)
235 websub = Repo.get_by(WebsubClientSubscription, id: websub.id, state: "accepted")
236 if websub, do: websub, else: helper.(helper)
237 end
238
239 helper.(helper)
240 end
241
242 task = Task.async(websub_checker)
243
244 with {:ok, %{status: 202}} <-
245 poster.(websub.hub, {:form, data}, "Content-type": "application/x-www-form-urlencoded"),
246 {:ok, websub} <- Task.yield(task, timeout) do
247 {:ok, websub}
248 else
249 e ->
250 Task.shutdown(task)
251
252 change = Ecto.Changeset.change(websub, %{state: "rejected"})
253 {:ok, websub} = Repo.update(change)
254
255 Logger.debug(fn -> "Couldn't confirm subscription: #{inspect(websub)}" end)
256 Logger.debug(fn -> "error: #{inspect(e)}" end)
257
258 {:error, websub}
259 end
260 end
261
262 def refresh_subscriptions(delta \\ 60 * 60 * 24) do
263 Logger.debug("Refreshing subscriptions")
264
265 cut_off = NaiveDateTime.add(NaiveDateTime.utc_now(), delta)
266
267 query = from(sub in WebsubClientSubscription, where: sub.valid_until < ^cut_off)
268
269 subs = Repo.all(query)
270
271 Enum.each(subs, fn sub ->
272 Pleroma.Web.Federator.enqueue(:request_subscription, sub)
273 end)
274 end
275
276 def publish_one(%{xml: xml, topic: topic, callback: callback, secret: secret} = params) do
277 signature = sign(secret || "", xml)
278 Logger.info(fn -> "Pushing #{topic} to #{callback}" end)
279
280 with {:ok, %{status: code}} when code in 200..299 <-
281 @httpoison.post(
282 callback,
283 xml,
284 [
285 {"Content-Type", "application/atom+xml"},
286 {"X-Hub-Signature", "sha1=#{signature}"}
287 ]
288 ) do
289 if !Map.has_key?(params, :unreachable_since) || params[:unreachable_since],
290 do: Instances.set_reachable(callback)
291
292 Logger.info(fn -> "Pushed to #{callback}, code #{code}" end)
293 {:ok, code}
294 else
295 {_post_result, response} ->
296 unless params[:unreachable_since], do: Instances.set_reachable(callback)
297 Logger.debug(fn -> "Couldn't push to #{callback}, #{inspect(response)}" end)
298 {:error, response}
299 end
300 end
301 end