Exemple #1
0
def _set(className, ifInState, create, update, destroy):
    """
    setFoos method, see specs.
    """
    # TODO handle ifInState
    newState = auth.current_user.getNextState()
    response = {
        'oldState': None,
        'newState': newState,
        'created': {},
        'updated': [],
        'destroyed': [],
        'notCreated': {},
        'notUpdated': {},
        'notDestroyed': {},
    }
    # create
    for id in create:
        try:
            obj = className()
            obj.data = create[id]
            obj.userId = auth.current_user.id
            obj.save()
            response['created'][id] = obj.toDict()
        except Exception as e:
            current_app.logger.warning(e)
            response['notCreated'][id] = {'type': e, 'description': str(e)}

    # update
    for id, data in update:
        try:
            # doesn't work
            obj = className.getById(id, auth.current_user.id)
            obj.setFromArray(data, newState)
            obj.save()
            response['updated'].push(id)
        except Exception as e:
            current_app.logger.warning(e)
            response['notUpdated'][id] = {'type': e, 'description': e.message}

    # destroy
    for id, data in destroy:
        try:
            obj = className.getById(id)
            obj.markAsDeleted(newState)
            obj.save()
            response['destroyed'].push(id)
        except Exception as e:
            current_app.logger.warning(e)
            response['notDestroyed'][id] = {
                'type': e,
                'description': e.message
            }

    database.commit()
    current_app.logger.debug(response)
    yield ['accounts', response]
Exemple #2
0
def revoke_token():
    """
    Revoke the current access token.
    """
    auth.require_authorization()

    auth.current_device.delete()
    database.commit()

    return Response(status=204)
Exemple #3
0
def revoke_token():
    """
    Revoke the current access token.
    """
    auth.require_authorization()

    auth.current_device.delete()
    database.commit()

    return Response(status=204)
Exemple #4
0
def access_token():
    """
    Generate an access token. Also do the password check.
    Do not handle rate limit.
    """
    # this throw a BadRequest if json is not sent
    data = request.get_json()

    if data is None:
        abort(400)

    if 'method' not in data:
        for param in ['username', 'clientVersion', 'clientName', 'deviceName']:
            if param not in data or data[param] == '':
                abort(400)

    else:
        if data['method'] != 'password':
            return make_response(get_continuation_token_response(data),
                                 status=401)

        try:
            # max age 5 minutes
            # FIXME should be moved to the config file
            tokenData = auth.getURLSafeSerializer().loads(data['token'],
                                                          max_age=300)
        except SignatureExpired:
            return make_response(get_continuation_token_response(tokenData),
                                 status=401)
        except BadSignature:
            abort(403)

        try:
            user = User.query.filter_by(username=tokenData['username']).one()
            user.checkPwd(data['password'])
        except database.NoResultFound:
            return make_response(get_continuation_token_response(tokenData),
                                 status=401)
        except User.BadPassword:
            return make_response(get_continuation_token_response(tokenData),
                                 status=401)

        # getOrCreate
        device = Device.findOrCreate(user.id, tokenData)
        device.save()
        database.commit()

        auth.setUserAndDevice(user, device)

        response = get_endpoints()
        response['accessToken'] = auth.createAccessToken()

        return make_response(response, status=201)

    return make_response(get_continuation_token_response(data))
Exemple #5
0
def user(password=None):
    user = User()
    user.username = func_name(depth=3)

    if not password:
        password = user.username + 'password'

    user.setPassword(password)
    user.save()
    commit()
    return user
Exemple #6
0
def user(password=None):
    user = User()
    user.username = func_name(depth=3)

    if not password:
        password = user.username + "password"

    user.setPassword(password)
    user.save()
    commit()
    return user
Exemple #7
0
def access_token():
    """
    Generate an access token. Also do the password check.
    Do not handle rate limit.
    """
    # this throw a BadRequest if json is not sent
    data = request.get_json()

    if data is None:
        abort(400)

    if 'method' not in data:
        for param in ['username', 'clientVersion', 'clientName', 'deviceName']:
            if param not in data or data[param] == '':
                abort(400)

    else:
        if data['method'] != 'password':
            return make_response(get_continuation_token_response(data), status=401)

        try:
            # max age 5 minutes
            # FIXME should be moved to the config file
            tokenData = auth.getURLSafeSerializer().loads(data['token'], max_age=300)
        except SignatureExpired:
            return make_response(get_continuation_token_response(tokenData), status=401)
        except BadSignature:
            abort(403)

        try:
            user = User.query.filter_by(username=tokenData['username']).one()
            user.checkPwd(data['password'])
        except database.NoResultFound:
            return make_response(get_continuation_token_response(tokenData), status=401)
        except User.BadPassword:
            return make_response(get_continuation_token_response(tokenData), status=401)

        # getOrCreate
        device = Device.findOrCreate(user.id, tokenData)
        device.save()
        database.commit()

        auth.setUserAndDevice(user, device)

        response = get_endpoints()
        response['accessToken'] = auth.createAccessToken();

        return make_response(response, status=201)

    return make_response(get_continuation_token_response(data))
Exemple #8
0
def deviceForUser(user):
    data = deviceData(user.username)
    device = Device._createFromTokenData(user.id, data)
    device.save()
    commit()
    return device
Exemple #9
0
 def result(ctx, *args, **kwargs):
     with app.app_context():
         result = ctx.invoke(f, *args, **kwargs)
         database.commit()
         return result
Exemple #10
0
def _set(className, ifInState, create, update, destroy):
    """
    setFoos method, see specs.
    """
    # TODO handle ifInState
    newState = auth.current_user.getNextState()
    response = {
        'oldState': None,
        'newState': newState,
        'created': {},
        'updated': [],
        'destroyed': [],
        'notCreated': {},
        'notUpdated': {},
        'notDestroyed': {},
    }
    # create
    for id in create:
        try:
            obj = className()
            obj.data = create[id]
            obj.userId = auth.current_user.id
            obj.save()
            response['created'][id] = obj.toDict()
        except Exception as e:
            current_app.logger.warning(e)
            response['notCreated'][id] = {
                'type': e,
                'description': str(e)
            }

    # update
    for id, data in update:
        try:
            # doesn't work
            obj = className.getById(id, auth.current_user.id)
            obj.setFromArray(data, newState)
            obj.save()
            response['updated'].push(id)
        except Exception as e:
            current_app.logger.warning(e)
            response['notUpdated'][id] = {
                'type': e,
                'description': e.message
            }

    # destroy
    for id, data in destroy:
        try:
            obj = className.getById(id)
            obj.markAsDeleted(newState)
            obj.save()
            response['destroyed'].push(id)
        except Exception as e:
            current_app.logger.warning(e)
            response['notDestroyed'][id] = {
                'type': e,
                'description': e.message
            }

    database.commit()
    current_app.logger.debug(response)
    yield ['accounts', response]
Exemple #11
0
 def result(ctx, *args, **kwargs):
     with app.app_context():
         result = ctx.invoke(f, *args, **kwargs)
         database.commit()
         return result
Exemple #12
0
def deviceForUser(user):
    data = deviceData(user.username)
    device = Device._createFromTokenData(user.id, data)
    device.save()
    commit()
    return device