Skip to content

Commit

Permalink
Rename JWTIdentityError to InvalidAuthorizationScheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Maillol committed Jan 28, 2025
1 parent e73eb60 commit 9b66e29
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions aiohttp_security/jwt_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@
try:
import jwt
HAS_JWT = True
_bases_error = (jwt.exceptions.PyJWTError, ValueError)
except ImportError: # pragma: no cover
HAS_JWT = False
_bases_error = (ValueError, )


AUTH_HEADER_NAME = 'Authorization'
AUTH_SCHEME = 'Bearer '


if HAS_JWT:
# This class inherits from ValueError to maintain backward compatibility
# with previous versions of aiohttp-security.
class JWTIdentityError(jwt.exceptions.PyJWTError, ValueError):
pass

else:
class JWTIdentityError(ValueError):
pass
# This class inherits from ValueError to maintain backward compatibility
# with previous versions of aiohttp-security
class InvalidAuthorizationScheme(*_bases_error):
pass


class JWTIdentityPolicy(AbstractIdentityPolicy):
Expand All @@ -45,15 +42,14 @@ async def identify(self, request: web.Request) -> Optional[str]:
return None

if not header_identity.startswith(AUTH_SCHEME):
raise JWTIdentityError("Invalid authorization scheme. "
+ "Should be `{}<token>`".format(AUTH_SCHEME))
raise InvalidAuthorizationScheme("Invalid authorization scheme. "
"Should be `{}<token>`".format(AUTH_SCHEME))

token = header_identity.split(' ')[1].strip()

identity = jwt.decode(token,
self.secret,
algorithms=[self.algorithm])

return identity.get(self.key) # type: ignore[no-any-return]

async def remember(self, request: web.Request, response: web.StreamResponse,
Expand Down

0 comments on commit 9b66e29

Please sign in to comment.