fastapi-multiauth 0.1.0__tar.gz → 0.2.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.
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/PKG-INFO +12 -3
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/README.md +10 -1
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/pyproject.toml +4 -4
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/__init__.py +1 -1
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/_http.py +1 -1
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/abc.py +6 -4
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/jwt.py +2 -2
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/oauth/utils.py +10 -4
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/sources/basic.py +2 -1
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/sources/bearer.py +2 -1
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/sources/cookie.py +2 -2
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/sources/header.py +2 -1
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/sources/query.py +2 -1
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/utils.py +4 -2
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/LICENSE +0 -0
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/_imports.py +0 -0
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/exceptions.py +0 -0
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/oauth/__init__.py +0 -0
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/oauth/client.py +0 -0
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/py.typed +0 -0
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/sources/__init__.py +0 -0
- {fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/sources/multi.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastapi-multiauth
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.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
|
|
@@ -28,7 +28,7 @@ Requires-Dist: async-lru>=2.0
|
|
|
28
28
|
Requires-Dist: fastapi>=0.100.0
|
|
29
29
|
Requires-Dist: httpx>=0.25.0
|
|
30
30
|
Requires-Dist: itsdangerous>=2.1
|
|
31
|
-
Requires-Dist: pyjwt[crypto]>=2.
|
|
31
|
+
Requires-Dist: pyjwt[crypto]>=2.13.0 ; extra == 'jwt'
|
|
32
32
|
Requires-Dist: httpx-oauth>=0.16 ; extra == 'oauth'
|
|
33
33
|
Requires-Python: >=3.10
|
|
34
34
|
Project-URL: Homepage, https://github.com/d3vyce/fastapi-multiauth
|
|
@@ -79,7 +79,13 @@ uv add "fastapi-multiauth[jwt,oauth]"
|
|
|
79
79
|
|
|
80
80
|
```python
|
|
81
81
|
from fastapi import FastAPI, Security
|
|
82
|
-
from fastapi_multiauth import
|
|
82
|
+
from fastapi_multiauth import (
|
|
83
|
+
HTTPBearerAuth,
|
|
84
|
+
APIKeyCookieAuth,
|
|
85
|
+
MultiAuth,
|
|
86
|
+
UnauthorizedError,
|
|
87
|
+
)
|
|
88
|
+
|
|
83
89
|
|
|
84
90
|
async def validate_token(token: str) -> dict:
|
|
85
91
|
user = await lookup_user_by_token(token)
|
|
@@ -87,9 +93,11 @@ async def validate_token(token: str) -> dict:
|
|
|
87
93
|
raise UnauthorizedError()
|
|
88
94
|
return user
|
|
89
95
|
|
|
96
|
+
|
|
90
97
|
async def validate_session(value: str) -> dict:
|
|
91
98
|
return await lookup_user_by_session(value)
|
|
92
99
|
|
|
100
|
+
|
|
93
101
|
bearer = HTTPBearerAuth(validate_token, prefix="user_")
|
|
94
102
|
session = APIKeyCookieAuth("session", validate_session, secret_key="...")
|
|
95
103
|
|
|
@@ -98,6 +106,7 @@ auth = MultiAuth(bearer, session)
|
|
|
98
106
|
|
|
99
107
|
app = FastAPI()
|
|
100
108
|
|
|
109
|
+
|
|
101
110
|
@app.get("/me")
|
|
102
111
|
async def me(user=Security(auth)):
|
|
103
112
|
return user
|
|
@@ -38,7 +38,13 @@ uv add "fastapi-multiauth[jwt,oauth]"
|
|
|
38
38
|
|
|
39
39
|
```python
|
|
40
40
|
from fastapi import FastAPI, Security
|
|
41
|
-
from fastapi_multiauth import
|
|
41
|
+
from fastapi_multiauth import (
|
|
42
|
+
HTTPBearerAuth,
|
|
43
|
+
APIKeyCookieAuth,
|
|
44
|
+
MultiAuth,
|
|
45
|
+
UnauthorizedError,
|
|
46
|
+
)
|
|
47
|
+
|
|
42
48
|
|
|
43
49
|
async def validate_token(token: str) -> dict:
|
|
44
50
|
user = await lookup_user_by_token(token)
|
|
@@ -46,9 +52,11 @@ async def validate_token(token: str) -> dict:
|
|
|
46
52
|
raise UnauthorizedError()
|
|
47
53
|
return user
|
|
48
54
|
|
|
55
|
+
|
|
49
56
|
async def validate_session(value: str) -> dict:
|
|
50
57
|
return await lookup_user_by_session(value)
|
|
51
58
|
|
|
59
|
+
|
|
52
60
|
bearer = HTTPBearerAuth(validate_token, prefix="user_")
|
|
53
61
|
session = APIKeyCookieAuth("session", validate_session, secret_key="...")
|
|
54
62
|
|
|
@@ -57,6 +65,7 @@ auth = MultiAuth(bearer, session)
|
|
|
57
65
|
|
|
58
66
|
app = FastAPI()
|
|
59
67
|
|
|
68
|
+
|
|
60
69
|
@app.get("/me")
|
|
61
70
|
async def me(user=Security(auth)):
|
|
62
71
|
return user
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "fastapi-multiauth"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.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"
|
|
@@ -44,7 +44,7 @@ Issues = "https://github.com/d3vyce/fastapi-multiauth/issues"
|
|
|
44
44
|
|
|
45
45
|
[project.optional-dependencies]
|
|
46
46
|
jwt = [
|
|
47
|
-
"pyjwt[crypto]>=2.
|
|
47
|
+
"pyjwt[crypto]>=2.13.0",
|
|
48
48
|
]
|
|
49
49
|
oauth = [
|
|
50
50
|
"httpx-oauth>=0.16",
|
|
@@ -72,7 +72,7 @@ docs = [
|
|
|
72
72
|
]
|
|
73
73
|
|
|
74
74
|
[build-system]
|
|
75
|
-
requires = ["uv_build>=0.11.
|
|
75
|
+
requires = ["uv_build>=0.11.21,<0.12.0"]
|
|
76
76
|
build-backend = "uv_build"
|
|
77
77
|
|
|
78
78
|
[tool.ruff.lint]
|
|
@@ -80,7 +80,7 @@ build-backend = "uv_build"
|
|
|
80
80
|
extend-select = ["S"]
|
|
81
81
|
|
|
82
82
|
[tool.ruff.lint.per-file-ignores]
|
|
83
|
-
"tests/**" = ["S"]
|
|
83
|
+
"tests/**" = ["S", "B008"]
|
|
84
84
|
|
|
85
85
|
[tool.pytest.ini_options]
|
|
86
86
|
testpaths = ["tests"]
|
|
@@ -15,7 +15,7 @@ DEFAULT_TIMEOUT = 10.0
|
|
|
15
15
|
def _require_https(url: str, description: str) -> str:
|
|
16
16
|
"""Reject non-string URLs and URLs that would send credentials over plaintext HTTP."""
|
|
17
17
|
if not isinstance(url, str):
|
|
18
|
-
raise ValueError(f"{description} must be a string URL (got {url!r})")
|
|
18
|
+
raise ValueError(f"{description} must be a string URL (got {url!r})") # noqa: TRY004
|
|
19
19
|
parsed = urlsplit(url)
|
|
20
20
|
if parsed.scheme == "https":
|
|
21
21
|
return url
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
import copy
|
|
4
4
|
import inspect
|
|
5
5
|
from abc import ABC, abstractmethod
|
|
6
|
-
from
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
from typing import TYPE_CHECKING, Annotated, Any
|
|
7
8
|
|
|
8
9
|
from fastapi import Depends, HTTPException, Request
|
|
9
10
|
from fastapi.security import SecurityScopes
|
|
@@ -12,7 +13,8 @@ from fastapi.security.base import SecurityBase
|
|
|
12
13
|
from fastapi_multiauth.exceptions import UnauthorizedError
|
|
13
14
|
from fastapi_multiauth.utils import add_challenge, challenge_headers, ensure_async
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from typing_extensions import Self
|
|
16
18
|
|
|
17
19
|
|
|
18
20
|
def _reject_scopes_kwarg(kwargs: dict[str, Any]) -> None:
|
|
@@ -71,7 +73,7 @@ class _DocOnlyScheme(SecurityBase):
|
|
|
71
73
|
self.model = scheme.model
|
|
72
74
|
self.scheme_name = scheme.scheme_name
|
|
73
75
|
|
|
74
|
-
async def __call__(self, request: Request) -> None:
|
|
76
|
+
async def __call__(self, request: Request) -> None:
|
|
75
77
|
return None
|
|
76
78
|
|
|
77
79
|
|
|
@@ -212,7 +214,7 @@ class ValidatedAuthSource(AuthSource):
|
|
|
212
214
|
"""Validate a credential, forwarding route-declared scopes to the validator."""
|
|
213
215
|
return await self._call_validator(credential, scopes=scopes)
|
|
214
216
|
|
|
215
|
-
def require(self
|
|
217
|
+
def require(self, **kwargs: Any) -> "Self":
|
|
216
218
|
"""Return a copy of this source with additional (or overriding) validator kwargs."""
|
|
217
219
|
_reject_scopes_kwarg(kwargs)
|
|
218
220
|
clone = copy.copy(self)
|
|
@@ -98,9 +98,10 @@ def oauth_encode_state(url: str, state_token: str) -> str:
|
|
|
98
98
|
|
|
99
99
|
def _destination_allowed(destination: str, allowed_hosts: Sequence[str]) -> bool:
|
|
100
100
|
"""Check a decoded destination against the open-redirect allowlist."""
|
|
101
|
-
# Browsers normalize backslashes to slashes
|
|
102
|
-
# scheme-relative redirect
|
|
103
|
-
|
|
101
|
+
# Browsers normalize backslashes to slashes ("/\evil.com" becomes a
|
|
102
|
+
# scheme-relative redirect) and strip tabs/newlines before parsing
|
|
103
|
+
# ("/\t/evil.com" becomes "//evil.com") — reject control chars outright.
|
|
104
|
+
if "\\" in destination or any(ch <= " " for ch in destination):
|
|
104
105
|
return False
|
|
105
106
|
parsed = urlsplit(destination)
|
|
106
107
|
if not parsed.scheme and not parsed.netloc:
|
|
@@ -125,7 +126,8 @@ def oauth_decode_state(
|
|
|
125
126
|
Args:
|
|
126
127
|
state: Raw ``state`` query parameter from the callback.
|
|
127
128
|
expected_state_token: Token stored before the authorization redirect;
|
|
128
|
-
a mismatch returns
|
|
129
|
+
a mismatch — or an empty token (e.g. a session miss) — returns
|
|
130
|
+
``fallback``.
|
|
129
131
|
fallback: URL returned when ``state`` is absent, malformed, or fails
|
|
130
132
|
verification.
|
|
131
133
|
allowed_hosts: Open-redirect guard — the decoded destination must be a
|
|
@@ -135,6 +137,10 @@ def oauth_decode_state(
|
|
|
135
137
|
Returns:
|
|
136
138
|
The destination URL embedded in ``state``, or ``fallback``.
|
|
137
139
|
"""
|
|
140
|
+
# An empty expected token (e.g. session.get(..., "") on a session miss)
|
|
141
|
+
# must never match — compare_digest("", "") is True.
|
|
142
|
+
if not expected_state_token:
|
|
143
|
+
return fallback
|
|
138
144
|
if not state or state == "null": # "null" guards against JS JSON.stringify(null)
|
|
139
145
|
return fallback
|
|
140
146
|
try:
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""Cookie-based authentication source."""
|
|
2
2
|
|
|
3
3
|
import hashlib
|
|
4
|
-
from collections.abc import Sequence
|
|
5
|
-
from typing import Any,
|
|
4
|
+
from collections.abc import Callable, Sequence
|
|
5
|
+
from typing import Any, Literal
|
|
6
6
|
|
|
7
7
|
from fastapi import Request, Response
|
|
8
8
|
from fastapi.security import APIKeyCookie
|
|
@@ -4,8 +4,10 @@ import functools
|
|
|
4
4
|
import hashlib
|
|
5
5
|
import hmac
|
|
6
6
|
import inspect
|
|
7
|
-
from
|
|
7
|
+
from collections.abc import Callable
|
|
8
|
+
from typing import Any
|
|
8
9
|
|
|
10
|
+
import anyio.to_thread
|
|
9
11
|
from fastapi import HTTPException, Request
|
|
10
12
|
|
|
11
13
|
|
|
@@ -43,7 +45,7 @@ def ensure_async(fn: Callable[..., Any]) -> Callable[..., Any]:
|
|
|
43
45
|
|
|
44
46
|
@functools.wraps(fn)
|
|
45
47
|
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
46
|
-
result = fn
|
|
48
|
+
result = await anyio.to_thread.run_sync(functools.partial(fn, *args, **kwargs))
|
|
47
49
|
if inspect.isawaitable(result):
|
|
48
50
|
return await result
|
|
49
51
|
return result
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastapi_multiauth-0.1.0 → fastapi_multiauth-0.2.0}/src/fastapi_multiauth/sources/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|