Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

94 líneas
3.7KB

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