fastapi-startkit-auth 0.8.0__tar.gz → 0.8.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/CHANGELOG.md +14 -0
  2. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/PKG-INFO +1 -1
  3. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/pyproject.toml +1 -1
  4. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/__init__.py +1 -1
  5. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/guards/session.py +2 -2
  6. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/middleware/csrf.py +16 -3
  7. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/state.py +3 -0
  8. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_spa_csrf.py +52 -0
  9. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/LICENSE.md +0 -0
  10. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/README.md +0 -0
  11. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/__init__.py +0 -0
  12. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/facade.py +0 -0
  13. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/manager.py +0 -0
  14. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/models.py +0 -0
  15. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/orm.py +0 -0
  16. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/repository.py +0 -0
  17. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/__init__.py +0 -0
  18. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/credentials.py +0 -0
  19. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/models.py +0 -0
  20. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/orm.py +0 -0
  21. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/repository.py +0 -0
  22. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/commands/__init__.py +0 -0
  23. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/commands/oauth2_client.py +0 -0
  24. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/concurrency.py +0 -0
  25. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/config/__init__.py +0 -0
  26. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/config/auth.py +0 -0
  27. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/dependencies.py +0 -0
  28. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/exceptions.py +0 -0
  29. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/facade.py +0 -0
  30. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/__init__.py +0 -0
  31. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/authorization_code.py +0 -0
  32. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/client_credentials.py +0 -0
  33. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/password.py +0 -0
  34. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/pkce.py +0 -0
  35. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/refresh.py +0 -0
  36. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/guards/__init__.py +0 -0
  37. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/guards/chain.py +0 -0
  38. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/guards/guard.py +0 -0
  39. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/guards/token.py +0 -0
  40. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/manager.py +0 -0
  41. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/middleware/__init__.py +0 -0
  42. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/middleware/context.py +0 -0
  43. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/middleware/session.py +0 -0
  44. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/orm.py +0 -0
  45. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/passwords/__init__.py +0 -0
  46. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/passwords/broker.py +0 -0
  47. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/passwords/repository.py +0 -0
  48. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/policy.py +0 -0
  49. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/provider.py +0 -0
  50. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/providers/__init__.py +0 -0
  51. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/providers/base.py +0 -0
  52. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/providers/fields.py +0 -0
  53. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/providers/memory.py +0 -0
  54. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/providers/model.py +0 -0
  55. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/cors.py +0 -0
  56. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/migrations/2026_09_30_000001_create_sessions_table.py +0 -0
  57. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/migrations/2026_09_30_000002_create_personal_api_tokens_table.py +0 -0
  58. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/migrations/2026_09_30_000003_create_oauth_access_tokens_table.py +0 -0
  59. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/migrations/2026_09_30_000004_create_oauth_refresh_tokens_table.py +0 -0
  60. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/migrations/2026_09_30_000005_create_oauth_auth_codes_table.py +0 -0
  61. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/migrations/2026_10_02_000001_add_resource_to_oauth_tables.py +0 -0
  62. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/migrations/2026_10_04_000001_create_oauth_clients_table.py +0 -0
  63. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/migrations/2026_10_04_000002_add_family_id_to_oauth_refresh_tokens_table.py +0 -0
  64. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/migrations/2026_10_05_000001_add_scopes_to_oauth_clients_table.py +0 -0
  65. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/py.typed +0 -0
  66. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/request_context.py +0 -0
  67. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/routes.py +0 -0
  68. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/routes_password.py +0 -0
  69. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/routes_spa.py +0 -0
  70. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/scoped_method.py +0 -0
  71. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/security/__init__.py +0 -0
  72. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/security/hashing.py +0 -0
  73. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/security/jwt.py +0 -0
  74. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/__init__.py +0 -0
  75. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/facade.py +0 -0
  76. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/models.py +0 -0
  77. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/orm.py +0 -0
  78. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/store.py +0 -0
  79. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/__init__.py +0 -0
  80. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/models.py +0 -0
  81. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/orm.py +0 -0
  82. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/repository.py +0 -0
  83. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/service.py +0 -0
  84. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/conftest.py +0 -0
  85. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_api_tokens.py +0 -0
  86. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_async_auth.py +0 -0
  87. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_async_refresh_race.py +0 -0
  88. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_async_safety.py +0 -0
  89. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_async_stores.py +0 -0
  90. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_class_config.py +0 -0
  91. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_client_scopes.py +0 -0
  92. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_clients.py +0 -0
  93. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_facade_api.py +0 -0
  94. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_grants.py +0 -0
  95. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_guard_dependency.py +0 -0
  96. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_guard_registry.py +0 -0
  97. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_hashing.py +0 -0
  98. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_integration.py +0 -0
  99. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_jwt.py +0 -0
  100. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_manager.py +0 -0
  101. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_model_provider.py +0 -0
  102. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_model_provider_postgres.py +0 -0
  103. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_multi_provider.py +0 -0
  104. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_oauth21.py +0 -0
  105. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_oauth2_client_command.py +0 -0
  106. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_oauth_config_aliases.py +0 -0
  107. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_oauth_hardening.py +0 -0
  108. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_password_reset.py +0 -0
  109. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_reset_throttle_enumeration.py +0 -0
  110. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_resource_binding.py +0 -0
  111. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_security_fixes.py +0 -0
  112. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_session_auth.py +0 -0
  113. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_session_store.py +0 -0
  114. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_startkit_provider.py +0 -0
  115. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_store_names.py +0 -0
  116. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_token_repository.py +0 -0
  117. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_token_service.py +0 -0
  118. {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_user_provider.py +0 -0
@@ -233,6 +233,20 @@ using the ORM stores:
233
233
 
234
234
  ### Fixed
235
235
 
236
+ - `CsrfMiddleware` no longer rejects native token clients that replay a
237
+ session cookie. An unsafe request with an `Authorization: Bearer` token and
238
+ no `Origin` header skips the session CSRF check, so a mobile app whose HTTP
239
+ stack keeps the session cookie no longer gets `csrf_token_mismatch`. Such a
240
+ request is never authenticated by the session: `SessionGuard` refuses it,
241
+ including inside a stateful `token` guard or a chain, so only its bearer can
242
+ authenticate it, and a garbage bearer gets 401. A browser cannot send
243
+ `Authorization` without `fetch`, which always sends `Origin`. Browser
244
+ requests, cookie-only requests and requests with an empty or non-bearer
245
+ `Authorization` header still need the CSRF token and keep session auth.
246
+ The token-client mark is set for every method, not only unsafe ones, and
247
+ browsers often omit `Origin` on same-origin `GET`s. So an SPA that sends a
248
+ stale bearer on such a `GET` loses the session fallback and gets 401;
249
+ drop the bearer to keep using the session.
236
250
  - Refresh, authorization-code exchange and introspection re-check the token
237
251
  owner against the provider of the issuing client instead of always using the
238
252
  default guard's provider. The password grant authenticates against the
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastapi-startkit-auth
3
- Version: 0.8.0
3
+ Version: 0.8.1
4
4
  Summary: Laravel-style authentication for FastAPI: OAuth2/JWT authorization server, cookie/session guard, SPA + CSRF protection, and Sanctum-style API tokens.
5
5
  Keywords: fastapi,oauth2,jwt,auth,passport,sanctum,authentication,authorization,security,tokens,session,cookie,csrf,spa,starter-kit
6
6
  Author: Bedram Tamang
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "fastapi-startkit-auth"
3
- version = "0.8.0"
3
+ version = "0.8.1"
4
4
  description = "Laravel-style authentication for FastAPI: OAuth2/JWT authorization server, cookie/session guard, SPA + CSRF protection, and Sanctum-style API tokens."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -79,7 +79,7 @@ from .sessions.models import SessionRecord
79
79
  from .sessions.store import InMemorySessionStore, SessionStore
80
80
  from .tokens.service import TokenService
81
81
 
82
- __version__ = "0.8.0"
82
+ __version__ = "0.8.1"
83
83
 
84
84
  __all__ = (
85
85
  "AuthConfig",
@@ -8,7 +8,7 @@ from ..exceptions import AuthenticationException
8
8
  from ..concurrency import call
9
9
  from ..providers.base import user_is_active
10
10
  from ..sessions.models import SessionRecord
11
- from ..sessions.state import FORGET_KEY, PENDING_KEY, SESSION_KEY
11
+ from ..sessions.state import FORGET_KEY, PENDING_KEY, SESSION_KEY, TOKEN_CLIENT_KEY
12
12
  from ..sessions.store import new_session_record
13
13
  from .guard import AuthContext
14
14
 
@@ -28,7 +28,7 @@ class SessionGuard:
28
28
 
29
29
  async def authenticate(self, request: Request) -> AuthContext:
30
30
  record = getattr(request.state, SESSION_KEY, None)
31
- if record is None:
31
+ if record is None or getattr(request.state, TOKEN_CLIENT_KEY, False):
32
32
  raise AuthenticationException("Not authenticated.")
33
33
  return await self._context(record)
34
34
 
@@ -10,7 +10,7 @@ from starlette.responses import JSONResponse, Response
10
10
  from starlette.types import ASGIApp, Message, Receive, Scope, Send
11
11
 
12
12
  from ..exceptions import CsrfTokenMismatchException
13
- from ..sessions.state import FORGET_KEY, SESSION_KEY
13
+ from ..sessions.state import FORGET_KEY, SESSION_KEY, TOKEN_CLIENT_KEY
14
14
 
15
15
  UNSAFE_METHODS = frozenset({"POST", "PUT", "PATCH", "DELETE"})
16
16
  FORM_CONTENT_TYPES = ("application/x-www-form-urlencoded", "multipart/form-data")
@@ -27,7 +27,14 @@ class CsrfMiddleware:
27
27
  ``X-XSRF-TOKEN``), or for HTML form posts the ``field`` (default ``_token``), matching the session's ``csrf_token``; the comparison is
28
28
  constant-time and failure is a 403 ``csrf_token_mismatch``. Requests
29
29
  without a session (bearer/token clients — no ambient credential) and safe
30
- methods are exempt, as are configured exempt paths. When
30
+ methods are exempt, as are configured exempt paths. So are native token
31
+ clients: a request with an ``Authorization: Bearer`` token and no
32
+ ``Origin`` header, such as a mobile app whose HTTP stack replays the
33
+ session cookie. Such a request is marked as a token client and is never
34
+ authenticated by the session: session guards refuse it, so only its
35
+ bearer can authenticate it. A page can only attach an ``Authorization``
36
+ header through ``fetch``, which always sends ``Origin``, so a browser
37
+ request still needs the CSRF token, whatever its bearer. When
31
38
  ``stateful_origins`` is set, a foreign ``Origin`` header is rejected before
32
39
  the token is even checked (defense in depth).
33
40
 
@@ -72,7 +79,8 @@ class CsrfMiddleware:
72
79
 
73
80
  state = scope.setdefault("state", {})
74
81
  record = state.get(SESSION_KEY)
75
- if scope["method"] in UNSAFE_METHODS and record is not None:
82
+ state[TOKEN_CLIENT_KEY] = self._is_token_client(scope)
83
+ if scope["method"] in UNSAFE_METHODS and record is not None and not state[TOKEN_CLIENT_KEY]:
76
84
  receive, rejection = await self._reject(scope, receive, record)
77
85
  if rejection is not None:
78
86
  await rejection(scope, receive, send)
@@ -104,6 +112,11 @@ class CsrfMiddleware:
104
112
  return receive, self._forbidden("CSRF token missing or invalid.")
105
113
  return receive, None
106
114
 
115
+ def _is_token_client(self, scope: Scope) -> bool:
116
+ scheme, _, token = (self._header_value(scope, b"authorization") or "").partition(" ")
117
+ is_bearer = scheme.lower() == "bearer" and bool(token.strip())
118
+ return is_bearer and self._header_value(scope, b"origin") is None
119
+
107
120
  def _is_form(self, scope: Scope) -> bool:
108
121
  content_type = self._header_value(scope, b"content-type") or ""
109
122
  return content_type.startswith(FORM_CONTENT_TYPES)
@@ -6,9 +6,12 @@ the cookie. ``LOADED_ID_KEY`` holds the session id that arrived on the request
6
6
  (if any) so a changed id is detected; ``FORGET_KEY`` marks an explicit logout.
7
7
  ``PENDING_KEY`` holds a guest session that is only persisted once the response
8
8
  starts, so a request that never needs it costs no store write.
9
+ ``TOKEN_CLIENT_KEY`` marks a native token client that ``CsrfMiddleware``
10
+ exempted from the CSRF check, so session guards refuse to authenticate it.
9
11
  """
10
12
 
11
13
  SESSION_KEY = "auth_session"
12
14
  LOADED_ID_KEY = "auth_session_loaded_id"
13
15
  FORGET_KEY = "auth_session_forget"
14
16
  PENDING_KEY = "auth_session_pending"
17
+ TOKEN_CLIENT_KEY = "auth_token_client"
@@ -250,6 +250,58 @@ def test_exempt_paths_skip_the_token_check():
250
250
  assert client.post("/mutate").status_code == 403
251
251
 
252
252
 
253
+ def logged_in_with_token(client):
254
+ client.get("/__auth__/csrf-cookie")
255
+ client.post(
256
+ "/login",
257
+ json={"email": "ada@example.com", "password": "secret"},
258
+ headers=csrf_headers(client),
259
+ )
260
+ return client.post("/tokens", headers=csrf_headers(client)).json()["token"]
261
+
262
+
263
+ def test_bearer_requests_replaying_the_session_cookie_skip_the_token_check():
264
+ client = make_client(default_guard="api")
265
+ token = logged_in_with_token(client)
266
+ bearer = {"Authorization": f"Bearer {token}"}
267
+ assert client.post("/mutate", headers=bearer).status_code == 200
268
+ assert client.post("/tokens", headers=bearer).status_code == 200
269
+
270
+
271
+ def test_session_requests_without_a_bearer_still_need_the_token():
272
+ client = make_client(default_guard="api")
273
+ logged_in_with_token(client)
274
+ assert client.post("/mutate").status_code == 403
275
+ assert client.post("/mutate", headers={"Authorization": "Basic YWRhOnNlY3JldA=="}).status_code == 403
276
+ assert client.post("/mutate", headers={"Authorization": "Bearer "}).status_code == 403
277
+
278
+
279
+ def test_browser_requests_need_the_token_whatever_their_bearer():
280
+ client = make_client(default_guard="api")
281
+ token = logged_in_with_token(client)
282
+ for bearer in (token, "garbage"):
283
+ headers = {"Authorization": f"Bearer {bearer}", "Origin": SPA_ORIGIN}
284
+ assert client.post("/mutate", headers=headers).status_code == 403
285
+ assert client.post("/mutate", headers={**headers, **csrf_headers(client)}).status_code == 200
286
+
287
+
288
+ def test_a_garbage_bearer_from_a_native_client_never_uses_the_session():
289
+ client = make_client(default_guard="api")
290
+ logged_in_with_token(client)
291
+ garbage = {"Authorization": "Bearer garbage"}
292
+ assert client.post("/mutate", headers=garbage).status_code == 200
293
+ assert client.post("/tokens", headers=garbage).status_code == 401
294
+ assert client.get("/me", headers=garbage).status_code == 401
295
+
296
+
297
+ def test_a_bearer_from_a_native_client_never_uses_the_session_guard():
298
+ client = make_client(default_guard="web")
299
+ token = logged_in_with_token(client)
300
+ assert client.get("/me").json()["id"] == 1
301
+ assert client.get("/me", headers={"Authorization": f"Bearer {token}"}).status_code == 401
302
+ assert client.post("/tokens", headers={"Authorization": "Bearer garbage"}).status_code == 401
303
+
304
+
253
305
  def test_configurable_cookie_and_header_names():
254
306
  client = make_client(session={"csrf_cookie": "MY-CSRF", "csrf_header": "X-MY-CSRF"})
255
307
  client.get("/__auth__/csrf-cookie")