logo

pleroma

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

web.ex (7605B)


  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 do
  5. @moduledoc """
  6. A module that keeps using definitions for controllers,
  7. views and so on.
  8. This can be used in your application as:
  9. use Pleroma.Web, :controller
  10. use Pleroma.Web, :view
  11. The definitions below will be executed for every view,
  12. controller, etc, so keep them short and clean, focused
  13. on imports, uses and aliases.
  14. Do NOT define functions inside the quoted expressions
  15. below.
  16. """
  17. alias Pleroma.Helpers.AuthHelper
  18. alias Pleroma.Web.Plugs.EnsureAuthenticatedPlug
  19. alias Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
  20. alias Pleroma.Web.Plugs.ExpectAuthenticatedCheckPlug
  21. alias Pleroma.Web.Plugs.ExpectPublicOrAuthenticatedCheckPlug
  22. alias Pleroma.Web.Plugs.OAuthScopesPlug
  23. alias Pleroma.Web.Plugs.PlugHelper
  24. def controller do
  25. quote do
  26. use Phoenix.Controller, namespace: Pleroma.Web
  27. import Plug.Conn
  28. import Pleroma.Web.Gettext
  29. import Pleroma.Web.TranslationHelpers
  30. alias Pleroma.Web.Router.Helpers, as: Routes
  31. plug(:set_put_layout)
  32. defp set_put_layout(conn, _) do
  33. put_layout(conn, Pleroma.Config.get(:app_layout, "app.html"))
  34. end
  35. # Marks plugs intentionally skipped and blocks their execution if present in plugs chain
  36. defp skip_plug(conn, plug_modules) do
  37. plug_modules
  38. |> List.wrap()
  39. |> Enum.reduce(
  40. conn,
  41. fn plug_module, conn ->
  42. try do
  43. plug_module.skip_plug(conn)
  44. rescue
  45. UndefinedFunctionError ->
  46. raise "`#{plug_module}` is not skippable. Append `use Pleroma.Web, :plug` to its code."
  47. end
  48. end
  49. )
  50. end
  51. defp skip_auth(conn, _) do
  52. skip_plug(conn, [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug])
  53. end
  54. defp skip_public_check(conn, _) do
  55. skip_plug(conn, EnsurePublicOrAuthenticatedPlug)
  56. end
  57. # Executed just before actual controller action, invokes before-action hooks (callbacks)
  58. defp action(conn, params) do
  59. with %{halted: false} = conn <-
  60. maybe_drop_authentication_if_oauth_check_ignored(conn),
  61. %{halted: false} = conn <- maybe_perform_public_or_authenticated_check(conn),
  62. %{halted: false} = conn <- maybe_perform_authenticated_check(conn),
  63. %{halted: false} = conn <- maybe_halt_on_missing_oauth_scopes_check(conn) do
  64. super(conn, params)
  65. end
  66. end
  67. # For non-authenticated API actions, drops auth info if OAuth scopes check was ignored
  68. # (neither performed nor explicitly skipped)
  69. defp maybe_drop_authentication_if_oauth_check_ignored(conn) do
  70. if PlugHelper.plug_called?(conn, ExpectPublicOrAuthenticatedCheckPlug) and
  71. not PlugHelper.plug_called_or_skipped?(conn, OAuthScopesPlug) do
  72. AuthHelper.drop_auth_info(conn)
  73. else
  74. conn
  75. end
  76. end
  77. # Ensures instance is public -or- user is authenticated if such check was scheduled
  78. defp maybe_perform_public_or_authenticated_check(conn) do
  79. if PlugHelper.plug_called?(conn, ExpectPublicOrAuthenticatedCheckPlug) do
  80. EnsurePublicOrAuthenticatedPlug.call(conn, %{})
  81. else
  82. conn
  83. end
  84. end
  85. # Ensures user is authenticated if such check was scheduled
  86. # Note: runs prior to action even if it was already executed earlier in plug chain
  87. # (since OAuthScopesPlug has option of proceeding unauthenticated)
  88. defp maybe_perform_authenticated_check(conn) do
  89. if PlugHelper.plug_called?(conn, ExpectAuthenticatedCheckPlug) do
  90. EnsureAuthenticatedPlug.call(conn, %{})
  91. else
  92. conn
  93. end
  94. end
  95. # Halts if authenticated API action neither performs nor explicitly skips OAuth scopes check
  96. defp maybe_halt_on_missing_oauth_scopes_check(conn) do
  97. if PlugHelper.plug_called?(conn, ExpectAuthenticatedCheckPlug) and
  98. not PlugHelper.plug_called_or_skipped?(conn, OAuthScopesPlug) do
  99. conn
  100. |> render_error(
  101. :forbidden,
  102. "Security violation: OAuth scopes check was neither handled nor explicitly skipped."
  103. )
  104. |> halt()
  105. else
  106. conn
  107. end
  108. end
  109. end
  110. end
  111. def view do
  112. quote do
  113. use Phoenix.View,
  114. root: "lib/pleroma/web/templates",
  115. namespace: Pleroma.Web
  116. # Import convenience functions from controllers
  117. import Phoenix.Controller, only: [get_csrf_token: 0, view_module: 1]
  118. import Pleroma.Web.ErrorHelpers
  119. import Pleroma.Web.Gettext
  120. alias Pleroma.Web.Router.Helpers, as: Routes
  121. require Logger
  122. @doc "Same as `render/3` but wrapped in a rescue block"
  123. def safe_render(view, template, assigns \\ %{}) do
  124. Phoenix.View.render(view, template, assigns)
  125. rescue
  126. error ->
  127. Logger.error(
  128. "#{__MODULE__} failed to render #{inspect({view, template})}\n" <>
  129. Exception.format(:error, error, __STACKTRACE__)
  130. )
  131. nil
  132. end
  133. @doc """
  134. Same as `render_many/4` but wrapped in rescue block.
  135. """
  136. def safe_render_many(collection, view, template, assigns \\ %{}) do
  137. Enum.map(collection, fn resource ->
  138. as = Map.get(assigns, :as) || view.__resource__
  139. assigns = Map.put(assigns, as, resource)
  140. safe_render(view, template, assigns)
  141. end)
  142. |> Enum.filter(& &1)
  143. end
  144. end
  145. end
  146. def router do
  147. quote do
  148. use Phoenix.Router
  149. # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
  150. import Plug.Conn
  151. import Phoenix.Controller
  152. end
  153. end
  154. def channel do
  155. quote do
  156. # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
  157. import Phoenix.Channel
  158. import Pleroma.Web.Gettext
  159. end
  160. end
  161. def plug do
  162. quote do
  163. @behaviour Pleroma.Web.Plug
  164. @behaviour Plug
  165. @doc """
  166. Marks a plug intentionally skipped and blocks its execution if it's present in plugs chain.
  167. """
  168. def skip_plug(conn) do
  169. PlugHelper.append_to_private_list(
  170. conn,
  171. PlugHelper.skipped_plugs_list_id(),
  172. __MODULE__
  173. )
  174. end
  175. @impl Plug
  176. @doc """
  177. Before-plug hook that
  178. * ensures the plug is not skipped
  179. * processes `:if_func` / `:unless_func` functional pre-run conditions
  180. * adds plug to the list of called plugs and calls `perform/2` if checks are passed
  181. Note: multiple invocations of the same plug (with different or same options) are allowed.
  182. """
  183. def call(%Plug.Conn{} = conn, options) do
  184. if PlugHelper.plug_skipped?(conn, __MODULE__) ||
  185. (options[:if_func] && !options[:if_func].(conn)) ||
  186. (options[:unless_func] && options[:unless_func].(conn)) do
  187. conn
  188. else
  189. conn =
  190. PlugHelper.append_to_private_list(
  191. conn,
  192. PlugHelper.called_plugs_list_id(),
  193. __MODULE__
  194. )
  195. apply(__MODULE__, :perform, [conn, options])
  196. end
  197. end
  198. end
  199. end
  200. @doc """
  201. When used, dispatch to the appropriate controller/view/etc.
  202. """
  203. defmacro __using__(which) when is_atom(which) do
  204. apply(__MODULE__, which, [])
  205. end
  206. end