41bbc0369ceb925c9a4c65d4a0f45018126e5d94
[akkoma] / lib / pleroma / web / websub / websub_controller.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.WebsubController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.{Repo, User}
9 alias Pleroma.Web.{Websub, Federator}
10 alias Pleroma.Web.Websub.WebsubClientSubscription
11
12 require Logger
13
14 plug(
15 Pleroma.Web.FederatingPlug
16 when action in [
17 :websub_subscription_request,
18 :websub_subscription_confirmation,
19 :websub_incoming
20 ]
21 )
22
23 def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
24 user = User.get_cached_by_nickname(nickname)
25
26 with {:ok, _websub} <- Websub.incoming_subscription_request(user, params) do
27 conn
28 |> send_resp(202, "Accepted")
29 else
30 {:error, reason} ->
31 conn
32 |> send_resp(500, reason)
33 end
34 end
35
36 # TODO: Extract this into the Websub module
37 def websub_subscription_confirmation(
38 conn,
39 %{
40 "id" => id,
41 "hub.mode" => "subscribe",
42 "hub.challenge" => challenge,
43 "hub.topic" => topic
44 } = params
45 ) do
46 Logger.debug("Got WebSub confirmation")
47 Logger.debug(inspect(params))
48
49 lease_seconds =
50 if params["hub.lease_seconds"] do
51 String.to_integer(params["hub.lease_seconds"])
52 else
53 # Guess 3 days
54 60 * 60 * 24 * 3
55 end
56
57 with %WebsubClientSubscription{} = websub <-
58 Repo.get_by(WebsubClientSubscription, id: id, topic: topic) do
59 valid_until = NaiveDateTime.add(NaiveDateTime.utc_now(), lease_seconds)
60 change = Ecto.Changeset.change(websub, %{state: "accepted", valid_until: valid_until})
61 {:ok, _websub} = Repo.update(change)
62
63 conn
64 |> send_resp(200, challenge)
65 else
66 _e ->
67 conn
68 |> send_resp(500, "Error")
69 end
70 end
71
72 def websub_subscription_confirmation(conn, params) do
73 Logger.info("Invalid WebSub confirmation request: #{inspect(params)}")
74
75 conn
76 |> send_resp(500, "Invalid parameters")
77 end
78
79 def websub_incoming(conn, %{"id" => id}) do
80 with "sha1=" <> signature <- hd(get_req_header(conn, "x-hub-signature")),
81 signature <- String.downcase(signature),
82 %WebsubClientSubscription{} = websub <- Repo.get(WebsubClientSubscription, id),
83 {:ok, body, _conn} = read_body(conn),
84 ^signature <- Websub.sign(websub.secret, body) do
85 Federator.incoming_doc(body)
86
87 conn
88 |> send_resp(200, "OK")
89 else
90 _e ->
91 Logger.debug("Can't handle incoming subscription post")
92
93 conn
94 |> send_resp(500, "Error")
95 end
96 end
97 end