|
| 1 | +from enums.lambda_error import LambdaError |
| 2 | +from oauthlib.oauth2 import WebApplicationClient |
| 3 | +from services.base.ssm_service import SSMService |
| 4 | +from services.dynamic_configuration_service import DynamicConfigurationService |
| 5 | +from services.get_fhir_document_reference_service import GetFhirDocumentReferenceService |
| 6 | +from services.oidc_service import OidcService |
| 7 | +from services.search_patient_details_service import SearchPatientDetailsService |
| 8 | +from utils.audit_logging_setup import LoggingService |
| 9 | +from utils.decorators.ensure_env_var import ensure_environment_variables |
| 10 | +from utils.decorators.handle_lambda_exceptions import handle_lambda_exceptions |
| 11 | +from utils.decorators.set_audit_arg import set_request_context_for_logging |
| 12 | +from utils.exceptions import AuthorisationException, OidcApiException |
| 13 | +from utils.lambda_exceptions import ( |
| 14 | + GetFhirDocumentReferenceException, |
| 15 | + SearchPatientException, |
| 16 | +) |
| 17 | +from utils.lambda_response import ApiGatewayResponse |
| 18 | + |
| 19 | +logger = LoggingService(__name__) |
| 20 | + |
| 21 | + |
| 22 | +@handle_lambda_exceptions |
| 23 | +@set_request_context_for_logging |
| 24 | +@ensure_environment_variables( |
| 25 | + names=[ |
| 26 | + "APPCONFIG_APPLICATION", |
| 27 | + "APPCONFIG_CONFIGURATION", |
| 28 | + "APPCONFIG_ENVIRONMENT", |
| 29 | + "LLOYD_GEORGE_DYNAMODB_NAME", |
| 30 | + "PRESIGNED_ASSUME_ROLE", |
| 31 | + "CLOUDFRONT_URL", |
| 32 | + ] |
| 33 | +) |
| 34 | +def lambda_handler(event, context): |
| 35 | + try: |
| 36 | + bearer_token = extract_bearer_token(event) |
| 37 | + selected_role_id = event.get("headers", {}).get("cis2-urid", None) |
| 38 | + document_id, snomed_code = extract_document_parameters(event) |
| 39 | + |
| 40 | + get_document_service = GetFhirDocumentReferenceService() |
| 41 | + document_reference = get_document_service.handle_get_document_reference_request( |
| 42 | + snomed_code, document_id |
| 43 | + ) |
| 44 | + |
| 45 | + if selected_role_id: |
| 46 | + verify_user_authorisation( |
| 47 | + bearer_token, selected_role_id, document_reference.nhs_number |
| 48 | + ) |
| 49 | + |
| 50 | + document_reference_response = ( |
| 51 | + get_document_service.create_document_reference_fhir_response( |
| 52 | + document_reference |
| 53 | + ) |
| 54 | + ) |
| 55 | + |
| 56 | + logger.info( |
| 57 | + f"Successfully retrieved document reference for document_id: {document_id}, snomed_code: {snomed_code}" |
| 58 | + ) |
| 59 | + |
| 60 | + return ApiGatewayResponse( |
| 61 | + status_code=200, body=document_reference_response, methods="GET" |
| 62 | + ).create_api_gateway_response() |
| 63 | + |
| 64 | + except GetFhirDocumentReferenceException as exception: |
| 65 | + return ApiGatewayResponse( |
| 66 | + status_code=exception.status_code, |
| 67 | + body=exception.error.create_error_response().create_error_fhir_response( |
| 68 | + exception.error.value.get("fhir_coding") |
| 69 | + ), |
| 70 | + methods="GET", |
| 71 | + ).create_api_gateway_response() |
| 72 | + |
| 73 | + |
| 74 | +def extract_bearer_token(event): |
| 75 | + """Extract and validate bearer token from event""" |
| 76 | + bearer_token = event.get("headers", {}).get("Authorization", None) |
| 77 | + if not bearer_token or not bearer_token.startswith("Bearer "): |
| 78 | + logger.warning("No bearer token found in request") |
| 79 | + raise GetFhirDocumentReferenceException( |
| 80 | + 401, LambdaError.DocumentReferenceUnauthorised |
| 81 | + ) |
| 82 | + return bearer_token |
| 83 | + |
| 84 | + |
| 85 | +def extract_document_parameters(event): |
| 86 | + """Extract document ID and SNOMED code from path parameters""" |
| 87 | + path_params = event.get("pathParameters", {}).get("id", None) |
| 88 | + document_id, snomed_code = get_id_and_snomed_from_path_parameters(path_params) |
| 89 | + |
| 90 | + if not document_id or not snomed_code: |
| 91 | + logger.error("Missing document id or snomed code in request path parameters.") |
| 92 | + raise GetFhirDocumentReferenceException( |
| 93 | + 400, LambdaError.DocumentReferenceInvalidRequest |
| 94 | + ) |
| 95 | + |
| 96 | + return document_id, snomed_code |
| 97 | + |
| 98 | + |
| 99 | +def verify_user_authorisation(bearer_token, selected_role_id, nhs_number): |
| 100 | + """Verify user authorisation for accessing patient data""" |
| 101 | + logger.info("Detected a cis2 user access request, checking for access permission") |
| 102 | + |
| 103 | + try: |
| 104 | + configuration_service = DynamicConfigurationService() |
| 105 | + configuration_service.set_auth_ssm_prefix() |
| 106 | + |
| 107 | + oidc_service = OidcService() |
| 108 | + oidc_service.set_up_oidc_parameters(SSMService, WebApplicationClient) |
| 109 | + userinfo = oidc_service.fetch_userinfo(bearer_token) |
| 110 | + |
| 111 | + org_ods_code = oidc_service.fetch_user_org_code(userinfo, selected_role_id) |
| 112 | + smartcard_role_code, _ = oidc_service.fetch_user_role_code( |
| 113 | + userinfo, selected_role_id, "R" |
| 114 | + ) |
| 115 | + except (OidcApiException, AuthorisationException) as e: |
| 116 | + logger.error(f"Authorization error: {str(e)}") |
| 117 | + raise GetFhirDocumentReferenceException( |
| 118 | + 403, LambdaError.DocumentReferenceUnauthorised |
| 119 | + ) |
| 120 | + |
| 121 | + try: |
| 122 | + search_patient_service = SearchPatientDetailsService( |
| 123 | + smartcard_role_code, org_ods_code |
| 124 | + ) |
| 125 | + search_patient_service.handle_search_patient_request(nhs_number, False) |
| 126 | + except SearchPatientException as e: |
| 127 | + raise GetFhirDocumentReferenceException(e.status_code, e.error) |
| 128 | + |
| 129 | + |
| 130 | +def get_id_and_snomed_from_path_parameters(path_parameters): |
| 131 | + """Extract document ID and SNOMED code from path parameters""" |
| 132 | + if path_parameters: |
| 133 | + params = path_parameters.split("~") |
| 134 | + if len(params) == 2: |
| 135 | + return params[1], params[0] |
| 136 | + return None, None |
0 commit comments