ActivityPub: Basic note federation with Mastodon.

This commit is contained in:
lain 2018-02-11 20:43:33 +01:00
commit 8cf97ee8e1
7 changed files with 122 additions and 16 deletions

View file

@ -1,6 +1,7 @@
# https://tools.ietf.org/html/draft-cavage-http-signatures-08
defmodule Pleroma.Web.HTTPSignatures do
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
def split_signature(sig) do
default = %{"headers" => "date"}
@ -28,7 +29,16 @@ defmodule Pleroma.Web.HTTPSignatures do
# For now, fetch the key for the actor.
with actor_id <- conn.params["actor"],
{:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
validate_conn(conn, public_key)
if validate_conn(conn, public_key) do
true
else
# Fetch user anew and try one more time
with actor_id <- conn.params["actor"],
{:ok, _user} <- ActivityPub.make_user_from_ap_id(actor_id),
{:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
validate_conn(conn, public_key)
end
end
else
_ -> false
end
@ -45,4 +55,22 @@ defmodule Pleroma.Web.HTTPSignatures do
|> Enum.map(fn (header) -> "#{header}: #{headers[header]}" end)
|> Enum.join("\n")
end
def sign(user, headers) do
with {:ok, %{info: %{"keys" => keys}}} <- Pleroma.Web.WebFinger.ensure_keys_present(user),
{:ok, private_key, _} = Pleroma.Web.Salmon.keys_from_pem(keys) do
sigstring = build_signing_string(headers, Map.keys(headers))
signature = :public_key.sign(sigstring, :sha256, private_key)
|> Base.encode64()
[
keyId: user.ap_id <> "#main-key",
algorithm: "rsa-sha256",
headers: Map.keys(headers) |> Enum.join(" "),
signature: signature
]
|> Enum.map(fn({k, v}) -> "#{k}=\"#{v}\"" end)
|> Enum.join(",")
end
end
end