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