fastapi-betterauth 0.1.0__py3-none-any.whl
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.
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from ssl import SSLContext
|
|
2
|
+
from typing import Any, TypedDict, Unpack
|
|
3
|
+
|
|
4
|
+
import jwt
|
|
5
|
+
from jwt import PyJWKClient
|
|
6
|
+
|
|
7
|
+
CLIENT: PyJWKClient | None = None
|
|
8
|
+
BASE_URL: str | None = None
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class PyJWKClientKwargs(TypedDict, total=False):
|
|
12
|
+
cache_keys: bool
|
|
13
|
+
max_cached_keys: int
|
|
14
|
+
cache_jwk_set: bool
|
|
15
|
+
lifespan: int
|
|
16
|
+
headers: dict[str, Any] | None
|
|
17
|
+
timeout: int
|
|
18
|
+
ssl_context: SSLContext | None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def init_client(
|
|
22
|
+
base_url: str,
|
|
23
|
+
auth_path: str = "/api/auth/jwks",
|
|
24
|
+
**kwargs: Unpack[PyJWKClientKwargs],
|
|
25
|
+
):
|
|
26
|
+
full_url = f"{base_url}{auth_path}"
|
|
27
|
+
global BASE_URL, CLIENT
|
|
28
|
+
BASE_URL = base_url
|
|
29
|
+
base_url = base_url
|
|
30
|
+
CLIENT = PyJWKClient(
|
|
31
|
+
**kwargs,
|
|
32
|
+
uri=full_url,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _assert_client() -> PyJWKClient:
|
|
37
|
+
if CLIENT is None:
|
|
38
|
+
raise RuntimeError("Client is not initialized call init_client first")
|
|
39
|
+
return CLIENT
|
|
40
|
+
|
|
41
|
+
def validate_token(token: str):
|
|
42
|
+
client = _assert_client()
|
|
43
|
+
signing_key = client.get_signing_key_from_jwt(token)
|
|
44
|
+
return jwt.decode(
|
|
45
|
+
token,
|
|
46
|
+
signing_key.key,
|
|
47
|
+
algorithms=["EdDSA"],
|
|
48
|
+
issuer=BASE_URL,
|
|
49
|
+
audience=BASE_URL,
|
|
50
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-betterauth
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: FastAPI helpers for Better Auth JWT verification
|
|
5
|
+
Project-URL: Homepage, https://github.com/lukonik/fastapi-betterauth
|
|
6
|
+
Project-URL: Repository, https://github.com/lukonik/fastapi-betterauth
|
|
7
|
+
Project-URL: Issues, https://github.com/lukonik/fastapi-betterauth/issues
|
|
8
|
+
Author: Luka Onikadze
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: auth,better-auth,fastapi,jwks,jwt
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Framework :: FastAPI
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
21
|
+
Classifier: Topic :: Security
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Requires-Dist: pyjwt[crypto]>=2.12.1
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: fastapi>=0.100; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=7.4; extra == 'dev'
|
|
28
|
+
Provides-Extra: fastapi
|
|
29
|
+
Requires-Dist: fastapi>=0.100; extra == 'fastapi'
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# fastapi-betterauth
|
|
33
|
+
|
|
34
|
+
FastAPI helpers for Better Auth JWT verification.
|
|
35
|
+
|
|
36
|
+
The package verifies Better Auth JWTs with PyJWT and JWKS, then exposes the
|
|
37
|
+
verified claims either through a plain Python verifier or a FastAPI dependency.
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install fastapi-betterauth
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
For FastAPI dependency helpers:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install "fastapi-betterauth[fastapi]"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from fastapi_betterauth import BetterAuthVerifier
|
|
55
|
+
|
|
56
|
+
verifier = BetterAuthVerifier("https://your-app.example.com")
|
|
57
|
+
|
|
58
|
+
claims = verifier.validate_token(token)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
By default, the verifier:
|
|
62
|
+
|
|
63
|
+
- reads keys from `/api/auth/jwks`
|
|
64
|
+
- verifies the `EdDSA` algorithm
|
|
65
|
+
- expects `iss` to match `base_url`
|
|
66
|
+
- expects `aud` to match `base_url`
|
|
67
|
+
|
|
68
|
+
## FastAPI
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from typing import Annotated
|
|
72
|
+
|
|
73
|
+
from fastapi import Depends, FastAPI
|
|
74
|
+
from fastapi_betterauth import BetterAuthVerifier
|
|
75
|
+
|
|
76
|
+
app = FastAPI()
|
|
77
|
+
verifier = BetterAuthVerifier("https://your-app.example.com")
|
|
78
|
+
current_user = verifier.fastapi_dependency()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@app.get("/me")
|
|
82
|
+
def me(claims: Annotated[dict, Depends(current_user)]):
|
|
83
|
+
return claims
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Invalid tokens are converted to `401 Unauthorized` responses.
|
|
87
|
+
|
|
88
|
+
## Configuration
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from fastapi_betterauth import BetterAuthVerifier
|
|
92
|
+
|
|
93
|
+
verifier = BetterAuthVerifier(
|
|
94
|
+
"https://your-app.example.com",
|
|
95
|
+
jwks_path="/api/auth/jwks",
|
|
96
|
+
issuer="https://your-app.example.com",
|
|
97
|
+
audience="https://your-app.example.com",
|
|
98
|
+
algorithms=("EdDSA",),
|
|
99
|
+
cache_jwk_set=True,
|
|
100
|
+
lifespan=300,
|
|
101
|
+
)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Set `issuer=None` or `audience=None` to disable that specific validation.
|
|
105
|
+
|
|
106
|
+
## Compatibility API
|
|
107
|
+
|
|
108
|
+
The older module-level style is still available:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from fastapi_betterauth import init_client, validate_token
|
|
112
|
+
|
|
113
|
+
init_client("https://your-app.example.com")
|
|
114
|
+
claims = validate_token(token)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Prefer `BetterAuthVerifier` for new code because it avoids global state and
|
|
118
|
+
works better for tests, multiple apps, and multi-tenant services.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
fastapi_betterauth/__init__.py,sha256=s40hmNJ0ClI5yWL3lkacxtnnBsmH3KZ69eEsZ8iuwxE,1137
|
|
2
|
+
fastapi_betterauth/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
3
|
+
fastapi_betterauth-0.1.0.dist-info/METADATA,sha256=INkW2TnTgItqkEQ4_v8-zp6jFcdYc3h4YUqlQIbpBxQ,3134
|
|
4
|
+
fastapi_betterauth-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
5
|
+
fastapi_betterauth-0.1.0.dist-info/licenses/LICENSE,sha256=PhBYCG7HSJxZL3-AxevMDyRbnmwsNJn9EqnUUyTYhDY,1070
|
|
6
|
+
fastapi_betterauth-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Luka Onikadze
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|