2017-04-21 01:59:11 +00:00
|
|
|
defmodule Pleroma.Web.Websub do
|
2017-04-27 13:18:50 +00:00
|
|
|
alias Ecto.Changeset
|
2017-04-21 01:59:11 +00:00
|
|
|
alias Pleroma.Repo
|
2017-04-22 10:07:51 +00:00
|
|
|
alias Pleroma.Web.Websub.WebsubServerSubscription
|
|
|
|
alias Pleroma.Web.OStatus.FeedRepresenter
|
2017-04-22 11:44:21 +00:00
|
|
|
alias Pleroma.Web.OStatus
|
2017-04-22 10:07:51 +00:00
|
|
|
|
|
|
|
import Ecto.Query
|
2017-04-21 01:59:11 +00:00
|
|
|
|
2017-04-22 11:44:21 +00:00
|
|
|
@websub_verifier Application.get_env(:pleroma, :websub_verifier)
|
|
|
|
|
2017-04-27 13:18:50 +00:00
|
|
|
def verify(subscription, getter \\ &HTTPoison.get/3) do
|
2017-04-21 01:59:11 +00:00
|
|
|
challenge = Base.encode16(:crypto.strong_rand_bytes(8))
|
2017-04-27 13:18:50 +00:00
|
|
|
lease_seconds = NaiveDateTime.diff(subscription.valid_until, subscription.updated_at)
|
|
|
|
lease_seconds = lease_seconds |> to_string
|
2017-04-22 10:07:51 +00:00
|
|
|
|
|
|
|
params = %{
|
|
|
|
"hub.challenge": challenge,
|
|
|
|
"hub.lease_seconds": lease_seconds,
|
|
|
|
"hub.topic": subscription.topic,
|
|
|
|
"hub.mode": "subscribe"
|
|
|
|
}
|
|
|
|
|
|
|
|
url = hd(String.split(subscription.callback, "?"))
|
|
|
|
query = URI.parse(subscription.callback).query || ""
|
|
|
|
params = Map.merge(params, URI.decode_query(query))
|
|
|
|
with {:ok, response} <- getter.(url, [], [params: params]),
|
2017-04-21 01:59:11 +00:00
|
|
|
^challenge <- response.body
|
|
|
|
do
|
2017-04-27 13:18:50 +00:00
|
|
|
changeset = Changeset.change(subscription, %{state: "active"})
|
2017-04-21 01:59:11 +00:00
|
|
|
Repo.update(changeset)
|
|
|
|
else _e ->
|
2017-04-27 13:18:50 +00:00
|
|
|
changeset = Changeset.change(subscription, %{state: "rejected"})
|
|
|
|
{:ok, subscription} = Repo.update(changeset)
|
2017-04-21 01:59:11 +00:00
|
|
|
{:error, subscription}
|
|
|
|
end
|
|
|
|
end
|
2017-04-22 10:07:51 +00:00
|
|
|
|
|
|
|
def publish(topic, user, activity) do
|
|
|
|
query = from sub in WebsubServerSubscription,
|
|
|
|
where: sub.topic == ^topic and sub.state == "active"
|
|
|
|
subscriptions = Repo.all(query)
|
|
|
|
Enum.each(subscriptions, fn(sub) ->
|
2017-04-27 13:18:50 +00:00
|
|
|
response = user
|
|
|
|
|> FeedRepresenter.to_simple_form([activity], [user])
|
2017-04-22 10:07:51 +00:00
|
|
|
|> :xmerl.export_simple(:xmerl_xml)
|
|
|
|
|
2017-04-27 13:18:50 +00:00
|
|
|
signature = Base.encode16(:crypto.hmac(:sha, sub.secret, response))
|
2017-04-22 10:07:51 +00:00
|
|
|
|
|
|
|
HTTPoison.post(sub.callback, response, [
|
|
|
|
{"Content-Type", "application/atom+xml"},
|
|
|
|
{"X-Hub-Signature", "sha1=#{signature}"}
|
|
|
|
])
|
|
|
|
end)
|
|
|
|
end
|
2017-04-22 11:44:21 +00:00
|
|
|
|
2017-04-22 11:48:10 +00:00
|
|
|
def incoming_subscription_request(user, %{"hub.mode" => "subscribe"} = params) do
|
2017-04-22 11:44:21 +00:00
|
|
|
with {:ok, topic} <- valid_topic(params, user),
|
|
|
|
{:ok, lease_time} <- lease_time(params),
|
|
|
|
secret <- params["hub.secret"],
|
|
|
|
callback <- params["hub.callback"]
|
|
|
|
do
|
|
|
|
subscription = get_subscription(topic, callback)
|
|
|
|
data = %{
|
|
|
|
state: subscription.state || "requested",
|
|
|
|
topic: topic,
|
|
|
|
secret: secret,
|
|
|
|
callback: callback
|
|
|
|
}
|
|
|
|
|
2017-04-27 13:18:50 +00:00
|
|
|
change = Changeset.change(subscription, data)
|
2017-04-22 11:44:21 +00:00
|
|
|
websub = Repo.insert_or_update!(change)
|
|
|
|
|
2017-04-27 13:18:50 +00:00
|
|
|
change = Changeset.change(websub, %{valid_until:
|
|
|
|
NaiveDateTime.add(websub.updated_at, lease_time)})
|
2017-04-22 11:44:21 +00:00
|
|
|
websub = Repo.update!(change)
|
|
|
|
|
|
|
|
# Just spawn that for now, maybe pool later.
|
|
|
|
spawn(fn -> @websub_verifier.verify(websub) end)
|
|
|
|
|
|
|
|
{:ok, websub}
|
|
|
|
else {:error, reason} ->
|
|
|
|
{:error, reason}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_subscription(topic, callback) do
|
2017-04-27 13:18:50 +00:00
|
|
|
Repo.get_by(WebsubServerSubscription, topic: topic, callback: callback) ||
|
|
|
|
%WebsubServerSubscription{}
|
2017-04-22 11:44:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp lease_time(%{"hub.lease_seconds" => lease_seconds}) do
|
|
|
|
{:ok, String.to_integer(lease_seconds)}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp lease_time(_) do
|
|
|
|
{:ok, 60 * 60 * 24 * 3} # three days
|
|
|
|
end
|
|
|
|
|
|
|
|
defp valid_topic(%{"hub.topic" => topic}, user) do
|
|
|
|
if topic == OStatus.feed_path(user) do
|
|
|
|
{:ok, topic}
|
|
|
|
else
|
|
|
|
{:error, "Wrong topic requested, expected #{OStatus.feed_path(user)}, got #{topic}"}
|
|
|
|
end
|
|
|
|
end
|
2017-04-21 01:59:11 +00:00
|
|
|
end
|