fix formatting
[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 import Plug.Conn
7 require Logger
8
9 def init(options) do
10 options
11 end
12
13 def call(%{assigns: %{valid_signature: true}} = conn, _opts) do
14 conn
15 end
16
17 def call(conn, _opts) do
18 [signature | _] = get_req_header(conn, "signature")
19
20 if signature do
21 # set (request-target) header to the appropriate value
22 # we also replace the digest header with the one we computed
23 conn =
24 conn
25 |> put_req_header(
26 "(request-target)",
27 String.downcase("#{conn.method}") <> " #{conn.request_path}"
28 )
29
30 conn =
31 if conn.assigns[:digest] do
32 conn
33 |> put_req_header("digest", conn.assigns[:digest])
34 else
35 conn
36 end
37
38 assign(conn, :valid_signature, HTTPSignatures.validate_conn(conn))
39 else
40 Logger.debug("No signature header!")
41 conn
42 end
43 end
44 end