Skip to content

Commit 02374f4

Browse files
djw8605jpadilla
authored andcommitted
Fix bug if application does not specify audience (jpadilla#336)
* Fix bug if application does not specify audience * Update changelog * Fixing blank line * Fixing error message with missing audience
1 parent 9d98078 commit 02374f4

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

AUTHORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ Patches and Suggestions
2525
- Michael Davis <mike.philip.davis@gmail.com> <mike.davis@workiva.com>
2626

2727
- Vinod Gupta <codervinod@gmail.com>
28+
29+
- Derek Weitzel <djw8605@gmail.com>

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
### Fixed
1212

13+
- Audience parameter throws `InvalidAudienceError` when application does not specify an audience, but the token does. [#336][336]
14+
1315
### Added
1416

1517
[v1.6.0][1.6.0]
@@ -222,3 +224,4 @@ rarely used. Users affected by this should upgrade to 3.3+.
222224
[315]: https://github.com/jpadilla/pyjwt/pull/315
223225
[316]: https://github.com/jpadilla/pyjwt/pull/316
224226
[7c1e61d]: https://github.com/jpadilla/pyjwt/commit/7c1e61dde27bafe16e7d1bb6e35199e778962742
227+
[336]: https://github.com/jpadilla/pyjwt/pull/336

jwt/api_jwt.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ def _validate_aud(self, payload, audience):
168168
# verified since the token does not contain a claim.
169169
raise MissingRequiredClaimError('aud')
170170

171+
if audience is None and 'aud' in payload:
172+
# Application did not specify an audience, but
173+
# the token has the 'aud' claim
174+
raise InvalidAudienceError('Invalid audience')
175+
171176
audience_claims = payload['aud']
172177

173178
if isinstance(audience_claims, string_types):

tests/test_api_jwt.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ def test_check_audience_list_when_valid(self, jwt):
288288
token = jwt.encode(payload, 'secret')
289289
jwt.decode(token, 'secret', audience=['urn:you', 'urn:me'])
290290

291+
def test_check_audience_none_specified(self, jwt):
292+
payload = {
293+
'some': 'payload',
294+
'aud': 'urn:me'
295+
}
296+
token = jwt.encode(payload, 'secret')
297+
with pytest.raises(InvalidAudienceError):
298+
jwt.decode(token, 'secret')
299+
291300
def test_raise_exception_invalid_audience_list(self, jwt):
292301
payload = {
293302
'some': 'payload',

0 commit comments

Comments
 (0)