示例#1
0
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)
示例#2
0
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)
示例#3
0
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)
示例#4
0
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)
示例#5
0
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)
示例#6
0
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)
示例#7
0
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)
示例#8
0
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)
示例#9
0
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')
示例#10
0
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)
示例#11
0
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
示例#12
0
def handle_forbidden(exception_instance):
    error("handle_forbidden")
    message = exception_instance.description
    return fault(message, STORAGE_0009, 403, PERMISSION_DENIED)
示例#13
0
def unhandled_exception(exception_instance):
    error("unhandled_exception")
    exception(exception_instance)
    message = str(exception_instance)
    return fault(message, STORAGE_0002, 404, RESOURCE_NOT_FOUND)