Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
7.2KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 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. # Executed just before actual controller action, invokes before-action hooks (callbacks)
  52. defp action(conn, params) do
  53. with %{halted: false} = conn <-
  54. maybe_drop_authentication_if_oauth_check_ignored(conn),
  55. %{halted: false} = conn <- maybe_perform_public_or_authenticated_check(conn),
  56. %{halted: false} = conn <- maybe_perform_authenticated_check(conn),
  57. %{halted: false} = conn <- maybe_halt_on_missing_oauth_scopes_check(conn) do
  58. super(conn, params)
  59. end
  60. end
  61. # For non-authenticated API actions, drops auth info if OAuth scopes check was ignored
  62. # (neither performed nor explicitly skipped)
  63. defp maybe_drop_authentication_if_oauth_check_ignored(conn) do
  64. if PlugHelper.plug_called?(conn, ExpectPublicOrAuthenticatedCheckPlug) and
  65. not PlugHelper.plug_called_or_skipped?(conn, OAuthScopesPlug) do
  66. AuthHelper.drop_auth_info(conn)
  67. else
  68. conn
  69. end
  70. end
  71. # Ensures instance is public -or- user is authenticated if such check was scheduled
  72. defp maybe_perform_public_or_authenticated_check(conn) do
  73. if PlugHelper.plug_called?(conn, ExpectPublicOrAuthenticatedCheckPlug) do
  74. EnsurePublicOrAuthenticatedPlug.call(conn, %{})
  75. else
  76. conn
  77. end
  78. end
  79. # Ensures user is authenticated if such check was scheduled
  80. # Note: runs prior to action even if it was already executed earlier in plug chain
  81. # (since OAuthScopesPlug has option of proceeding unauthenticated)
  82. defp maybe_perform_authenticated_check(conn) do
  83. if PlugHelper.plug_called?(conn, ExpectAuthenticatedCheckPlug) do
  84. EnsureAuthenticatedPlug.call(conn, %{})
  85. else
  86. conn
  87. end
  88. end
  89. # Halts if authenticated API action neither performs nor explicitly skips OAuth scopes check
  90. defp maybe_halt_on_missing_oauth_scopes_check(conn) do
  91. if PlugHelper.plug_called?(conn, ExpectAuthenticatedCheckPlug) and
  92. not PlugHelper.plug_called_or_skipped?(conn, OAuthScopesPlug) do
  93. conn
  94. |> render_error(
  95. :forbidden,
  96. "Security violation: OAuth scopes check was neither handled nor explicitly skipped."
  97. )
  98. |> halt()
  99. else
  100. conn
  101. end
  102. end
  103. end
  104. end
  105. def view do
  106. quote do
  107. use Phoenix.View,
  108. root: "lib/pleroma/web/templates",
  109. namespace: Pleroma.Web
  110. # Import convenience functions from controllers
  111. import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
  112. import Pleroma.Web.ErrorHelpers
  113. import Pleroma.Web.Gettext
  114. alias Pleroma.Web.Router.Helpers, as: Routes
  115. require Logger
  116. @doc "Same as `render/3` but wrapped in a rescue block"
  117. def safe_render(view, template, assigns \\ %{}) do
  118. Phoenix.View.render(view, template, assigns)
  119. rescue
  120. error ->
  121. Logger.error(
  122. "#{__MODULE__} failed to render #{inspect({view, template})}\n" <>
  123. Exception.format(:error, error, __STACKTRACE__)
  124. )
  125. nil
  126. end
  127. @doc """
  128. Same as `render_many/4` but wrapped in rescue block.
  129. """
  130. def safe_render_many(collection, view, template, assigns \\ %{}) do
  131. Enum.map(collection, fn resource ->
  132. as = Map.get(assigns, :as) || view.__resource__
  133. assigns = Map.put(assigns, as, resource)
  134. safe_render(view, template, assigns)
  135. end)
  136. |> Enum.filter(& &1)
  137. end
  138. end
  139. end
  140. def router do
  141. quote do
  142. use Phoenix.Router
  143. # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
  144. import Plug.Conn
  145. import Phoenix.Controller
  146. end
  147. end
  148. def channel do
  149. quote do
  150. # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
  151. import Phoenix.Channel
  152. import Pleroma.Web.Gettext
  153. end
  154. end
  155. def plug do
  156. quote do
  157. @behaviour Pleroma.Web.Plug
  158. @behaviour Plug
  159. @doc """
  160. Marks a plug intentionally skipped and blocks its execution if it's present in plugs chain.
  161. """
  162. def skip_plug(conn) do
  163. PlugHelper.append_to_private_list(
  164. conn,
  165. PlugHelper.skipped_plugs_list_id(),
  166. __MODULE__
  167. )
  168. end
  169. @impl Plug
  170. @doc """
  171. Before-plug hook that
  172. * ensures the plug is not skipped
  173. * processes `:if_func` / `:unless_func` functional pre-run conditions
  174. * adds plug to the list of called plugs and calls `perform/2` if checks are passed
  175. Note: multiple invocations of the same plug (with different or same options) are allowed.
  176. """
  177. def call(%Plug.Conn{} = conn, options) do
  178. if PlugHelper.plug_skipped?(conn, __MODULE__) ||
  179. (options[:if_func] && !options[:if_func].(conn)) ||
  180. (options[:unless_func] && options[:unless_func].(conn)) do
  181. conn
  182. else
  183. conn =
  184. PlugHelper.append_to_private_list(
  185. conn,
  186. PlugHelper.called_plugs_list_id(),
  187. __MODULE__
  188. )
  189. apply(__MODULE__, :perform, [conn, options])
  190. end
  191. end
  192. end
  193. end
  194. @doc """
  195. When used, dispatch to the appropriate controller/view/etc.
  196. """
  197. defmacro __using__(which) when is_atom(which) do
  198. apply(__MODULE__, which, [])
  199. end
  200. end