Skip to content

Commit 56f157c

Browse files
committed
jwt compatability
1 parent 03b0d12 commit 56f157c

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

lambdas/id_sync/poetry.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lambdas/id_sync/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ coverage = "^7.8.0"
2727
redis = "^4.6.0"
2828
jwt = "^1.4.0"
2929
cache = "^1.0.3"
30+
pyjwt = "^2.10.1"
3031

3132
[tool.poetry.group.dev.dependencies]
3233
coverage = "^7.8.0"

lambdas/shared/src/common/authentication.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from .cache import Cache
1010
from common.models.errors import UnhandledResponseError
11+
from common.clients import logger
1112

1213

1314
class Service(Enum):
@@ -37,7 +38,9 @@ def get_service_secrets(self):
3738
return secret_object
3839

3940
def create_jwt(self, now: int):
41+
logger.info("create_jwt")
4042
secret_object = self.get_service_secrets()
43+
logger.info(f"Secret object: {secret_object}")
4144
claims = {
4245
"iss": secret_object['api_key'],
4346
"sub": secret_object['api_key'],
@@ -46,17 +49,45 @@ def create_jwt(self, now: int):
4649
"exp": now + self.expiry,
4750
"jti": str(uuid.uuid4())
4851
}
49-
return jwt.encode(claims, secret_object['private_key'], algorithm='RS512',
50-
headers={"kid": secret_object['kid']})
52+
logger.info(f"JWT claims: {claims}")
53+
# ✅ Version-compatible JWT encoding
54+
try:
55+
# PyJWT 2.x
56+
return jwt.encode(
57+
claims,
58+
secret_object['private_key'],
59+
algorithm='RS512',
60+
headers={"kid": secret_object['kid']}
61+
)
62+
except TypeError:
63+
# PyJWT 1.x (older versions return bytes)
64+
token = jwt.encode(
65+
claims,
66+
secret_object['private_key'],
67+
algorithm='RS512',
68+
headers={"kid": secret_object['kid']}
69+
)
70+
# Convert bytes to string if needed
71+
return token.decode('utf-8') if isinstance(token, bytes) else token
5172

5273
def get_access_token(self):
74+
logger.info("get_access_token")
5375
now = int(time.time())
76+
logger.info(f"Current time: {now}, Expiry time: {now + self.expiry}")
77+
# Check if token is cached and not expired
78+
logger.info(f"Cache key: {self.cache_key}")
79+
logger.info("Checking cache for access token")
5480
cached = self.cache.get(self.cache_key)
81+
logger.info(f"Cached token: {cached}")
5582
if cached and cached["expires_at"] > now:
83+
logger.info("Returning cached access token")
5684
return cached["token"]
5785

86+
logger.info("No valid cached token found, creating new token")
5887
_jwt = self.create_jwt(now)
5988

89+
logger.info(f"JWT created: {_jwt}")
90+
6091
headers = {
6192
'Content-Type': 'application/x-www-form-urlencoded'
6293
}

lambdas/shared/src/common/pds_service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
class PdsService:
1010
def __init__(self, authenticator: AppRestrictedAuth, environment):
11+
logger.info(f"PdsService init: {environment}")
1112
self.authenticator = authenticator
1213

1314
self.base_url = f"https://{environment}.api.service.nhs.uk/personal-demographics/FHIR/R4/Patient" \
@@ -17,6 +18,7 @@ def __init__(self, authenticator: AppRestrictedAuth, environment):
1718

1819
def get_patient_details(self, patient_id) -> dict | None:
1920
logger.info(f"PDS. Get patient details for ID: {patient_id}")
21+
logger.info("PDS. Getting access token")
2022
access_token = self.authenticator.get_access_token()
2123
logger.info(f"PDS. Access token: {access_token}")
2224
request_headers = {

0 commit comments

Comments
 (0)