1414 default_unauthorized_callback , default_needs_fresh_token_callback ,
1515 default_revoked_token_callback , default_user_loader_error_callback ,
1616 default_claims_verification_callback , default_verify_claims_failed_callback ,
17- default_decode_key_callback
17+ default_decode_key_callback , default_encode_key_callback
1818)
1919from flask_jwt_extended .tokens import (
2020 encode_refresh_token , encode_access_token
@@ -55,6 +55,7 @@ def __init__(self, app=None):
5555 self ._claims_verification_callback = default_claims_verification_callback
5656 self ._verify_claims_failed_callback = default_verify_claims_failed_callback
5757 self ._decode_key_callback = default_decode_key_callback
58+ self ._encode_key_callback = default_encode_key_callback
5859
5960 # Register this extension with the flask app now (if it is provided)
6061 if app is not None :
@@ -385,16 +386,34 @@ def decode_key_loader(self, callback):
385386 This decorator sets the callback function for getting the JWT decode key and
386387 can be used to dynamically choose the appropriate decode key based on token
387388 contents.
388- The default implementation returns the decode key from config (either
389- `JWT_SECRET_KEY` or `JWT_PUBLIC_KEY` depending on signing algorithm).
389+
390+ The default implementation returns the decode key specified by
391+ `JWT_SECRET_KEY` or `JWT_PUBLIC_KEY`, depending on the signing algorithm.
390392
391393 *HINT*: The callback function must be a function that takes only **one** argument,
392- which is a dictionary of the claims encoded in the JWT and must return a *string*
394+ which is the unverified claims of the jwt (dictionary) and must return a *string*
393395 which is the decode key to verify the token.
394396 """
395397 self ._decode_key_callback = callback
396398 return callback
397399
400+ def encode_key_loader (self , callback ):
401+ """
402+ This decorator sets the callback function for getting the JWT encode key and
403+ can be used to dynamically choose the appropriate encode key based on the
404+ token identity.
405+
406+ The default implementation returns the encode key specified by
407+ `JWT_SECRET_KEY` or `JWT_PRIVATE_KEY`, depending on the signing algorithm.
408+
409+ *HINT*: The callback function must be a function that takes only **one**
410+ argument, which is the identity as passed into the create_access_token
411+ or create_refresh_token functions, and must return a *string* which is
412+ the decode key to verify the token.
413+ """
414+ self ._encode_key_callback = callback
415+ return callback
416+
398417 def _create_refresh_token (self , identity , expires_delta = None ):
399418 if expires_delta is None :
400419 expires_delta = config .refresh_expires
@@ -406,7 +425,7 @@ def _create_refresh_token(self, identity, expires_delta=None):
406425
407426 refresh_token = encode_refresh_token (
408427 identity = self ._user_identity_callback (identity ),
409- secret = config . encode_key ,
428+ secret = self . _encode_key_callback ( identity ) ,
410429 algorithm = config .algorithm ,
411430 expires_delta = expires_delta ,
412431 user_claims = user_claims ,
@@ -423,7 +442,7 @@ def _create_access_token(self, identity, fresh=False, expires_delta=None):
423442
424443 access_token = encode_access_token (
425444 identity = self ._user_identity_callback (identity ),
426- secret = config . encode_key ,
445+ secret = self . _encode_key_callback ( identity ) ,
427446 algorithm = config .algorithm ,
428447 expires_delta = expires_delta ,
429448 fresh = fresh ,
0 commit comments