def auth_alveo(remote_user_id, remote_api_key):
    if not remote_api_key:
        abort(400, "Alveo API key was not provided")

    alveo_metadata = get_module_metadata(DOMAIN)
    if alveo_metadata is None:
        abort(404, "'alveo' module not found.")
    api_url = alveo_metadata['api_url']

    client = pyalveo.OAuth2(api_url=api_url, api_key=remote_api_key)
    user_data = client.get_user_data()

    if user_data is None:
        abort(400, "Request could not be completed. API key may be invalid.")

    user_id = user_data['user_id']

    if user_id is None:
        abort(
            400,
            "Malformed or unexpected data was received from the Alveo application server. Request could not be completed."
        )

    user_ref = User.query.filter(User.remote_id == user_id).filter(
        User.domain == DOMAIN).first()
    if user_ref is None:
        user_ref = User(user_id, DOMAIN)
        db.session.add(user_ref)
        db.session.commit()

    return user_ref
Exemplo n.º 2
0
def verify_access(remote_path, api_key):
    api_path = str(urlparse(remote_path).path)
    if '/' not in api_path or api_path == "/":
        abort(
            400,
            'Request did not include an Alveo document identifier to work with'
        )

    # We care more about the user itself than the user_id, another option
    # is to query the database for something that matches the key but that
    # would be slower
    alveo_metadata = get_module_metadata("alveo")
    api_url = alveo_metadata['api_url']
    client = pyalveo.Client(api_url=api_url,
                            api_key=api_key,
                            use_cache=False,
                            update_cache=False,
                            cache_dir=None)

    # Check if we can access the list first.
    # Would be good if we could just check Alveo permissions instead of retrieving the item directly.
    # https://github.com/Alveo/pyalveo/issues/11
    try:
        item_path = remote_path.split('/document/')[0]
        client.get_item(item_path)  # We just test for access
    except APIError as e:
        return False, str(e)

    return True, "200"
Exemplo n.º 3
0
def retrieve_doc_as_user(document_id, api_key):
    alveo_metadata = get_module_metadata("alveo")
    if alveo_metadata is None:
        abort(404, "Could not segment document. 'alveo' module not loaded")

    api_url = alveo_metadata['api_url']
    client = pyalveo.Client(api_url=api_url,
                            api_key=api_key,
                            use_cache=False,
                            update_cache=False,
                            cache_dir=None)

    audio_data = None
    try:
        audio_data = client.get_document(document_id)
    except BaseException:
        pass

    return audio_data
    def _processor_get(self, user_id, remote_path):
        api_path = str(urlparse(remote_path).path)
        if '/' not in api_path or api_path == "/":
            abort(
                400,
                'Request did not include an Alveo document identifier to segment'
            )

        # We care more about the user itself than the user_id, another option
        # is to query the database for something that matches the key but that
        # would be slower
        api_key = g.user.remote_api_key

        alveo_metadata = get_module_metadata("alveo")
        api_url = alveo_metadata['api_url']
        client = pyalveo.Client(api_url=api_url,
                                api_key=api_key,
                                use_cache=False,
                                update_cache=False,
                                cache_dir=None)

        # Check if we can access the list first.
        # Would be good if we could just check Alveo permissions instead of retrieving the item directly.
        # https://github.com/Alveo/pyalveo/issues/11
        try:
            itemlist_path = remote_path.split('/document/')[0]
            itemlist = client.get_item(itemlist_path)
        except APIError as e:
            abort(400, "Response from remote host: \n" + str(e))

        result = get_cached_result(shorten_path(remote_path))
        if result is None:
            result = segment_document(remote_path, api_key)
            if result is None:
                abort(400, 'Could not access requested document')
            else:
                cache_result(shorten_path(remote_path), result)

        return result
Exemplo n.º 5
0
from application import db
from application.alveo.module import DOMAIN
from application.datastore.model import Datastore
from application.users.model import User
from application.misc.modules import get_module_metadata

from tests import ATSTests

ALVEO_API_KEY = None
ALVEO_API_URL = None
DEFAULT_HEADERS = None

TEST_STORAGE_SPEC_VERSION = "1.0-test"

alveo_metadata = get_module_metadata('alveo')

if alveo_metadata is None:
    raise Exception('Module not loaded in app configuration.')

ALVEO_API_URL = alveo_metadata['api_url']
ALVEO_API_KEY = os.environ.get('ALVEO_API_KEY', None)

if ALVEO_API_KEY is None:
    raise Exception(
        'ALVEO_API_KEY environment variable is not set. Cannot proceed.')

DEFAULT_HEADERS = {
    'X-Api-Key': ALVEO_API_KEY,
    'X-Api-Domain': 'app.alveo.edu.au'
}