logo

pleroma

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

ensure_public_or_authenticated_plug.ex (907B)


  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.EnsurePublicOrAuthenticatedPlug do
  5. @moduledoc """
  6. Ensures instance publicity or _user_ authentication
  7. (app-bound user-unbound tokens are accepted only if the instance is public).
  8. """
  9. import Pleroma.Web.TranslationHelpers
  10. import Plug.Conn
  11. alias Pleroma.Config
  12. alias Pleroma.User
  13. use Pleroma.Web, :plug
  14. def init(options) do
  15. options
  16. end
  17. @impl true
  18. def perform(conn, _) do
  19. public? = Config.get!([:instance, :public])
  20. case {public?, conn} do
  21. {true, _} ->
  22. conn
  23. {false, %{assigns: %{user: %User{}}}} ->
  24. conn
  25. {false, _} ->
  26. conn
  27. |> render_error(:forbidden, "This resource requires authentication.")
  28. |> halt
  29. end
  30. end
  31. end