Skip to content

Commit de48278

Browse files
committed
Merge branch 'aramboi-master'
2 parents 8a31646 + 59dc999 commit de48278

File tree

7 files changed

+55
-7
lines changed

7 files changed

+55
-7
lines changed

README.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,22 @@ Installation
2929
Usage
3030
=====
3131

32-
Add the following lines to your Django settings.py file:
32+
Add the following lines to your Django ``settings.py`` file:
3333

3434
.. code-block:: python
3535
3636
COGNITO_AWS_REGION = '<aws region>' # 'eu-central-1'
37-
   COGNITO_USER_POOL = '<user pool>' # 'eu-central-1_xYzaq'
38-
   COGNITO_AUDIENCE = '<client id>'
37+
COGNITO_USER_POOL = '<user pool>' # 'eu-central-1_xYzaq'
38+
COGNITO_AUDIENCE = '<client id>'
39+
40+
(Optional) If you want to cache the Cognito public keys between requests you can
41+
enable the ``COGNITO_PUBLIC_KEYS_CACHING_ENABLED`` setting (it only works if you
42+
have the Django ``CACHES`` setup to anything other than the dummy backend).
43+
44+
.. code-block:: python
45+
46+
COGNITO_PUBLIC_KEYS_CACHING_ENABLED = True
47+
COGNITO_PUBLIC_KEYS_CACHING_TIMEOUT = 60*60*24 # 24h caching, default is 300s
3948
4049
Also update the rest framework settings to use the correct authentication backend:
4150

docs/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,3 @@
349349
# If true, do not generate a @detailmenu in the "Top" node's menu.
350350
#
351351
# texinfo_no_detailmenu = False
352-

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ testpaths = tests
77
universal=1
88

99
[flake8]
10-
max-line-length = 99
10+
max-line-length = 119
1111

1212
[bumpversion]
1313
current_version = 0.0.1

src/django_cognito_jwt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
__version__ = '0.0.1'
22

3-
from .backend import JSONWebTokenAuthentication
3+
from .backend import JSONWebTokenAuthentication # noqa

src/django_cognito_jwt/validator.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import requests
44
from jwt.algorithms import RSAAlgorithm
55

6+
from django.conf import settings
7+
from django.core.cache import cache
68
from django.utils.functional import cached_property
79

810

@@ -34,7 +36,17 @@ def _get_public_key(self, token):
3436
except jwt.DecodeError as exc:
3537
raise TokenError(str(exc))
3638

37-
jwk_data = self._json_web_keys.get(headers['kid'])
39+
if getattr(settings, 'COGNITO_PUBLIC_KEYS_CACHING_ENABLED', False):
40+
cache_key = 'django_cognito_jwt:%s' % headers['kid']
41+
jwk_data = cache.get(cache_key)
42+
43+
if not jwk_data:
44+
jwk_data = self._json_web_keys.get(headers['kid'])
45+
timeout = getattr(settings, 'COGNITO_PUBLIC_KEYS_CACHING_TIMEOUT', 300)
46+
cache.set(cache_key, jwk_data, timeout=timeout)
47+
else:
48+
jwk_data = self._json_web_keys.get(headers['kid'])
49+
3850
if jwk_data:
3951
return RSAAlgorithm.from_jwk(jwk_data)
4052

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def pytest_configure():
3030
ROOT_URLCONF='urls',
3131
)
3232

33+
3334
def _private_to_public_key(private_key):
3435
data = copy.deepcopy(private_key)
3536
del data['d']
@@ -62,6 +63,7 @@ def jwk_private_key_one():
6263
)
6364
}
6465

66+
6567
@pytest.fixture()
6668
def jwk_public_key_one(jwk_private_key_one):
6769
return _private_to_public_key(jwk_private_key_one)

tests/test_validator.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,29 @@ def test_validate_token_error_aud(cognito_well_known_keys, jwk_private_key_one):
4141

4242
with pytest.raises(validator.TokenError):
4343
auth.validate(token)
44+
45+
46+
@pytest.mark.parametrize("is_cache_enabled,responses_calls", [
47+
(None, 2),
48+
(False, 2),
49+
(True, 1),
50+
])
51+
def test_validate_token_caching(cognito_well_known_keys, jwk_private_key_one, settings, responses, is_cache_enabled,
52+
responses_calls):
53+
if is_cache_enabled is not None:
54+
settings.COGNITO_PUBLIC_KEYS_CACHING_ENABLED = is_cache_enabled
55+
56+
token = create_jwt_token(
57+
jwk_private_key_one,
58+
{
59+
'iss': 'https://cognito-idp.eu-central-1.amazonaws.com/bla',
60+
'aud': 'my-audience',
61+
'sub': 'username',
62+
})
63+
auth = validator.TokenValidator('eu-central-1', 'bla', 'my-audience')
64+
auth.validate(token)
65+
assert len(responses.calls) == 1
66+
67+
auth_again = validator.TokenValidator('eu-central-1', 'bla', 'my-audience')
68+
auth_again.validate(token)
69+
assert len(responses.calls) == responses_calls

0 commit comments

Comments
 (0)