2019-07-30 16:40:59 +02:00
|
|
|
# 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
|
|
|
|
|
|
|
|
defmodule Mobilizon.Service.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
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
@behaviour HTTPSignatures.Adapter
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
2019-09-09 00:52:49 +02:00
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
def key_id_to_actor_url(key_id) do
|
2019-09-09 09:35:50 +02:00
|
|
|
%{path: path} =
|
|
|
|
uri =
|
2019-09-09 00:52:49 +02:00
|
|
|
key_id
|
|
|
|
|> URI.parse()
|
2019-07-30 16:40:59 +02:00
|
|
|
|> Map.put(:fragment, nil)
|
|
|
|
|
|
|
|
uri =
|
2019-09-09 09:35:50 +02:00
|
|
|
if is_nil(path) do
|
2019-07-30 16:40:59 +02:00
|
|
|
uri
|
2019-09-09 09:35:50 +02:00
|
|
|
else
|
|
|
|
Map.put(uri, :path, String.trim_trailing(path, "/publickey"))
|
2019-07-30 16:40:59 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a public key for a given ActivityPub actor ID (url).
|
|
|
|
"""
|
|
|
|
@spec get_public_key_for_url(String.t()) ::
|
|
|
|
{:ok, String.t()} | {:error, :actor_fetch_error | :pem_decode_error}
|
|
|
|
def get_public_key_for_url(url) do
|
2019-10-25 17:43:37 +02:00
|
|
|
with {:ok, %Actor{keys: keys}} <- ActivityPub.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}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Logger.error("Unable to fetch actor, so no keys for you")
|
|
|
|
|
|
|
|
{:error, :actor_fetch_error}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
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
|
2019-07-30 16:40:59 +02:00
|
|
|
{:ok, public_key}
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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} <- ActivityPub.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
|
2019-07-30 16:40:59 +02:00
|
|
|
{:ok, public_key}
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
def sign(%Actor{keys: keys} = actor, headers) do
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.debug("Signing 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
|
2019-07-30 16:40:59 +02:00
|
|
|
HTTPSignatures.sign(key, actor.url <> "#main-key", headers)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_date_header(date \\ Timex.now("GMT")) do
|
|
|
|
case Timex.format(date, "%a, %d %b %Y %H:%M:%S %Z", :strftime) do
|
|
|
|
{:ok, date} ->
|
|
|
|
date
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
Logger.error("Unable to generate date header")
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_request_target(method, path), do: "#{method} #{path}"
|
|
|
|
|
|
|
|
def build_digest(body) do
|
|
|
|
"SHA-256=" <> (:crypto.hash(:sha256, body) |> Base.encode64())
|
|
|
|
end
|
|
|
|
end
|