2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-03-17 16:09:58 +00:00
|
|
|
defmodule Pleroma.Web do
|
|
|
|
@moduledoc """
|
|
|
|
A module that keeps using definitions for controllers,
|
|
|
|
views and so on.
|
|
|
|
|
|
|
|
This can be used in your application as:
|
|
|
|
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
use Pleroma.Web, :view
|
|
|
|
|
|
|
|
The definitions below will be executed for every view,
|
|
|
|
controller, etc, so keep them short and clean, focused
|
|
|
|
on imports, uses and aliases.
|
|
|
|
|
|
|
|
Do NOT define functions inside the quoted expressions
|
|
|
|
below.
|
|
|
|
"""
|
|
|
|
|
2020-12-06 10:59:10 +00:00
|
|
|
alias Pleroma.Helpers.AuthHelper
|
2020-06-24 08:55:56 +01:00
|
|
|
alias Pleroma.Web.Plugs.EnsureAuthenticatedPlug
|
2020-06-24 08:53:10 +01:00
|
|
|
alias Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
|
2020-06-24 08:44:50 +01:00
|
|
|
alias Pleroma.Web.Plugs.ExpectAuthenticatedCheckPlug
|
2020-06-24 08:43:19 +01:00
|
|
|
alias Pleroma.Web.Plugs.ExpectPublicOrAuthenticatedCheckPlug
|
2020-06-24 07:57:27 +01:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2020-06-24 07:42:36 +01:00
|
|
|
alias Pleroma.Web.Plugs.PlugHelper
|
2023-08-15 11:22:18 +01:00
|
|
|
require Pleroma.Constants
|
2020-04-21 14:29:19 +01:00
|
|
|
|
2017-03-17 16:09:58 +00:00
|
|
|
def controller do
|
|
|
|
quote do
|
|
|
|
use Phoenix.Controller, namespace: Pleroma.Web
|
2019-07-10 08:52:41 +01:00
|
|
|
|
2017-03-17 16:09:58 +00:00
|
|
|
import Plug.Conn
|
2020-04-21 14:29:19 +01:00
|
|
|
|
2019-02-09 15:20:18 +00:00
|
|
|
import Pleroma.Web.Gettext
|
2019-07-10 08:52:41 +01:00
|
|
|
import Pleroma.Web.TranslationHelpers
|
2019-02-21 15:55:19 +00:00
|
|
|
|
2023-08-15 11:22:18 +01:00
|
|
|
unquote(verified_routes())
|
2021-05-18 23:11:25 +01:00
|
|
|
|
2019-02-28 10:00:54 +00:00
|
|
|
plug(:set_put_layout)
|
|
|
|
|
|
|
|
defp set_put_layout(conn, _) do
|
|
|
|
put_layout(conn, Pleroma.Config.get(:app_layout, "app.html"))
|
|
|
|
end
|
2020-04-06 08:20:44 +01:00
|
|
|
|
2020-04-21 14:29:19 +01:00
|
|
|
# Marks plugs intentionally skipped and blocks their execution if present in plugs chain
|
|
|
|
defp skip_plug(conn, plug_modules) do
|
|
|
|
plug_modules
|
|
|
|
|> List.wrap()
|
|
|
|
|> Enum.reduce(
|
|
|
|
conn,
|
|
|
|
fn plug_module, conn ->
|
|
|
|
try do
|
|
|
|
plug_module.skip_plug(conn)
|
|
|
|
rescue
|
|
|
|
UndefinedFunctionError ->
|
2022-12-14 12:38:48 +00:00
|
|
|
reraise(
|
|
|
|
"`#{plug_module}` is not skippable. Append `use Pleroma.Web, :plug` to its code.",
|
|
|
|
__STACKTRACE__
|
|
|
|
)
|
2020-04-21 14:29:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
2020-04-06 08:20:44 +01:00
|
|
|
end
|
|
|
|
|
2021-06-09 01:14:12 +01:00
|
|
|
defp skip_auth(conn, _) do
|
|
|
|
skip_plug(conn, [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug])
|
|
|
|
end
|
|
|
|
|
|
|
|
defp skip_public_check(conn, _) do
|
|
|
|
skip_plug(conn, EnsurePublicOrAuthenticatedPlug)
|
|
|
|
end
|
|
|
|
|
2020-04-17 19:21:10 +01:00
|
|
|
# Executed just before actual controller action, invokes before-action hooks (callbacks)
|
2020-04-06 08:20:44 +01:00
|
|
|
defp action(conn, params) do
|
2021-02-24 21:23:45 +00:00
|
|
|
with %{halted: false} = conn <-
|
|
|
|
maybe_drop_authentication_if_oauth_check_ignored(conn),
|
2020-04-23 19:47:33 +01:00
|
|
|
%{halted: false} = conn <- maybe_perform_public_or_authenticated_check(conn),
|
|
|
|
%{halted: false} = conn <- maybe_perform_authenticated_check(conn),
|
|
|
|
%{halted: false} = conn <- maybe_halt_on_missing_oauth_scopes_check(conn) do
|
2020-04-17 19:21:10 +01:00
|
|
|
super(conn, params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 16:50:25 +01:00
|
|
|
# For non-authenticated API actions, drops auth info if OAuth scopes check was ignored
|
|
|
|
# (neither performed nor explicitly skipped)
|
|
|
|
defp maybe_drop_authentication_if_oauth_check_ignored(conn) do
|
|
|
|
if PlugHelper.plug_called?(conn, ExpectPublicOrAuthenticatedCheckPlug) and
|
|
|
|
not PlugHelper.plug_called_or_skipped?(conn, OAuthScopesPlug) do
|
2020-12-06 10:59:10 +00:00
|
|
|
AuthHelper.drop_auth_info(conn)
|
2020-04-22 16:50:25 +01:00
|
|
|
else
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 14:29:19 +01:00
|
|
|
# Ensures instance is public -or- user is authenticated if such check was scheduled
|
|
|
|
defp maybe_perform_public_or_authenticated_check(conn) do
|
|
|
|
if PlugHelper.plug_called?(conn, ExpectPublicOrAuthenticatedCheckPlug) do
|
|
|
|
EnsurePublicOrAuthenticatedPlug.call(conn, %{})
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Ensures user is authenticated if such check was scheduled
|
|
|
|
# Note: runs prior to action even if it was already executed earlier in plug chain
|
|
|
|
# (since OAuthScopesPlug has option of proceeding unauthenticated)
|
|
|
|
defp maybe_perform_authenticated_check(conn) do
|
|
|
|
if PlugHelper.plug_called?(conn, ExpectAuthenticatedCheckPlug) do
|
|
|
|
EnsureAuthenticatedPlug.call(conn, %{})
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-17 19:21:10 +01:00
|
|
|
# Halts if authenticated API action neither performs nor explicitly skips OAuth scopes check
|
|
|
|
defp maybe_halt_on_missing_oauth_scopes_check(conn) do
|
2020-04-21 14:29:19 +01:00
|
|
|
if PlugHelper.plug_called?(conn, ExpectAuthenticatedCheckPlug) and
|
|
|
|
not PlugHelper.plug_called_or_skipped?(conn, OAuthScopesPlug) do
|
2020-04-06 08:20:44 +01:00
|
|
|
conn
|
|
|
|
|> render_error(
|
|
|
|
:forbidden,
|
|
|
|
"Security violation: OAuth scopes check was neither handled nor explicitly skipped."
|
|
|
|
)
|
|
|
|
|> halt()
|
|
|
|
else
|
2020-04-17 19:21:10 +01:00
|
|
|
conn
|
2020-04-06 08:20:44 +01:00
|
|
|
end
|
|
|
|
end
|
2017-03-17 16:09:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-15 19:19:16 +01:00
|
|
|
def plug do
|
|
|
|
quote do
|
2020-04-21 14:29:19 +01:00
|
|
|
@behaviour Pleroma.Web.Plug
|
|
|
|
@behaviour Plug
|
2020-04-15 19:19:16 +01:00
|
|
|
|
2020-04-17 19:21:10 +01:00
|
|
|
@doc """
|
|
|
|
Marks a plug intentionally skipped and blocks its execution if it's present in plugs chain.
|
|
|
|
"""
|
|
|
|
def skip_plug(conn) do
|
|
|
|
PlugHelper.append_to_private_list(
|
|
|
|
conn,
|
|
|
|
PlugHelper.skipped_plugs_list_id(),
|
|
|
|
__MODULE__
|
|
|
|
)
|
|
|
|
end
|
2020-04-15 19:19:16 +01:00
|
|
|
|
|
|
|
@impl Plug
|
2020-04-21 14:29:19 +01:00
|
|
|
@doc """
|
2020-04-30 16:19:51 +01:00
|
|
|
Before-plug hook that
|
|
|
|
* ensures the plug is not skipped
|
|
|
|
* processes `:if_func` / `:unless_func` functional pre-run conditions
|
|
|
|
* adds plug to the list of called plugs and calls `perform/2` if checks are passed
|
|
|
|
|
2020-04-21 14:29:19 +01:00
|
|
|
Note: multiple invocations of the same plug (with different or same options) are allowed.
|
|
|
|
"""
|
2020-04-15 19:19:16 +01:00
|
|
|
def call(%Plug.Conn{} = conn, options) do
|
2020-04-30 16:19:51 +01:00
|
|
|
if PlugHelper.plug_skipped?(conn, __MODULE__) ||
|
|
|
|
(options[:if_func] && !options[:if_func].(conn)) ||
|
|
|
|
(options[:unless_func] && options[:unless_func].(conn)) do
|
2020-04-15 19:19:16 +01:00
|
|
|
conn
|
|
|
|
else
|
2020-04-21 14:29:19 +01:00
|
|
|
conn =
|
|
|
|
PlugHelper.append_to_private_list(
|
|
|
|
conn,
|
|
|
|
PlugHelper.called_plugs_list_id(),
|
|
|
|
__MODULE__
|
|
|
|
)
|
|
|
|
|
|
|
|
apply(__MODULE__, :perform, [conn, options])
|
2020-04-15 19:19:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-02 03:29:02 +00:00
|
|
|
def view do
|
|
|
|
quote do
|
|
|
|
use Phoenix.View,
|
|
|
|
root: "lib/pleroma/web/templates",
|
|
|
|
namespace: Pleroma.Web
|
|
|
|
|
|
|
|
# Import convenience functions from controllers
|
|
|
|
import Phoenix.Controller,
|
2023-08-15 11:22:18 +01:00
|
|
|
only: [view_module: 1, view_template: 1]
|
|
|
|
|
|
|
|
import Phoenix.Flash
|
|
|
|
alias Phoenix.Flash
|
2023-01-02 03:29:02 +00:00
|
|
|
|
|
|
|
# Include shared imports and aliases for views
|
|
|
|
unquote(view_helpers())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def live_view do
|
|
|
|
quote do
|
|
|
|
use Phoenix.LiveView,
|
|
|
|
layout: {Pleroma.Web.LayoutView, "live.html"}
|
|
|
|
|
|
|
|
unquote(view_helpers())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def live_component do
|
|
|
|
quote do
|
|
|
|
use Phoenix.LiveComponent
|
|
|
|
|
|
|
|
unquote(view_helpers())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def component do
|
|
|
|
quote do
|
|
|
|
use Phoenix.Component
|
|
|
|
|
|
|
|
unquote(view_helpers())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def router do
|
|
|
|
quote do
|
2023-08-15 11:22:18 +01:00
|
|
|
use Phoenix.Router, helpers: false
|
2023-01-02 03:29:02 +00:00
|
|
|
|
|
|
|
import Plug.Conn
|
|
|
|
import Phoenix.Controller
|
|
|
|
import Phoenix.LiveView.Router
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def channel do
|
|
|
|
quote do
|
|
|
|
use Phoenix.Channel
|
|
|
|
import Pleroma.Web.Gettext
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp view_helpers do
|
|
|
|
quote do
|
|
|
|
# Use all HTML functionality (forms, tags, etc)
|
|
|
|
use Phoenix.HTML
|
|
|
|
|
|
|
|
# Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
|
|
|
|
import Phoenix.LiveView.Helpers
|
|
|
|
|
|
|
|
# Import basic rendering functionality (render, render_layout, etc)
|
|
|
|
import Phoenix.View
|
|
|
|
|
|
|
|
import Pleroma.Web.ErrorHelpers
|
|
|
|
import Pleroma.Web.Gettext
|
2023-08-15 11:22:18 +01:00
|
|
|
unquote(verified_routes())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def static_paths, do: Pleroma.Constants.static_only_files()
|
|
|
|
|
|
|
|
def verified_routes do
|
|
|
|
quote do
|
|
|
|
use Phoenix.VerifiedRoutes,
|
|
|
|
endpoint: Pleroma.Web.Endpoint,
|
|
|
|
router: Pleroma.Web.Router,
|
|
|
|
statics: Pleroma.Web.static_paths()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mailer do
|
|
|
|
quote do
|
|
|
|
unquote(verified_routes())
|
2023-01-02 03:29:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-17 16:09:58 +00:00
|
|
|
@doc """
|
|
|
|
When used, dispatch to the appropriate controller/view/etc.
|
|
|
|
"""
|
|
|
|
defmacro __using__(which) when is_atom(which) do
|
|
|
|
apply(__MODULE__, which, [])
|
|
|
|
end
|
|
|
|
end
|