in dev, allow dev FE
[akkoma] / lib / pleroma / web / plugs / digest_plug.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.DigestPlug do
6 alias Plug.Conn
7 require Logger
8
9 def read_body(conn, opts) do
10 digest_algorithm =
11 with [digest_header] <- Conn.get_req_header(conn, "digest") do
12 digest_header
13 |> String.split("=", parts: 2)
14 |> List.first()
15 else
16 _ -> "SHA-256"
17 end
18
19 unless String.downcase(digest_algorithm) == "sha-256" do
20 raise ArgumentError,
21 message: "invalid value for digest algorithm, got: #{digest_algorithm}"
22 end
23
24 {:ok, body, conn} = Conn.read_body(conn, opts)
25 encoded_digest = :crypto.hash(:sha256, body) |> Base.encode64()
26 {:ok, body, Conn.assign(conn, :digest, "#{digest_algorithm}=#{encoded_digest}")}
27 end
28 end