1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.EnsurePublicOrAuthenticatedPlug
13 alias Pleroma.Web.Plugs.OAuthScopesPlug
15 # Serves only with proper OAuth token (:api and :authenticated_api)
16 # Skipping EnsurePublicOrAuthenticatedPlug has no effect in this case
18 # Suggested use case: all :authenticated_api endpoints (makes no sense for :api endpoints)
19 plug(OAuthScopesPlug, %{scopes: ["read"]} when action == :do_oauth_check)
21 # Via :api, keeps :user if token has requested scopes (if :user is dropped, serves if public)
22 # Via :authenticated_api, serves if token is present and has requested scopes
24 # Suggested use case: vast majority of :api endpoints (no sense for :authenticated_api ones)
27 %{scopes: ["read"], fallback: :proceed_unauthenticated}
28 when action == :fallback_oauth_check
31 # Keeps :user if present, executes regardless of token / token scopes
32 # Fails with no :user for :authenticated_api / no user for :api on private instance
33 # Note: EnsurePublicOrAuthenticatedPlug is not skipped (private instance fails on no :user)
34 # Note: Basic Auth processing results in :skip_plug call for OAuthScopesPlug
36 # Suggested use: suppressing OAuth checks for other auth mechanisms (like Basic Auth)
37 # For controller-level use, see :skip_oauth_skip_publicity_check instead
40 OAuthScopesPlug when action == :skip_oauth_check
43 # (Shouldn't be executed since the plug is skipped)
44 plug(OAuthScopesPlug, %{scopes: ["admin"]} when action == :skip_oauth_check)
46 # Via :api, keeps :user if token has requested scopes, and continues with nil :user otherwise
47 # Via :authenticated_api, serves if token is present and has requested scopes
49 # Suggested use: as :fallback_oauth_check but open with nil :user for :api on private instances
52 EnsurePublicOrAuthenticatedPlug when action == :fallback_oauth_skip_publicity_check
57 %{scopes: ["read"], fallback: :proceed_unauthenticated}
58 when action == :fallback_oauth_skip_publicity_check
61 # Via :api, keeps :user if present, serves regardless of token presence / scopes / :user presence
62 # Via :authenticated_api, serves if :user is set (regardless of token presence and its scopes)
64 # Suggested use: making an :api endpoint always accessible (e.g. email confirmation endpoint)
67 [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug]
68 when action == :skip_oauth_skip_publicity_check
71 # Via :authenticated_api, always fails with 403 (endpoint is insecure)
72 # Via :api, drops :user if present and serves if public (private instance rejects on no user)
74 # Suggested use: none; please define OAuth rules for all :api / :authenticated_api endpoints
75 plug(:skip_plug, [] when action == :missing_oauth_check_definition)
77 def do_oauth_check(conn, _params), do: conn_state(conn)
79 def fallback_oauth_check(conn, _params), do: conn_state(conn)
81 def skip_oauth_check(conn, _params), do: conn_state(conn)
83 def fallback_oauth_skip_publicity_check(conn, _params), do: conn_state(conn)
85 def skip_oauth_skip_publicity_check(conn, _params), do: conn_state(conn)
87 def missing_oauth_check_definition(conn, _params), do: conn_state(conn)
89 defp conn_state(%{assigns: %{user: %User{} = user}} = conn),
90 do: json(conn, %{user_id: user.id})
92 defp conn_state(conn), do: json(conn, %{user_id: nil})