11"""Token Verifier module"""
2+ from __future__ import annotations
3+
4+ from typing import TYPE_CHECKING , Any
5+
26from .. import TokenValidationError
37from ..rest_async import AsyncRestClient
48from .token_verifier import AsymmetricSignatureVerifier , JwksFetcher , TokenVerifier
59
10+ if TYPE_CHECKING :
11+ from aiohttp import ClientSession
12+ from cryptography .hazmat .primitives .asymmetric .rsa import RSAPublicKey
13+
614
715class AsyncAsymmetricSignatureVerifier (AsymmetricSignatureVerifier ):
816 """Async verifier for RSA signatures, which rely on public key certificates.
@@ -12,11 +20,11 @@ class AsyncAsymmetricSignatureVerifier(AsymmetricSignatureVerifier):
1220 algorithm (str, optional): The expected signing algorithm. Defaults to "RS256".
1321 """
1422
15- def __init__ (self , jwks_url , algorithm = "RS256" ):
23+ def __init__ (self , jwks_url : str , algorithm : str = "RS256" ) -> None :
1624 super ().__init__ (jwks_url , algorithm )
1725 self ._fetcher = AsyncJwksFetcher (jwks_url )
1826
19- def set_session (self , session ) :
27+ def set_session (self , session : ClientSession ) -> None :
2028 """Set Client Session to improve performance by reusing session.
2129
2230 Args:
@@ -32,7 +40,7 @@ async def _fetch_key(self, key_id=None):
3240 key_id (str): The key's key id."""
3341 return await self ._fetcher .get_key (key_id )
3442
35- async def verify_signature (self , token ):
43+ async def verify_signature (self , token ) -> dict [ str , Any ] :
3644 """Verifies the signature of the given JSON web token.
3745
3846 Args:
@@ -57,11 +65,11 @@ class AsyncJwksFetcher(JwksFetcher):
5765 cache_ttl (str, optional): The lifetime of the JWK set cache in seconds. Defaults to 600 seconds.
5866 """
5967
60- def __init__ (self , * args , ** kwargs ) :
68+ def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
6169 super ().__init__ (* args , ** kwargs )
6270 self ._async_client = AsyncRestClient (None )
6371
64- def set_session (self , session ) :
72+ def set_session (self , session : ClientSession ) -> None :
6573 """Set Client Session to improve performance by reusing session.
6674
6775 Args:
@@ -70,7 +78,7 @@ def set_session(self, session):
7078 """
7179 self ._async_client .set_session (session )
7280
73- async def _fetch_jwks (self , force = False ):
81+ async def _fetch_jwks (self , force : bool = False ) -> dict [ str , RSAPublicKey ] :
7482 """Attempts to obtain the JWK set from the cache, as long as it's still valid.
7583 When not, it will perform a network request to the jwks_url to obtain a fresh result
7684 and update the cache value with it.
@@ -90,7 +98,7 @@ async def _fetch_jwks(self, force=False):
9098 self ._cache_is_fresh = False
9199 return self ._cache_value
92100
93- async def get_key (self , key_id ) :
101+ async def get_key (self , key_id : str ) -> RSAPublicKey :
94102 """Obtains the JWK associated with the given key id.
95103
96104 Args:
@@ -126,7 +134,13 @@ class AsyncTokenVerifier(TokenVerifier):
126134 Defaults to 60 seconds.
127135 """
128136
129- def __init__ (self , signature_verifier , issuer , audience , leeway = 0 ):
137+ def __init__ (
138+ self ,
139+ signature_verifier : AsyncAsymmetricSignatureVerifier ,
140+ issuer : str ,
141+ audience : str ,
142+ leeway : int = 0 ,
143+ ) -> None :
130144 if not signature_verifier or not isinstance (
131145 signature_verifier , AsyncAsymmetricSignatureVerifier
132146 ):
@@ -140,7 +154,7 @@ def __init__(self, signature_verifier, issuer, audience, leeway=0):
140154 self ._sv = signature_verifier
141155 self ._clock = None # legacy testing requirement
142156
143- def set_session (self , session ) :
157+ def set_session (self , session : ClientSession ) -> None :
144158 """Set Client Session to improve performance by reusing session.
145159
146160 Args:
@@ -149,7 +163,13 @@ def set_session(self, session):
149163 """
150164 self ._sv .set_session (session )
151165
152- async def verify (self , token , nonce = None , max_age = None , organization = None ):
166+ async def verify (
167+ self ,
168+ token : str ,
169+ nonce : str | None = None ,
170+ max_age : int | None = None ,
171+ organization : str | None = None ,
172+ ) -> dict [str , Any ]:
153173 """Attempts to verify the given ID token, following the steps defined in the OpenID Connect spec.
154174
155175 Args:
0 commit comments