-
Notifications
You must be signed in to change notification settings - Fork 697
fix: Incorrect Token
, TokenBackend
typing
#889
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ | |
) | ||
|
||
from .exceptions import TokenBackendError, TokenBackendExpiredToken | ||
from .tokens import Token | ||
from .utils import format_lazy | ||
|
||
try: | ||
|
@@ -24,6 +23,8 @@ | |
except ImportError: | ||
JWK_CLIENT_AVAILABLE = False | ||
|
||
RawToken = Union[bytes, str] | ||
|
||
ALLOWED_ALGORITHMS = { | ||
"HS256", | ||
"HS384", | ||
|
@@ -114,12 +115,14 @@ def get_leeway(self) -> timedelta: | |
) | ||
) | ||
|
||
def get_verifying_key(self, token: Token) -> Any: | ||
def get_verifying_key(self, token: RawToken) -> Any: | ||
if self.algorithm.startswith("HS"): | ||
return self.prepared_signing_key | ||
|
||
if self.jwks_client: | ||
try: | ||
if isinstance(token, bytes): | ||
token = token.decode("utf-8") | ||
|
||
return self.jwks_client.get_signing_key_from_jwt(token).key | ||
except PyJWKClientError as e: | ||
raise TokenBackendError(_("Token is invalid")) from e | ||
|
@@ -148,7 +151,7 @@ def encode(self, payload: dict[str, Any]) -> str: | |
# For PyJWT >= 2.0.0a1 | ||
return token | ||
|
||
def decode(self, token: Token, verify: bool = True) -> dict[str, Any]: | ||
def decode(self, token: RawToken, verify: bool = True) -> dict[str, Any]: | ||
""" | ||
Performs a validation of the given token and returns its payload | ||
dictionary. | ||
|
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.
hm i wish there was a way to customize the typing based on the version of JWT.
Specifically, I wonder if we can import something consistent from PyJWT itself that represents the token. Using a union is also not entirely correct (better than what it was before for sure).
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.
It is annotated as this exact union in the recent PyJWT versions.
FWIW,
bytes | str
is also technically supported since 1.7.1 despite the formaljwt: str
annotation — see https://github.com/jpadilla/pyjwt/blob/b65e1ac6dc4d11801f3642eaab34ae6a54162c18/jwt/api_jws.py#L171-L177