def value_error(error_instance): error("value_error") exception(error_instance) message = str(error_instance) code = 400 title = 'Bad request' return fault(message, STORAGE_0011, code, title)
def type_error(error_instance): error("type_error") exception(error_instance) message = str(error_instance).replace("__init__() ", "") code = 400 title = 'Bad request' return fault(message, STORAGE_0012, code, title)
def connexion_bad_request(exception_instance): error("connexion_bad_request") exception(exception_instance) message = exception_instance.detail code = exception_instance.status title = exception_instance.title return fault(message, STORAGE_9999, code, title)
def storage_bad_request(exception_instance): error("storage_bad_request") exception(exception_instance) message = exception_instance.description code = 409 title = 'Bad request' return fault(message, exception_instance.error_code, code, title)
def bad_request(exception_instance): error("bad_request") exception(exception_instance) message = exception_instance.description code = 400 title = 'Bad request' return fault(message, STORAGE_0004, code, title)
def fault(detail, error_code, status, title, entity_type=None, instance=None, action=None): json_fault = empty_json_object() json_fault['error_code'] = error_code add_system_parameters(json_fault, detail, status, title) parameters = request_parameters() if entity_type is None: entity_type = parameters.get('type') if instance is None: instance = parameters.get('instance') if action is None: action = parameters.get('action') if entity_type is not None: json_fault['type'] = entity_type if instance is not None: json_fault['instance'] = instance if action is not None: json_fault['action'] = action error('json.fault: %s, %s', status, json_fault) if "fault" in g: json_ext_fault = g.pop('fault') json_fault['fault'] = json_ext_fault return make_json_response(json_fault, status)
def internal_server_error(error_instance): error("internal_server_error") exception(error_instance) message = str(error_instance) code = 500 if isinstance(error_instance, HTTPException): code = error_instance.code title = 'Internal server error' return fault(message, STORAGE_0001, code, title)
def integrity_error(error_instance): error("integrity_error") exception(error_instance) if "unique constraint" in str(error_instance): message = "Unique constraint: please check request values on uniquiness" else: message = str(error_instance) code = 400 title = 'Bad request' return fault(message, STORAGE_0014, code, title)
def decode_jwt_token(token, secret): try: decode = jwt.decode(token, secret, algorithms=['HS256']) return decode except jwt.ExpiredSignatureError: error('Permission denied. Token expired') raise Unauthorized('Permission denied. Token expired') except jwt.DecodeError: error('Permission denied. Wrong token') raise Unauthorized('Permission denied. Wrong token')
def invalid_request_error(error_instance): error("invalid_request_error") exception(error_instance) message = str(error_instance) \ .replace("'", "") \ .replace("class ", "") \ .replace("app.classes.", "") \ .replace("<", "") \ .replace(">", "") code = 400 title = 'Bad request' return fault(message, STORAGE_0013, code, title)
def verify_auth_token(token, ip='0.0.0.0', path='/', grant='get_all', raise_error=True): token_checked = ("token_checked" in g and g.token_checked) if token_checked: return True if '127.0.0.1' in ip or '0.0.0.0' in ip: info('Permission granted. System request.') g.token_checked = True return True try: payload = decode_token(token) if "login" in payload: g.login = payload['login'] if "id" in payload: g.user_id = payload['id'] if "role" in payload: g.user_role = payload['role'] except Exception as e: exception(e) if raise_error: raise e else: return False permission = check_grants(payload['role'], path, grant) if permission is None: error('Permission denied. No rights for ' + path) if raise_error: raise Unauthorized('Permission denied. No rights for ' + path) else: return False g.token_checked = True return True
def handle_forbidden(exception_instance): error("handle_forbidden") message = exception_instance.description return fault(message, STORAGE_0009, 403, PERMISSION_DENIED)
def unhandled_exception(exception_instance): error("unhandled_exception") exception(exception_instance) message = str(exception_instance) return fault(message, STORAGE_0002, 404, RESOURCE_NOT_FOUND)