mobilizon/lib/federation/http_signatures/signature.ex

137 lines
4.4 KiB
Elixir
Raw Normal View History

# Portions of this file are derived from Pleroma:
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social>
# SPDX-License-Identifier: AGPL-3.0-only
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/signature.ex
2020-01-22 02:14:42 +01:00
defmodule Mobilizon.Federation.HTTPSignatures.Signature do
@moduledoc """
Adapter for the `HTTPSignatures` lib that handles signing and providing public keys to verify HTTPSignatures
"""
2019-09-09 00:52:49 +02:00
@behaviour HTTPSignatures.Adapter
alias Mobilizon.Actors.Actor
2020-01-22 02:14:42 +01:00
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
alias Mobilizon.Service.ErrorReporting.Sentry
2019-09-09 00:52:49 +02:00
require Logger
@spec key_id_to_actor_url(String.t()) :: String.t()
def key_id_to_actor_url(key_id) do
%URI{path: path} =
uri =
2019-09-09 00:52:49 +02:00
key_id
|> URI.parse()
|> Map.put(:fragment, nil)
uri =
if is_nil(path) do
uri
else
%URI{uri | path: String.trim_trailing(path, "/publickey")}
end
URI.to_string(uri)
end
2019-09-09 00:52:49 +02:00
@doc """
Convert internal PEM encoded keys to public key format.
"""
@spec prepare_public_key(String.t()) :: {:ok, tuple} | {:error, :pem_decode_error}
def prepare_public_key(public_key_code) do
case :public_key.pem_decode(public_key_code) do
[public_key_entry] ->
{:ok, :public_key.pem_entry_decode(public_key_entry)}
_ ->
{:error, :pem_decode_error}
end
end
# Gets a public key for a given ActivityPub actor ID (url).
2019-09-09 00:52:49 +02:00
@spec get_public_key_for_url(String.t()) ::
{:ok, String.t()}
| {:error, :actor_fetch_error | :pem_decode_error | :actor_not_fetchable}
defp get_public_key_for_url(url) do
with {:ok, %Actor{keys: keys}} <- ActivityPubActor.get_or_fetch_actor_by_url(url),
2019-09-09 00:52:49 +02:00
{:ok, public_key} <- prepare_public_key(keys) do
{:ok, public_key}
else
{:error, :pem_decode_error} ->
Logger.error("Error while decoding PEM")
{:error, :pem_decode_error}
{:error, "Could not fetch by AP id"} ->
{:error, :actor_not_fetchable}
err ->
Sentry.capture_message("Unable to fetch actor, so no keys for you",
extra: %{url: url}
)
2019-09-09 00:52:49 +02:00
Logger.error("Unable to fetch actor, so no keys for you")
Logger.error(inspect(err))
2019-09-09 00:52:49 +02:00
{:error, :actor_fetch_error}
end
end
@spec fetch_public_key(Plug.Conn.t()) ::
{:ok, String.t()}
| {:error, :actor_fetch_error | :actor_not_fetchable | :pem_decode_error}
def fetch_public_key(conn) do
with %{"keyId" => kid} <- HTTPSignatures.signature_for_conn(conn),
actor_id <- key_id_to_actor_url(kid),
:ok <- Logger.debug("Fetching public key for #{actor_id}"),
2019-09-09 00:52:49 +02:00
{:ok, public_key} <- get_public_key_for_url(actor_id) do
{:ok, public_key}
end
end
@spec refetch_public_key(Plug.Conn.t()) ::
{:ok, String.t()}
| {:error, :actor_fetch_error | :actor_not_fetchable | :pem_decode_error}
def refetch_public_key(conn) do
with %{"keyId" => kid} <- HTTPSignatures.signature_for_conn(conn),
actor_id <- key_id_to_actor_url(kid),
:ok <- Logger.debug("Refetching public key for #{actor_id}"),
{:ok, _actor} <- ActivityPubActor.make_actor_from_url(actor_id),
2019-09-09 00:52:49 +02:00
{:ok, public_key} <- get_public_key_for_url(actor_id) do
{:ok, public_key}
end
end
@spec sign(Actor.t(), map()) :: String.t()
def sign(%Actor{domain: domain, keys: keys} = actor, headers) when is_nil(domain) do
Logger.debug("Signing a payload on behalf of #{actor.url}")
Logger.debug("headers")
Logger.debug(inspect(headers))
2019-09-09 00:52:49 +02:00
with {:ok, key} <- prepare_public_key(keys) do
HTTPSignatures.sign(key, actor.url <> "#main-key", headers)
end
end
def sign(%Actor{url: url}, _) do
Logger.error("Can't do a signature on remote actor #{url}")
raise ArgumentError, message: "Can't do a signature on remote actor #{url}"
end
@spec generate_date_header :: String.t()
def generate_date_header, do: generate_date_header(NaiveDateTime.utc_now())
def generate_date_header(%NaiveDateTime{} = date) do
Timex.format!(date, "{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
end
@spec generate_request_target(String.t(), String.t()) :: String.t()
def generate_request_target(method, path), do: "#{method} #{path}"
@spec build_digest(String.t()) :: String.t()
def build_digest(body) do
2020-01-22 02:14:42 +01:00
"SHA-256=#{:sha256 |> :crypto.hash(body) |> Base.encode64()}"
end
end