Fix uploaded media plug test
[akkoma] / lib / pleroma / plugs / http_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.Web.Plugs.HTTPSignaturePlug do
6 alias Pleroma.Web.HTTPSignatures
7 alias Pleroma.Web.ActivityPub.Utils
8 import Plug.Conn
9 require Logger
10
11 def init(options) do
12 options
13 end
14
15 def call(%{assigns: %{valid_signature: true}} = conn, _opts) do
16 conn
17 end
18
19 def call(conn, _opts) do
20 user = Utils.get_ap_id(conn.params["actor"])
21 Logger.debug("Checking sig for #{user}")
22 [signature | _] = get_req_header(conn, "signature")
23
24 cond do
25 signature && String.contains?(signature, user) ->
26 # set (request-target) header to the appropriate value
27 # we also replace the digest header with the one we computed
28 conn =
29 conn
30 |> put_req_header(
31 "(request-target)",
32 String.downcase("#{conn.method}") <> " #{conn.request_path}"
33 )
34
35 conn =
36 if conn.assigns[:digest] do
37 conn
38 |> put_req_header("digest", conn.assigns[:digest])
39 else
40 conn
41 end
42
43 assign(conn, :valid_signature, HTTPSignatures.validate_conn(conn))
44
45 signature ->
46 Logger.debug("Signature not from actor")
47 assign(conn, :valid_signature, false)
48
49 true ->
50 Logger.debug("No signature header!")
51 conn
52 end
53 end
54 end