2019-05-14 14:03:13 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-05-14 14:03:13 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Signature do
|
|
|
|
@behaviour HTTPSignatures.Adapter
|
|
|
|
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2024-06-26 22:06:27 -06:00
|
|
|
alias Pleroma.User.SigningKey
|
2022-07-18 08:21:27 -06:00
|
|
|
|
2019-07-18 09:35:42 -06:00
|
|
|
def key_id_to_actor_id(key_id) do
|
2024-06-26 22:06:27 -06:00
|
|
|
# Given the key ID, first attempt to look it up in the signing keys table.
|
|
|
|
# If it's not found, then attempt to look it up via request to the remote instance.
|
|
|
|
case SigningKey.key_id_to_ap_id(key_id) do
|
|
|
|
nil ->
|
|
|
|
# this requires us to look up the url!
|
|
|
|
request_key_id_from_remote_instance(key_id)
|
2020-05-01 07:54:38 -06:00
|
|
|
|
2024-06-26 22:06:27 -06:00
|
|
|
key ->
|
|
|
|
{:ok, key}
|
2020-05-01 07:54:38 -06:00
|
|
|
end
|
2019-07-17 13:18:19 -06:00
|
|
|
end
|
2019-05-14 14:03:13 -06:00
|
|
|
|
2024-06-26 22:06:27 -06:00
|
|
|
def request_key_id_from_remote_instance(key_id) do
|
|
|
|
case SigningKey.fetch_remote_key(key_id) do
|
|
|
|
{:ok, key_id} ->
|
|
|
|
{:ok, key_id}
|
|
|
|
|
|
|
|
{:error, _} ->
|
|
|
|
{:error, "Key ID not found"}
|
2022-07-18 08:21:27 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-14 14:03:13 -06:00
|
|
|
def fetch_public_key(conn) do
|
2019-07-17 13:18:19 -06:00
|
|
|
with %{"keyId" => kid} <- HTTPSignatures.signature_for_conn(conn),
|
2020-05-01 07:54:38 -06:00
|
|
|
{:ok, actor_id} <- key_id_to_actor_id(kid),
|
2020-11-17 07:28:30 -07:00
|
|
|
{:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
|
2019-05-14 14:03:13 -06:00
|
|
|
{:ok, public_key}
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def refetch_public_key(conn) do
|
2019-07-17 13:18:19 -06:00
|
|
|
with %{"keyId" => kid} <- HTTPSignatures.signature_for_conn(conn),
|
2020-05-01 07:54:38 -06:00
|
|
|
{:ok, actor_id} <- key_id_to_actor_id(kid),
|
2020-11-17 07:28:30 -07:00
|
|
|
{:ok, _user} <- ActivityPub.make_user_from_ap_id(actor_id),
|
|
|
|
{:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
|
2019-05-14 14:03:13 -06:00
|
|
|
{:ok, public_key}
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-26 22:06:27 -06:00
|
|
|
def sign(%User{} = user, headers) do
|
|
|
|
with {:ok, private_key} <- SigningKey.private_key(user) do
|
2019-05-14 14:03:13 -06:00
|
|
|
HTTPSignatures.sign(private_key, user.ap_id <> "#main-key", headers)
|
|
|
|
end
|
|
|
|
end
|
2019-08-22 13:39:06 -06:00
|
|
|
|
|
|
|
def signed_date, do: signed_date(NaiveDateTime.utc_now())
|
|
|
|
|
|
|
|
def signed_date(%NaiveDateTime{} = date) do
|
2023-08-07 06:39:59 -06:00
|
|
|
Timex.lformat!(date, "{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT", "en")
|
2019-08-22 13:39:06 -06:00
|
|
|
end
|
2019-05-14 14:03:13 -06:00
|
|
|
end
|