2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 08:41:47 -07:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-04-20 09:47:33 -06:00
|
|
|
defmodule Pleroma.Web.Websub.WebsubController do
|
|
|
|
use Pleroma.Web, :controller
|
2017-04-28 07:45:10 -06:00
|
|
|
alias Pleroma.{Repo, User}
|
2017-05-06 04:34:40 -06:00
|
|
|
alias Pleroma.Web.{Websub, Federator}
|
2017-04-28 07:45:10 -06:00
|
|
|
alias Pleroma.Web.Websub.WebsubClientSubscription
|
|
|
|
require Logger
|
|
|
|
|
2018-11-05 07:19:03 -07:00
|
|
|
plug(
|
|
|
|
Pleroma.Web.FederatingPlug
|
|
|
|
when action in [
|
|
|
|
:websub_subscription_request,
|
|
|
|
:websub_subscription_confirmation,
|
|
|
|
:websub_incoming
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2017-04-20 09:47:33 -06:00
|
|
|
def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
|
|
|
|
user = User.get_cached_by_nickname(nickname)
|
|
|
|
|
2018-03-30 07:01:53 -06:00
|
|
|
with {:ok, _websub} <- Websub.incoming_subscription_request(user, params) do
|
2017-04-20 09:47:33 -06:00
|
|
|
conn
|
|
|
|
|> send_resp(202, "Accepted")
|
2018-03-30 07:01:53 -06:00
|
|
|
else
|
|
|
|
{:error, reason} ->
|
|
|
|
conn
|
|
|
|
|> send_resp(500, reason)
|
2017-04-20 09:47:33 -06:00
|
|
|
end
|
|
|
|
end
|
2017-04-28 01:51:47 -06:00
|
|
|
|
2017-05-10 10:45:55 -06:00
|
|
|
# TODO: Extract this into the Websub module
|
2018-03-30 07:01:53 -06:00
|
|
|
def websub_subscription_confirmation(
|
|
|
|
conn,
|
|
|
|
%{
|
|
|
|
"id" => id,
|
|
|
|
"hub.mode" => "subscribe",
|
|
|
|
"hub.challenge" => challenge,
|
|
|
|
"hub.topic" => topic
|
|
|
|
} = params
|
|
|
|
) do
|
2018-03-19 11:41:04 -06:00
|
|
|
Logger.debug("Got WebSub confirmation")
|
2017-08-02 04:03:45 -06:00
|
|
|
Logger.debug(inspect(params))
|
2017-05-10 11:04:27 -06:00
|
|
|
|
2018-03-30 07:01:53 -06:00
|
|
|
lease_seconds =
|
|
|
|
if params["hub.lease_seconds"] do
|
|
|
|
String.to_integer(params["hub.lease_seconds"])
|
|
|
|
else
|
|
|
|
# Guess 3 days
|
|
|
|
60 * 60 * 24 * 3
|
|
|
|
end
|
|
|
|
|
|
|
|
with %WebsubClientSubscription{} = websub <-
|
|
|
|
Repo.get_by(WebsubClientSubscription, id: id, topic: topic) do
|
|
|
|
valid_until = NaiveDateTime.add(NaiveDateTime.utc_now(), lease_seconds)
|
2017-05-10 10:45:55 -06:00
|
|
|
change = Ecto.Changeset.change(websub, %{state: "accepted", valid_until: valid_until})
|
2017-04-28 07:45:10 -06:00
|
|
|
{:ok, _websub} = Repo.update(change)
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2017-04-28 07:45:10 -06:00
|
|
|
conn
|
|
|
|
|> send_resp(200, challenge)
|
2018-03-30 07:01:53 -06:00
|
|
|
else
|
|
|
|
_e ->
|
|
|
|
conn
|
|
|
|
|> send_resp(500, "Error")
|
2017-04-28 07:45:10 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-15 11:00:20 -07:00
|
|
|
def websub_subscription_confirmation(conn, params) do
|
|
|
|
Logger.info("Invalid WebSub confirmation request: #{inspect(params)}")
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> send_resp(500, "Invalid parameters")
|
|
|
|
end
|
|
|
|
|
2017-04-28 07:45:10 -06:00
|
|
|
def websub_incoming(conn, %{"id" => id}) do
|
|
|
|
with "sha1=" <> signature <- hd(get_req_header(conn, "x-hub-signature")),
|
2017-05-03 12:06:20 -06:00
|
|
|
signature <- String.downcase(signature),
|
2017-04-28 07:45:10 -06:00
|
|
|
%WebsubClientSubscription{} = websub <- Repo.get(WebsubClientSubscription, id),
|
|
|
|
{:ok, body, _conn} = read_body(conn),
|
|
|
|
^signature <- Websub.sign(websub.secret, body) do
|
2017-05-06 04:34:40 -06:00
|
|
|
Federator.enqueue(:incoming_doc, body)
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2017-04-28 07:45:10 -06:00
|
|
|
conn
|
|
|
|
|> send_resp(200, "OK")
|
2018-03-30 07:01:53 -06:00
|
|
|
else
|
|
|
|
_e ->
|
|
|
|
Logger.debug("Can't handle incoming subscription post")
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> send_resp(500, "Error")
|
2017-04-28 07:45:10 -06:00
|
|
|
end
|
2017-04-28 01:51:47 -06:00
|
|
|
end
|
2017-04-20 09:47:33 -06:00
|
|
|
end
|