fastapi-multiauth 0.2.0__tar.gz → 0.3.0__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 (23) hide show
  1. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/PKG-INFO +1 -1
  2. fastapi_multiauth-0.3.0/pyproject.toml +106 -0
  3. fastapi_multiauth-0.2.0/pyproject.toml → fastapi_multiauth-0.3.0/pyproject.toml.orig +2 -2
  4. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/__init__.py +1 -1
  5. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/_http.py +8 -2
  6. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/jwt.py +1 -1
  7. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/oauth/client.py +22 -4
  8. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/LICENSE +0 -0
  9. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/README.md +0 -0
  10. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/_imports.py +0 -0
  11. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/abc.py +0 -0
  12. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/exceptions.py +0 -0
  13. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/oauth/__init__.py +0 -0
  14. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/oauth/utils.py +0 -0
  15. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/py.typed +0 -0
  16. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/sources/__init__.py +0 -0
  17. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/sources/basic.py +0 -0
  18. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/sources/bearer.py +0 -0
  19. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/sources/cookie.py +0 -0
  20. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/sources/header.py +0 -0
  21. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/sources/multi.py +0 -0
  22. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/sources/query.py +0 -0
  23. {fastapi_multiauth-0.2.0 → fastapi_multiauth-0.3.0}/src/fastapi_multiauth/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastapi-multiauth
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Composable authentication sources for FastAPI: bring your own validator, combine multiple schemes, correct OpenAPI by construction
5
5
  Keywords: fastapi,authentication,security,oauth,api-key,bearer,cookie
6
6
  Author: d3vyce
@@ -0,0 +1,106 @@
1
+ [project]
2
+ name = "fastapi-multiauth"
3
+ version = "0.3.0"
4
+ description = "Composable authentication sources for FastAPI: bring your own validator, combine multiple schemes, correct OpenAPI by construction"
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ requires-python = ">=3.10"
9
+ keywords = [
10
+ "fastapi",
11
+ "authentication",
12
+ "security",
13
+ "oauth",
14
+ "api-key",
15
+ "bearer",
16
+ "cookie",
17
+ ]
18
+ classifiers = [
19
+ "Development Status :: 4 - Beta",
20
+ "Framework :: AsyncIO",
21
+ "Framework :: FastAPI",
22
+ "Intended Audience :: Developers",
23
+ "Operating System :: OS Independent",
24
+ "Programming Language :: Python :: 3 :: Only",
25
+ "Programming Language :: Python :: 3.10",
26
+ "Programming Language :: Python :: 3.11",
27
+ "Programming Language :: Python :: 3.12",
28
+ "Programming Language :: Python :: 3.13",
29
+ "Programming Language :: Python :: 3.14",
30
+ "Topic :: Internet :: WWW/HTTP :: Session",
31
+ "Topic :: Security",
32
+ "Topic :: Software Development :: Libraries :: Python Modules",
33
+ "Topic :: Software Development :: Libraries",
34
+ "Typing :: Typed",
35
+ ]
36
+ dependencies = [
37
+ "anyio>=3.7",
38
+ "async-lru>=2.0",
39
+ "fastapi>=0.100.0",
40
+ "httpx>=0.25.0",
41
+ "itsdangerous>=2.1",
42
+ ]
43
+
44
+ [[project.authors]]
45
+ name = "d3vyce"
46
+ email = "contact@d3vyce.fr"
47
+
48
+ [project.urls]
49
+ Homepage = "https://github.com/d3vyce/fastapi-multiauth"
50
+ Documentation = "https://fastapi-multiauth.d3vyce.fr/"
51
+ Repository = "https://github.com/d3vyce/fastapi-multiauth"
52
+ Issues = "https://github.com/d3vyce/fastapi-multiauth/issues"
53
+
54
+ [project.optional-dependencies]
55
+ jwt = ["pyjwt[crypto]>=2.13.0"]
56
+ oauth = ["httpx-oauth>=0.16"]
57
+
58
+ [dependency-groups]
59
+ dev = [
60
+ { include-group = "tests" },
61
+ { include-group = "docs" },
62
+ "fastapi_multiauth[jwt,oauth]",
63
+ "prek>=0.3.8",
64
+ "ruff>=0.1.0",
65
+ "ty>=0.0.34",
66
+ ]
67
+ tests = [
68
+ "coverage>=7.0.0",
69
+ "pytest-anyio>=0.0.0",
70
+ "pytest-cov>=4.0.0",
71
+ "pytest>=8.0.0",
72
+ "respx>=0.22",
73
+ ]
74
+ docs = [
75
+ "mkdocstrings-python>=2.0.2",
76
+ "zensical>=0.0.30",
77
+ ]
78
+
79
+ [build-system]
80
+ requires = ["uv_build>=0.11.21,<0.13.0"]
81
+ build-backend = "uv_build"
82
+
83
+ [tool.ruff.lint]
84
+ extend-select = ["S"]
85
+
86
+ [tool.ruff.lint.per-file-ignores]
87
+ "tests/**" = [
88
+ "S",
89
+ "B008",
90
+ ]
91
+
92
+ [tool.pytest.ini_options]
93
+ testpaths = ["tests"]
94
+ anyio_mode = "auto"
95
+ filterwarnings = ["ignore::DeprecationWarning"]
96
+
97
+ [tool.coverage.run]
98
+ source = ["src/fastapi_multiauth"]
99
+ branch = true
100
+
101
+ [tool.coverage.report]
102
+ exclude_lines = [
103
+ "pragma: no cover",
104
+ "if TYPE_CHECKING:",
105
+ "raise NotImplementedError",
106
+ ]
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "fastapi-multiauth"
3
- version = "0.2.0"
3
+ version = "0.3.0"
4
4
  description = "Composable authentication sources for FastAPI: bring your own validator, combine multiple schemes, correct OpenAPI by construction"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -72,7 +72,7 @@ docs = [
72
72
  ]
