-
Notifications
You must be signed in to change notification settings - Fork 19
feat: jwt_backends - create backend mechanism and add authlib support #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
06d3132
jwt_backends: create backend mechanism and add authlib support
hasB4K 54594f9
Merge branch 'main' into authlib_backend
k4black ea60e91
style: multiple style fixes including typings
k4black 93cbdee
fix: remove unused typings Self import
k4black ad4221d
refactor: backends design + tests
k4black 67866bf
chore: add # pragma: no cover where needed
k4black 57b463b
docs: update README.md
k4black File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| from .jwt import * # noqa: F401, F403 | ||
| from .jwt_backends import * # noqa: F401, F403 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| from abc import ABC | ||
| from datetime import datetime, timedelta | ||
| from typing import Any, Dict, Optional, Set | ||
| from typing import Any, Dict, Optional, Set, Type | ||
| from uuid import uuid4 | ||
|
|
||
| from fastapi.exceptions import HTTPException | ||
|
|
@@ -9,13 +9,24 @@ | |
| from fastapi.security import APIKeyCookie, HTTPBearer | ||
| from starlette.status import HTTP_401_UNAUTHORIZED | ||
|
|
||
| try: | ||
| from jose import jwt | ||
| except ImportError: # pragma: nocover | ||
| jwt = None # type: ignore[assignment] | ||
| from .jwt_backends import AbstractJWTBackend, authlib_backend, python_jose_backend | ||
| from .jwt_backends.abstract_backend import BackendException | ||
|
|
||
| DEFAULT_JWT_BACKEND: Optional[Type[AbstractJWTBackend]] = None | ||
| if authlib_backend.authlib_jose is not None: | ||
| DEFAULT_JWT_BACKEND = authlib_backend.AuthlibJWTBackend | ||
| elif python_jose_backend.jose is not None: | ||
| DEFAULT_JWT_BACKEND = python_jose_backend.PythonJoseJWTBackend | ||
| else: # pragma: nocover | ||
| raise ImportError("No JWT backend found, please install 'python-jose' or 'authlib'") | ||
|
|
||
| def utcnow(): | ||
|
|
||
| def force_jwt_backend(cls: Type[AbstractJWTBackend]) -> None: | ||
| global DEFAULT_JWT_BACKEND | ||
| DEFAULT_JWT_BACKEND = cls | ||
|
|
||
|
|
||
| def utcnow() -> datetime: | ||
| try: | ||
| from datetime import UTC | ||
| except ImportError: # pragma: nocover | ||
|
|
@@ -27,6 +38,7 @@ def utcnow(): | |
|
|
||
|
|
||
| __all__ = [ | ||
| "force_jwt_backend", | ||
| "JwtAuthorizationCredentials", | ||
| "JwtAccessBearer", | ||
| "JwtAccessCookie", | ||
|
|
@@ -49,15 +61,11 @@ def __getitem__(self, item: str) -> Any: | |
| class JwtAuthBase(ABC): | ||
| class JwtAccessCookie(APIKeyCookie): | ||
| def __init__(self, *args: Any, **kwargs: Any): | ||
| APIKeyCookie.__init__( | ||
| self, *args, name="access_token_cookie", auto_error=False, **kwargs | ||
| ) | ||
| APIKeyCookie.__init__(self, *args, name="access_token_cookie", auto_error=False, **kwargs) | ||
|
|
||
| class JwtRefreshCookie(APIKeyCookie): | ||
| def __init__(self, *args: Any, **kwargs: Any): | ||
| APIKeyCookie.__init__( | ||
| self, *args, name="refresh_token_cookie", auto_error=False, **kwargs | ||
| ) | ||
| APIKeyCookie.__init__(self, *args, name="refresh_token_cookie", auto_error=False, **kwargs) | ||
|
|
||
| class JwtAccessBearer(HTTPBearer): | ||
| def __init__(self, *args: Any, **kwargs: Any): | ||
|
|
@@ -72,38 +80,35 @@ def __init__( | |
| secret_key: str, | ||
| places: Optional[Set[str]] = None, | ||
| auto_error: bool = True, | ||
| algorithm: str = jwt.ALGORITHMS.HS256, # type: ignore[attr-defined] | ||
| algorithm: Optional[str] = None, | ||
| access_expires_delta: Optional[timedelta] = None, | ||
| refresh_expires_delta: Optional[timedelta] = None, | ||
| ): | ||
| assert jwt is not None, "python-jose must be installed to use JwtAuth" | ||
| if places: | ||
| assert places.issubset( | ||
| {"header", "cookie"} | ||
| ), "only 'header'/'cookie' are supported" | ||
| algorithm = algorithm.upper() | ||
| assert ( | ||
| hasattr(jwt.ALGORITHMS, algorithm) is True # type: ignore[attr-defined] | ||
| ), f"{algorithm} algorithm is not supported by python-jose library" | ||
| assert DEFAULT_JWT_BACKEND is not None, "No JWT backend found, please install 'python-jose' or 'authlib'" | ||
|
|
||
| self.jwt_backend = DEFAULT_JWT_BACKEND(algorithm) | ||
| self.secret_key = secret_key | ||
|
|
||
| self.places = places or {"header"} | ||
| assert self.places.issubset({"header", "cookie"}), "only 'header' and/or 'cookie' places are supported" | ||
| self.auto_error = auto_error | ||
| self.algorithm = algorithm | ||
| self.access_expires_delta = access_expires_delta or timedelta(minutes=15) | ||
| self.refresh_expires_delta = refresh_expires_delta or timedelta(days=31) | ||
|
|
||
| @property | ||
| def algorithm(self) -> str: | ||
| return self.jwt_backend.algorithm | ||
|
|
||
| @classmethod | ||
| def from_other( | ||
| cls, | ||
| other: 'JwtAuthBase', | ||
| other: "JwtAuthBase", | ||
| secret_key: Optional[str] = None, | ||
| auto_error: Optional[bool] = None, | ||
| algorithm: Optional[str] = None, | ||
| access_expires_delta: Optional[timedelta] = None, | ||
| refresh_expires_delta: Optional[timedelta] = None, | ||
| ) -> 'JwtAuthBase': | ||
| ) -> "JwtAuthBase": | ||
| return cls( | ||
| secret_key=secret_key or other.secret_key, | ||
| auto_error=auto_error or other.auto_error, | ||
|
|
@@ -112,30 +117,6 @@ def from_other( | |
| refresh_expires_delta=refresh_expires_delta or other.refresh_expires_delta, | ||
| ) | ||
|
|
||
| def _decode(self, token: str) -> Optional[Dict[str, Any]]: | ||
| try: | ||
| payload: Dict[str, Any] = jwt.decode( | ||
| token, | ||
| self.secret_key, | ||
| algorithms=[self.algorithm], | ||
| options={"leeway": 10}, | ||
| ) | ||
| return payload | ||
| except jwt.ExpiredSignatureError as e: # type: ignore[attr-defined] | ||
| if self.auto_error: | ||
| raise HTTPException( | ||
| status_code=HTTP_401_UNAUTHORIZED, detail=f"Token time expired: {e}" | ||
| ) | ||
| else: | ||
| return None | ||
| except jwt.JWTError as e: # type: ignore[attr-defined] | ||
| if self.auto_error: | ||
| raise HTTPException( | ||
| status_code=HTTP_401_UNAUTHORIZED, detail=f"Wrong token: {e}" | ||
| ) | ||
| else: | ||
| return None | ||
|
|
||
|
Comment on lines
-115
to
-138
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was moved in the backend PythonJoseJWTBackend. Since the error strings are now in the backend, a dev could create its own backend to customize the error handling like thhis PR wanted to do: #7 |
||
| def _generate_payload( | ||
| self, | ||
| subject: Dict[str, Any], | ||
|
|
@@ -144,7 +125,6 @@ def _generate_payload( | |
| token_type: str, | ||
| ) -> Dict[str, Any]: | ||
| now = utcnow() | ||
|
|
||
| return { | ||
| "subject": subject.copy(), # main subject | ||
| "type": token_type, # 'access' or 'refresh' token | ||
|
|
@@ -165,15 +145,18 @@ async def _get_payload( | |
| # Check token exist | ||
| if not token: | ||
| if self.auto_error: | ||
| raise HTTPException( | ||
| status_code=HTTP_401_UNAUTHORIZED, detail="Credentials are not provided" | ||
| ) | ||
| raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Credentials are not provided") | ||
| else: | ||
| return None | ||
|
|
||
| # Try to decode jwt token. auto_error on error | ||
| payload = self._decode(token) | ||
| return payload | ||
| try: | ||
| return self.jwt_backend.decode(token, self.secret_key) | ||
| except BackendException as e: | ||
| if self.auto_error: | ||
| raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail=str(e)) | ||
| else: | ||
| return None | ||
|
|
||
| def create_access_token( | ||
| self, | ||
|
|
@@ -183,14 +166,8 @@ def create_access_token( | |
| ) -> str: | ||
| expires_delta = expires_delta or self.access_expires_delta | ||
| unique_identifier = unique_identifier or str(uuid4()) | ||
| to_encode = self._generate_payload( | ||
| subject, expires_delta, unique_identifier, "access" | ||
| ) | ||
|
|
||
| jwt_encoded: str = jwt.encode( | ||
| to_encode, self.secret_key, algorithm=self.algorithm | ||
| ) | ||
| return jwt_encoded | ||
| to_encode = self._generate_payload(subject, expires_delta, unique_identifier, "access") | ||
| return self.jwt_backend.encode(to_encode, self.secret_key) | ||
|
|
||
| def create_refresh_token( | ||
| self, | ||
|
|
@@ -200,22 +177,12 @@ def create_refresh_token( | |
| ) -> str: | ||
| expires_delta = expires_delta or self.refresh_expires_delta | ||
| unique_identifier = unique_identifier or str(uuid4()) | ||
| to_encode = self._generate_payload( | ||
| subject, expires_delta, unique_identifier, "refresh" | ||
| ) | ||
|
|
||
| jwt_encoded: str = jwt.encode( | ||
| to_encode, self.secret_key, algorithm=self.algorithm | ||
| ) | ||
| return jwt_encoded | ||
| to_encode = self._generate_payload(subject, expires_delta, unique_identifier, "refresh") | ||
| return self.jwt_backend.encode(to_encode, self.secret_key) | ||
|
|
||
| @staticmethod | ||
| def set_access_cookie( | ||
| response: Response, access_token: str, expires_delta: Optional[timedelta] = None | ||
| ) -> None: | ||
| seconds_expires: Optional[int] = ( | ||
| int(expires_delta.total_seconds()) if expires_delta else None | ||
| ) | ||
| def set_access_cookie(response: Response, access_token: str, expires_delta: Optional[timedelta] = None) -> None: | ||
| seconds_expires: Optional[int] = int(expires_delta.total_seconds()) if expires_delta else None | ||
| response.set_cookie( | ||
| key="access_token_cookie", | ||
| value=access_token, | ||
|
|
@@ -229,9 +196,7 @@ def set_refresh_cookie( | |
| refresh_token: str, | ||
| expires_delta: Optional[timedelta] = None, | ||
| ) -> None: | ||
| seconds_expires: Optional[int] = ( | ||
| int(expires_delta.total_seconds()) if expires_delta else None | ||
| ) | ||
| seconds_expires: Optional[int] = int(expires_delta.total_seconds()) if expires_delta else None | ||
| response.set_cookie( | ||
| key="refresh_token_cookie", | ||
| value=refresh_token, | ||
|
|
@@ -241,15 +206,11 @@ def set_refresh_cookie( | |
|
|
||
| @staticmethod | ||
| def unset_access_cookie(response: Response) -> None: | ||
| response.set_cookie( | ||
| key="access_token_cookie", value="", httponly=False, max_age=-1 | ||
| ) | ||
| response.set_cookie(key="access_token_cookie", value="", httponly=False, max_age=-1) | ||
|
|
||
| @staticmethod | ||
| def unset_refresh_cookie(response: Response) -> None: | ||
| response.set_cookie( | ||
| key="refresh_token_cookie", value="", httponly=True, max_age=-1 | ||
| ) | ||
| response.set_cookie(key="refresh_token_cookie", value="", httponly=True, max_age=-1) | ||
|
|
||
|
|
||
| class JwtAccess(JwtAuthBase): | ||
|
|
@@ -261,7 +222,7 @@ def __init__( | |
| secret_key: str, | ||
| places: Optional[Set[str]] = None, | ||
| auto_error: bool = True, | ||
| algorithm: str = jwt.ALGORITHMS.HS256, # type: ignore[attr-defined] | ||
| algorithm: Optional[str] = None, | ||
| access_expires_delta: Optional[timedelta] = None, | ||
| refresh_expires_delta: Optional[timedelta] = None, | ||
| ): | ||
|
|
@@ -282,9 +243,7 @@ async def _get_credentials( | |
| payload = await self._get_payload(bearer, cookie) | ||
|
|
||
| if payload: | ||
| return JwtAuthorizationCredentials( | ||
| payload["subject"], payload.get("jti", None) | ||
| ) | ||
| return JwtAuthorizationCredentials(payload["subject"], payload.get("jti", None)) | ||
| return None | ||
|
|
||
|
|
||
|
|
@@ -293,7 +252,7 @@ def __init__( | |
| self, | ||
| secret_key: str, | ||
| auto_error: bool = True, | ||
| algorithm: str = jwt.ALGORITHMS.HS256, # type: ignore[attr-defined] | ||
| algorithm: Optional[str] = None, | ||
| access_expires_delta: Optional[timedelta] = None, | ||
| refresh_expires_delta: Optional[timedelta] = None, | ||
| ): | ||
|
|
@@ -317,7 +276,7 @@ def __init__( | |
| self, | ||
| secret_key: str, | ||
| auto_error: bool = True, | ||
| algorithm: str = jwt.ALGORITHMS.HS256, # type: ignore[attr-defined] | ||
| algorithm: Optional[str] = None, | ||
| access_expires_delta: Optional[timedelta] = None, | ||
| refresh_expires_delta: Optional[timedelta] = None, | ||
| ): | ||
|
|
@@ -342,7 +301,7 @@ def __init__( | |
| self, | ||
| secret_key: str, | ||
| auto_error: bool = True, | ||
| algorithm: str = jwt.ALGORITHMS.HS256, # type: ignore[attr-defined] | ||
| algorithm: Optional[str] = None, | ||
| access_expires_delta: Optional[timedelta] = None, | ||
| refresh_expires_delta: Optional[timedelta] = None, | ||
| ): | ||
|
|
@@ -372,7 +331,7 @@ def __init__( | |
| secret_key: str, | ||
| places: Optional[Set[str]] = None, | ||
| auto_error: bool = True, | ||
| algorithm: str = jwt.ALGORITHMS.HS256, # type: ignore[attr-defined] | ||
| algorithm: Optional[str] = None, | ||
| access_expires_delta: Optional[timedelta] = None, | ||
| refresh_expires_delta: Optional[timedelta] = None, | ||
| ): | ||
|
|
@@ -399,22 +358,20 @@ async def _get_credentials( | |
| if self.auto_error: | ||
| raise HTTPException( | ||
| status_code=HTTP_401_UNAUTHORIZED, | ||
| detail="Wrong token: 'type' is not 'refresh'", | ||
| detail="Invalid token: 'type' is not 'refresh'", | ||
| ) | ||
| else: | ||
| return None | ||
|
|
||
| return JwtAuthorizationCredentials( | ||
| payload["subject"], payload.get("jti", None) | ||
| ) | ||
| return JwtAuthorizationCredentials(payload["subject"], payload.get("jti", None)) | ||
|
|
||
|
|
||
| class JwtRefreshBearer(JwtRefresh): | ||
| def __init__( | ||
| self, | ||
| secret_key: str, | ||
| auto_error: bool = True, | ||
| algorithm: str = jwt.ALGORITHMS.HS256, # type: ignore[attr-defined] | ||
| algorithm: Optional[str] = None, | ||
| access_expires_delta: Optional[timedelta] = None, | ||
| refresh_expires_delta: Optional[timedelta] = None, | ||
| ): | ||
|
|
@@ -438,7 +395,7 @@ def __init__( | |
| self, | ||
| secret_key: str, | ||
| auto_error: bool = True, | ||
| algorithm: str = jwt.ALGORITHMS.HS256, # type: ignore[attr-defined] | ||
| algorithm: Optional[str] = None, | ||
| access_expires_delta: Optional[timedelta] = None, | ||
| refresh_expires_delta: Optional[timedelta] = None, | ||
| ): | ||
|
|
@@ -463,7 +420,7 @@ def __init__( | |
| self, | ||
| secret_key: str, | ||
| auto_error: bool = True, | ||
| algorithm: str = jwt.ALGORITHMS.HS256, # type: ignore[attr-defined] | ||
| algorithm: Optional[str] = None, | ||
| access_expires_delta: Optional[timedelta] = None, | ||
| refresh_expires_delta: Optional[timedelta] = None, | ||
| ): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from . import abstract_backend, authlib_backend, python_jose_backend # noqa: F401 | ||
| from .abstract_backend import AbstractJWTBackend # noqa: F401 | ||
| from .authlib_backend import AuthlibJWTBackend # noqa: F401 | ||
| from .python_jose_backend import PythonJoseJWTBackend # noqa: F401 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default algorithm is now handled in the jwt backend directly.