@@ -66,12 +66,15 @@ def decode(token, key, algorithms=None, options=None, audience=None, issuer=None
6666 the provided claim.
6767 options (dict): A dictionary of options for skipping validation steps.
6868
69- default = {
69+ defaults = {
7070 'verify_signature': True,
7171 'verify_aud': True,
7272 'verify_iat': True,
7373 'verify_exp': True,
7474 'verify_nbf': True,
75+ 'verify_iss': True,
76+ 'verify_sub': True,
77+ 'verify_jti': True,
7578 'leeway': 0,
7679 }
7780
@@ -96,6 +99,8 @@ def decode(token, key, algorithms=None, options=None, audience=None, issuer=None
9699 'verify_exp' : True ,
97100 'verify_nbf' : True ,
98101 'verify_iss' : True ,
102+ 'verify_sub' : True ,
103+ 'verify_jti' : True ,
99104 'leeway' : 0 ,
100105 }
101106
@@ -245,6 +250,50 @@ def _validate_iss(claims, issuer=None):
245250 raise JWTClaimsError ('Invalid issuer' )
246251
247252
253+ def _validate_sub (claims ):
254+ """Validates that the 'sub' claim is valid.
255+
256+ The "sub" (subject) claim identifies the principal that is the
257+ subject of the JWT. The claims in a JWT are normally statements
258+ about the subject. The subject value MUST either be scoped to be
259+ locally unique in the context of the issuer or be globally unique.
260+ The processing of this claim is generally application specific. The
261+ "sub" value is a case-sensitive string containing a StringOrURI
262+ value. Use of this claim is OPTIONAL.
263+
264+ Args:
265+ claims (dict): The claims dictionary to validate.
266+ """
267+
268+ if 'sub' not in claims :
269+ return
270+
271+ if not isinstance (claims ['sub' ], string_types ):
272+ raise JWTClaimsError ('Subject must be a string.' )
273+
274+
275+ def _validate_jti (claims ):
276+ """Validates that the 'jti' claim is valid.
277+
278+ The "jti" (JWT ID) claim provides a unique identifier for the JWT.
279+ The identifier value MUST be assigned in a manner that ensures that
280+ there is a negligible probability that the same value will be
281+ accidentally assigned to a different data object; if the application
282+ uses multiple issuers, collisions MUST be prevented among values
283+ produced by different issuers as well. The "jti" claim can be used
284+ to prevent the JWT from being replayed. The "jti" value is a case-
285+ sensitive string. Use of this claim is OPTIONAL.
286+
287+ Args:
288+ claims (dict): The claims dictionary to validate.
289+ """
290+ if 'jti' not in claims :
291+ return
292+
293+ if not isinstance (claims ['jti' ], string_types ):
294+ raise JWTClaimsError ('JWT ID must be a string.' )
295+
296+
248297def _validate_claims (claims , audience = None , issuer = None , options = None ):
249298
250299 leeway = options .get ('leeway' , 0 )
@@ -269,3 +318,9 @@ def _validate_claims(claims, audience=None, issuer=None, options=None):
269318
270319 if options .get ('verify_iss' ):
271320 _validate_iss (claims , issuer = issuer )
321+
322+ if options .get ('verify_sub' ):
323+ _validate_sub (claims )
324+
325+ if options .get ('verify_jti' ):
326+ _validate_jti (claims )
0 commit comments