logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

auth_test_controller.ex (3603B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. # A test controller reachable only in :test env.
  5. defmodule Pleroma.Tests.AuthTestController do
  6. @moduledoc false
  7. use Pleroma.Web, :controller
  8. alias Pleroma.User
  9. alias Pleroma.Web.Plugs.OAuthScopesPlug
  10. # Serves only with proper OAuth token (:api and :authenticated_api)
  11. # Skipping EnsurePublicOrAuthenticatedPlug has no effect in this case
  12. #
  13. # Suggested use case: all :authenticated_api endpoints (makes no sense for :api endpoints)
  14. plug(OAuthScopesPlug, %{scopes: ["read"]} when action == :do_oauth_check)
  15. # Via :api, keeps :user if token has requested scopes (if :user is dropped, serves if public)
  16. # Via :authenticated_api, serves if token is present and has requested scopes
  17. #
  18. # Suggested use case: vast majority of :api endpoints (no sense for :authenticated_api ones)
  19. plug(
  20. OAuthScopesPlug,
  21. %{scopes: ["read"], fallback: :proceed_unauthenticated}
  22. when action == :fallback_oauth_check
  23. )
  24. # Keeps :user if present, executes regardless of token / token scopes
  25. # Fails with no :user for :authenticated_api / no user for :api on private instance
  26. # Note: EnsurePublicOrAuthenticatedPlug is not skipped (private instance fails on no :user)
  27. # Note: Basic Auth processing results in :skip_plug call for OAuthScopesPlug
  28. #
  29. # Suggested use: suppressing OAuth checks for other auth mechanisms (like Basic Auth)
  30. # For controller-level use, see :skip_oauth_skip_publicity_check instead
  31. plug(
  32. :skip_plug,
  33. OAuthScopesPlug when action == :skip_oauth_check
  34. )
  35. # (Shouldn't be executed since the plug is skipped)
  36. plug(OAuthScopesPlug, %{scopes: ["admin"]} when action == :skip_oauth_check)
  37. # Via :api, keeps :user if token has requested scopes, and continues with nil :user otherwise
  38. # Via :authenticated_api, serves if token is present and has requested scopes
  39. #
  40. # Suggested use: as :fallback_oauth_check but open with nil :user for :api on private instances
  41. plug(:skip_public_check when action == :fallback_oauth_skip_publicity_check)
  42. plug(
  43. OAuthScopesPlug,
  44. %{scopes: ["read"], fallback: :proceed_unauthenticated}
  45. when action == :fallback_oauth_skip_publicity_check
  46. )
  47. # Via :api, keeps :user if present, serves regardless of token presence / scopes / :user presence
  48. # Via :authenticated_api, serves if :user is set (regardless of token presence and its scopes)
  49. #
  50. # Suggested use: making an :api endpoint always accessible (e.g. email confirmation endpoint)
  51. plug(:skip_auth when action == :skip_oauth_skip_publicity_check)
  52. # Via :authenticated_api, always fails with 403 (endpoint is insecure)
  53. # Via :api, drops :user if present and serves if public (private instance rejects on no user)
  54. #
  55. # Suggested use: none; please define OAuth rules for all :api / :authenticated_api endpoints
  56. plug(:skip_plug, [] when action == :missing_oauth_check_definition)
  57. def do_oauth_check(conn, _params), do: conn_state(conn)
  58. def fallback_oauth_check(conn, _params), do: conn_state(conn)
  59. def skip_oauth_check(conn, _params), do: conn_state(conn)
  60. def fallback_oauth_skip_publicity_check(conn, _params), do: conn_state(conn)
  61. def skip_oauth_skip_publicity_check(conn, _params), do: conn_state(conn)
  62. def missing_oauth_check_definition(conn, _params), do: conn_state(conn)
  63. defp conn_state(%{assigns: %{user: %User{} = user}} = conn),
  64. do: json(conn, %{user_id: user.id})
  65. defp conn_state(conn), do: json(conn, %{user_id: nil})
  66. end