Handle incoming websub subscriptions.
This commit is contained in:
parent
451d18af63
commit
1422e7aa84
7 changed files with 102 additions and 11 deletions
|
|
@ -2,7 +2,7 @@ defmodule Pleroma.Web.Federator do
|
|||
alias Pleroma.User
|
||||
require Logger
|
||||
|
||||
@websub_verifier Application.get_env(:pleroma, :websub_verifier)
|
||||
@websub Application.get_env(:pleroma, :websub)
|
||||
|
||||
def handle(:publish, activity) do
|
||||
Logger.debug("Running publish for #{activity.data["id"]}")
|
||||
|
|
@ -13,7 +13,7 @@ defmodule Pleroma.Web.Federator do
|
|||
|
||||
def handle(:verify_websub, websub) do
|
||||
Logger.debug("Running websub verification for #{websub.id} (#{websub.topic}, #{websub.callback})")
|
||||
@websub_verifier.verify(websub)
|
||||
@websub.verify(websub)
|
||||
end
|
||||
|
||||
def handle(type, payload) do
|
||||
|
|
|
|||
|
|
@ -75,8 +75,9 @@ defmodule Pleroma.Web.Router do
|
|||
|
||||
get "/users/:nickname/feed", OStatus.OStatusController, :feed
|
||||
post "/users/:nickname/salmon", OStatus.OStatusController, :salmon_incoming
|
||||
post "/push/subscriptions/:id", Websub.WebsubController, :websub_subscription_confirmation
|
||||
post "/push/hub/:nickname", Websub.WebsubController, :websub_subscription_request
|
||||
get "/push/subscriptions/:id", Websub.WebsubController, :websub_subscription_confirmation
|
||||
post "/push/subscriptions/:id", Websub.WebsubController, :websub_incoming
|
||||
end
|
||||
|
||||
scope "/.well-known", Pleroma.Web do
|
||||
|
|
|
|||
|
|
@ -42,8 +42,7 @@ defmodule Pleroma.Web.Websub do
|
|||
response = FeedRepresenter.to_simple_form(user, [activity], [user])
|
||||
|> :xmerl.export_simple(:xmerl_xml)
|
||||
|
||||
signature = :crypto.hmac(:sha, sub.secret, response) |> Base.encode16
|
||||
|
||||
signature = sign(sub.secret, response)
|
||||
HTTPoison.post(sub.callback, response, [
|
||||
{"Content-Type", "application/atom+xml"},
|
||||
{"X-Hub-Signature", "sha1=#{signature}"}
|
||||
|
|
@ -51,6 +50,10 @@ defmodule Pleroma.Web.Websub do
|
|||
end)
|
||||
end
|
||||
|
||||
def sign(secret, doc) do
|
||||
:crypto.hmac(:sha, secret, doc) |> Base.encode16
|
||||
end
|
||||
|
||||
def incoming_subscription_request(user, %{"hub.mode" => "subscribe"} = params) do
|
||||
with {:ok, topic} <- valid_topic(params, user),
|
||||
{:ok, lease_time} <- lease_time(params),
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
defmodule Pleroma.Web.Websub.WebsubController do
|
||||
use Pleroma.Web, :controller
|
||||
alias Pleroma.User
|
||||
alias Pleroma.{Repo, User}
|
||||
alias Pleroma.Web.Websub
|
||||
alias Pleroma.Web.Websub.WebsubClientSubscription
|
||||
require Logger
|
||||
|
||||
@ostatus Application.get_env(:pleroma, :ostatus)
|
||||
|
||||
def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
|
||||
user = User.get_cached_by_nickname(nickname)
|
||||
|
|
@ -16,8 +20,30 @@ defmodule Pleroma.Web.Websub.WebsubController do
|
|||
end
|
||||
end
|
||||
|
||||
def websub_subscription_confirmation(conn, params) do
|
||||
IO.inspect(params)
|
||||
conn
|
||||
def websub_subscription_confirmation(conn, %{"id" => id, "hub.mode" => "subscribe", "hub.challenge" => challenge, "hub.topic" => topic}) do
|
||||
with %WebsubClientSubscription{} = websub <- Repo.get_by(WebsubClientSubscription, id: id, topic: topic) do
|
||||
change = Ecto.Changeset.change(websub, %{state: "accepted"})
|
||||
{:ok, _websub} = Repo.update(change)
|
||||
conn
|
||||
|> send_resp(200, challenge)
|
||||
else _e ->
|
||||
conn
|
||||
|> send_resp(500, "Error")
|
||||
end
|
||||
end
|
||||
|
||||
def websub_incoming(conn, %{"id" => id}) do
|
||||
with "sha1=" <> signature <- hd(get_req_header(conn, "x-hub-signature")),
|
||||
%WebsubClientSubscription{} = websub <- Repo.get(WebsubClientSubscription, id),
|
||||
{:ok, body, _conn} = read_body(conn),
|
||||
^signature <- Websub.sign(websub.secret, body) do
|
||||
@ostatus.handle_incoming(body)
|
||||
conn
|
||||
|> send_resp(200, "OK")
|
||||
else _e ->
|
||||
Logger.debug("Can't handle incoming subscription post")
|
||||
conn
|
||||
|> send_resp(500, "Error")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue