Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit 0e3afe6

Browse files
authored
Merge pull request #226 from umccr/feature/handle-access-token-collection
Handle access token collection
2 parents 6922e5c + 6b6cabd commit 0e3afe6

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

deploy/cttso-ica-to-pieriandx-cdk/lambdas/layers/lambda_utils/globals.py

+2
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,5 @@ class SampleType(Enum):
192192

193193

194194
NTC_SUBJECT_ID = "SBJ00006"
195+
196+
JWT_EXPIRY_BUFFER = 60 # 1 minute

deploy/cttso-ica-to-pieriandx-cdk/lambdas/layers/lambda_utils/pieriandx_helpers.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66

77
import os
88
import re
9+
from datetime import datetime
910
from typing import Tuple, Dict, List, Union
1011

1112
from mypy_boto3_lambda import LambdaClient
1213
from pyriandx.client import Client
1314
import json
1415
import pandas as pd
1516
import time
17+
import jwt
18+
from jwt import DecodeError
19+
1620

1721
from pyriandx.utils import retry_session
1822

1923
from .globals import \
2024
PIERIANDX_CDK_SSM_LIST, \
2125
PIERIANDX_CDK_SSM_PATH, \
2226
MAX_ATTEMPTS_GET_CASES, LIST_CASES_RETRY_TIME, \
23-
PanelType, SampleType, PIERIANDX_USER_AUTH_TOKEN_LAMBDA_PATH
27+
PanelType, SampleType, PIERIANDX_USER_AUTH_TOKEN_LAMBDA_PATH, JWT_EXPIRY_BUFFER
2428

2529
from .miscell import \
2630
change_case
@@ -76,7 +80,7 @@ def get_pieriandx_env_vars() -> Tuple:
7680
output_dict[env_var] = parameter_value
7781

7882
# Set PIERIANDX_USER_AUTH_TOKEN based on secret
79-
if "PIERIANDX_USER_AUTH_TOKEN" in os.environ:
83+
if "PIERIANDX_USER_AUTH_TOKEN" in os.environ and jwt_is_valid(os.environ["PIERIANDX_USER_AUTH_TOKEN"]):
8084
# Already here!
8185
output_dict["PIERIANDX_USER_AUTH_TOKEN"] = os.environ["PIERIANDX_USER_AUTH_TOKEN"]
8286
else:
@@ -91,8 +95,12 @@ def get_pieriandx_env_vars() -> Tuple:
9195
InvocationType="RequestResponse"
9296
)
9397
auth_token_resp = response['Payload'].read().decode('utf-8')
98+
if auth_token_resp is None or auth_token_resp == 'null' or json.loads(auth_token_resp).get("auth_token") is None:
99+
logger.info("Could not get valid auth token from lambda, trying again in five seconds")
100+
time.sleep(5)
94101

95102
output_dict["PIERIANDX_USER_AUTH_TOKEN"] = json.loads(auth_token_resp).get("auth_token")
103+
os.environ["PIERIANDX_USER_AUTH_TOKEN"] = output_dict["PIERIANDX_USER_AUTH_TOKEN"]
96104

97105
return (
98106
output_dict.get("PIERIANDX_USER_EMAIL"),
@@ -479,3 +487,25 @@ def get_pieriandx_status_for_missing_sample(case_id: str) -> pd.Series:
479487
case_dict["pieriandx_report_status"] = report["status"]
480488

481489
return pd.Series(case_dict)
490+
491+
492+
def decode_jwt(jwt_string: str) -> Dict:
493+
return jwt.decode(
494+
jwt_string,
495+
algorithms=["HS256"],
496+
options={"verify_signature": False}
497+
)
498+
499+
500+
def jwt_is_valid(jwt_string: str) -> bool:
501+
try:
502+
decode_jwt(jwt_string)
503+
timestamp_exp = decode_jwt(jwt_string).get("exp")
504+
505+
# If timestamp will expire in less than one minute's time, return False
506+
if int(timestamp_exp) < (int(datetime.now().timestamp()) + JWT_EXPIRY_BUFFER):
507+
return False
508+
else:
509+
return True
510+
except DecodeError as e:
511+
return False

deploy/cttso-ica-to-pieriandx-cdk/lambdas/layers/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ pytz==2022.7.1
1515
requests==2.31.0
1616
setuptools==67.2.0
1717
urllib3<2
18+
pyjwt==2.8.0

0 commit comments

Comments
 (0)