示例#1
0
def reflesh():
    data = request.get_json()
    if data is None:
        raise BadRequest(description='Request has empty data')

    allow_fields = {'token'}
    if not data.keys() >= allow_fields:
        raise BadRequest('Request data has invalid fields')

    is_black_list = BlackListMapper.token_in_black_list(data['token'])
    if is_black_list:
        raise BadRequest(description='Token in blacklist')

    auth_data = AuthMapper.find_by_token(data['token'])
    if auth_data is None:
        raise BadRequest(description='Invalid token')

    reflesh_token = AuthMapper.generate_auth_token(auth_data)
    if not reflesh_token:
        raise InternalServerError(description='Failed publish token')

    # dispose token & add black list
    is_disposed = AuthMapper.dispose_token(data['token'])
    if not is_disposed:
        raise InternalServerError(description='Failed dispose token')

    body = ApiResponseBody()
    body.token = reflesh_token

    return ApiResponse(STATUS_OK, body)
def api_error_handler(error):
    """
        Flask register_error_handler callback function
    """
    body = ApiResponseBody()
    if error is None:
        body.message = 'Internal Server Error'
        return ApiResponse(STATUS_INTERNAL_SERVER_ERROR, body)
    if hasattr(error, 'description') and error.description:
        body.message = error.description
    if hasattr(error, 'response') and error.response is not None:
        body.errors = error.response
    return ApiResponse(error.code, body)
示例#3
0
def login():
    data = request.get_json()
    if data is None:
        raise BadRequest()

    allow_fields = {'email', 'password'}
    if not data.keys() >= allow_fields:
        raise BadRequest()

    user = UserMapper.find_user_by_email('test')
    if user is None:
        raise Unauthorized()

    is_match = user.verify_password(data['password'])
    if not is_match:
        raise Unauthorized(description='Password unmatch')

    logged_in_token = AuthMapper.get_logged_in_user_token(user.id)
    if logged_in_token:
        body = ApiResponseBody('Already logged in')
        body.logged_in = True
        body.token = logged_in_token
        return ApiResponse(STATUS_OK, body)

    token = AuthMapper.generate_auth_token(user)
    if not token:
        raise InternalServerError(description='Failed publish token')

    body = ApiResponseBody('Login successfully')
    body.logged_in = True
    body.token = token

    return ApiResponse(STATUS_OK, body)
示例#4
0
def add():
    data = request.get_json()
    if data is None:
        raise BadRequest()

    allow_fields = {'id', 'name', 'email', 'password'}
    if not data.keys() >= allow_fields:
        raise BadRequest()

    user = User(**data)
    saved = UserMapper.save(user)
    if not saved:
        raise Conflict(description='Failed add data')

    body = ApiResponseBody()
    body.message = 'created'
    body.result = True
    return ApiResponse(STATUS_CREATED, body)
示例#5
0
def logout():
    data = request.get_json()
    if data is None:
        raise BadRequest()

    allow_fields = {'token'}
    if not data.keys() >= allow_fields:
        raise BadRequest('Invalid fields')

    # dispose token & add black list
    is_disposed = AuthMapper.dispose_token(data['token'])
    if not is_disposed:
        raise InternalServerError(description='Failed dispose token')

    body = ApiResponseBody('Logged out')
    body.logged_out = True
    body.token = ''
    return ApiResponse(STATUS_OK, body)
示例#6
0
def edit(id):
    data = request.get_json()
    if data is None:
        raise BadRequest()

    allow_fields = {'id', 'name', 'email', 'password'}
    if not data.keys() >= allow_fields:
        raise BadRequest()

    is_exist = UserMapper.exist_user(id)
    if not is_exist:
        raise NotFound(description='Not exist user')

    user = User(**data)
    saved = UserMapper.save(user)
    if not saved:
        raise Conflict(description='Failed edit data')

    body = ApiResponseBody()
    body.message = 'edited'
    body.result = True
    return ApiResponse(STATUS_OK, body)
示例#7
0
def index():
    users = UserMapper.find_users()
    body = ApiResponseBody()
    body.users = users
    return ApiResponse(STATUS_OK, data=body)