1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web do
7 A module that keeps using definitions for controllers,
10 This can be used in your application as:
12 use Pleroma.Web, :controller
13 use Pleroma.Web, :view
15 The definitions below will be executed for every view,
16 controller, etc, so keep them short and clean, focused
17 on imports, uses and aliases.
19 Do NOT define functions inside the quoted expressions
23 alias Pleroma.Helpers.AuthHelper
24 alias Pleroma.Web.Plugs.EnsureAuthenticatedPlug
25 alias Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
26 alias Pleroma.Web.Plugs.ExpectAuthenticatedCheckPlug
27 alias Pleroma.Web.Plugs.ExpectPublicOrAuthenticatedCheckPlug
28 alias Pleroma.Web.Plugs.OAuthScopesPlug
29 alias Pleroma.Web.Plugs.PlugHelper
33 use Phoenix.Controller, namespace: Pleroma.Web
37 import Pleroma.Web.Gettext
38 import Pleroma.Web.TranslationHelpers
40 alias Pleroma.Web.Router.Helpers, as: Routes
44 defp set_put_layout(conn, _) do
45 put_layout(conn, Pleroma.Config.get(:app_layout, "app.html"))
48 # Marks plugs intentionally skipped and blocks their execution if present in plugs chain
49 defp skip_plug(conn, plug_modules) do
54 fn plug_module, conn ->
56 plug_module.skip_plug(conn)
58 UndefinedFunctionError ->
60 "`#{plug_module}` is not skippable. Append `use Pleroma.Web, :plug` to its code.",
68 defp skip_auth(conn, _) do
69 skip_plug(conn, [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug])
72 defp skip_public_check(conn, _) do
73 skip_plug(conn, EnsurePublicOrAuthenticatedPlug)
76 # Executed just before actual controller action, invokes before-action hooks (callbacks)
77 defp action(conn, params) do
78 with %{halted: false} = conn <-
79 maybe_drop_authentication_if_oauth_check_ignored(conn),
80 %{halted: false} = conn <- maybe_perform_public_or_authenticated_check(conn),
81 %{halted: false} = conn <- maybe_perform_authenticated_check(conn),
82 %{halted: false} = conn <- maybe_halt_on_missing_oauth_scopes_check(conn) do
87 # For non-authenticated API actions, drops auth info if OAuth scopes check was ignored
88 # (neither performed nor explicitly skipped)
89 defp maybe_drop_authentication_if_oauth_check_ignored(conn) do
90 if PlugHelper.plug_called?(conn, ExpectPublicOrAuthenticatedCheckPlug) and
91 not PlugHelper.plug_called_or_skipped?(conn, OAuthScopesPlug) do
92 AuthHelper.drop_auth_info(conn)
98 # Ensures instance is public -or- user is authenticated if such check was scheduled
99 defp maybe_perform_public_or_authenticated_check(conn) do
100 if PlugHelper.plug_called?(conn, ExpectPublicOrAuthenticatedCheckPlug) do
101 EnsurePublicOrAuthenticatedPlug.call(conn, %{})
107 # Ensures user is authenticated if such check was scheduled
108 # Note: runs prior to action even if it was already executed earlier in plug chain
109 # (since OAuthScopesPlug has option of proceeding unauthenticated)
110 defp maybe_perform_authenticated_check(conn) do
111 if PlugHelper.plug_called?(conn, ExpectAuthenticatedCheckPlug) do
112 EnsureAuthenticatedPlug.call(conn, %{})
118 # Halts if authenticated API action neither performs nor explicitly skips OAuth scopes check
119 defp maybe_halt_on_missing_oauth_scopes_check(conn) do
120 if PlugHelper.plug_called?(conn, ExpectAuthenticatedCheckPlug) and
121 not PlugHelper.plug_called_or_skipped?(conn, OAuthScopesPlug) do
125 "Security violation: OAuth scopes check was neither handled nor explicitly skipped."
137 @behaviour Pleroma.Web.Plug
141 Marks a plug intentionally skipped and blocks its execution if it's present in plugs chain.
143 def skip_plug(conn) do
144 PlugHelper.append_to_private_list(
146 PlugHelper.skipped_plugs_list_id(),
153 Before-plug hook that
154 * ensures the plug is not skipped
155 * processes `:if_func` / `:unless_func` functional pre-run conditions
156 * adds plug to the list of called plugs and calls `perform/2` if checks are passed
158 Note: multiple invocations of the same plug (with different or same options) are allowed.
160 def call(%Plug.Conn{} = conn, options) do
161 if PlugHelper.plug_skipped?(conn, __MODULE__) ||
162 (options[:if_func] && !options[:if_func].(conn)) ||
163 (options[:unless_func] && options[:unless_func].(conn)) do
167 PlugHelper.append_to_private_list(
169 PlugHelper.called_plugs_list_id(),
173 apply(__MODULE__, :perform, [conn, options])
182 root: "lib/pleroma/web/templates",
183 namespace: Pleroma.Web
185 # Import convenience functions from controllers
186 import Phoenix.Controller,
187 only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1]
189 # Include shared imports and aliases for views
190 unquote(view_helpers())
196 use Phoenix.LiveView,
197 layout: {Pleroma.Web.LayoutView, "live.html"}
199 unquote(view_helpers())
203 def live_component do
205 use Phoenix.LiveComponent
207 unquote(view_helpers())
213 use Phoenix.Component
215 unquote(view_helpers())
224 import Phoenix.Controller
225 import Phoenix.LiveView.Router
232 import Pleroma.Web.Gettext
238 # Use all HTML functionality (forms, tags, etc)
241 # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
242 import Phoenix.LiveView.Helpers
244 # Import basic rendering functionality (render, render_layout, etc)
247 import Pleroma.Web.ErrorHelpers
248 import Pleroma.Web.Gettext
249 alias Pleroma.Web.Router.Helpers, as: Routes
254 When used, dispatch to the appropriate controller/view/etc.
256 defmacro __using__(which) when is_atom(which) do
257 apply(__MODULE__, which, [])