logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

ensure_authenticated_plug.ex (915B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Plugs.EnsureAuthenticatedPlug do
  5. import Plug.Conn
  6. import Pleroma.Web.TranslationHelpers
  7. alias Pleroma.User
  8. use Pleroma.Web, :plug
  9. def init(options) do
  10. options
  11. end
  12. @impl true
  13. def perform(
  14. %{
  15. assigns: %{
  16. auth_credentials: %{password: _},
  17. user: %User{multi_factor_authentication_settings: %{enabled: true}}
  18. }
  19. } = conn,
  20. _
  21. ) do
  22. conn
  23. |> render_error(:forbidden, "Two-factor authentication enabled, you must use a access token.")
  24. |> halt()
  25. end
  26. def perform(%{assigns: %{user: %User{}}} = conn, _) do
  27. conn
  28. end
  29. def perform(conn, _) do
  30. conn
  31. |> render_error(:forbidden, "Invalid credentials.")
  32. |> halt()
  33. end
  34. end