73
73
 
74
74
  [build-system]
75
- requires = ["uv_build>=0.11.21,<0.12.0"]
75
+ requires = ["uv_build>=0.11.21,<0.13.0"]
76
76
  build-backend = "uv_build"
77
77
 
78
78
  [tool.ruff.lint]
@@ -23,7 +23,7 @@ from .sources import (
23
23
  )
24
24
  from .utils import hash_token, verify_token_hash
25
25
 
26
- __version__ = "0.2.0"
26
+ __version__ = "0.3.0"
27
27
 
28
28
  __all__ = [
29
29
  "APIKeyCookieAuth",
@@ -1,5 +1,6 @@
1
1
  """Shared HTTP transport hygiene for the OAuth and JWT modules."""
2
2
 
3
+ import contextlib
3
4
  import json
4
5
  from typing import Any
5
6
  from urllib.parse import urlsplit
@@ -31,11 +32,16 @@ async def _get_json(
31
32
  error_cls: type[Exception],
32
33
  description: str,
33
34
  headers: dict[str, str] | None = None,
35
+ client: httpx.AsyncClient | None = None,
34
36
  ) -> Any:
35
37
  """GET *url* and decode the JSON body, mapping failures to *error_cls*."""
36
- async with httpx.AsyncClient(timeout=timeout) as client:
38
+ async with (
39
+ contextlib.nullcontext(client)
40
+ if client is not None
41
+ else httpx.AsyncClient(timeout=timeout)
42
+ ) as http:
37
43
  try:
38
- resp = await client.get(url, headers=headers)
44
+ resp = await http.get(url, headers=headers)
39
45
  resp.raise_for_status()
40
46
  return resp.json()
41
47
  except httpx.HTTPError as exc:
@@ -149,7 +149,7 @@ class JWTValidator:
149
149
  leeway=self._leeway,
150
150
  options=self._decode_options,
151
151
  )
152
- except self._jwt.InvalidTokenError:
152
+ except (self._jwt.exceptions.PyJWTError, TypeError, ValueError):
153
153
  raise UnauthorizedError() from None
154
154
  if scopes:
155
155
  missing = set(scopes) - self._granted_scopes(claims)
@@ -1,5 +1,6 @@
1
1
  """OAuth 2.0 / OIDC network operations: discovery, code exchange, userinfo."""
2
2
 
3
+ import contextlib
3
4
  import functools
4
5
  from typing import Any, Literal, NamedTuple
