activitypub: normalize the actor to ensure we have its URI
[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.normalize_actor(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 conn =
23 conn
24 |> put_req_header(
25 "(request-target)",
26 String.downcase("#{conn.method}") <> " #{conn.request_path}"
27 )
28
29 assign(conn, :valid_signature, HTTPSignatures.validate_conn(conn))
30
31 signature ->
32 Logger.debug("Signature not from actor")
33 assign(conn, :valid_signature, false)
34
35 true ->
36 Logger.debug("No signature header!")
37 conn
38 end
39 end
40 end