Beispiel #1
0
def post_request(body):
    # Create key
    key = app_common.APIKey()
    key.generate()

    # Set key properties
    if body:
        key.set_expire_time(body.get('expire_time'))
        key.set_active(body.get('active'))
        key.set_admin(body.get('admin'))
        key.set_description(body.get('description'))

    # Write key to DB
    key.save()

    return api_common.build_response(
        200, {
            'key': {
                'key_id': key.key_id,
                'secret_id': key.secret_id,
                'expire_time': key.expire_time,
                'active': key.get_active(),
                'admin': key.get_admin(),
                'description': key.description
            }
        })
Beispiel #2
0
def delete_request(body):
    if 'key_id' not in body:
        return api_common.build_response(
            400, {'error': "Parameter 'key_id' is required."})

    key = app_common.APIKey(body['key_id'])
    if not key.exists:
        return api_common.build_response(404, {'error': "Key not found."})

    key.delete()
    return api_common.build_response(204)
Beispiel #3
0
def get_request(body):
    if body.get('key_id'):
        # Fetch single key
        key = app_common.APIKey(body['key_id'])

        if not key.exists:
            return api_common.build_response(404, {'error': "Key not found."})

        return api_common.build_response(200, {'key': key.get_dict()})

    # Fetch all keys
    keys = app_common.APIKey.get_all_keys()
    response = {'count': len(keys), 'keys': keys}
    return api_common.build_response(200, response)
Beispiel #4
0
def patch_request(body):
    if 'key_id' not in body:
        return api_common.build_response(
            400, {'error': "Parameter 'key_id' is required."})

    key = app_common.APIKey(body['key_id'])
    if not key.exists:
        return api_common.build_response(404, {'error': "Key not found."})

    if 'expire_time' in body:
        key.set_expire_time(body['expire_time'])
    if 'active' in body:
        key.set_active(body['active'])
    if 'admin' in body:
        key.set_admin(body['admin'])
    if 'description' in body:
        key.set_description(body['description'])

    key.save()

    return api_common.build_response(200, {'key': key.get_dict()})
Beispiel #5
0
def authenticate_user(current_time, key_id, secret_id, provision_key=None):
    if provision_key:
        ssm = boto3.client('ssm')
        verify = ssm.get_parameter(
            Name="/{}/api/provision_key".format(os.environ['APP_NAME']),
            WithDecryption=True
        )['Parameter']['Value']
        if provision_key == verify:
            return {'auth': True, 'admin': True}

    if not key_id or not secret_id:
        return {'auth': False, 'admin': False}

    key = app_common.APIKey(key_id)

    # Fail if not exists, not active, or expired.
    if not key.exists or not key.active or (key.expire_time != 0 and key.expire_time <= current_time):
        return {'auth': False, 'admin': False}

    return {
        'auth': key.verify_secret(secret_id),
        'admin': key.get_admin()
    }