5
6
 
@@ -74,6 +75,7 @@ def _oauth2_client_class() -> type:
74
75
  access_token_endpoint: str,
75
76
  timeout: float,
76
77
  token_endpoint_auth_method: TokenEndpointAuthMethod,
78
+ client: httpx.AsyncClient | None,
77
79
  ) -> None:
78
80
  super().__init__(
79
81
  client_id,
@@ -85,8 +87,13 @@ def _oauth2_client_class() -> type:
85
87
  token_endpoint_auth_method=token_endpoint_auth_method,
86
88
  )
87
89
  self._timeout = timeout
90
+ self._client = client
88
91
 
89
- def get_httpx_client(self) -> httpx.AsyncClient:
92
+ def get_httpx_client(self) -> Any:
93
+ # httpx-oauth only uses the return value as an async context
94
+ # manager, so a borrowed client is wrapped to survive the exit.
95
+ if self._client is not None:
96
+ return contextlib.nullcontext(self._client)
90
97
  return httpx.AsyncClient(timeout=self._timeout)
91
98
 
92
99
  return _TimeoutOAuth2
@@ -159,6 +166,7 @@ async def oauth_exchange_code(
159
166
  code_verifier: str | None = None,
160
167
  token_endpoint_auth_method: TokenEndpointAuthMethod = "client_secret_post", # noqa: S107 — method name, not a secret
161
168
  timeout: float = DEFAULT_TIMEOUT,
169
+ client: httpx.AsyncClient | None = None,
162
170
  ) -> dict[str, Any]:
163
171
  """Exchange an authorization code for tokens and return the token response.
164
172
 
@@ -176,7 +184,11 @@ async def oauth_exchange_code(
176
184
  (from :func:`~fastapi_multiauth.oauth.oauth_generate_pkce_pair`).
177
185
  token_endpoint_auth_method: How credentials are sent — in the POST body
178
186
  (``client_secret_post``, default) or as a Basic header.
179
- timeout: Timeout in seconds for the token request.
187
+ timeout: Timeout in seconds for the token request. Ignored when
188
+ *client* is provided.
189
+ client: Optional ``httpx.AsyncClient`` to reuse (connection pooling
190
+ across the login flow). Borrowed, not closed; its own configuration
191
+ (timeouts included) governs the request.
180
192
 
181
193
  Returns:
182
194
  The full token response as a ``dict`` (``access_token`` plus whatever
@@ -190,7 +202,7 @@ async def oauth_exchange_code(
190
202
  _require_https(token_url, "OAuth token_url")
191
203
 
192
204
  oauth_client = _oauth2_client_class()(
193
- client_id, client_secret, token_url, timeout, token_endpoint_auth_method
205
+ client_id, client_secret, token_url, timeout, token_endpoint_auth_method, client
194
206
  )
195
207
  try:
196
208
  token_data = await oauth_client.get_access_token(
@@ -221,13 +233,18 @@ async def oauth_fetch_userinfo(
221
233
  userinfo_url: str,
222
234
  access_token: str,
223
235
  timeout: float = DEFAULT_TIMEOUT,
236
+ client: httpx.AsyncClient | None = None,
224
237
  ) -> dict[str, Any]:
225
238
  """Fetch the userinfo payload with a bearer access token.
226
239
 
227
240
  Args:
228
241
  userinfo_url: Provider's userinfo endpoint.
229
242
  access_token: Access token from :func:`oauth_exchange_code`.
230
- timeout: Timeout in seconds for the userinfo request.
243
+ timeout: Timeout in seconds for the userinfo request. Ignored when
244
+ *client* is provided.
245
+ client: Optional ``httpx.AsyncClient`` to reuse (connection pooling
246
+ across the login flow). Borrowed, not closed; its own configuration
247
+ (timeouts included) governs the request.
231
248
 
232
249
  Returns:
233
250
  The JSON payload returned by the userinfo endpoint as a plain ``dict``.
@@ -243,4 +260,5 @@ async def oauth_fetch_userinfo(
243
260
  error_cls=OAuthUserinfoError,
244
261
  description="userinfo",
245
262
  headers={"Authorization": f"Bearer {access_token}"},
263
+ client=client,
246
264
  )