1111try :
1212 import jwt
1313 HAS_JWT = True
14+ _bases_error = (jwt .exceptions .PyJWTError , ValueError )
1415except ImportError : # pragma: no cover
1516 HAS_JWT = False
17+ _bases_error = (ValueError , )
1618
1719
1820AUTH_HEADER_NAME = 'Authorization'
1921AUTH_SCHEME = 'Bearer '
2022
2123
22- if HAS_JWT :
23- # This class inherits from ValueError to maintain backward compatibility
24- # with previous versions of aiohttp-security.
25- class JWTIdentityError (jwt .exceptions .PyJWTError , ValueError ):
26- pass
27-
28- else :
29- class JWTIdentityError (ValueError ):
30- pass
24+ # This class inherits from ValueError to maintain backward compatibility
25+ # with previous versions of aiohttp-security
26+ class InvalidAuthorizationScheme (* _bases_error ):
27+ pass
3128
3229
3330class JWTIdentityPolicy (AbstractIdentityPolicy ):
@@ -45,15 +42,14 @@ async def identify(self, request: web.Request) -> Optional[str]:
4542 return None
4643
4744 if not header_identity .startswith (AUTH_SCHEME ):
48- raise JWTIdentityError ("Invalid authorization scheme. "
49- + "Should be `{}<token>`" .format (AUTH_SCHEME ))
45+ raise InvalidAuthorizationScheme ("Invalid authorization scheme. "
46+ "Should be `{}<token>`" .format (AUTH_SCHEME ))
5047
5148 token = header_identity .split (' ' )[1 ].strip ()
5249
5350 identity = jwt .decode (token ,
5451 self .secret ,
5552 algorithms = [self .algorithm ])
56-
5753 return identity .get (self .key ) # type: ignore[no-any-return]
5854
5955 async def remember (self , request : web .Request , response : web .StreamResponse ,
0 commit comments