Skip to content
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

Check presence among claims #98

Merged
merged 1 commit into from
Apr 28, 2019
Merged

Check presence among claims #98

merged 1 commit into from
Apr 28, 2019

Conversation

shenek
Copy link
Contributor

@shenek shenek commented May 30, 2018

Right now if you e.g. trying to validate that subject matches a name and the subject is missing in the claim, the validation passes.

>>> token = jwt.encode(algorithm='HS256', claims={'a': 'b'}, key='secret')
>>> jwt.decode(algorithms=['HS256'], key='secret', subject= 'my_sub', token=token)
>>> {'a': 'b'}

This patch adds options to suppress such behaviour.

>>> token = jwt.encode(algorithm='HS256', claims={'a': 'b'}, key='secret')
>>> jwt.decode(algorithms=['HS256'], key='secret', subject= 'my_sub', token=token, options={'present_sub': True})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/<my_venvs>/JWT/lib/python3.6/site-packages/jose/jwt.py", line 168, in decode
    options=defaults)
  File "/<my_venvs>/JWT/lib/python3.6/site-packages/jose/jwt.py", line 469, in _validate_claims
    raise JWTError('missing required key "%s" among claims' % required_claim)
jose.exceptions.JWTError: missing required key "sub" among claims

@codecov-io
Copy link

codecov-io commented May 30, 2018

Codecov Report

Merging #98 into master will decrease coverage by 13.1%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master      #98       +/-   ##
===========================================
- Coverage   96.54%   83.44%   -13.11%     
===========================================
  Files          14       14               
  Lines        1071     1075        +4     
===========================================
- Hits         1034      897      -137     
- Misses         37      178      +141
Impacted Files Coverage Δ
jose/jwt.py 100% <100%> (ø) ⬆️
jose/backends/__init__.py 36.36% <0%> (-63.64%) ⬇️
jose/backends/ecdsa_backend.py 61.33% <0%> (-37.34%) ⬇️
jose/backends/pycrypto_backend.py 58.47% <0%> (-37.29%) ⬇️
jose/backends/rsa_backend.py 59.44% <0%> (-37.07%) ⬇️
jose/utils.py 71.92% <0%> (-15.79%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1c3c1a6...ec4ad99. Read the comment docs.

@zejn
Copy link
Collaborator

zejn commented Apr 15, 2019

Hi, @shenek!

I'm sorry to come back to this after such a long time.

I find your contribution generally sensible. I'd change the "present" word into something less ambiguous - different people might read "present" as either presence or presentation (or even a gift), so I think changing this to "require" might make it a bit more obvious what is meant.

Additionally, if a require_claim options is set, the verify_claim option should automatically be set too.

The pull request should also add an entry to changelog.

Note that if you don't have time to look into this again after all this time, I'll totally understand.

@shenek shenek force-pushed the present_check branch 4 times, most recently from ebd206e to 6c0c8f1 Compare April 26, 2019 14:52
@shenek
Copy link
Contributor Author

shenek commented Apr 26, 2019

Hi,

I tried to resolve your notes. I hope that it is fine now.

Cheers.

@zejn
Copy link
Collaborator

zejn commented Apr 27, 2019

Thanks, @shenek, this is pretty awesome.

@zejn zejn merged commit 739f44f into mpdavis:master Apr 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants