switch to pleroma/http_signatures library
[akkoma] / lib / pleroma / signature.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Signature do
6 @behaviour HTTPSignatures.Adapter
7
8 alias Pleroma.User
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.ActivityPub.Utils
11 alias Pleroma.Web.Salmon
12 alias Pleroma.Web.WebFinger
13
14 def fetch_public_key(conn) do
15 with actor_id <- Utils.get_ap_id(conn.params["actor"]),
16 {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
17 {:ok, public_key}
18 else
19 e ->
20 {:error, e}
21 end
22 end
23
24 def refetch_public_key(conn) do
25 with actor_id <- Utils.get_ap_id(conn.params["actor"]),
26 {:ok, _user} <- ActivityPub.make_user_from_ap_id(actor_id),
27 {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
28 {:ok, public_key}
29 else
30 e ->
31 {:error, e}
32 end
33 end
34
35 def sign(%User{} = user, headers) do
36 with {:ok, %{info: %{keys: keys}}} <- WebFinger.ensure_keys_present(user),
37 {:ok, private_key, _} <- Salmon.keys_from_pem(keys) do
38 HTTPSignatures.sign(private_key, user.ap_id <> "#main-key", headers)
39 end
40 end
41 end