1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Websub.WebsubController do
6 use Pleroma.Web, :controller
10 alias Pleroma.Web.Federator
11 alias Pleroma.Web.Websub
12 alias Pleroma.Web.Websub.WebsubClientSubscription
17 Pleroma.Web.FederatingPlug
19 :websub_subscription_request,
20 :websub_subscription_confirmation,
25 def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
26 user = User.get_cached_by_nickname(nickname)
28 with {:ok, _websub} <- Websub.incoming_subscription_request(user, params) do
30 |> send_resp(202, "Accepted")
34 |> send_resp(500, reason)
38 # TODO: Extract this into the Websub module
39 def websub_subscription_confirmation(
43 "hub.mode" => "subscribe",
44 "hub.challenge" => challenge,
48 Logger.debug("Got WebSub confirmation")
49 Logger.debug(inspect(params))
52 if params["hub.lease_seconds"] do
53 String.to_integer(params["hub.lease_seconds"])
59 with %WebsubClientSubscription{} = websub <-
60 Repo.get_by(WebsubClientSubscription, id: id, topic: topic) do
61 valid_until = NaiveDateTime.add(NaiveDateTime.utc_now(), lease_seconds)
62 change = Ecto.Changeset.change(websub, %{state: "accepted", valid_until: valid_until})
63 {:ok, _websub} = Repo.update(change)
66 |> send_resp(200, challenge)
70 |> send_resp(500, "Error")
74 def websub_subscription_confirmation(conn, params) do
75 Logger.info("Invalid WebSub confirmation request: #{inspect(params)}")
78 |> send_resp(500, "Invalid parameters")
81 def websub_incoming(conn, %{"id" => id}) do
82 with "sha1=" <> signature <- hd(get_req_header(conn, "x-hub-signature")),
83 signature <- String.downcase(signature),
84 %WebsubClientSubscription{} = websub <- Repo.get(WebsubClientSubscription, id),
85 {:ok, body, _conn} = read_body(conn),
86 ^signature <- Websub.sign(websub.secret, body) do
87 Federator.incoming_doc(body)
90 |> send_resp(200, "OK")
93 Logger.debug("Can't handle incoming subscription post")
96 |> send_resp(500, "Error")