logo

pleroma

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

ensure_authenticated_plug.ex (1025B)


  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.EnsureAuthenticatedPlug do
  5. @moduledoc """
  6. Ensures _user_ authentication (app-bound user-unbound tokens are not accepted).
  7. """
  8. import Plug.Conn
  9. import Pleroma.Web.TranslationHelpers
  10. alias Pleroma.User
  11. use Pleroma.Web, :plug
  12. def init(options) do
  13. options
  14. end
  15. @impl true
  16. def perform(
  17. %{
  18. assigns: %{
  19. auth_credentials: %{password: _},
  20. user: %User{multi_factor_authentication_settings: %{enabled: true}}
  21. }
  22. } = conn,
  23. _
  24. ) do
  25. conn
  26. |> render_error(:forbidden, "Two-factor authentication enabled, you must use a access token.")
  27. |> halt()
  28. end
  29. def perform(%{assigns: %{user: %User{}}} = conn, _) do
  30. conn
  31. end
  32. def perform(conn, _) do
  33. conn
  34. |> render_error(:forbidden, "Invalid credentials.")
  35. |> halt()
  36. end
  37. end