Skip to content

Commit

Permalink
Avoid using hardcoded 'identity'
Browse files Browse the repository at this point in the history
  • Loading branch information
psafont committed Jul 13, 2017
1 parent 3efd522 commit f150fe0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions examples/database_blacklist/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def login():
refresh_token = create_refresh_token(identity=username)

# Store the tokens in our store with a status of not currently revoked.
add_token_to_database(access_token)
add_token_to_database(refresh_token)
add_token_to_database(access_token, app.config['JWT_IDENTITY_CLAIM'])
add_token_to_database(refresh_token, app.config['JWT_IDENTITY_CLAIM'])

ret = {
'access_token': access_token,
Expand All @@ -72,7 +72,7 @@ def refresh():
# Do the same thing that we did in the login endpoint here
current_user = get_jwt_identity()
access_token = create_access_token(identity=current_user)
add_token_to_database(access_token)
add_token_to_database(access_token, app.config['JWT_IDENTITY_CLAIM'])
return jsonify({'access_token': access_token}), 201

# Provide a way for a user to look at their tokens
Expand Down
5 changes: 3 additions & 2 deletions examples/database_blacklist/blacklist_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ def _epoch_utc_to_datetime(epoch_utc):
return datetime.fromtimestamp(epoch_utc)


def add_token_to_database(encoded_token):
def add_token_to_database(encoded_token, identity_claim):
"""
Adds a new token to the database. It is not revoked when it is added.
:param identity_claim:
"""
decoded_token = decode_token(encoded_token)
jti = decoded_token['jti']
token_type = decoded_token['type']
user_identity = decoded_token['identity']
user_identity = decoded_token[identity_claim]
expires = _epoch_utc_to_datetime(decoded_token['exp'])
revoked = False

Expand Down
8 changes: 4 additions & 4 deletions flask_jwt_extended/view_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def jwt_required(fn):
def wrapper(*args, **kwargs):
jwt_data = _decode_jwt_from_request(request_type='access')
ctx_stack.top.jwt = jwt_data
_load_user(jwt_data['identity'])
_load_user(jwt_data[config.identity_claim])
return fn(*args, **kwargs)
return wrapper

Expand All @@ -53,7 +53,7 @@ def wrapper(*args, **kwargs):
try:
jwt_data = _decode_jwt_from_request(request_type='access')
ctx_stack.top.jwt = jwt_data
_load_user(jwt_data['identity'])
_load_user(jwt_data[config.identity_claim])
except NoAuthorizationError:
pass
return fn(*args, **kwargs)
Expand All @@ -77,7 +77,7 @@ def wrapper(*args, **kwargs):
raise FreshTokenRequired('Fresh token required')

ctx_stack.top.jwt = jwt_data
_load_user(jwt_data['identity'])
_load_user(jwt_data[config.identity_claim])
return fn(*args, **kwargs)
return wrapper

Expand All @@ -92,7 +92,7 @@ def jwt_refresh_token_required(fn):
def wrapper(*args, **kwargs):
jwt_data = _decode_jwt_from_request(request_type='refresh')
ctx_stack.top.jwt = jwt_data
_load_user(jwt_data['identity'])
_load_user(jwt_data[config.identity_claim])
return fn(*args, **kwargs)
return wrapper

Expand Down

0 comments on commit f150fe0

Please sign in to comment.