-
-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added JWT authentication. #537
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,7 +182,7 @@ def jwt_authenticator_name(): | |
def json_web_token(request, response, verify_token, jwt_secret): | ||
"""JWT verification | ||
|
||
Checks for the Authorization header and verifies it using the verify_token function | ||
Checks for the Authorization header and verifies it using the verify_token function. | ||
""" | ||
authorization = request.get_header('Authorization') | ||
if authorization: | ||
|
@@ -197,13 +197,9 @@ def verify_jwt(authorization, response, jwt_secret): | |
try: | ||
token = authorization.split(' ')[1] | ||
decoding = jwt.decode(token, jwt_secret, algorithm='HS256') | ||
print(decoding) | ||
return decoding['user_id'] | ||
except Exception as ex: | ||
template = "An exception of type {0} occurred. Arguments:\n{1!r}" | ||
print(template.format(type(ex).__name__, ex.args)) | ||
except: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you catching all exceptions ? 😮 Wouldn't this make things hard to debug ? Isn't there a specific exception that can be caught ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it would, I'll go figure out which one/s I'm expecting to catch when the token is no longer valid. Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @OGKevin If an exception is thrown that isn't caught here what happens to the server? Does it continue running and print and error message, or does it break? Where is the code that handles the exceptions that get bubbled up? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dsmurrell The individual request will die, but the server itself will keep running, if you have an @hug.exception handler you can manage how that exception is handled |
||
return False | ||
return None | ||
|
||
def new_jwt(user_id, token_expiration_seconds, jwt_secret): | ||
return jwt.encode({'user_id': user_id, | ||
|
@@ -220,10 +216,6 @@ def refresh_jwt(authorization, token_refresh_seconds, token_expiration_seconds, | |
return jwt.encode({'user_id': decoding['user_id'], | ||
'exp': datetime.utcnow() + timedelta(seconds=token_expiration_seconds)}, | ||
jwt_secret, algorithm='HS256').decode("utf-8") | ||
else: | ||
return None | ||
except Exception as ex: | ||
template = "An exception of type {0} occurred. Arguments:\n{1!r}" | ||
print(template.format(type(ex).__name__, ex.args)) | ||
except: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above ☝️ |
||
return None | ||
# END - JWT AUTH # |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally there will be 2 new lines between function and classes and one new line between class methods. Please add a new line.