21
21
22
22
23
23
class PyJWT :
24
- def __init__ (self , options = None ):
24
+ def __init__ (self , options : Optional [ dict [ str , Any ]] = None ) -> None :
25
25
if options is None :
26
26
options = {}
27
- self .options = {** self ._get_default_options (), ** options }
27
+ self .options : dict [ str , Any ] = {** self ._get_default_options (), ** options }
28
28
29
29
@staticmethod
30
30
def _get_default_options () -> Dict [str , Union [bool , List [str ]]]:
@@ -157,7 +157,7 @@ def decode(
157
157
leeway : Union [int , float , timedelta ] = 0 ,
158
158
# kwargs
159
159
** kwargs ,
160
- ) -> Dict [ str , Any ] :
160
+ ) -> Any :
161
161
if kwargs :
162
162
warnings .warn (
163
163
"passing additional kwargs to decode() is deprecated "
@@ -178,7 +178,14 @@ def decode(
178
178
)
179
179
return decoded ["payload" ]
180
180
181
- def _validate_claims (self , payload , options , audience = None , issuer = None , leeway = 0 ):
181
+ def _validate_claims (
182
+ self ,
183
+ payload : dict [str , Any ],
184
+ options : dict [str , Any ],
185
+ audience = None ,
186
+ issuer = None ,
187
+ leeway : float | timedelta = 0 ,
188
+ ) -> None :
182
189
if isinstance (leeway , timedelta ):
183
190
leeway = leeway .total_seconds ()
184
191
@@ -204,12 +211,21 @@ def _validate_claims(self, payload, options, audience=None, issuer=None, leeway=
204
211
if options ["verify_aud" ]:
205
212
self ._validate_aud (payload , audience )
206
213
207
- def _validate_required_claims (self , payload , options ):
214
+ def _validate_required_claims (
215
+ self ,
216
+ payload : dict [str , Any ],
217
+ options : dict [str , Any ],
218
+ ) -> None :
208
219
for claim in options ["require" ]:
209
220
if payload .get (claim ) is None :
210
221
raise MissingRequiredClaimError (claim )
211
222
212
- def _validate_iat (self , payload , now , leeway ):
223
+ def _validate_iat (
224
+ self ,
225
+ payload : dict [str , Any ],
226
+ now : float ,
227
+ leeway : float ,
228
+ ) -> None :
213
229
iat = payload ["iat" ]
214
230
try :
215
231
int (iat )
@@ -218,7 +234,12 @@ def _validate_iat(self, payload, now, leeway):
218
234
if iat > (now + leeway ):
219
235
raise ImmatureSignatureError ("The token is not yet valid (iat)" )
220
236
221
- def _validate_nbf (self , payload , now , leeway ):
237
+ def _validate_nbf (
238
+ self ,
239
+ payload : dict [str , Any ],
240
+ now : float ,
241
+ leeway : float ,
242
+ ) -> None :
222
243
try :
223
244
nbf = int (payload ["nbf" ])
224
245
except ValueError :
@@ -227,7 +248,12 @@ def _validate_nbf(self, payload, now, leeway):
227
248
if nbf > (now + leeway ):
228
249
raise ImmatureSignatureError ("The token is not yet valid (nbf)" )
229
250
230
- def _validate_exp (self , payload , now , leeway ):
251
+ def _validate_exp (
252
+ self ,
253
+ payload : dict [str , Any ],
254
+ now : float ,
255
+ leeway : float ,
256
+ ) -> None :
231
257
try :
232
258
exp = int (payload ["exp" ])
233
259
except ValueError :
@@ -236,7 +262,11 @@ def _validate_exp(self, payload, now, leeway):
236
262
if exp <= (now - leeway ):
237
263
raise ExpiredSignatureError ("Signature has expired" )
238
264
239
- def _validate_aud (self , payload , audience ):
265
+ def _validate_aud (
266
+ self ,
267
+ payload : dict [str , Any ],
268
+ audience : Optional [Union [str , Iterable [str ]]],
269
+ ) -> None :
240
270
if audience is None :
241
271
if "aud" not in payload or not payload ["aud" ]:
242
272
return
@@ -264,7 +294,7 @@ def _validate_aud(self, payload, audience):
264
294
if all (aud not in audience_claims for aud in audience ):
265
295
raise InvalidAudienceError ("Invalid audience" )
266
296
267
- def _validate_iss (self , payload , issuer ) :
297
+ def _validate_iss (self , payload : dict [ str , Any ], issuer : Any ) -> None :
268
298
if issuer is None :
269
299
return
270
300
0 commit comments