88from aws_lambda_typing .events import APIGatewayProxyEventV1
99from fhir .resources .R4B .immunization import Immunization
1010from boto3 import client as boto3_client
11- from clients import logger
1211
13- from authorization import Authorization , UnknownPermission
1412from fhir_repository import ImmunizationRepository , create_table
1513from fhir_service import FhirService , UpdateOutcome , get_service_url
1614from models .errors import (
2422 IdentifierDuplicationError ,
2523 ParameterException ,
2624 UnauthorizedVaxError ,
27- UnauthorizedVaxOnRecordError ,
28- UnauthorizedSystemError ,
2925)
3026from models .utils .generic_utils import check_keys_in_sources
31- from models .utils .permissions import get_supplier_permissions
32- from models .utils .permission_checker import ApiOperationCode , validate_permissions , _expand_permissions
3327from parameter_parser import process_params , process_search_params , create_query_string
3428import urllib .parse
3529
@@ -43,28 +37,23 @@ def make_controller(
4337 endpoint_url = "http://localhost:4566" if immunization_env == "local" else None
4438 imms_repo = ImmunizationRepository (create_table (endpoint_url = endpoint_url ))
4539
46- authorizer = Authorization ()
4740 service = FhirService (imms_repo = imms_repo )
4841
49- return FhirController (authorizer = authorizer , fhir_service = service )
42+ return FhirController (fhir_service = service )
5043
5144
5245class FhirController :
5346 immunization_id_pattern = r"^[A-Za-z0-9\-.]{1,64}$"
5447
5548 def __init__ (
5649 self ,
57- authorizer : Authorization ,
5850 fhir_service : FhirService ,
5951 ):
6052 self .fhir_service = fhir_service
61- self .authorizer = authorizer
6253
6354 def get_immunization_by_identifier (self , aws_event ) -> dict :
6455 try :
6556 if aws_event .get ("headers" ):
66- if response := self .authorize_request (aws_event ):
67- return response
6857 query_params = aws_event .get ("queryStringParameters" , {})
6958 else :
7059 raise UnauthorizedError ()
@@ -88,13 +77,7 @@ def get_immunization_by_identifier(self, aws_event) -> dict:
8877 if id_error := self ._validate_identifier_system (identifier , element ):
8978 return self .create_response (400 , id_error )
9079 identifiers = identifier .replace ("|" , "#" )
91- try :
92- supplier_system = self ._identify_supplier_system (aws_event )
93- imms_vax_type_perms = get_supplier_permissions (supplier_system )
94- if len (imms_vax_type_perms ) == 0 :
95- raise UnauthorizedVaxError ()
96- except UnauthorizedVaxError as unauthorized :
97- return self .create_response (403 , unauthorized .to_operation_outcome ())
80+ supplier_system = self ._identify_supplier_system (aws_event )
9881
9982 try :
10083 if resource := self .fhir_service .get_immunization_by_identifier (
@@ -104,25 +87,17 @@ def get_immunization_by_identifier(self, aws_event) -> dict:
10487 return self .create_response (403 , unauthorized .to_operation_outcome ())
10588
10689 def get_immunization_by_id (self , aws_event ) -> dict :
107- if response := self .authorize_request (aws_event ):
108- return response
109-
11090 imms_id = aws_event ["pathParameters" ]["id" ]
11191 if id_error := self ._validate_id (imms_id ):
11292 return self .create_response (400 , id_error )
11393
11494 try :
11595 if aws_event .get ("headers" ):
11696 supplier_system = self ._identify_supplier_system (aws_event )
117- imms_vax_type_perms = get_supplier_permissions (supplier_system )
118- if len (imms_vax_type_perms ) == 0 :
119- raise UnauthorizedVaxError ()
12097 else :
12198 raise UnauthorizedError ()
12299 except UnauthorizedError as unauthorized :
123100 return self .create_response (403 , unauthorized .to_operation_outcome ())
124- except UnauthorizedVaxError as unauthorized :
125- return self .create_response (403 , unauthorized .to_operation_outcome ())
126101
127102 try :
128103 if resource := self .fhir_service .get_immunization_by_id (imms_id , supplier_system ):
@@ -147,22 +122,18 @@ def get_immunization_by_id(self, aws_event) -> dict:
147122 return self .create_response (403 , unauthorized .to_operation_outcome ())
148123
149124 def create_immunization (self , aws_event ):
150- try :
151- if aws_event .get ("headers" ):
152- if response := self .authorize_request (aws_event ):
153- return response
154- else :
155- raise UnauthorizedError ()
156- except UnauthorizedError as unauthorized :
157- return self .create_response (403 , unauthorized .to_operation_outcome ())
125+ if not aws_event .get ("headers" ):
126+ return self .create_response (
127+ 403 ,
128+ create_operation_outcome (
129+ resource_id = str (uuid .uuid4 ()),
130+ severity = Severity .error ,
131+ code = Code .forbidden ,
132+ diagnostics = "Unauthorized request"
133+ )
134+ )
158135
159- # Call the common method and unpack the results
160- # TODO - can remove this and the block above. Only need supplier system
161- response , imms_vax_type_perms , supplier_system = self .check_vaccine_type_permissions (
162- aws_event
163- )
164- if response :
165- return response
136+ supplier_system = self ._identify_supplier_system (aws_event )
166137
167138 try :
168139 immunisation = json .loads (aws_event ["body" ], parse_float = Decimal )
@@ -194,17 +165,13 @@ def create_immunization(self, aws_event):
194165 def update_immunization (self , aws_event ):
195166 try :
196167 if aws_event .get ("headers" ):
197- if response := self .authorize_request (aws_event ):
198- return response
199168 imms_id = aws_event ["pathParameters" ]["id" ]
200169 else :
201170 raise UnauthorizedError ()
202171 except UnauthorizedError as unauthorized :
203172 return self .create_response (403 , unauthorized .to_operation_outcome ())
204- # Call the common method and unpack the results
205- response , imms_vax_type_perms , supplier_system = self .check_vaccine_type_permissions (aws_event )
206- if response :
207- return response
173+
174+ supplier_system = self ._identify_supplier_system (aws_event )
208175
209176 # Validate the imms id - start
210177 if id_error := self ._validate_id (imms_id ):
@@ -254,11 +221,6 @@ def update_immunization(self, aws_event):
254221 return self .create_response (400 , error .to_operation_outcome ())
255222 # Validate if the imms resource does not exist - end
256223
257- # Check vaccine type permissions on the existing record - start
258- if not validate_permissions (imms_vax_type_perms , ApiOperationCode .UPDATE , [existing_record ["VaccineType" ]]):
259- return self .create_response (403 , UnauthorizedVaxOnRecordError ().to_operation_outcome ())
260- # Check vaccine type permissions on the existing record - end
261-
262224 existing_resource_version = int (existing_record ["Version" ])
263225
264226 try :
@@ -355,8 +317,6 @@ def update_immunization(self, aws_event):
355317 def delete_immunization (self , aws_event ):
356318 try :
357319 if aws_event .get ("headers" ):
358- if response := self .authorize_request (aws_event ):
359- return response
360320 imms_id = aws_event ["pathParameters" ]["id" ]
361321 else :
362322 raise UnauthorizedError ()
@@ -367,11 +327,7 @@ def delete_immunization(self, aws_event):
367327 if id_error := self ._validate_id (imms_id ):
368328 return FhirController .create_response (400 , json .dumps (id_error ))
369329
370- # Call the common method and unpack the results
371- response , imms_vax_type_perms , supplier_system = self .check_vaccine_type_permissions (
372- aws_event )
373- if response :
374- return response
330+ supplier_system = self ._identify_supplier_system (aws_event )
375331
376332 try :
377333 self .fhir_service .delete_immunization (imms_id , supplier_system )
@@ -385,9 +341,6 @@ def delete_immunization(self, aws_event):
385341 return self .create_response (403 , unauthorized .to_operation_outcome ())
386342
387343 def search_immunizations (self , aws_event : APIGatewayProxyEventV1 ) -> dict :
388- if response := self .authorize_request (aws_event ):
389- return response
390-
391344 try :
392345 search_params = process_search_params (process_params (aws_event ))
393346 except ParameterException as e :
@@ -399,16 +352,11 @@ def search_immunizations(self, aws_event: APIGatewayProxyEventV1) -> dict:
399352 try :
400353 if aws_event .get ("headers" ):
401354 supplier_system = self ._identify_supplier_system (aws_event )
402- imms_vax_type_perms = get_supplier_permissions (supplier_system )
403- if len (imms_vax_type_perms ) == 0 :
404- raise UnauthorizedVaxError ()
405355 else :
406356 raise UnauthorizedError ()
407357 except UnauthorizedError as unauthorized :
408358 return self .create_response (403 , unauthorized .to_operation_outcome ())
409- except UnauthorizedVaxError as unauthorized :
410- return self .create_response (403 , unauthorized .to_operation_outcome ())
411- # Check vaxx type permissions on the existing record - start
359+
412360 try :
413361 result , request_contained_unauthorised_vaccs = self .fhir_service .search_immunizations (
414362 search_params .patient_identifier ,
@@ -512,21 +460,6 @@ def _create_bad_request(self, message):
512460 )
513461 return self .create_response (400 , error )
514462
515-
516- def authorize_request (self , aws_event : dict ) -> Optional [dict ]:
517- try :
518- self .authorizer .authorize (aws_event )
519- except UnauthorizedError as e :
520- return self .create_response (403 , e .to_operation_outcome ())
521- except UnknownPermission :
522- id_error = create_operation_outcome (
523- resource_id = str (uuid .uuid4 ()),
524- severity = Severity .error ,
525- code = Code .server_error ,
526- diagnostics = "Application includes invalid authorization values" ,
527- )
528- return self .create_response (500 , id_error )
529-
530463 def fetch_identifier_system_and_element (self , event : dict ):
531464 """
532465 Extracts `identifier` and `_elements` from an incoming FHIR search request.
@@ -605,25 +538,6 @@ def create_response_for_identifier(self, not_required, has_identifier, has_eleme
605538 )
606539 return self .create_response (400 , error )
607540
608- def check_vaccine_type_permissions (self , aws_event ):
609- try :
610- supplier_system = self ._identify_supplier_system (aws_event )
611- if len (supplier_system ) == 0 :
612- raise UnauthorizedSystemError ()
613- imms_vax_type_perms = get_supplier_permissions (supplier_system )
614- logger .info (f" update imms = { imms_vax_type_perms } " )
615- if len (imms_vax_type_perms ) == 0 :
616- raise UnauthorizedVaxError ()
617- # Return the values needed for later use
618- return None , imms_vax_type_perms , supplier_system
619-
620- except UnauthorizedVaxError as unauthorized :
621- return self .create_response (403 , unauthorized .to_operation_outcome ()), None , None
622- except UnauthorizedSystemError as unauthorized :
623- return self .create_response (403 , unauthorized .to_operation_outcome ()), None , None
624- except UnauthorizedError as e :
625- return self ._create_bad_request (str (e )), None , None
626-
627541 @staticmethod
628542 def create_response (status_code , body = None , headers = None ):
629543 if body :
0 commit comments