move key generation functions into Pleroma.Keys module
[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.Keys
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.ActivityPub.Utils
12
13 def fetch_public_key(conn) do
14 with actor_id <- Utils.get_ap_id(conn.params["actor"]),
15 {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
16 {:ok, public_key}
17 else
18 e ->
19 {:error, e}
20 end
21 end
22
23 def refetch_public_key(conn) do
24 with actor_id <- Utils.get_ap_id(conn.params["actor"]),
25 {:ok, _user} <- ActivityPub.make_user_from_ap_id(actor_id),
26 {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
27 {:ok, public_key}
28 else
29 e ->
30 {:error, e}
31 end
32 end
33
34 def sign(%User{} = user, headers) do
35 with {:ok, %{info: %{keys: keys}}} <- User.ensure_keys_present(user),
36 {:ok, private_key, _} <- Keys.keys_from_pem(keys) do
37 HTTPSignatures.sign(private_key, user.ap_id <> "#main-key", headers)
38 end
39 end
40 end