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.
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/CHANGELOG.md +14 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/PKG-INFO +1 -1
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/pyproject.toml +1 -1
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/__init__.py +1 -1
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/guards/session.py +2 -2
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/middleware/csrf.py +16 -3
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/state.py +3 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_spa_csrf.py +52 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/LICENSE.md +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/README.md +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/facade.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/manager.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/models.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/orm.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/apitokens/repository.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/credentials.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/models.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/orm.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/repository.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/commands/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/commands/oauth2_client.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/concurrency.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/config/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/config/auth.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/dependencies.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/exceptions.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/facade.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/authorization_code.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/client_credentials.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/password.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/pkce.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/refresh.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/guards/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/guards/chain.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/guards/guard.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/guards/token.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/manager.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/middleware/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/middleware/context.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/middleware/session.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/orm.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/passwords/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/passwords/broker.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/passwords/repository.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/policy.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/provider.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/providers/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/providers/base.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/providers/fields.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/providers/memory.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/providers/model.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/publishable/cors.py +0 -0
- {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
- {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
- {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
- {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
- {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
- {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
- {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
- {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
- {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
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/py.typed +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/request_context.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/routes.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/routes_password.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/routes_spa.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/scoped_method.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/security/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/security/hashing.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/security/jwt.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/facade.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/models.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/orm.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/sessions/store.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/__init__.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/models.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/orm.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/repository.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/service.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/conftest.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_api_tokens.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_async_auth.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_async_refresh_race.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_async_safety.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_async_stores.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_class_config.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_client_scopes.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_clients.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_facade_api.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_grants.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_guard_dependency.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_guard_registry.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_hashing.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_integration.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_jwt.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_manager.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_model_provider.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_model_provider_postgres.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_multi_provider.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_oauth21.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_oauth2_client_command.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_oauth_config_aliases.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_oauth_hardening.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_password_reset.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_reset_throttle_enumeration.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_resource_binding.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_security_fixes.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_session_auth.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_session_store.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_startkit_provider.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_store_names.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_token_repository.py +0 -0
- {fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_token_service.py +0 -0
- {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.
|
|
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.
|
|
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"
|
|
@@ -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.
|
|
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
|
-
|
|
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")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/clients/orm.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/concurrency.py
RENAMED
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/config/auth.py
RENAMED
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/exceptions.py
RENAMED
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/facade.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/grants/pkce.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/manager.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/orm.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/policy.py
RENAMED
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/provider.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/py.typed
RENAMED
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/routes.py
RENAMED
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/routes_spa.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/src/fastapi_startkit_auth/tokens/orm.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_async_refresh_race.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_model_provider_postgres.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_oauth2_client_command.py
RENAMED
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_oauth_config_aliases.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_startkit_auth-0.8.0 → fastapi_startkit_auth-0.8.1}/tests/test_reset_throttle_enumeration.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|