Skip to content

Update tests and documentation to show that multiple audiences are supported #28

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/docs/_build/
/htmlcov/

env/

# Editors
.idea/
.vscode/
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Add the following lines to your Django ``settings.py`` file:

COGNITO_AWS_REGION = '<aws region>' # 'eu-central-1'
COGNITO_USER_POOL = '<user pool>' # 'eu-central-1_xYzaq'
COGNITO_AUDIENCE = '<client id>'
COGNITO_AUDIENCE = '<client id>' # or = ['<client id 1>', 'client id 2', ...]

(Optional) If you want to cache the Cognito public keys between requests you can
enable the ``COGNITO_PUBLIC_KEYS_CACHING_ENABLED`` setting (it only works if you
Expand Down
1 change: 1 addition & 0 deletions src/django_cognito_jwt/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TokenValidator:
def __init__(self, aws_region, aws_user_pool, audience):
self.aws_region = aws_region
self.aws_user_pool = aws_user_pool
#should be either a single audience string, or an array of audience strings
self.audience = audience

@cached_property
Expand Down
28 changes: 28 additions & 0 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ def test_validate_token(cognito_well_known_keys, jwk_private_key_one):
auth.validate(token)


def test_validate_token_multiple_aud(cognito_well_known_keys, jwk_private_key_one):
token = create_jwt_token(
jwk_private_key_one,
{
"iss": "https://cognito-idp.eu-central-1.amazonaws.com/bla",
"aud": "my-audience-2",
"sub": "username",
},
)
auth = validator.TokenValidator("eu-central-1", "bla", ["my-audience", "my-audience-2", "my-audience-3"])
auth.validate(token)


def test_validate_token_error_key(cognito_well_known_keys, jwk_private_key_two):
token = create_jwt_token(
jwk_private_key_two,
Expand Down Expand Up @@ -46,6 +59,21 @@ def test_validate_token_error_aud(cognito_well_known_keys, jwk_private_key_one):
auth.validate(token)


def test_validate_token_multiple_aud_error_aud(cognito_well_known_keys, jwk_private_key_one):
token = create_jwt_token(
jwk_private_key_one,
{
"iss": "https://cognito-idp.eu-central-1.amazonaws.com/bla",
"aud": "other-audience",
"sub": "username",
},
)
auth = validator.TokenValidator("eu-central-1", "bla", ["my-audience", "my-audience-2", "my-audience-3"])

with pytest.raises(validator.TokenError):
auth.validate(token)


@pytest.mark.parametrize(
"is_cache_enabled,responses_calls", [(None, 2), (False, 2), (True, 1)]
)
Expand Down