-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
176 additions
and
321 deletions.
There are no files selected for viewing
This file contains 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 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,11 +1,4 @@ | ||
try: | ||
from .authlib_backend import AuthlibJWTBackend | ||
except ImportError: | ||
AuthlibJWTBackend = None # type: ignore | ||
|
||
try: | ||
from .python_jose_backend import PythonJoseJWTBackend | ||
except ImportError: | ||
PythonJoseJWTBackend = None # type: ignore | ||
|
||
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 |
This file contains 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 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 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,52 +1,46 @@ | ||
import warnings | ||
from typing import Any, Dict, Optional | ||
|
||
from fastapi import HTTPException | ||
from starlette.status import HTTP_401_UNAUTHORIZED | ||
|
||
try: | ||
from jose import jwt | ||
import jose | ||
import jose.jwt | ||
except ImportError: | ||
jwt = None # type: ignore | ||
jose = None # type: ignore | ||
|
||
from .abstract_backend import AbstractJWTBackend | ||
from .abstract_backend import AbstractJWTBackend, BackendException | ||
|
||
|
||
class PythonJoseJWTBackend(AbstractJWTBackend): | ||
def __init__(self, algorithm: Optional[str] = None) -> None: | ||
assert jwt is not None, "To use PythonJoseJWTBackend, you need to install python-jose" | ||
assert jose is not None, "To use PythonJoseJWTBackend, you need to install python-jose" | ||
warnings.warn("PythonJoseJWTBackend is deprecated as python-jose library is not maintained anymore.") | ||
|
||
self._algorithm = algorithm or self.default_algorithm | ||
assert ( | ||
hasattr(jwt.ALGORITHMS, self._algorithm) is True # type: ignore[attr-defined] | ||
hasattr(jose.jwt.ALGORITHMS, self._algorithm) is True # type: ignore[attr-defined] | ||
), f"{algorithm} algorithm is not supported by python-jose library" | ||
|
||
@property | ||
def default_algorithm(self) -> str: | ||
return jwt.ALGORITHMS.HS256 | ||
return jose.jwt.ALGORITHMS.HS256 # type: ignore[attr-defined] | ||
|
||
@property | ||
def algorithm(self) -> str: | ||
return self._algorithm | ||
|
||
def encode(self, to_encode: Dict[str, Any], secret_key: str) -> str: | ||
return jwt.encode(to_encode, secret_key, algorithm=self._algorithm) | ||
return jose.jwt.encode(to_encode, secret_key, algorithm=self._algorithm) | ||
|
||
def decode(self, token: str, secret_key: str, auto_error: bool) -> Optional[Dict[str, Any]]: | ||
def decode(self, token: str, secret_key: str) -> Optional[Dict[str, Any]]: | ||
try: | ||
payload: Dict[str, Any] = jwt.decode( | ||
payload: Dict[str, Any] = jose.jwt.decode( | ||
token, | ||
secret_key, | ||
algorithms=[self._algorithm], | ||
options={"leeway": 10}, | ||
) | ||
return payload | ||
except jwt.ExpiredSignatureError as e: # type: ignore[attr-defined] | ||
if 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 auto_error: | ||
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail=f"Wrong token: {e}") | ||
else: | ||
return None | ||
except jose.jwt.ExpiredSignatureError as e: # type: ignore[attr-defined] | ||
raise BackendException(f"Token time expired: {e}") | ||
except jose.jwt.JWTError as e: # type: ignore[attr-defined] | ||
raise BackendException(f"Invalid token: {e}") |
This file contains 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,10 @@ | ||
from typing import Type | ||
|
||
import pytest | ||
|
||
from fastapi_jwt.jwt_backends import AbstractJWTBackend, AuthlibJWTBackend, PythonJoseJWTBackend | ||
|
||
|
||
@pytest.fixture(params=[PythonJoseJWTBackend, AuthlibJWTBackend]) | ||
def jwt_backend(request: pytest.FixtureRequest) -> Type[AbstractJWTBackend]: | ||
return request.param |
This file contains 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
Oops, something went wrong.