Skip to content

Commit

Permalink
update JWT usage to reflect flask-jwt-extended changes
Browse files Browse the repository at this point in the history
- lib had breaking changes in 4.0.0
  • Loading branch information
guruofgentoo committed Jul 6, 2021
1 parent 63485f3 commit 1cd0895
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
12 changes: 7 additions & 5 deletions keg_auth/libs/authenticators.py
Expand Up @@ -927,24 +927,26 @@ def __init__(self, app):
def user_identity_loader(user):
"""
Serialize a user entity to the JWT token
This method is the complement of `user_loader_callback_loader`
This method is the complement of `user_lookup_loader`
"""
return user.session_key

@jwt_manager.user_loader_callback_loader
def user_loader_callback_loader(session_key):
@jwt_manager.user_lookup_loader
def user_loader_callback_loader(jwt_header, jwt_data):
"""
Load a user entity from the JWT token
This method is the complement of `user_identity_loader`
Note, if user is not found or inactive, fail silently - user just won't get loaded
"""
return self.user_ent.get_by(session_key=session_key, is_active=True)
data_key = flask.current_app.config.get('JWT_IDENTITY_CLAIM')
return self.user_ent.get_by(session_key=jwt_data[data_key], is_active=True)

@staticmethod
def get_authenticated_user():
try:
flask_jwt_extended.verify_jwt_in_request()
if flask_jwt_extended.verify_jwt_in_request() is None:
return None
user = flask_jwt_extended.get_current_user()
flask_login.login_user(user)
return user
Expand Down
2 changes: 1 addition & 1 deletion keg_auth/tests/test_authenticators.py
Expand Up @@ -258,7 +258,7 @@ def test_create_access_token(self):
user = User.testing_create()
jwt_auth = auth.JwtRequestLoader(flask.current_app)
token = jwt_auth.create_access_token(user)
assert flask_jwt_extended.decode_token(token)['identity'] == user.session_key
assert flask_jwt_extended.decode_token(token)['sub'] == user.session_key


class TestPasswordPolicy:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -51,7 +51,7 @@
'email_validator',
'flake8',
'flask-bootstrap',
'flask-jwt-extended<4.0',
'flask-jwt-extended>=4.0.0',
'flask-mail',
'flask-oidc',
'flask-webtest',
Expand All @@ -70,7 +70,7 @@
'webgrid[i18n]'
],
'jwt': [
'flask-jwt-extended',
'flask-jwt-extended>=4.0.0',
],
'ldap': [
'python-ldap',
Expand Down

0 comments on commit 1cd0895

Please sign in to comment.