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