-
-
Notifications
You must be signed in to change notification settings - Fork 247
Ignoring OPTIONS method in jwt_required (fix #119) #120
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
Conversation
vimalloc
left a comment
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.
Got some minor nit-picks here, but all in all this looks great! Most of these relate to the fact that flask allows you to create your own OPTIONS method, and even though I'm sure no one actually does that, because it is something supported by flask I want to make sure this extension doesn't break that.
Thanks for contributing! 👍
| if not verify_token_claims(jwt_data[config.user_claims_key]): | ||
| raise UserClaimsVerificationError('User claims verification failed') | ||
| _load_user(jwt_data[config.identity_claim_key]) | ||
| return fn(*args, **kwargs) |
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.
If we don't return fn(*args, **kwargs) in an else block here, this will break if someone is creating their own OPTIONS endpoint via someting like @app.route('/foo', methods=['GET', 'OPTIONS']). I doubt anyone is doing that, but because flask allows this to happen, I would rather get that fixed up anyways, just in case :)
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.
You're right! I didn't mean to indent also return fn(*args, **kwargs)!
| raise UserClaimsVerificationError('User claims verification failed') | ||
| _load_user(jwt_data[config.identity_claim_key]) | ||
| return fn(*args, **kwargs) | ||
| if request.method not in config.exempt_methods: |
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.
Could we add this same logic to the other view decorators in this file as well, for completeness sake? I'm alright with having the unit tests be only for the @jwt_required decorator (unless you feel like adding unit tests for the other decorators as well, in which case 👍 👍)
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.
Yea! I'll add it also in fresh_jwt_required and jwt_refresh_token_required! I'm not sure if we need to implement it in jwt_optional, since it should works well!
I'll wrote more tests anyway :D
tests/test_options_method.py
Outdated
| app.config['JWT_SECRET_KEY'] = 'secret' | ||
| JWTManager(app) | ||
|
|
||
| protected_bp = Blueprint('protected', __name__) |
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.
I don't think you need to check this with a blueprint here. Testing it with just an app should be sufficient and a bit cleaner to read.
tests/test_options_method.py
Outdated
|
|
||
| def test_access_protected_enpoint_options(app): | ||
| client = app.test_client() | ||
| assert client.options('/protected').status_code == 200 # test fails |
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.
Could we add one more test here for creating a custom OPTIONS endpiont, and verifying that the data returned is the returned value of that endpoint? Not likely that this is happening in real codebases, but I want to make sure we don't break flask functionality with this extension, and that is something flask allows you to do.
flask_jwt_extended/config.py
Outdated
|
|
||
| @property | ||
| def exempt_methods(self): | ||
| return set(["OPTIONS"]) |
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.
We don't need to support python2.6, so you could write this as the set literal (ex: {'foo'}) instead.
`jwt_refresh_token_required`, improve and add tests
|
Ok! I've improved the code according to the review! |
|
Great, thanks! I'll get a new released pushed out with these changes later today 👍 |
This shoud fix #119!
All test passes!