logo

pleroma

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

basic_auth_decoder_plug.ex (844B)


  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.BasicAuthDecoderPlug do
  5. @moduledoc """
  6. Decodes HTTP Basic Auth information and assigns `:auth_credentials`.
  7. NOTE: no checks are performed at this step, auth_credentials/username could be easily faked.
  8. """
  9. import Plug.Conn
  10. def init(options) do
  11. options
  12. end
  13. def call(conn, _opts) do
  14. with ["Basic " <> header] <- get_req_header(conn, "authorization"),
  15. {:ok, userinfo} <- Base.decode64(header),
  16. [username, password] <- String.split(userinfo, ":", parts: 2) do
  17. conn
  18. |> assign(:auth_credentials, %{
  19. username: username,
  20. password: password
  21. })
  22. else
  23. _ -> conn
  24. end
  25. end
  26. end