logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

digest_plug.ex (881B)


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