Skip to content

Commit 5c12134

Browse files
committed
Cache JWKSClient property
The JWKSClient caches the keys from the given discovery keys endpoint, so this property should also be cached on the Authentication instance. Cache a couple of other properties we only need to create once per instance.
1 parent 6230bbc commit 5c12134

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

manage_breast_screening/dicom/authentication.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import os
3+
from functools import cached_property
34

45
import jwt
56
from django.conf import settings
@@ -28,10 +29,10 @@ def _decode(self, token: str) -> dict | None:
2829
Checks the signature, audience, and issuer claims to ensure the token is valid and intended for this API.
2930
"""
3031
try:
31-
signing_key = self.jwks_client.get_signing_key_from_jwt(token).key
32+
signing_key = self.jwks_client.get_signing_key_from_jwt(token)
3233
payload = jwt.decode(
3334
token,
34-
signing_key,
35+
signing_key.key,
3536
algorithms=ALLOWED_ALGORITHMS,
3637
audience=self.audience,
3738
issuer=self.issuers,
@@ -48,7 +49,7 @@ def _decode(self, token: str) -> dict | None:
4849
except Exception:
4950
logger.exception("Unable to parse authentication token.")
5051

51-
@property
52+
@cached_property
5253
def jwks_client(self) -> jwt.PyJWKClient:
5354
"""
5455
Creates a PyJWKClient instance for fetching and caching the JWKS keys from Azure AD.
@@ -62,7 +63,7 @@ def jwks_client(self) -> jwt.PyJWKClient:
6263
lifespan=JWT_SET_CACHE_TTL_SECONDS,
6364
)
6465

65-
@property
66+
@cached_property
6667
def discovery_keys_url(self) -> str:
6768
return f"https://login.microsoftonline.com/{self.tenant_id}/discovery/v2.0/keys"
6869

@@ -80,7 +81,7 @@ def tenant_id(self) -> str | None:
8081
"""
8182
return os.getenv("TENANT_ID", "")
8283

83-
@property
84+
@cached_property
8485
def issuers(self) -> list:
8586
"""
8687
The expected issuer claim(s) in the JWT token. This should match the tenant ID and the Azure AD endpoints.

manage_breast_screening/dicom/tests/test_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from pydicom.uid import generate_uid
1010

1111
from manage_breast_screening.core.api import api
12-
from manage_breast_screening.dicom.models import Study
1312
from manage_breast_screening.gateway.models import GatewayActionStatus
1413
from manage_breast_screening.gateway.tests.factories import GatewayActionFactory
1514
from manage_breast_screening.participants.models.appointment import (
@@ -157,7 +156,9 @@ def test_upload_missing_uids(dataset, mock_authentication, appointment_stub):
157156
)
158157

159158

160-
def test_upload_appointment_not_in_progress(dicom_file, mock_authentication, appointment_stub):
159+
def test_upload_appointment_not_in_progress(
160+
dicom_file, mock_authentication, appointment_stub
161+
):
161162
appointment_stub.is_in_progress.return_value = False
162163

163164
with patch(

0 commit comments

Comments
 (0)