Skip to content

Commit f300015

Browse files
committed
Add spec and docs for JWT_DECODE_AUDIENCE being a list
Refs #219
1 parent b4ab25a commit f300015

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

docs/options.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ General Options:
4444
``JWT_ERROR_MESSAGE_KEY`` The key of the error message in a JSON error response when using
4545
the default error handlers.
4646
Defaults to ``'msg'``.
47-
``JWT_DECODE_AUDIENCE`` The audience you expect in a JWT when decoding it.
48-
If this option differs from the 'aud' claim in a JWT, the ``'invalid_token_callback'`` is invoked.
47+
``JWT_DECODE_AUDIENCE`` The audience or list of audiences you expect in a JWT when decoding it.
48+
The ``'invalid_token_callback'`` is invoked when a JWTs audience is invalid.
4949
Defaults to ``'None'``.
5050
``JWT_DECODE_LEEWAY`` Define the leeway part of the expiration time definition, which
5151
means you can validate an expiration time which is in the past but

tests/test_decode_tokens.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,23 @@ def get_decode_key_1(claims, headers):
229229
decode_token(token)
230230

231231

232-
def test_valid_aud(app, default_access_token):
232+
@pytest.mark.parametrize("token_aud", ['foo', ['bar'], ['foo', 'bar', 'baz']])
233+
def test_valid_aud(app, default_access_token, token_aud):
234+
app.config['JWT_DECODE_AUDIENCE'] = ['foo', 'bar']
235+
236+
default_access_token['aud'] = token_aud
237+
invalid_token = encode_token(app, default_access_token)
238+
with app.test_request_context():
239+
decoded = decode_token(invalid_token)
240+
assert decoded['aud'] == token_aud
241+
242+
243+
@pytest.mark.parametrize("token_aud", ['bar', ['bar'], ['bar', 'baz']])
244+
def test_invalid_aud(app, default_access_token, token_aud):
233245
app.config['JWT_DECODE_AUDIENCE'] = 'foo'
234246

235-
default_access_token['aud'] = 'bar'
247+
default_access_token['aud'] = token_aud
236248
invalid_token = encode_token(app, default_access_token)
237249
with pytest.raises(InvalidAudienceError):
238250
with app.test_request_context():
239251
decode_token(invalid_token)
240-
241-
default_access_token['aud'] = 'foo'
242-
valid_token = encode_token(app, default_access_token)
243-
with app.test_request_context():
244-
decoded = decode_token(valid_token)
245-
assert decoded['aud'] == 'foo'

0 commit comments

Comments
 (0)