fastapi-oidc-jwks 0.0.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_oidc_jwks-0.0.1/LICENSE +21 -0
- fastapi_oidc_jwks-0.0.1/PKG-INFO +18 -0
- fastapi_oidc_jwks-0.0.1/README.md +3 -0
- fastapi_oidc_jwks-0.0.1/pyproject.toml +19 -0
- fastapi_oidc_jwks-0.0.1/setup.cfg +4 -0
- fastapi_oidc_jwks-0.0.1/src/fastapi-oidc-jwks/__init__.py +0 -0
- fastapi_oidc_jwks-0.0.1/src/fastapi-oidc-jwks/dependency.py +39 -0
- fastapi_oidc_jwks-0.0.1/src/fastapi-oidc-jwks/middleware.py +54 -0
- fastapi_oidc_jwks-0.0.1/src/fastapi_oidc_jwks.egg-info/PKG-INFO +18 -0
- fastapi_oidc_jwks-0.0.1/src/fastapi_oidc_jwks.egg-info/SOURCES.txt +10 -0
- fastapi_oidc_jwks-0.0.1/src/fastapi_oidc_jwks.egg-info/dependency_links.txt +1 -0
- fastapi_oidc_jwks-0.0.1/src/fastapi_oidc_jwks.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Maxime Moreillon
|
|
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.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-oidc-jwks
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A FastAPI dependency used to verify a user's OAuth access token with JWKS.
|
|
5
|
+
Author-email: Maxime Moreillon <m.moreillon@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/maximemoreillon/fastapi-oidc-jwks
|
|
8
|
+
Project-URL: Issues, https://github.com/maximemoreillon/fastapi-oidc-jwks/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# FastAPI OIDC JWKS
|
|
17
|
+
|
|
18
|
+
A FastAPI dependency used to verify a user's OAuth access token with JWKS.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "fastapi-oidc-jwks"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
authors = [
|
|
5
|
+
{ name="Maxime Moreillon", email="m.moreillon@gmail.com" },
|
|
6
|
+
]
|
|
7
|
+
description = "A FastAPI dependency used to verify a user's OAuth access token with JWKS."
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.9"
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"Operating System :: OS Independent",
|
|
13
|
+
]
|
|
14
|
+
license = "MIT"
|
|
15
|
+
license-files = ["LICEN[CS]E*"]
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
Homepage = "https://github.com/maximemoreillon/fastapi-oidc-jwks"
|
|
19
|
+
Issues = "https://github.com/maximemoreillon/fastapi-oidc-jwks/issues"
|
|
File without changes
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from fastapi import FastAPI, HTTPException, Security
|
|
2
|
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
3
|
+
import jwt
|
|
4
|
+
from jwt import PyJWKClient
|
|
5
|
+
|
|
6
|
+
app = FastAPI()
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class AuthDependency:
|
|
10
|
+
def __init__(self, jwks_uri: str):
|
|
11
|
+
self.jwks_uri = jwks_uri
|
|
12
|
+
self.jwks_client = PyJWKClient(self.jwks_uri)
|
|
13
|
+
self.audience = None # For now
|
|
14
|
+
|
|
15
|
+
def __call__(
|
|
16
|
+
self,
|
|
17
|
+
credentials: HTTPAuthorizationCredentials = Security(HTTPBearer()),
|
|
18
|
+
):
|
|
19
|
+
token = credentials.credentials
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
signing_key = self.jwks_client.get_signing_key_from_jwt(token)
|
|
23
|
+
return jwt.decode(
|
|
24
|
+
token,
|
|
25
|
+
signing_key.key,
|
|
26
|
+
algorithms=["RS256"],
|
|
27
|
+
audience=self.audience,
|
|
28
|
+
# issuer=self.issuer, # For now
|
|
29
|
+
options={"verify_aud": self.audience is not None},
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
except jwt.ExpiredSignatureError:
|
|
33
|
+
raise HTTPException(status_code=401, detail="Token expired")
|
|
34
|
+
except jwt.InvalidAudienceError:
|
|
35
|
+
raise HTTPException(status_code=401, detail="Invalid audience")
|
|
36
|
+
except jwt.InvalidIssuerError:
|
|
37
|
+
raise HTTPException(status_code=401, detail="Invalid issuer")
|
|
38
|
+
except jwt.PyJWTError as e:
|
|
39
|
+
raise HTTPException(status_code=401, detail=f"Invalid token: {str(e)}")
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from typing import Dict, Any, Optional
|
|
2
|
+
from fastapi import FastAPI, Request
|
|
3
|
+
from fastapi.responses import JSONResponse
|
|
4
|
+
from fastapi.security.utils import get_authorization_scheme_param
|
|
5
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
6
|
+
import jwt
|
|
7
|
+
from jwt import PyJWKClient
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class JWTAuthMiddleware(BaseHTTPMiddleware):
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
app: FastAPI,
|
|
14
|
+
jwks_url: str,
|
|
15
|
+
issuer: Optional[str] = None,
|
|
16
|
+
audience: Optional[str] = None,
|
|
17
|
+
):
|
|
18
|
+
super().__init__(app)
|
|
19
|
+
self.jwks_client = PyJWKClient(jwks_url)
|
|
20
|
+
self.issuer = issuer
|
|
21
|
+
self.audience = audience
|
|
22
|
+
|
|
23
|
+
async def dispatch(self, request: Request, call_next):
|
|
24
|
+
# Require Authorization header
|
|
25
|
+
auth: str = request.headers.get("Authorization")
|
|
26
|
+
scheme, token = get_authorization_scheme_param(auth)
|
|
27
|
+
|
|
28
|
+
if not token or scheme.lower() != "bearer":
|
|
29
|
+
return JSONResponse({"detail": "Not authenticated"}, status_code=401)
|
|
30
|
+
|
|
31
|
+
try:
|
|
32
|
+
signing_key = self.jwks_client.get_signing_key_from_jwt(token)
|
|
33
|
+
payload: Dict[str, Any] = jwt.decode(
|
|
34
|
+
token,
|
|
35
|
+
signing_key.key,
|
|
36
|
+
algorithms=["RS256"],
|
|
37
|
+
audience=self.audience,
|
|
38
|
+
issuer=self.issuer,
|
|
39
|
+
options={"verify_aud": self.audience is not None},
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# Attach user claims to request state
|
|
43
|
+
request.state.user = payload
|
|
44
|
+
|
|
45
|
+
except jwt.ExpiredSignatureError:
|
|
46
|
+
return JSONResponse({"detail": "Token expired"}, status_code=401)
|
|
47
|
+
except jwt.InvalidAudienceError:
|
|
48
|
+
return JSONResponse({"detail": "Invalid audience"}, status_code=401)
|
|
49
|
+
except jwt.InvalidIssuerError:
|
|
50
|
+
return JSONResponse({"detail": "Invalid issuer"}, status_code=401)
|
|
51
|
+
except jwt.PyJWTError as e:
|
|
52
|
+
return JSONResponse({"detail": f"Invalid token: {str(e)}"}, status_code=401)
|
|
53
|
+
|
|
54
|
+
return await call_next(request)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-oidc-jwks
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A FastAPI dependency used to verify a user's OAuth access token with JWKS.
|
|
5
|
+
Author-email: Maxime Moreillon <m.moreillon@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/maximemoreillon/fastapi-oidc-jwks
|
|
8
|
+
Project-URL: Issues, https://github.com/maximemoreillon/fastapi-oidc-jwks/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# FastAPI OIDC JWKS
|
|
17
|
+
|
|
18
|
+
A FastAPI dependency used to verify a user's OAuth access token with JWKS.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/fastapi-oidc-jwks/__init__.py
|
|
5
|
+
src/fastapi-oidc-jwks/dependency.py
|
|
6
|
+
src/fastapi-oidc-jwks/middleware.py
|
|
7
|
+
src/fastapi_oidc_jwks.egg-info/PKG-INFO
|
|
8
|
+
src/fastapi_oidc_jwks.egg-info/SOURCES.txt
|
|
9
|
+
src/fastapi_oidc_jwks.egg-info/dependency_links.txt
|
|
10
|
+
src/fastapi_oidc_jwks.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fastapi-oidc-jwks
|