@@ -25,7 +25,7 @@ def _encode_jwt(additional_token_data, expires_delta, secret, algorithm):
2525
2626
2727def encode_access_token (identity , secret , algorithm , expires_delta , fresh ,
28- user_claims , csrf ):
28+ user_claims , csrf , identity_claim ):
2929 """
3030 Creates a new encoded (utf-8) access token.
3131
@@ -40,11 +40,12 @@ def encode_access_token(identity, secret, algorithm, expires_delta, fresh,
4040 be json serializable
4141 :param csrf: Whether to include a csrf double submit claim in this token
4242 (boolean)
43+ :param identity_claim: Which claim should be used to store the identity in
4344 :return: Encoded access token
4445 """
4546 # Create the jwt
4647 token_data = {
47- 'identity' : identity ,
48+ identity_claim : identity ,
4849 'fresh' : fresh ,
4950 'type' : 'access' ,
5051 'user_claims' : user_claims ,
@@ -54,7 +55,7 @@ def encode_access_token(identity, secret, algorithm, expires_delta, fresh,
5455 return _encode_jwt (token_data , expires_delta , secret , algorithm )
5556
5657
57- def encode_refresh_token (identity , secret , algorithm , expires_delta , csrf ):
58+ def encode_refresh_token (identity , secret , algorithm , expires_delta , csrf , identity_claim ):
5859 """
5960 Creates a new encoded (utf-8) refresh token.
6061
@@ -65,18 +66,19 @@ def encode_refresh_token(identity, secret, algorithm, expires_delta, csrf):
6566 (datetime.timedelta)
6667 :param csrf: Whether to include a csrf double submit claim in this token
6768 (boolean)
69+ :param identity_claim: Which claim should be used to store the identity in
6870 :return: Encoded refresh token
6971 """
7072 token_data = {
71- 'identity' : identity ,
73+ identity_claim : identity ,
7274 'type' : 'refresh' ,
7375 }
7476 if csrf :
7577 token_data ['csrf' ] = _create_csrf_token ()
7678 return _encode_jwt (token_data , expires_delta , secret , algorithm )
7779
7880
79- def decode_jwt (encoded_token , secret , algorithm , csrf ):
81+ def decode_jwt (encoded_token , secret , algorithm , csrf , identity_claim ):
8082 """
8183 Decodes an encoded JWT
8284
@@ -85,6 +87,7 @@ def decode_jwt(encoded_token, secret, algorithm, csrf):
8587 :param algorithm: Algorithm used to encode the JWT
8688 :param csrf: If this token is expected to have a CSRF double submit
8789 value present (boolean)
90+ :param identity_claim: expected claim that is used to identify the subject
8891 :return: Dictionary containing contents of the JWT
8992 """
9093 # This call verifies the ext, iat, and nbf claims
@@ -93,8 +96,8 @@ def decode_jwt(encoded_token, secret, algorithm, csrf):
9396 # Make sure that any custom claims we expect in the token are present
9497 if 'jti' not in data :
9598 raise JWTDecodeError ("Missing claim: jti" )
96- if 'identity' not in data :
97- raise JWTDecodeError ("Missing claim: identity" )
99+ if identity_claim not in data :
100+ raise JWTDecodeError ("Missing claim: {}" . format ( identity_claim ) )
98101 if 'type' not in data or data ['type' ] not in ('refresh' , 'access' ):
99102 raise JWTDecodeError ("Missing or invalid claim: type" )
100103 if data ['type' ] == 'access' :
0 commit comments