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 line
7.2KB

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