def store_ssr(ssr_entry=None, endpoint="store_ssr()"): if ssr_entry is None: raise AttributeError("Provide ssr_entry as parameter") # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) try: # SSR Insert cursor = ssr_entry.to_db(cursor=cursor) db.connection.commit() except Exception as exp: logger.error('Slr and Ssr commit failed: ' + repr(exp)) db.connection.rollback() logger.error('--> rollback') logger.error("ssr_entry: " + ssr_entry.log_entry) raise ApiError(code=500, title="Failed to store slr and ssr", detail=repr(exp), source=endpoint) else: logger.debug('Ssr commited') logger.debug("ssr_entry: " + ssr_entry.log_entry) return ssr_entry
def sign_csr(account_id=None, payload=None, endpoint="sign_csr(account_id, payload, endpoint)"): if account_id is None: raise AttributeError("Provide account_id as parameter") if payload is None: raise AttributeError("Provide ssr_payload as parameter") logger.info("Signing Service Link Status Record") # Sign csr try: csr_signed_json = generate_and_sign_jws(account_id=account_id, jws_payload=json.dumps(payload)) except Exception as exp: logger.error('Could not create Consent Status Record: ' + repr(exp)) raise ApiError(code=500, title="Failed to create Consent Status Record", detail=repr(exp), source=endpoint) else: logger.info('Consent Status Record created and signed') logger.debug('csr_signed_json: ' + csr_signed_json) try: logger.info("Converting signed CSR from json to dict") csr_signed_dict = json.loads(csr_signed_json) except Exception as exp: logger.error('Could not convert signed CSR from json to dict: ' + repr(exp)) raise ApiError(code=500, title="Failed to convert signed CSR from json to dict", detail=repr(exp), source=endpoint) else: logger.info('Converted signed CSR from json to dict') logger.debug('csr_signed_dict: ' + json.dumps(csr_signed_dict)) return csr_signed_dict
def not_found(error): not_found_error = ApiError(code=404, title="Not Found", detail="Endpoint not found", status="NotFound") error_dict = not_found_error.to_dict() return make_json_response(errors=error_dict, status_code=str(error_dict['code']))
def sign_cr(account_id=None, payload=None, endpoint="sign_slr(account_id, payload, endpoint)"): if account_id is None: raise AttributeError("Provide account_id as parameter") if payload is None: raise AttributeError("Provide payload as parameter") logger.info("Signing Consent Record") # Get Account owner's public key try: account_public_key, account_kid = get_account_public_key( account_id=account_id) account_public_key = json.loads(account_public_key) except Exception as exp: logger.error("Could not get account owner's public key: " + repr(exp)) raise ApiError(code=500, title="Failed to get account owner's public key", detail=repr(exp), source=endpoint) else: logger.info("Account owner's public key and kid fetched") # Sign cr try: cr_signed_json = generate_and_sign_jws(account_id=account_id, jws_payload=json.dumps(payload)) except Exception as exp: logger.error('Could not create Consent Record: ' + repr(exp)) raise ApiError(code=500, title="Failed to create Consent Record", detail=repr(exp), source=endpoint) else: logger.info('Consent Record created and signed') logger.debug('cr_signed_json: ' + cr_signed_json) try: logger.info("Converting signed CR from json to dict") cr_signed_dict = json.loads(cr_signed_json) except Exception as exp: logger.error('Could not convert signed CSR from json to dict: ' + repr(exp)) raise ApiError( code=500, title="Failed to convert signed CSR from json to dict", detail=repr(exp), source=endpoint) else: logger.info('Converted signed CR from json to dict') logger.debug('cr_signed_dict: ' + json.dumps(cr_signed_dict)) return cr_signed_dict
def not_found(error): try: request_url = request.path except Exception as exp: request_url = "Unknown" not_found_error = ApiError(code=404, title="Not Found", detail="Endpoint not found", status="NotFound", source=request_url) error_dict = not_found_error.to_dict() return make_json_response(errors=error_dict, status_code=str(error_dict['code']))
def get(self, cr_id): try: endpoint = str(api.url_for(self, cr_id=cr_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: cr_id = str(cr_id) except Exception as exp: raise ApiError(code=400, title="Unsupported cr_id", detail=repr(exp), source=endpoint) else: logger.debug("cr_id: " + repr(cr_id)) # Get last Consent Status Record try: last_csr_object = get_last_cr_status(cr_id=cr_id) except Exception as exp: error_title = "Failed to get last Consent Status Record of Consent" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("last_cr_status_object: " + last_csr_object.log_entry) # Response data container try: response_data = {} response_data['data'] = last_csr_object.to_record_dict except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + json.dumps(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + json.dumps(response_data_dict)) return make_json_response(data=response_data_dict, status_code=200)
def get(self): """ Clear Database content :return: """ # Response data container response_data = {} # Clear MySQL Database logger.info("Clearing MySQL Database") try: clear_mysql_db() except Exception as exp: logger.error("Could not clear MySQL Database: " + repr(exp)) raise ApiError(code=500, title="Could not clear MySQL Database", detail=repr(exp)) else: logger.info("MySQL Database cleared") response_data['Account'] = "MySQL Database cleared" # Clear Blackbox Database logger.info("Clearing Blackbox Database") try: clear_blackbox_db() except Exception as exp: logger.error("Could not clear Blackbox Database: " + repr(exp)) raise ApiError(code=500, title="Could not clear Blackbox Database", detail=repr(exp)) else: logger.info("Blackbox Database cleared") response_data['Blackbox'] = "Blackbox Database cleared" # Clear ApiKey Database logger.info("Clearing ApiKey Database") try: clear_api_key_db() except Exception as exp: logger.error("Could not clear ApiKey Database: " + repr(exp)) raise ApiError(code=500, title="Could not clear ApiKey Database", detail=repr(exp)) else: logger.info("ApiKey Database cleared") response_data['ApiKey'] = "ApiKey Database cleared" # Response return make_json_response(data=response_data, status_code=200)
def get(self): try: endpoint = str(api.url_for(self)) except Exception as exp: endpoint = str(__name__) logger.info("Authenticating user") auth = request.authorization if not auth or not self.check_basic_auth(auth.username, auth.password): return self.authenticate() else: logger.info("Authenticated") try: api_key = get_account_api_key(account_id=self.account_id) except ApiKeyNotFoundError as exp: error_title = "ApiKey not found for authenticated user" logger.error(error_title) logger.error(repr(exp)) raise ApiError(code=404, title=error_title, detail=repr(exp), source=endpoint) except Exception as exp: error_title = "Could not get ApiKey for authenticated user" logger.error(error_title) logger.error(repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("account_id: " + str(self.account_id)) logger.debug("api_key: " + str(api_key)) create_event_log_entry(account_id=int(self.account_id), actor="AccountOwner", action="GET", resource=endpoint, timestamp=get_utc_time()) response_data = { 'Api-Key-User': api_key, 'account_id': str(self.account_id) } return make_json_response(data=response_data, status_code=200)
def sign_csr(account_id=None, payload=None, endpoint="sign_csr(account_id, payload, endpoint)"): if account_id is None: raise AttributeError("Provide account_id as parameter") if payload is None: raise AttributeError("Provide ssr_payload as parameter") logger.info("Signing Service Link Status Record") # Fill timestamp to created in slr try: timestamp_to_fill = get_utc_time() except Exception as exp: logger.error("Could not get UTC time: " + repr(exp)) raise ApiError(code=500, title="Could not get UTC time", detail=repr(exp), source=endpoint) else: logger.info("timestamp_to_fill: " + timestamp_to_fill) try: payload['iat'] = timestamp_to_fill except Exception as exp: logger.error("Could not fill timestamp to iat in csr_payload: " + repr(exp)) raise ApiError(code=500, title="Failed to fill timestamp to iat in csr_payload", detail=repr(exp), source=endpoint) else: logger.info("Timestamp filled to created in csr_payload") # Sign csr try: csr_signed = generate_and_sign_jws(account_id=account_id, jws_payload=json.dumps(payload)) except Exception as exp: logger.error('Could not create Consent Status Record: ' + repr(exp)) raise ApiError(code=500, title="Failed to create Consent Status Record", detail=repr(exp), source=endpoint) else: logger.info('SConsent Status Record created and signed') logger.debug('csr_signed: ' + csr_signed) return csr_signed, timestamp_to_fill
def store_slr_and_ssr(slr_entry=None, ssr_entry=None, endpoint="sign_ssr(account_id, slr_payload, endpoint)"): if slr_entry is None: raise AttributeError("Provide slr_entry as parameter") if ssr_entry is None: raise AttributeError("Provide ssr_entry as parameter") # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) try: # SLR update cursor = slr_entry.update_db(cursor=cursor) # SLR primary key to SSR ssr_entry.service_link_records_id = slr_entry.id # SSR Insert cursor = ssr_entry.to_db(cursor=cursor) db.connection.commit() except Exception as exp: logger.error('Slr and Ssr commit failed: ' + repr(exp)) db.connection.rollback() logger.error('--> rollback') logger.error("slr_entry: " + slr_entry.log_entry) logger.error("ssr_entry: " + ssr_entry.log_entry) raise ApiError(code=500, title="Failed to store slr and ssr", detail=repr(exp), source=endpoint) else: logger.debug('Slr and Ssr commited') logger.debug("slr_entry: " + slr_entry.log_entry) logger.debug("ssr_entry: " + ssr_entry.log_entry) return slr_entry, ssr_entry
def provide_api_key(missing="Api-Key", endpoint="provide_api_key()"): """Sends a 401 response""" try: missing = str(missing) except Exception as exp: logger.debug("Could not convert missing to str. Using default value.") try: endpoint = str(endpoint) except Exception as exp: logger.debug("Could not convert endpoint to str. Using default value.") error_detail = missing + " MUST be provided for authentication." raise ApiError(code=401, title="No required ApiKey at Request Headers", detail=error_detail, source=endpoint)
def get(self, account_id): try: endpoint = str(api.url_for(self, account_id=account_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: account_id = str(account_id) except Exception as exp: raise ApiError(code=400, title="Unsupported account_id", detail=repr(exp), source=endpoint) # Response data container try: response_data = {} response_data['meta'] = {} response_data['meta']['activationInstructions'] = "Account Export" response_data['data'] = {} response_data['data']['type'] = "Account" except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + repr(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + repr(response_data_dict)) return make_json_response(data=response_data_dict, status_code=201)
def get(self): """ Status check :param: :return: """ logger.info("Checking system status") try: response_data_dict = system_check() except Exception as exp: error_title = "API seems to be working fine, but database connection might be down." error_detail = repr(exp) logger.error(error_title + " - " + error_detail) raise ApiError(code=500, title=error_title, detail=error_detail) else: logger.info("System running as intended") # Response logger.info(json.dumps(response_data_dict)) return make_json_response(data=response_data_dict, status_code=200)
def get(self, account_id): try: endpoint = str(api.url_for(self, account_id=account_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: account_id = str(account_id) except Exception as exp: raise ApiError(code=400, title="Unsupported account_id", detail=repr(exp), source=endpoint) response_data = {'api_key': api_key, 'account_id': account_id} return make_json_response(data=response_data, status_code=200)
def get_last_cr_status(consent_id=None, account_id="", endpoint="get_last_cr_status()"): if consent_id is None: raise AttributeError("Provide consent_id as parameter") if account_id is None: raise AttributeError("Provide account_id as parameter") # Get DB cursor try: logger.info("Getting database cursor") cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise # Init Consent Record Object try: logger.info("Create ConsentRecord object") cr_entry = ConsentRecord(consent_id=consent_id) logger.info(cr_entry.log_entry) except Exception as exp: error_title = "Failed to create Consent Record object" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("ConsentRecord object: " + cr_entry.log_entry) # Get Consent Record from DB try: logger.info("Getting Consent Record from DB") cursor = cr_entry.from_db(cursor=cursor) except IndexError as exp: error_title = "Consent Record not found from DB with given ID" logger.error(error_title + ": " + repr(exp)) raise except Exception as exp: error_title = "Failed to fetch Consent Record from DB" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("cr_entry: " + cr_entry.log_entry) # Create Consent Status Record object try: logger.info("Creating Consent Status Record object") csr_entry = ConsentStatusRecord() except Exception as exp: error_title = "Failed to create Consent Status Record object" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("Consent Status Record object: " + csr_entry.log_entry) # Get Consent Status Record ID try: logger.info("Getting ID of last Consent Status Record") cursor, csr_id = get_last_csr_id(cursor=cursor, consent_id=consent_id, account_id=account_id, table_name=csr_entry.table_name) except IndexError as exp: error_title = "Consent Status Record not found from DB with given Consent Record ID" logger.error(error_title + ": " + repr(exp)) raise except Exception as exp: error_title = "Failed to get last Consent Status Record ID from database" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("Consent Status Record ID: " + str(csr_id)) # Append IDs to Consent Status Record Object try: logger.info("Appending IDs to Consent Status Record object") csr_entry.consent_status_record_id = csr_id csr_entry.accounts_id = account_id except Exception as exp: error_title = "Failed to append IDs to Consent Status Record object" logger.error(error_title + ": " + repr(exp)) raise else: logger.info("Appended IDs to Consent Status Record object: " + csr_entry.log_entry) # Get Consent Status Record from DB try: logger.info("Getting Consent Status Record from DB") cursor = csr_entry.from_db(cursor=cursor) except IndexError as exp: error_title = "Consent Status Record not found from DB with given ID" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=404, title=error_title, detail=repr(exp), source=endpoint) except Exception as exp: error_title = "Failed to fetch Consent Status Record from DB" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("Consent Status Record object: " + csr_entry.log_entry) return csr_entry.to_api_dict
def get(self, cr_id): logger.info("CrStatus") try: endpoint = str(api.url_for(self, cr_id=cr_id)) except Exception as exp: endpoint = str(__name__) try: logger.info("Fetching Api-Key from Headers") api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) else: logger.info("Api-Key: " + api_key) try: cr_id = str(cr_id) except Exception as exp: error_title = "Unsupported cr_id" logger.error(error_title) raise ApiError(code=400, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("cr_id: " + cr_id) # Get last CSR ID from query parameters try: logger.info("Get last CSR ID from query parameters") last_csr_id = request.args.get('csr_id', None) except Exception as exp: error_title = "Unexpected error when getting last CSR ID from query parameters" logger.error(error_title + " " + repr(exp)) raise ApiError(code=403, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("last_csr_id: " + repr(last_csr_id)) # Get ConsentStatusRecords try: logger.info("Fetching ConsentStatusRecords") db_entries = get_csrs(cr_id=cr_id, last_csr_id=last_csr_id) except StandardError as exp: error_title = "ConsentStatusRecords not accessible" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=403, title=error_title, detail=repr(exp), source=endpoint) except Exception as exp: error_title = "No ConsentStatusRecords found" logger.error(error_title + repr(exp)) raise ApiError(code=404, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("ConsentStatusRecords Fetched") # Response data container try: db_entry_list = db_entries response_data = {} response_data['data'] = db_entry_list except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + repr(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + repr(response_data_dict)) return make_json_response(data=response_data_dict, status_code=200)
def post(self, account_id): try: endpoint = str(api.url_for(self, account_id=account_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: account_id = str(account_id) except Exception as exp: raise ApiError(code=400, title="Unsupported account_id", detail=repr(exp), source=endpoint) # load JSON json_data = request.get_json() if not json_data: error_detail = { '0': 'Set application/json as Content-Type', '1': 'Provide json payload' } raise ApiError(code=400, title="No input data provided", detail=error_detail, source=endpoint) else: logger.debug("json_data: " + json.dumps(json_data)) # Validate payload content schema = VerifyServiceLink() schema_validation_result = schema.load(json_data) # Check validation errors if schema_validation_result.errors: raise ApiError(code=400, title="Invalid payload", detail=dict(schema_validation_result.errors), source=endpoint) else: logger.debug("JSON validation -> OK") ###### # SLR ###### # # Get slr try: slr = json_data['data']['slr']['attributes']['slr'] except Exception as exp: raise ApiError(code=400, title="Could not fetch slr from json", detail=repr(exp), source=endpoint) else: logger.debug("Got slr: " + json.dumps(slr)) # Get surrogate_id try: surrogate_id = json_data['data']['surrogate_id']['attributes'][ 'surrogate_id'] except Exception as exp: raise ApiError(code=400, title="Could not fetch surrogate id from json", detail=repr(exp), source=endpoint) else: logger.debug("Got surrogate_id: " + str(surrogate_id)) # Decode slr payload try: #print (json.dumps(json_data)) slr_payload_encoded = slr['payload'] slr_payload_encoded += '=' * (-len(slr_payload_encoded) % 4 ) # Fix incorrect padding, base64 slr_payload_decoded = b64decode(slr_payload_encoded).replace( '\\', '').replace('"{', '{').replace('}"', '}') slr_payload_dict = json.loads(slr_payload_decoded) except Exception as exp: raise ApiError(code=400, title="Could not decode slr payload", detail=repr(exp), source=endpoint) else: logger.debug("slr_payload_decoded: " + str(slr_payload_decoded)) # Get service_link_record_id try: slr_id = slr_payload_dict['link_id'] except Exception as exp: raise ApiError( code=400, title="Could not fetch service link record id from json", detail=repr(exp), source=endpoint) else: logger.debug("Got slr_id: " + str(slr_id)) # Get service_id try: service_id = slr_payload_dict['service_id'] except Exception as exp: raise ApiError(code=400, title="Could not fetch service id from json", detail=repr(exp), source=endpoint) else: logger.debug("Got service_id: " + str(service_id)) # Get operator_id try: operator_id = slr_payload_dict['operator_id'] except Exception as exp: raise ApiError(code=400, title="Could not fetch operator id from json", detail=repr(exp), source=endpoint) else: logger.debug("Got operator_id: " + str(operator_id)) ####### # Ssr ####### # # Get ssr payload try: ssr_payload = json_data['data']['ssr']['attributes'] except Exception as exp: raise ApiError(code=400, title="Could not fetch ssr from json", detail=repr(exp), source=endpoint) # Get ssr_id try: ssr_id = ssr_payload['record_id'] except Exception as exp: raise ApiError(code=400, title="Could not fetch record_id from ssr_payload", detail=repr(exp), source=endpoint) else: logger.debug("Got ssr_id: " + str(ssr_id)) # Get ssr_status try: ssr_status = ssr_payload['sl_status'] except Exception as exp: raise ApiError(code=400, title="Could not fetch sl_status from ssr_payload", detail=repr(exp), source=endpoint) else: logger.debug("Got ssr_status: " + str(ssr_status)) # Get slr_id_from_ssr try: slr_id_from_ssr = ssr_payload['slr_id'] except Exception as exp: raise ApiError(code=400, title="Could not fetch slr_id from ssr_payload", detail=repr(exp), source=endpoint) else: logger.debug("Got slr_id: " + str(slr_id)) # Get prev_ssr_id try: prev_ssr_id = ssr_payload['prev_record_id'] except Exception as exp: raise ApiError( code=400, title="Could not fetch prev_ssr_id from ssr_payload", detail=repr(exp), source=endpoint) else: logger.debug("Got prev_ssr_id: " + str(prev_ssr_id)) # Get iat try: ssr_iat = int(ssr_payload['iat']) except Exception as exp: raise ApiError(code=400, title="Could not fetch iat from ssr_payload", detail=repr(exp), source=endpoint) else: logger.debug("Got iat: " + str(prev_ssr_id)) # # Get code try: code = json_data['code'] except Exception as exp: raise ApiError(code=400, title="Could not fetch code from json", detail=repr(exp), source=endpoint) else: logger.debug("Got code: " + str(code)) ## ## # Check that slr and ssr payload are matching # TODO: Jatka slr:n ja ssr:n keskinäistä vertailua ja validointia if not slr_id == slr_id_from_ssr: detail_data = { 'slr_id': str(slr_id), 'slr_id_from_ssr': str(slr_id_from_ssr) } raise ApiError(code=409, title="Service Link Record ID's are not matching", detail=detail_data, source=endpoint) ## ## # Verify Account owner's signature in Service Link Record try: slr_verified = verify_jws_signature_with_jwk( account_id=account_id, jws_json_to_verify=json.dumps(slr)) except Exception as exp: logger.error( "Could not verify Account owner's signature in Service Link Record: " + repr(exp)) raise ApiError( code=500, title= "Failed to verify Account owner's signature in Service Link Record", detail=repr(exp), source=endpoint) else: logger.info('Service Link Record verified') logger.info('Verification passed: ' + str(slr_verified)) # Sign Ssr try: ssr_signed = sign_ssr(account_id=account_id, ssr_payload=ssr_payload, endpoint=str(endpoint)) except Exception as exp: logger.error("Could not sign Ssr") logger.debug("Could not sign Ssr: " + repr(exp)) raise # Store slr and ssr logger.info( "Storing Service Link Record and Service Link Status Record") try: slr_entry = ServiceLinkRecord(service_link_record=slr, service_link_record_id=slr_id, service_id=service_id, surrogate_id=surrogate_id, operator_id=operator_id, account_id=account_id) except Exception as exp: logger.error('Could not create Service Link Record object: ' + repr(exp)) raise ApiError(code=500, title="Failed to create Service Link Record object", detail=repr(exp), source=endpoint) try: ssr_entry = ServiceLinkStatusRecord( service_link_status_record_id=ssr_id, status=ssr_status, service_link_status_record=ssr_signed, service_link_record_id=slr_id_from_ssr, issued_at=ssr_iat, prev_record_id=prev_ssr_id) except Exception as exp: logger.error( 'Could not create Service Link Status Record object: ' + repr(exp)) raise ApiError( code=500, title="Failed to create Service Link Status Record object", detail=repr(exp), source=endpoint) try: stored_slr_entry, stored_ssr_entry = store_slr_and_ssr( slr_entry=slr_entry, ssr_entry=ssr_entry, endpoint=str(endpoint)) except Exception as exp: logger.error( "Could not store Service Link Record and Service Link Status Record" ) logger.debug("Could not store SLR and Ssr: " + repr(exp)) raise else: logger.info( "Stored Service Link Record and Service Link Status Record") logger.debug("stored_slr_entry: " + stored_slr_entry.log_entry) logger.debug("stored_ssr_entry: " + stored_ssr_entry.log_entry) # Response data container try: response_data = {} response_data['code'] = code response_data['data'] = {} response_data['data']['slr'] = stored_slr_entry.to_record_dict response_data['data']['ssr'] = stored_ssr_entry.to_record_dict response_data['data']['surrogate_id'] = surrogate_id except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + repr(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + repr(response_data_dict)) return make_json_response(data=response_data_dict, status_code=201)
def sign_slr(account_id=None, slr_payload=None, endpoint="sign_slr(account_id, slr_payload, endpoint)"): if account_id is None: raise AttributeError("Provide account_id as parameter") if slr_payload is None: raise AttributeError("Provide slr_payload as parameter") logger.info("Signing Service Link Record") # Get Account owner's public key try: account_public_key, account_kid = get_account_public_key( account_id=account_id) account_public_key_log_entry = account_public_key account_public_key = json.loads(account_public_key) except Exception as exp: logger.error("Could not get account owner's public key: " + repr(exp)) raise ApiError(code=500, title="Failed to get account owner's public key", detail=repr(exp), source=endpoint) else: logger.info("Account owner's public key and kid fetched") logger.debug("account_public_key: " + account_public_key_log_entry) # Fill Account key to cr_keys try: keys = [] keys.append(account_public_key) slr_payload['cr_keys'] = keys except Exception as exp: logger.error("Could not fill account owner's public key to cr_keys: " + repr(exp)) raise ApiError( code=500, title="Failed to fill account owner's public key to cr_keys", detail=repr(exp), source=endpoint) else: logger.info("Account owner's public key added to cr_keys") # Sign slr slr_signed = {} try: slr_signed_json = generate_and_sign_jws( account_id=account_id, jws_payload=json.dumps(slr_payload)) except Exception as exp: logger.error('Could not create Service Link Record: ' + repr(exp)) raise ApiError(code=500, title="Failed to create Service Link Record", detail=repr(exp), source=endpoint) else: logger.info('Service Link Record created and signed') logger.debug("slr_payload: " + json.dumps(slr_payload)) logger.debug("slr_signed_json: " + slr_signed_json) try: logger.info("Converting signed CSR from json to dict") slr_signed_dict = json.loads(slr_signed_json) except Exception as exp: logger.error('Could not convert signed SLR from json to dict: ' + repr(exp)) raise ApiError( code=500, title="Failed to convert signed SLR from json to dict", detail=repr(exp), source=endpoint) else: logger.info('Converted signed SLR from json to dict') logger.debug('slr_signed_dict: ' + json.dumps(slr_signed_dict)) return slr_signed_dict
def post(self, cr_id): logger.info("CrStatus") try: endpoint = str(api.url_for(self, cr_id=cr_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: cr_id = str(cr_id) except Exception as exp: raise ApiError(code=400, title="Unsupported cr_id", detail=repr(exp), source=endpoint) else: logger.debug("cr_id: " + repr(cr_id)) # load JSON json_data = request.get_json() if not json_data: error_detail = { '0': 'Set application/json as Content-Type', '1': 'Provide json payload' } raise ApiError(code=400, title="No input data provided", detail=error_detail, source=endpoint) else: logger.debug("json_data: " + json.dumps(json_data)) # Validate payload content schema = NewConsentStatus() schema_validation_result = schema.load(json_data) # Check validation errors if schema_validation_result.errors: logger.error("Invalid payload") raise ApiError(code=400, title="Invalid payload", detail=dict(schema_validation_result.errors), source=endpoint) else: logger.debug("JSON validation -> OK") # Payload # Consent Status Record try: csr_payload = json_data['data']['attributes'] except Exception as exp: raise ApiError( code=400, title="Could not fetch source_csr_payload from json", detail=repr(exp), source=endpoint) else: logger.debug("Got csr_payload: " + json.dumps(csr_payload)) # # Create new Consent Status Record try: new_csr_object = add_csr(cr_id=cr_id, csr_payload=csr_payload, endpoint=endpoint) except ApiError as exp: error_title = "Failed to add new Consent Status Record for Consent" logger.error(error_title + ": " + repr(exp)) raise except Exception as exp: error_title = "Unexpected error. Failed to add new Consent Status Record for Consent" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("new_csr_object: " + new_csr_object.log_entry) # Response data container try: response_data = {} response_data['data'] = new_csr_object.to_record_dict except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + json.dumps(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + json.dumps(response_data_dict)) return make_json_response(data=response_data_dict, status_code=201)
def get(self, sink_cr_id): try: endpoint = str(api.url_for(self, sink_cr_id=sink_cr_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: sink_cr_id = str(sink_cr_id) except Exception as exp: raise ApiError(code=400, title="Unsupported sink_cr_id", detail=repr(exp), source=endpoint) else: logger.debug("sink_cr_id: " + repr(sink_cr_id)) # Init Sink's Consent Record Object try: sink_cr_entry = ConsentRecord(consent_id=sink_cr_id, role="Sink") except Exception as exp: error_title = "Failed to create Sink's Consent Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("sink_cr_entry: " + sink_cr_entry.log_entry) try: source_cr, sink_slr = get_auth_token_data( sink_cr_object=sink_cr_entry) except Exception as exp: error_title = "Failed to get Authorization token data" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("source_cr: " + source_cr.log_entry) logger.debug("sink_slr: " + sink_slr.log_entry) # Response data container try: response_data = {} response_data['data'] = {} response_data['data']['source'] = {} response_data['data']['source']['consentRecord'] = {} response_data['data']['source']['consentRecord'][ 'type'] = "ConsentRecord" response_data['data']['source']['consentRecord']['attributes'] = {} response_data['data']['source']['consentRecord']['attributes'][ 'cr'] = source_cr.to_record_dict response_data['data']['sink'] = {} response_data['data']['sink']['serviceLinkRecord'] = {} response_data['data']['sink']['serviceLinkRecord'][ 'type'] = "ServiceLinkRecord" response_data['data']['sink']['serviceLinkRecord'][ 'attributes'] = {} response_data['data']['sink']['serviceLinkRecord']['attributes'][ 'slr'] = sink_slr.to_record_dict except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + json.dumps(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + json.dumps(response_data_dict)) return make_json_response(data=response_data_dict, status_code=200)
def post(self, account_id, source_slr_id, sink_slr_id): try: endpoint = str( api.url_for(self, account_id=account_id, source_slr_id=source_slr_id, sink_slr_id=sink_slr_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: account_id = str(account_id) except Exception as exp: raise ApiError(code=400, title="Unsupported account_id", detail=repr(exp), source=endpoint) try: source_slr_id = str(source_slr_id) except Exception as exp: raise ApiError(code=400, title="Unsupported source_slr_id", detail=repr(exp), source=endpoint) try: sink_slr_id = str(sink_slr_id) except Exception as exp: raise ApiError(code=400, title="Unsupported sink_slr_id", detail=repr(exp), source=endpoint) # load JSON json_data = request.get_json() if not json_data: error_detail = { '0': 'Set application/json as Content-Type', '1': 'Provide json payload' } raise ApiError(code=400, title="No input data provided", detail=error_detail, source=endpoint) else: logger.debug("json_data: " + json.dumps(json_data)) # Validate payload content schema = NewConsent() schema_validation_result = schema.load(json_data) # Check validation errors if schema_validation_result.errors: logger.error("Invalid payload") raise ApiError(code=400, title="Invalid payload", detail=dict(schema_validation_result.errors), source=endpoint) else: logger.debug("JSON validation -> OK") ###### # Source ###### # Consent Record try: source_cr_payload = json_data['data']['source'][ 'consentRecordPayload']['attributes'] except Exception as exp: raise ApiError(code=400, title="Could not fetch source_cr_payload from json", detail=repr(exp), source=endpoint) else: logger.debug("Got source_cr_payload: " + json.dumps(source_cr_payload)) # Consent Status Record try: source_csr_payload = json_data['data']['source'][ 'consentStatusRecordPayload']['attributes'] except Exception as exp: raise ApiError( code=400, title="Could not fetch source_csr_payload from json", detail=repr(exp), source=endpoint) else: logger.debug("Got source_csr_payload: " + json.dumps(source_csr_payload)) ###### # Sink ###### # Consent Record try: sink_cr_payload = json_data['data']['sink'][ 'consentRecordPayload']['attributes'] except Exception as exp: raise ApiError(code=400, title="Could not fetch sink_cr_payload from json", detail=repr(exp), source=endpoint) else: logger.debug("Got sink_cr_payload: " + json.dumps(sink_cr_payload)) # Consent Status Record try: sink_csr_payload = json_data['data']['sink'][ 'consentStatusRecordPayload']['attributes'] except Exception as exp: raise ApiError(code=400, title="Could not fetch sink_csr_payload from json", detail=repr(exp), source=endpoint) else: logger.debug("Got sink_csr_payload: " + json.dumps(sink_csr_payload)) ##### # IDs from CR and CSR payloads ##### try: # Source CR try: source_cr_cr_id = source_cr_payload['common_part']['cr_id'] source_cr_rs_id = source_cr_payload['common_part'][ 'rs_description']['resource_set']['rs_id'] source_cr_slr_id = source_cr_payload['common_part']['slr_id'] source_cr_subject_id = source_cr_payload['common_part'][ 'subject_id'] source_cr_surrogate_id = source_cr_payload['common_part'][ 'surrogate_id'] source_cr_role = source_cr_payload['common_part']['role'] except Exception as exp: error_title = "Could not fetch IDs from Source CR payload" raise # Source CSR try: source_csr_surrogate_id = source_csr_payload['surrogate_id'] source_csr_cr_id = source_csr_payload['cr_id'] source_csr_prev_record_id = source_csr_payload[ 'prev_record_id'] source_csr_record_id = source_csr_payload['record_id'] source_csr_consent_status = source_csr_payload[ 'consent_status'] source_csr_issued = source_csr_payload['iat'] except Exception as exp: error_title = "Could not fetch IDs from Source CSR payload" raise # Sink CR try: sink_cr_cr_id = sink_cr_payload['common_part']['cr_id'] sink_cr_rs_id = sink_cr_payload['common_part'][ 'rs_description']['resource_set']['rs_id'] sink_cr_slr_id = sink_cr_payload['common_part']['slr_id'] sink_cr_subject_id = sink_cr_payload['common_part'][ 'subject_id'] sink_cr_surrogate_id = sink_cr_payload['common_part'][ 'surrogate_id'] sink_cr_role = sink_cr_payload['common_part']['role'] except Exception as exp: error_title = "Could not fetch IDs from Sink CR payload" raise # Sink CSR try: sink_csr_surrogate_id = sink_csr_payload['surrogate_id'] sink_csr_cr_id = sink_csr_payload['cr_id'] sink_csr_prev_record_id = sink_csr_payload['prev_record_id'] sink_csr_record_id = sink_csr_payload['record_id'] sink_csr_consent_status = sink_csr_payload['consent_status'] sink_csr_issued = sink_csr_payload['iat'] except Exception as exp: error_title = "Could not fetch IDs from Sink CSR payload" raise except Exception as exp: logger.error(error_title) raise ApiError(code=400, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("IDs fetched from CR and CSR payloads") ###### # Sign #### # Sign Source CR try: source_cr_signed = sign_cr(account_id=account_id, payload=source_cr_payload, endpoint=endpoint) except Exception as exp: logger.error("Could not sign Source's CR: " + repr(exp)) raise else: logger.info("Source CR signed") logger.debug("source_cr_signed: " + json.dumps(source_cr_signed)) # Sign Source CSR try: source_csr_signed = sign_csr(account_id=account_id, payload=source_csr_payload, endpoint=endpoint) except Exception as exp: logger.error("Could not sign Source's CSR: " + repr(exp)) raise else: logger.info("Source CSR signed") logger.debug("source_csr_signed: " + json.dumps(source_csr_signed)) # Sign Sink CR try: sink_cr_signed = sign_cr(account_id=account_id, payload=sink_cr_payload, endpoint=endpoint) except Exception as exp: logger.error("Could not sign Source's CR: " + repr(exp)) raise else: logger.info("Sink's CR signed") logger.debug("sink_cr_signed: " + json.dumps(sink_cr_signed)) # Sign Sink CSR try: sink_csr_signed = sign_csr(account_id=account_id, payload=sink_csr_payload, endpoint=endpoint) except Exception as exp: logger.error("Could not sign Sink's CSR: " + repr(exp)) raise else: logger.info("Sink's CSR signed") logger.debug("sink_csr_signed: " + json.dumps(sink_csr_signed)) ######### # Store # ######### logger.info("Creating objects to store") # Source SLR try: logger.info("Creating Source SLR") source_slr_entry = ServiceLinkRecord( surrogate_id=source_cr_surrogate_id, account_id=account_id, service_link_record_id=source_cr_slr_id) except Exception as exp: error_title = "Failed to create Source's Service Link Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("source_slr_entry created") logger.info("source_slr_entry: " + source_slr_entry.log_entry) # Sink SLR try: logger.info("Creating Sink SLR") sink_slr_entry = ServiceLinkRecord( surrogate_id=sink_cr_surrogate_id, account_id=account_id, service_link_record_id=sink_cr_slr_id) except Exception as exp: error_title = "Failed to create Sink's Service Link Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("sink_slr_entry created") logger.info("sink_slr_entry: " + sink_slr_entry.log_entry) # Source CR try: logger.info("Creating Source CR") source_cr_entry = ConsentRecord( consent_record=source_cr_signed, consent_id=source_cr_cr_id, surrogate_id=source_cr_surrogate_id, resource_set_id=source_cr_rs_id, service_link_record_id=source_cr_slr_id, subject_id=source_cr_subject_id, role=source_cr_role) except Exception as exp: error_title = "Failed to create Source's Consent Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("source_cr_entry created") logger.info("source_cr_entry: " + source_cr_entry.log_entry) # Sink CR try: logger.info("Creating Sink CR") sink_cr_entry = ConsentRecord( consent_record=sink_cr_signed, consent_id=sink_cr_cr_id, surrogate_id=sink_cr_surrogate_id, resource_set_id=sink_cr_rs_id, service_link_record_id=sink_cr_slr_id, subject_id=sink_cr_subject_id, role=sink_cr_role) except Exception as exp: error_title = "Failed to create Sink's Consent Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("sink_cr_entry created") logger.info("sink_cr_entry: " + sink_cr_entry.log_entry) # Source CSR try: logger.info("Creating Source CSR") source_csr_entry = ConsentStatusRecord( consent_status_record_id=source_csr_record_id, status=source_csr_consent_status, consent_status_record=source_csr_signed, consent_record_id=source_csr_cr_id, issued_at=source_csr_issued, prev_record_id=source_csr_prev_record_id) except Exception as exp: error_title = "Failed to create Source's Consent Status Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("source_csr_entry created") logger.info("source_csr_entry: " + source_csr_entry.log_entry) # Sink CSR try: logger.info("Creating Sink CSR") sink_csr_entry = ConsentStatusRecord( consent_status_record_id=sink_csr_record_id, status=sink_csr_consent_status, consent_status_record=sink_csr_signed, consent_record_id=sink_csr_cr_id, issued_at=sink_csr_issued, prev_record_id=sink_csr_prev_record_id) except Exception as exp: error_title = "Failed to create Sink's Consent Status Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("sink_csr_entry created") logger.info("sink_csr_entry: " + sink_csr_entry.log_entry) # Store CRs and CSRs try: logger.info( "About to store Consent Records and Consent Status Records") stored_source_cr_entry, stored_source_csr_entry, stored_sink_cr_entry, stored_sink_csr_entry = store_cr_and_csr( source_slr_entry=source_slr_entry, sink_slr_entry=sink_slr_entry, source_cr_entry=source_cr_entry, source_csr_entry=source_csr_entry, sink_cr_entry=sink_cr_entry, sink_csr_entry=sink_csr_entry, endpoint=endpoint) except Exception as exp: error_title = "Could not store Consent Record and Consent Status Record" logger.error(error_title + ": " + repr(exp)) raise else: logger.info("Stored Consent Record and Consent Status Record") logger.info("Source CR: " + stored_source_cr_entry.log_entry) logger.info("Source CSR: " + stored_source_csr_entry.log_entry) logger.info("Sink CR: " + stored_sink_cr_entry.log_entry) logger.info("Sink CSR: " + stored_sink_csr_entry.log_entry) # Response data container try: response_data = {} response_data['data'] = {} response_data['data']['source'] = {} response_data['data']['source']['consentRecord'] = {} response_data['data']['source']['consentRecord'][ 'type'] = "ConsentRecord" response_data['data']['source']['consentRecord']['attributes'] = {} response_data['data']['source']['consentRecord']['attributes'][ 'cr'] = stored_source_cr_entry.to_record_dict response_data['data']['source']['consentStatusRecord'] = {} response_data['data']['source']['consentStatusRecord'][ 'type'] = "ConsentStatusRecord" response_data['data']['source']['consentStatusRecord'][ 'attributes'] = {} response_data['data']['source']['consentStatusRecord'][ 'attributes']['csr'] = stored_source_csr_entry.to_record_dict response_data['data']['sink'] = {} response_data['data']['sink']['consentRecord'] = {} response_data['data']['sink']['consentRecord'][ 'type'] = "ConsentRecord" response_data['data']['sink']['consentRecord']['attributes'] = {} response_data['data']['sink']['consentRecord']['attributes'][ 'cr'] = stored_sink_cr_entry.to_record_dict response_data['data']['sink']['consentStatusRecord'] = {} response_data['data']['sink']['consentStatusRecord'][ 'type'] = "ConsentStatusRecord" response_data['data']['sink']['consentStatusRecord'][ 'attributes'] = {} response_data['data']['sink']['consentStatusRecord']['attributes'][ 'csr'] = stored_sink_csr_entry.to_record_dict except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + json.dumps(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + json.dumps(response_data_dict)) return make_json_response(data=response_data_dict, status_code=201)
def get_last_slr_status(account_id=None, slr_id=None, endpoint="get_last_slr_status()"): if slr_id is None: raise AttributeError("Provide slr_id as parameter") if account_id is None: raise AttributeError("Provide account_id as parameter") # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) # Init ServiceLinkRecord Object try: logger.info("Create ServiceLinkRecord object") slr_entry = ServiceLinkRecord(service_link_record_id=slr_id, account_id=account_id) logger.info(slr_entry.log_entry) except Exception as exp: error_title = "Failed to create Service Link Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("slr_entry: " + slr_entry.log_entry) # Get ServiceLinkRecord from DB try: cursor = slr_entry.from_db(cursor=cursor) except IndexError as exp: error_title = "Service Link Status Record not found with provided information." error_detail = "Account ID was {} and Service Link Record ID was {}.".format( account_id, slr_id) logger.error(error_title + " " + error_detail + ": " + repr(exp)) raise ApiError(code=404, title=error_title, detail=error_detail, source=endpoint) except Exception as exp: error_title = "Failed to fetch Service Link Record from DB" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=str(exp.message), source=endpoint) else: logger.debug("slr_entry: " + slr_entry.log_entry) # Create ServiceLinkStatusRecord object try: slsr_entry = ServiceLinkStatusRecord() except Exception as exp: error_title = "Failed to create ServiceLinkStatusRecord object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("slsr_entry: " + slsr_entry.log_entry) # Get database table name for ServiceLinkStatusRecord try: logger.info("Get ServiceLinkStatusRecord table name") slsr_table_name = slsr_entry.table_name except Exception as exp: error_title = "Failed to get ServiceLinkStatusRecord table name" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Got ServiceLinkStatusRecord table name: " + str(slsr_table_name)) # Get ServiceLinkStatusRecord ID try: cursor, slsr_id = get_last_slsr_id(cursor=cursor, slr_id=slr_id, table_name=slsr_table_name) except IndexError as exp: error_title = "ServiceLinkStatusRecord not found from DB with given Consent Record ID" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=404, title=error_title, detail=repr(exp), source=endpoint) except Exception as exp: error_title = "Failed to get last ServiceLinkStatusRecord ID from database" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("slsr_id: " + str(slsr_id)) # Append ID to ServiceLinkStatusRecord Object try: logger.info("Append ID to ServiceLinkStatusRecord object: " + slsr_entry.log_entry) slsr_entry.service_link_status_record_id = slsr_id except Exception as exp: error_title = "Failed to append ID to ServiceLinkStatusRecord object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Appended ID to ServiceLinkStatusRecord object: " + slsr_entry.log_entry) # Get ServiceLinkStatusRecord from DB try: cursor = slsr_entry.from_db(cursor=cursor) except IndexError as exp: error_title = "ServiceLinkStatusRecord not found from DB with given ID" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=404, title=error_title, detail=repr(exp), source=endpoint) except Exception as exp: error_title = "Failed to fetch ServiceLinkStatusRecord from DB" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("slsr_entry: " + slsr_entry.log_entry) return slsr_entry.to_api_dict
def post(self): """ Example JSON { "username": "******", "password": "******", "firstName": "string", "lastName": "string", "email": "*****@*****.**", "dateOfBirth": "18-08-2016", "acceptTermsOfService": "True" } :return: """ try: endpoint = str(api.url_for(self)) except Exception as exp: endpoint = str(__name__) # load JSON json_data = request.get_json() if not json_data: error_detail = {'0': 'Set application/json as Content-Type', '1': 'Provide json payload'} raise ApiError(code=400, title="No input data provided", detail=error_detail, source=endpoint) else: logger.debug("json_data: " + json.dumps(json_data)) # Validate payload content schema = AccountSchema2() schema_validation_result = schema.load(json_data) # Check validation errors if schema_validation_result.errors: logger.error("Invalid payload") raise ApiError(code=400, title="Invalid payload", detail=dict(schema_validation_result.errors), source=endpoint) else: logger.debug("JSON validation -> OK") try: username = json_data['username'] password = json_data['password'] firstName = json_data['firstName'] lastName = json_data['lastName'] email_address = json_data['email'] dateOfBirth = json_data['dateOfBirth'] acceptTermsOfService = json_data['acceptTermsOfService'] global_identifier = str(uuid.uuid4()) salt_str = str(bcrypt.gensalt()) pwd_hash = bcrypt.hashpw(str(password), salt_str) except Exception as exp: error_title = "Could not prepare Account data" logger.error(error_title) raise ApiError(code=400, title=error_title, detail=repr(exp), source=endpoint) # DB cursor cursor = get_db_cursor() try: ### # Accounts logger.debug('Accounts') account = Account(global_identifyer=global_identifier) account.to_db(cursor=cursor) ### # localIdentityPWDs logger.debug('localIdentityPWDs') local_pwd = LocalIdentityPWD(password=pwd_hash) local_pwd.to_db(cursor=cursor) ### # localIdentities logger.debug('localIdentities') local_identity = LocalIdentity( username=username, pwd_id=local_pwd.id, accounts_id=account.id ) local_identity.to_db(cursor=cursor) ### # salts logger.debug('salts') salt = Salt( salt=salt_str, identity_id=local_identity.id ) salt.to_db(cursor=cursor) ### # Particulars logger.debug('particulars') particulars = Particulars( firstname=firstName, lastname=lastName, date_of_birth=dateOfBirth, account_id=account.id ) logger.debug("to_dict: " + repr(particulars.to_dict)) cursor = particulars.to_db(cursor=cursor) ### # emails logger.debug('emails') email = Email( email=email_address, type="Personal", prime=1, account_id=account.id ) email.to_db(cursor=cursor) ### # Commit db.connection.commit() except Exception as exp: error_title = "Could not create Account" logger.debug('commit failed: ' + repr(exp)) logger.debug('--> rollback') logger.error(error_title) db.connection.rollback() raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug('Account commited') try: logger.info("Generating Key for Account") kid = gen_account_key(account_id=account.id) except Exception as exp: error_title = "Could not generate Key for Account" logger.debug(error_title + ': ' + repr(exp)) #raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Generated Key for Account with Key ID: " + str(kid)) try: logger.info("Generating API Key for Account") api_key = gen_account_api_key(account_id=account.id) except Exception as exp: error_title = "Could not generate API Key for Account" logger.debug(error_title + ': ' + repr(exp)) #raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Generated API Key: " + str(api_key)) data = cursor.fetchall() logger.debug('data: ' + repr(data)) # Response data container try: response_data = {} response_data['meta'] = {} response_data['meta']['activationInstructions'] = "Account activated already" response_data['data'] = {} response_data['data']['type'] = "Account" response_data['data']['id'] = str(account.id) response_data['data']['attributes'] = {} response_data['data']['attributes']['username'] = username response_data['data']['attributes']['firstName'] = firstName response_data['data']['attributes']['lastName'] = lastName response_data['data']['attributes']['email'] = email_address response_data['data']['attributes']['dateOfBirth'] = dateOfBirth response_data['data']['attributes']['acceptTermsOfService'] = acceptTermsOfService except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + repr(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + repr(response_data_dict)) return make_json_response(data=response_data_dict, status_code=201)
def store_csr(account_id=None, record_id=None, cr_id=None, surrogate_id=None, consent_status=None, iat=None, prev_record_id=None, csr_signed=None, endpoint="store_csr()"): # Parameter Check if account_id is None: raise AttributeError("Provide account_id as parameter") if record_id is None: raise AttributeError("Provide record_id as parameter") if cr_id is None: raise AttributeError("Provide cr_id as parameter") if surrogate_id is None: raise AttributeError("Provide surrogate_id as parameter") if consent_status is None: raise AttributeError("Provide consent_status as parameter") if iat is None: raise AttributeError("Provide iat as parameter") if prev_record_id is None: raise AttributeError("Provide prev_record_id as parameter") if csr_signed is None: raise AttributeError("Provide csr_signed as parameter") # Parameter type check try: account_id = str(account_id) except Exception: raise TypeError("account_id MUST be str, not " + str(type(account_id))) try: record_id = str(record_id) except Exception: raise TypeError("record_id MUST be str, not " + str(type(record_id))) try: cr_id = str(cr_id) except Exception: raise TypeError("cr_id MUST be str, not " + str(type(cr_id))) try: surrogate_id = str(surrogate_id) except Exception: raise TypeError("surrogate_id MUST be str, not " + str(type(surrogate_id))) try: consent_status = str(consent_status) except Exception: raise TypeError("consent_status MUST be str, not " + str(type(consent_status))) try: iat = int(iat) except Exception: raise TypeError("iat MUST be int, not " + str(type(iat))) try: prev_record_id = str(prev_record_id) except Exception: raise TypeError("prev_record_id MUST be str, not " + str(type(prev_record_id))) if not isinstance(csr_signed, dict): raise TypeError("csr_signed MUST be dict, not " + str(type(csr_signed))) ###### # Base information #### # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) ########### # Entries # ########### # Existing Consent Record ### # Init Consent Record Object try: logger.info("Create ConsentRecord object") cr_entry = ConsentRecord(consent_id=cr_id, accounts_id=account_id, surrogate_id=surrogate_id) except Exception as exp: error_title = "Failed to create Consent Record object" error_detail = str(exp.message) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.debug("cr_entry: " + cr_entry.log_entry) # Get Consent Record from DB try: logger.info("Get Consent Record from DB") cursor = cr_entry.from_db(cursor=cursor) except IndexError as exp: error_title = "Consent Record not found" error_detail = str(exp.message) logger.error(error_title + ": " + repr(exp)) raise IndexError(error_title + " - " + error_detail) except Exception as exp: error_title = "Failed to fetch Consent Record" error_detail = str(exp.message) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.debug("cr_entry: " + cr_entry.log_entry) # Get primary key of Consent Record database entry try: logger.info("Get primary key of Consent Record database entry") cr_entry_primary_key = cr_entry.id except Exception as exp: error_title = "Failed to fetch Consent Record primary key" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise KeyError(error_title + " - " + error_detail) else: logger.debug("cr_entry_primary_key: " + str(cr_entry_primary_key)) # CSR try: logger.info("Create ConsentStatusRecord object") csr_entry = ConsentStatusRecord( consent_status_record_id=record_id, status=consent_status, consent_status_record=csr_signed, consent_record_id=cr_id, issued_at=iat, prev_record_id=prev_record_id, consent_records_id=int(cr_entry_primary_key), accounts_id=int(account_id)) except Exception as exp: error_title = "Failed to create Consent Status Record object" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.info("csr_entry: " + csr_entry.log_entry) ########### # Store # ########### # CSR # Get database table name for Consent Status Record try: logger.info("Get Consent Status Record table name") csr_table_name = csr_entry.table_name except Exception as exp: error_title = "Could not get table name of Consent Status Record database table" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.info("Got Consent Status Record table name: " + str(csr_table_name)) # Store CSR try: logger.info("Store ConsentStatusRecord") try: cursor = csr_entry.to_db(cursor=cursor) except Exception as exp: error_title = "Failed to store Consent Status Record of Sink Service" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.debug("csr_entry: " + csr_entry.log_entry) # Commit db.connection.commit() except Exception as exp: logger.error('Consent Status Record Commit failed: ' + repr(exp)) db.connection.rollback() logger.error('--> rollback') error_title = "Could not write to database" error_detail = repr(exp) logger.error(error_title + ": " + error_detail) raise else: logger.info("Consent Status Record commited") return csr_entry
def get_slr_record(account_id=None, slr_id=None, endpoint="get_slr_record()"): logger.info("get_slr_record()") if account_id is None: raise AttributeError("Provide account_id as parameter") if slr_id is None: raise AttributeError("Provide slr_id as parameter") if not isinstance(account_id, str): try: account_id = str(account_id) except Exception: raise TypeError("account_id MUST be str, not " + str(type(account_id))) if not isinstance(slr_id, str): try: slr_id = str(slr_id) except Exception: raise TypeError("slr_id MUST be str, not " + str(type(slr_id))) if not isinstance(endpoint, str): try: endpoint = str(endpoint) except Exception: raise TypeError("endpoint MUST be str, not " + str(type(endpoint))) logger.info("Creating ServiceLinkRecord object") try: slr_entry = ServiceLinkRecord(service_link_record_id=slr_id, account_id=account_id) except Exception as exp: logger.error('Could not create Service Link Record object: ' + repr(exp)) raise ApiError(code=500, title="Failed to create Service Link Record object", detail=repr(exp), source=endpoint) else: logger.info("Service Link Record entry created") logger.debug(slr_entry.log_entry) # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) logger.info("Get ServiceLinkRecord from database") try: cursor = slr_entry.from_db(cursor=cursor) except Exception as exp: error_title = "Could not get ServiceLinkRecord from database" error_detail = str(exp.message) logger.error(error_title + " - " + error_detail) raise else: logger.info('Got ServiceLinkRecord from database') logger.debug("slr_entry: " + slr_entry.log_entry) return slr_entry
def store_cr_and_csr(source_slr_entry=None, sink_slr_entry=None, source_cr_entry=None, source_csr_entry=None, sink_cr_entry=None, sink_csr_entry=None, endpoint="store_cr_and_csr()"): if source_slr_entry is None: raise AttributeError("Provide source_slr_entry as parameter") if sink_slr_entry is None: raise AttributeError("Provide sink_slr_entry as parameter") if source_cr_entry is None: raise AttributeError("Provide source_cr_entry as parameter") if source_csr_entry is None: raise AttributeError("Provide source_csr_entry as parameter") if sink_cr_entry is None: raise AttributeError("Provide sink_cr_entry as parameter") if sink_csr_entry is None: raise AttributeError("Provide sink_csr_entry as parameter") # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) try: # Get Source's SLR from DB try: logger.info("Get Source SLR from database") cursor = source_slr_entry.from_db(cursor=cursor) except Exception as exp: error_title = "Failed to fetch Service Link Record of Source Service from DB" error_detail = str(exp.message) logger.error(error_title + ": " + repr(exp)) raise IndexError(error_title + " - " + error_detail) else: logger.debug("source_slr_entry: " + source_slr_entry.log_entry) # Get Sink's SLR from DB try: logger.info("Get Sink SLR from database") cursor = sink_slr_entry.from_db(cursor=cursor) except Exception as exp: error_title = "Failed to fetch Service Link Record of Sink Service from DB" error_detail = str(exp.message) logger.error(error_title + ": " + repr(exp)) raise IndexError(error_title + " - " + error_detail) else: logger.debug("sink_slr_entry: " + sink_slr_entry.log_entry) # Get Source's SLR ID try: logger.info("Source SLR ID to Source CR") source_cr_entry.service_link_records_id = source_slr_entry.id except Exception as exp: error_title = "Failed to link Consent Record of Source Service with Service Link Record" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise KeyError(error_title + " - " + error_detail) else: logger.debug("source_cr_entry: " + source_cr_entry.log_entry) # Get Sink's SLR ID try: logger.info("Sink SLR ID to Sink CR") sink_cr_entry.service_link_records_id = sink_slr_entry.id except Exception as exp: error_title = "Failed to link Consent Record of Sink Service with Service Link Record" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise KeyError(error_title + " - " + error_detail) else: logger.debug("sink_cr_entry: " + sink_cr_entry.log_entry) # Store Source CR try: logger.info("Store Source CR") cursor = source_cr_entry.to_db(cursor=cursor) except Exception as exp: error_title = "Failed to store Consent Record of Source Service" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.debug("source_cr_entry: " + source_cr_entry.log_entry) # Link Source's CSR with it's CR try: logger.info("Source CR ID to Source CSR") source_csr_entry.consent_records_id = source_cr_entry.id except Exception as exp: error_title = "Failed to link Consent Record of Source Service with Consent Status Record" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise KeyError(error_title + " - " + error_detail) else: logger.debug(source_csr_entry.log_entry) # Store Source CSR try: logger.info("Store Source CSR") cursor = source_csr_entry.to_db(cursor=cursor) except Exception as exp: error_title = "Failed to store Consent Status Record of Source Service" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.debug("source_csr_entry: " + source_csr_entry.log_entry) # Store Sink CR try: logger.info("Store Sink CR") cursor = sink_cr_entry.to_db(cursor=cursor) except Exception as exp: error_title = "Failed to store Consent Record of Sink Service" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.debug("sink_cr_entry: " + sink_cr_entry.log_entry) # Link Sink's CSR with it's CR try: logger.info("Sink CR ID to Sink CSR") sink_csr_entry.consent_records_id = sink_cr_entry.id except Exception as exp: error_title = "Failed to link Consent Record of Sink Service with Consent Status Record" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise KeyError(error_title + " - " + error_detail) else: logger.debug("sink_csr_entry: " + sink_csr_entry.log_entry) # Store Sink CSR try: logger.info("Store Sink CSR") cursor = sink_csr_entry.to_db(cursor=cursor) except Exception as exp: error_title = "Failed to store Consent Status Record of Sink Service" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.debug("sink_csr_entry: " + sink_csr_entry.log_entry) # Commit db.connection.commit() except Exception as exp: logger.debug('commit failed: ' + repr(exp)) db.connection.rollback() logger.debug('--> rollback') error_title = "Could not write to database" error_detail = repr(exp) logger.error(error_title + ": " + error_detail) raise else: logger.info("CR's and CSR's commited") return source_cr_entry, source_csr_entry, sink_cr_entry, sink_csr_entry
def init_slr_sink(account_id=None, slr_id=None, pop_key=None, endpoint="init_slr_sink()"): logger.info("init_slr_sink()") if account_id is None: raise AttributeError("Provide account_id as parameter") if slr_id is None: raise AttributeError("Provide slr_id as parameter") if pop_key is None: raise AttributeError("Provide pop_key as parameter") if not isinstance(account_id, str): try: account_id = str(account_id) except Exception: raise TypeError("account_id MUST be str, not " + str(type(account_id))) if not isinstance(slr_id, str): try: slr_id = str(slr_id) except Exception: raise TypeError("slr_id MUST be str, not " + str(type(slr_id))) if not isinstance(pop_key, dict): try: pop_key = dict(pop_key) except Exception: raise TypeError("pop_key MUST be dict, not " + str(type(pop_key))) if not isinstance(endpoint, str): try: endpoint = str(endpoint) except Exception: raise TypeError("endpoint MUST be str, not " + str(type(endpoint))) logger.info("Initing SLR") try: slr_entry = ServiceLinkRecord(service_link_record_id=slr_id, account_id=account_id, pop_key=pop_key) except Exception as exp: logger.error('Could not create Service Link Record object: ' + repr(exp)) raise ApiError(code=500, title="Failed to create Service Link Record object", detail=repr(exp), source=endpoint) else: logger.info("Service Link Record entry created") logger.debug(slr_entry.log_entry) # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) # Store DB entry try: cursor = slr_entry.to_db(cursor=cursor) slr_id = slr_entry.service_link_record_id db.connection.commit() except IntegrityError as exp: error_title = "Service Link ID already exists" error_detail = str(exp.args[1]) logger.error(error_title + " - " + error_detail) db.connection.rollback() logger.debug('--> rollback') raise ApiError(code=409, title=error_title, detail=error_detail, source=endpoint) except Exception as exp: logger.error('Slr init commit failed: ' + repr(exp)) db.connection.rollback() logger.debug('--> rollback') raise ApiError(code=500, title="Failed to store init SLR", detail=repr(exp), source=endpoint) else: logger.info('Slr initialized commited') logger.debug("slr_entry: " + slr_entry.log_entry) return slr_id
def get(self, account_id, service_id): try: endpoint = str( api.url_for(self, account_id=account_id, service_id=service_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: account_id = str(account_id) except Exception as exp: raise ApiError(code=400, title="Unsupported account_id", detail=repr(exp), source=endpoint) try: service_id = str(service_id) except Exception as exp: raise ApiError(code=400, title="Unsupported service_id", detail=repr(exp), source=endpoint) try: surrogate_id = get_surrogate_id_by_account_and_service( account_id=account_id, service_id=service_id, endpoint=endpoint) except IndexError as exp: raise ApiError( code=404, title="Nothing could not be found with provided information", detail=repr(exp), source=endpoint) except Exception as exp: logger.error('Could not get surrogate_id: ' + repr(exp)) raise ApiError(code=500, title="Could not get surrogate_id", detail=repr(exp), source=endpoint) else: logger.debug('Got surrogate_id: ' + repr(surrogate_id)) # Response data container try: response_data = {} response_data['data'] = {} response_data['data']['surrogate_id'] = {} response_data['data']['surrogate_id']['type'] = "SurrogateId" response_data['data']['surrogate_id']['attributes'] = surrogate_id except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + repr(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + repr(response_data_dict)) return make_json_response(data=response_data_dict, status_code=200)
def post(self, account_id): try: endpoint = str(api.url_for(self, account_id=account_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: account_id = str(account_id) except Exception as exp: raise ApiError(code=400, title="Unsupported account_id", detail=repr(exp), source=endpoint) # load JSON json_data = request.get_json() if not json_data: error_detail = { '0': 'Set application/json as Content-Type', '1': 'Provide json payload' } raise ApiError(code=400, title="No input data provided", detail=error_detail, source=endpoint) else: logger.debug("json_data: " + json.dumps(json_data)) # Validate payload content schema = NewServiceLink() schema_validation_result = schema.load(json_data) # Check validation errors if schema_validation_result.errors: logger.error("Invalid payload") raise ApiError(code=400, title="Invalid payload", detail=dict(schema_validation_result.errors), source=endpoint) else: logger.debug("JSON validation -> OK") # Get slr payload try: slr_payload = json_data['data']['slr']['attributes'] except Exception as exp: logger.error("Could not fetch slr payload from json") raise ApiError(code=400, title="Could not fetch slr payload from json", detail=repr(exp), source=endpoint) # Get surrogate_id try: surrogate_id = json_data['data']['surrogate_id'] except Exception as exp: logger.error("Could not fetch surrogate id from json") raise ApiError(code=400, title="Could not fetch surrogate id from json", detail=repr(exp), source=endpoint) # Get code try: code = json_data['code'] except Exception as exp: logger.error("Could not fetch code from json") raise ApiError(code=400, title="Could not fetch code from json", detail=repr(exp), source=endpoint) # Sign SLR try: slr_signed_dict = sign_slr(account_id=account_id, slr_payload=slr_payload, endpoint=str(endpoint)) except Exception as exp: logger.error("Could not sign SLR") logger.debug("Could not sign SLR: " + repr(exp)) raise # Response data container try: response_data = {} response_data['code'] = code response_data['data'] = {} response_data['data']['slr'] = {} response_data['data']['slr']['type'] = "ServiceLinkRecord" response_data['data']['slr']['attributes'] = {} response_data['data']['slr']['attributes']['slr'] = slr_signed_dict response_data['data']['surrogate_id'] = surrogate_id except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + repr(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + repr(response_data_dict)) return make_json_response(data=response_data_dict, status_code=201)
def get_account_id_by_cr(cr_id=None, endpoint="get_account_id_by_cr(cr_id, endpoint)"): if cr_id is None: raise AttributeError("Provide cr_id as parameter") logger.info("Executing for: " + str(endpoint)) ## # Account try: logger.info("Create Account object") account_entry = Account() except Exception as exp: error_title = "Failed to create Account object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("account_entry: " + account_entry.log_entry) # Get database table name for Consent Status Record try: logger.info("Get Account table name") account_table_name = account_entry.table_name except Exception as exp: error_title = "Failed to get Account table name" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Got Account table name: " + str(account_table_name)) ## # ServiceLinkRecord try: logger.info("Create ServiceLinkRecord object") slr_entry = ServiceLinkRecord() except Exception as exp: error_title = "Failed to create ServiceLinkRecord object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("slr_entry: " + slr_entry.log_entry) # Get database table name for Consent Status Record try: logger.info("Get ServiceLinkRecord table name") slr_table_name = slr_entry.table_name except Exception as exp: error_title = "Failed to get ServiceLinkRecord table name" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Got ServiceLinkRecord table name: " + str(slr_table_name)) ## # ConsentRecord try: logger.info("Create ConsentRecord object") cr_entry = ConsentRecord() except Exception as exp: error_title = "Failed to create Consent Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("cr_entry: " + cr_entry.log_entry) # Get database table name for Consent Status Record try: logger.info("Get Consent Record table name") cr_table_name = cr_entry.table_name except Exception as exp: error_title = "Failed to get Consent Record table name" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Got Consent Record table name: " + str(cr_table_name)) # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) # Get Account ID try: logger.info("Get Account ID") cursor, account_id = get_account_id_by_csr_id( cursor=cursor, cr_id=cr_id, acc_table_name=account_table_name, slr_table_name=slr_table_name, cr_table_name=cr_table_name) except IndexError as exp: error_title = "Account ID Not Found" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=404, title=error_title, detail=repr(exp), source=endpoint) except Exception as exp: error_title = "Failed to get Account ID" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Got Account ID: " + str(cr_table_name)) return account_id