class StartServiceLinking(Resource): def __init__(self): super(StartServiceLinking, self).__init__() self.helpers = Helpers(current_app.config) self.service_key = self.helpers.get_key() self.is_sink = current_app.config["IS_SINK"] self.is_source = current_app.config["IS_SOURCE"] self.service_url = current_app.config["SERVICE_URL"] self.operator_url = current_app.config["OPERATOR_URL"] self.lock_wait_time = current_app.config["LOCK_WAIT_TIME"] self.parser = reqparse.RequestParser() self.parser.add_argument('code', type=str, help='session code') self.parser.add_argument('operator_id', type=str, help='Operator UUID.') self.parser.add_argument('return_url', type=str, help='Url safe Base64 coded return url.') self.parser.add_argument('surrogate_id', type=str, help="surrogate ID") # TODO: Verify service_id is unnecessary. #self.parser.add_argument('service_id', type=str, help="Service's ID") # Seems unnecessary to the flow. @error_handler @api_logging def post(self): args = self.parser.parse_args() debug_log.debug("StartServiceLinking got parameter:\n {}".format(args)) data = {"surrogate_id": args["surrogate_id"], "code": args["code"]} sq.task("Link code to generated surrogate_id") self.helpers.add_code_to_surrogate_id(args["code"], args["surrogate_id"]) if self.is_sink: data["token_key"] = self.service_key["pub"] # TODO: Are we implementing according to 1.3 in such way that we have # operator, service, user specific pop keys? sq.send_to("Operator_Components Mgmnt", "Send Operator_Components request to make SLR") endpoint = "/api/1.3/slr/link" # Todo: this needs to be fetched from somewhere result = post("{}{}".format(self.operator_url, endpoint), json=data) debug_log.info("####slr/link reply from operator: {}\n{}".format(result.status_code, result.text)) if result.ok: self.helpers.delete_session(args["code"]) return result.text, 201 elif result.status_code == 500: self.helpers.delete_session(args["code"]) raise DetailedHTTPException(status=500, detail={"msg": "Linking Service has failed due to server side issue."}, title="Could not link Service.") else: self.helpers.delete_session(args["code"]) raise DetailedHTTPException(status=result.status_code, detail={ "msg": "Something went wrong while posting to Operator_SLR for /link", "Error from Operator_SLR": loads(result.text)}, title=result.reason)
class GenerateSurrogateId(Resource): def __init__(self): super(GenerateSurrogateId, self).__init__() keysize = current_app.config["KEYSIZE"] cert_key_path = current_app.config["CERT_KEY_PATH"] self.helpers = Helpers(current_app.config) self.service_key = self.helpers.get_key() self.is_sink = current_app.config["IS_SINK"] self.is_source = current_app.config["IS_SOURCE"] self.service_id = current_app.config["SERVICE_ID"] self.service_url = current_app.config["SERVICE_URL"] self.operator_url = current_app.config["OPERATOR_URL"] self.lock_wait_time = current_app.config["LOCK_WAIT_TIME"] @timeme @error_handler @api_logging def post(self): try: # TODO: Verify this requests comes from Service Mockup(Is this our responsibility?) # This is now the point we want to double check that similar flow is not going on already for said user. user_id = request.json["user_id"] operator_id = request.json["operator_id"] sq.task("Checking if user_id is locked already.") user_is_locked = self.helpers.check_lock(user_id) if user_is_locked: time.sleep(self.lock_wait_time) user_is_locked = self.helpers.check_lock(user_id) if user_is_locked: raise DetailedHTTPException(status=503, detail={"msg": "Another SLR linking is in process, please try again once " "linking is over"}, title="User_id locked for SLR creation.") else: sq.task("Locking user_id.") self.helpers.lock_user(user_id) sq.task("Generate surrogate_id.") # TODO: Some logic to surrogate_id's? # surrogate_id is meant to be unique between operator and service. surrogate_id = sha256("{}_{}_{}".format(self.service_id, user_id, operator_id)).hexdigest() # Store surrogate_id to database self.helpers.add_surrogate_id_to_user_id(user_id, surrogate_id) sq.send_to("Service_Mockup", "Send surrogate_id to Service_Mockup") content_json = {"surrogate_id": surrogate_id} return content_json, 201 except DetailedHTTPException as e: debug_log.exception(e) self.helpers.delete_session(user=user_id) e.trace = traceback.format_exc(limit=100).splitlines() raise e except Exception as e: debug_log.exception(e) self.helpers.delete_session(user=user_id) raise DetailedHTTPException(exception=e, detail="Something failed in generating and delivering Surrogate_ID.", trace=traceback.format_exc(limit=100).splitlines())