Beispiel #1
0
    def create_api_app(self, user, expires_in=60):
        app = App(name='Test API', user=user)

        db.session.add(app)
        db.session.commit()

        app.secret = app.generate_auth_token(expires_in=expires_in)

        db.session.add(app)
        db.session.commit()

        return app
Beispiel #2
0
def insert_api_apps():
    """
    Insert API applications with secret keys.
    """

    for x in API_APPS:
        api_app = App.query.filter(App.name == x['name']).first()

        if not api_app:
            api_app = App(name=x['name'])

            api_app.secret = x['secret']
            db.session.add(api_app)

    db.session.commit()
Beispiel #3
0
def insert_api_apps():
    """
    Insert API applications with secret keys.
    """

    for x in API_APPS:
        api_app = App.query.filter(App.name == x['name']).first()

        if not api_app:
            api_app = App(name=x['name'])

            api_app.secret = x['secret']
            db.session.add(api_app)

    db.session.commit()
Beispiel #4
0
def get_token():
    token_schema = Token.from_json(json.loads(request.data.decode('utf-8')))
    app = App.verify_auth_token(token_schema.data['secret'])

    if not app or app.secret != token_schema.data['secret']:
        return {}, 400

    user = User.query.filter_by(email=token_schema.data['email']).first()
    if user and user.verify_password(token_schema.data['password']):
        token = Token.query.filter(Token.app_id == app.id,
                                   Token.user_id == user.id).first()
        if not token:
            token = Token(app_id=app.id, user_id=user.id)

        db.session.add(token)
        db.session.commit()

        token.token = token.generate_auth_token()

        db.session.add(token)
        db.session.commit()

        return {'token': token.token}, 200

    return {}, 400
Beispiel #5
0
def create_api_app():
    """
    Create an API application with secret key.
    """

    name = prompt('API application name')

    api_app = App(name=name)
    db.session.add(api_app)
    db.session.commit()

    api_app.secret = api_app.generate_auth_token()
    db.session.add(api_app)
    db.session.commit()

    sys.exit('\nApp "{}" was created with secret "{}"'.format(name, api_app.secret))
Beispiel #6
0
def create_api_app():
    """
    Create an API application with secret key.
    """

    name = prompt('API application name')

    api_app = App(name=name)
    db.session.add(api_app)
    db.session.commit()

    api_app.secret = api_app.generate_auth_token()
    db.session.add(api_app)
    db.session.commit()

    sys.exit('\nApp "{}" was created with secret "{}"'.format(
        name, api_app.secret))
Beispiel #7
0
def add_app():
    app_schema = App.from_json(json.loads(request.data.decode('utf-8')))

    if app_schema.errors:
        return {'errors': app_schema.errors}, 400

    user = User.query.filter(User.id == app_schema.data['user_id']).first()

    if not user:
        return {'errors': {'user': '******'}}, 400

    app = App(**app_schema.data)
    app.secret = app.generate_auth_token()
    db.session.add(app)
    db.session.commit()

    return app, 201
Beispiel #8
0
def edit_app(app_id):
    app = App.query.get_or_404(app_id)

    app_schema = App.from_json(json.loads(request.data.decode('utf-8')), partial=True)

    if app_schema.errors:
        return {'errors': app_schema.errors}, 400

    if 'user_id' in app_schema.data:
        user = User.query.filter(User.id == app_schema.data['user_id']).first()

        if not user:
            return {'errors': {'user': '******'}}, 400

    for field, value in app_schema.data.items():
        if hasattr(app, field):
            setattr(app, field, value)

    db.session.commit()

    return app, 200
Beispiel #9
0
def get_token():
    token_schema = Token.from_json(json.loads(request.data.decode('utf-8')))
    app = App.verify_auth_token(token_schema.data['secret'])

    if not app or app.secret != token_schema.data['secret']:
        return {}, 400

    user = User.query.filter_by(email=token_schema.data['email']).first()
    if user and user.verify_password(token_schema.data['password']):
        token = Token.query.filter(Token.app_id == app.id, Token.user_id == user.id).first()
        if not token:
            token = Token(app_id=app.id, user_id=user.id)

        db.session.add(token)
        db.session.commit()

        token.token = token.generate_auth_token()

        db.session.add(token)
        db.session.commit()

        return {'token': token.token}, 200

    return {}, 400