Example #1
0
def user_apikey_route(user_id, action):
    """manage apikey for user"""

    user = User.query.get(user_id)
    form = ButtonForm()
    if user and form.validate_on_submit():

        if action == 'generate':
            apikey = PWS.generate_apikey()
            user.apikey = PWS.hash_simple(apikey)
            db.session.commit()
            return jsonify({
                'title': 'Apikey operation',
                'detail': 'New apikey generated: %s' % apikey
            }), HTTPStatus.OK

        if action == 'revoke':
            user.apikey = None
            db.session.commit()
            return jsonify({
                'title': 'Apikey operation',
                'detail': 'Apikey revoked'
            }), HTTPStatus.OK

    return jsonify({
        'title': 'Apikey operation',
        'detail': 'Invalid request'
    }), HTTPStatus.BAD_REQUEST
Example #2
0
def apikey():
    """yield valid apikey for user in role agent"""

    tmp_apikey = PWS.generate_apikey()
    db.session.add(User(username='******', apikey=PWS.hash_simple(tmp_apikey), active=True, roles=['agent']))
    db.session.commit()
    yield tmp_apikey
Example #3
0
def add_agent():
    """add new agent"""

    apikey = PWS.generate_apikey()
    agent = User(username=f'agent_{uuid4()}',
                 apikey=apikey,
                 active=True,
                 roles=['agent'])
    db.session.add(agent)
    db.session.commit()
    print(f'new agent {agent.username} apikey {apikey}')