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 # A test controller reachable only in :test env.
6 defmodule Pleroma.Tests.AuthTestController do
9 use Pleroma.Web, :controller
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
14 # Serves only with proper OAuth token (:api and :authenticated_api)
15 # Skipping EnsurePublicOrAuthenticatedPlug has no effect in this case
17 # Suggested use case: all :authenticated_api endpoints (makes no sense for :api endpoints)
18 plug(OAuthScopesPlug, %{scopes: ["read"]} when action == :do_oauth_check)
20 # Via :api, keeps :user if token has requested scopes (if :user is dropped, serves if public)
21 # Via :authenticated_api, serves if token is present and has requested scopes
23 # Suggested use case: vast majority of :api endpoints (no sense for :authenticated_api ones)
26 %{scopes: ["read"], fallback: :proceed_unauthenticated}
27 when action == :fallback_oauth_check
30 # Keeps :user if present, executes regardless of token / token scopes
31 # Fails with no :user for :authenticated_api / no user for :api on private instance
32 # Note: EnsurePublicOrAuthenticatedPlug is not skipped (private instance fails on no :user)
33 # Note: Basic Auth processing results in :skip_plug call for OAuthScopesPlug
35 # Suggested use: suppressing OAuth checks for other auth mechanisms (like Basic Auth)
36 # For controller-level use, see :skip_oauth_skip_publicity_check instead
39 OAuthScopesPlug when action == :skip_oauth_check
42 # (Shouldn't be executed since the plug is skipped)
43 plug(OAuthScopesPlug, %{scopes: ["admin"]} when action == :skip_oauth_check)
45 # Via :api, keeps :user if token has requested scopes, and continues with nil :user otherwise
46 # Via :authenticated_api, serves if token is present and has requested scopes
48 # Suggested use: as :fallback_oauth_check but open with nil :user for :api on private instances
49 plug(:skip_public_check when action == :fallback_oauth_skip_publicity_check)
53 %{scopes: ["read"], fallback: :proceed_unauthenticated}
54 when action == :fallback_oauth_skip_publicity_check
57 # Via :api, keeps :user if present, serves regardless of token presence / scopes / :user presence
58 # Via :authenticated_api, serves if :user is set (regardless of token presence and its scopes)
60 # Suggested use: making an :api endpoint always accessible (e.g. email confirmation endpoint)
61 plug(:skip_auth when action == :skip_oauth_skip_publicity_check)
63 # Via :authenticated_api, always fails with 403 (endpoint is insecure)
64 # Via :api, drops :user if present and serves if public (private instance rejects on no user)
66 # Suggested use: none; please define OAuth rules for all :api / :authenticated_api endpoints
67 plug(:skip_plug, [] when action == :missing_oauth_check_definition)
69 def do_oauth_check(conn, _params), do: conn_state(conn)
71 def fallback_oauth_check(conn, _params), do: conn_state(conn)
73 def skip_oauth_check(conn, _params), do: conn_state(conn)
75 def fallback_oauth_skip_publicity_check(conn, _params), do: conn_state(conn)
77 def skip_oauth_skip_publicity_check(conn, _params), do: conn_state(conn)
79 def missing_oauth_check_definition(conn, _params), do: conn_state(conn)
81 defp conn_state(%{assigns: %{user: %User{} = user}} = conn),
82 do: json(conn, %{user_id: user.id})
84 defp conn_state(conn), do: json(conn, %{user_id: nil})