Пример #1
0
def login_token(credentials: HTTPBasicCredentials = Depends(security)):
    """Return token if given credentials are valid."""
    logger.info(f'Login request with {credentials=}')
    check_credentials(credentials)
    token = create_login_key()
    router.token_keys.append(token)
    return {'token': token}
Пример #2
0
def login_session(credentials: HTTPBasicCredentials = Depends(security)):
    """Create session if given credentials are valid."""
    logger.info(f'Login request with {credentials=}')
    check_credentials(credentials)
    response = Response(status_code=status.HTTP_201_CREATED)
    session = create_login_key()
    response.set_cookie('session_token', session)
    router.session_keys.append(session)
    return response
Пример #3
0
Файл: gs.py Проект: wbez/debates
def update_metadata(id, name, src_folder_id, dest_folder_id):
    """
    update metadata for newly created gas projects
    """

    fields = {
        'removeParents': '%s' % (src_folder_id),
        'addParents': '%s' % (dest_folder_id),
    }
    params = urlencode(fields)
    url = "%s/%s?%s" % (DRIVE_API_URL, id, params)

    # Compose payload
    payload = {
        'title': name,
    }

    kwargs = {
        'credentials': check_credentials(),
        'url': url,
        'method': 'PATCH',
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': json.dumps(payload)
    }

    resp = send_api_request(kwargs)

    if resp.status == 200:
        return True
    return False
Пример #4
0
Файл: gs.py Проект: wbez/debates
def get_project_metadata(name=None, verbose=False):
    """
    Get a google app project metadata, pass the id of the project
    """
    # Parse boolean fabric args
    verbose = prep_bool_arg(verbose)

    name = name if name else app_config.PROJECT_SLUG

    id = get_gas_project_id(name)

    if not id:
        exit()
    url = '%s/%s' % (DRIVE_API_URL, id)

    kwargs = {'credentials': check_credentials(), 'url': url, 'method': 'GET'}

    resp = send_api_request(kwargs)

    if resp.status == 200:
        display_project_metadata_results(resp, verbose)
    elif resp.status == 403:
        resp = send_api_request(kwargs, retry=True)
        if resp.status == 200:
            display_project_metadata_results(resp, verbose)
        else:
            logger.error('Error (%s).' % resp.status)
    else:
        logger.error('Error (%s).' % resp.status)
Пример #5
0
def catch_all(path):
    auth = True
    if settings.auth.type == 'basic':
        auth = request.authorization
        if not (auth and check_credentials(request.authorization.username,
                                           request.authorization.password)):
            return ('Unauthorized', 401, {
                'WWW-Authenticate': 'Basic realm="Login Required"'
            })
    elif settings.auth.type == 'form':
        if 'logged_in' not in session:
            auth = False

    try:
        updated = database.execute("SELECT updated FROM system",
                                   only_one=True)['updated']
    except:
        updated = False

    inject = dict()
    inject["baseUrl"] = base_url
    inject["canUpdate"] = not args.no_update
    inject["hasUpdate"] = updated != '0'

    if auth:
        inject["apiKey"] = settings.auth.apikey

    template_url = base_url
    if not template_url.endswith("/"):
        template_url += "/"

    return render_template("index.html",
                           BAZARR_SERVER_INJECT=inject,
                           baseUrl=template_url)
Пример #6
0
def list_projects(owner=None):
    """
    List all the google projects on your drive, pass owner to filter out
    """
    if (owner):
        url = "%s and '%s' in owners" % (GS_QUERY_URL, owner)
    else:
        url = GS_QUERY_URL

    kwargs = {
        'credentials': check_credentials(),
        'url': url,
        'method': 'GET'
    }

    resp = send_api_request(kwargs)

    if resp.status == 200:
        display_list_projects_results(resp)
    elif resp.status == 403:
        resp = send_api_request(kwargs, retry=True)
        if resp.status == 200:
            display_list_projects_results(resp)
        else:
            logger.error('Error (%s).' % resp.status)
    else:
        logger.error('Error (%s).' % resp.status)
Пример #7
0
Файл: gs.py Проект: wbez/debates
def get_doc_permissions(id=None):
    """
    get google doc permissions
    """

    require('settings', provided_by=['production', 'staging', 'development'])
    # Get the script id from the script name and deployment target
    # prioritize passed in parameters
    if not id:
        id = app_config.TRANSCRIPT_GDOC_KEY

    url = "%s/%s/permissions" % (DRIVE_API_URL, id)
    kwargs = {'credentials': check_credentials(), 'url': url, 'method': 'GET'}

    resp = send_api_request(kwargs)

    if resp.status == 200:
        with open('data/dict_permissions.txt', 'w') as f:
            for permission in resp.data['items']:
                try:
                    logger.info('Permission: %s' % permission)
                    extraRoles = permission['additionalRoles']
                    f.write("%s,%s\n" %
                            (permission['emailAddress'], ",".join(extraRoles)))
                except KeyError:
                    try:
                        f.write(
                            "%s,%s\n" %
                            (permission['emailAddress'], permission['role']))
                    except KeyError:
                        pass

    else:
        logger.error('Error (%s).' % resp.status)
Пример #8
0
Файл: gs.py Проект: wbez/debates
def get_folder_id(name):
    """
    gets the project id from the supplied name
    """
    FOLDER_MIME_TYPE = 'application/vnd.google-apps.folder'
    query = "title = '%s' and mimeType = '%s' and '%s' in parents" % (
        name, FOLDER_MIME_TYPE, app_config.PARENT_FOLDER_ID)

    fields = {'q': query, 'pageSize': 1, 'spaces': 'drive'}
    params = urlencode(fields)
    url = "%s?%s" % (DRIVE_API_URL, params)
    kwargs = {'credentials': check_credentials(), 'url': url, 'method': 'GET'}

    resp = send_api_request(kwargs)

    if resp.status == 200:
        return extract_id(resp)
    elif resp.status == 403:
        resp = send_api_request(kwargs, retry=True)
        if resp.status == 200:
            return extract_id(resp)
        else:
            logger.error('Error (%s).' % resp.status)
    else:
        logger.error('Error (%s).' % resp.status)
    return None
Пример #9
0
 def wrapper(*args, **kwargs):
     if settings.auth.type == 'basic':
         auth = request.authorization
         if not (auth and check_credentials(request.authorization.username, request.authorization.password)):
             return ('Unauthorized', 401, {
                 'WWW-Authenticate': 'Basic realm="Login Required"'
             })
     elif settings.auth.type == 'form':
         if 'logged_in' not in session:
             return abort(401, message="Unauthorized")
     actual_method(*args, **kwargs)
Пример #10
0
def execute_setup(script_name=None, doc_id=None, log_id=None):
    """
    execute script setup: params script_id, document_id, log_id
    """

    require('settings', provided_by=['production', 'staging', 'development'])

    # Get secrets
    secrets = app_config.get_secrets()
    verb8tm_srt_url = secrets.get('VERB8TM_SRT_API',
                                  app_config.PROJECT_SLUG)
    verb8tm_timestamp_url = secrets.get('VERB8TM_TIMESTAMP_API',
                                        app_config.PROJECT_SLUG)

    # Get the script id from the script name and deployment target
    # prioritize passed in parameters
    if not script_name:
        script_name = '%s_%s' % (app_config.DEPLOYMENT_TARGET,
                                 app_config.SCRIPT_PROJECT_NAME)

    script_id = get_gas_project_id(script_name)

    URL_PREFIX = 'https://script.googleapis.com/v1/scripts/'

    # url
    url = '%s%s:run' % (URL_PREFIX, script_id)

    # Compose payload we pass documentID and logID to setup script properties
    payload = {
        'function': 'setup',
        'parameters': [verb8tm_srt_url,
                       verb8tm_timestamp_url,
                       app_config.LIVEBLOG_GDOC_KEY,
                       app_config.GAS_LOG_KEY]
    }

    kwargs = {
        'credentials': check_credentials(),
        'url': url,
        'method': 'POST',
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(payload)
    }

    resp = send_api_request(kwargs)

    if resp.status == 200:
        return True
    else:
        print resp.status
        exit()
    return False
Пример #11
0
def get_project_files(name=None):
    """
    Get all files in a given google apps script project
    """
    credentials = check_credentials()

    name = name if name else app_config.PROJECT_SLUG

    id = get_gas_project_id(name)

    if not id:
        exit()
    url = '%s/%s' % (DRIVE_API_URL, id)

    kwargs = {
        'credentials': credentials,
        'url': url,
        'method': 'GET'
    }

    resp = send_api_request(kwargs)
    if resp.status == 200:
        url = resp.data['exportLinks'][MIMETYPE]
    elif resp.status == 403:
        resp = send_api_request(kwargs, retry=True)
        if resp.status == 200:
            url = resp.data['exportLinks'][MIMETYPE]
        else:
            logger.error('Error (%s).' % resp.status)
            exit()
    else:
        logger.error('Error (%s).' % resp.status)
        exit()

    if not url:
        logger.error('Did not find download url for %s' % (id))
        exit()

    kwargs = {
        'credentials': credentials,
        'url': url,
        'method': 'GET'
    }

    resp = send_api_request(kwargs)
    if resp.status == 200:
        return get_project_files_results(resp)
    else:
        logger.error('Error (%s).' % resp.status)
Пример #12
0
    def post(self):
        if settings.auth.type != 'form':
            return '', 405

        action = request.args.get('action')
        if action == 'login':
            username = request.form.get('username')
            password = request.form.get('password')
            if check_credentials(username, password):
                session['logged_in'] = True
                return '', 204
        elif action == 'logout':
            session.clear()
            gc.collect()
            return '', 204

        return '', 401
Пример #13
0
 def list_images(self):
     auth = check_credentials(self.opts['token'])
     endpoints, user_id = endpoints_and_user_id(auth)    
     plankton = init_plankton(endpoints['plankton'], self.opts['token'])
     list_current_images = plankton.list_public(True, 'default')
     available_images = []
     for image in list_current_images:
         # owner of image will be checked based on the uuid
         if image['owner'] == const_escience_uuid:
             image_properties = image['properties']
             if image_properties.has_key('escienceconf'):
                 available_images.append(image['name'])
         elif image['name'] == "Debian Base":
             available_images.append(image['name'])
     available_images.sort()
     for image in available_images:
         print "{name}".format(name=image)
Пример #14
0
Файл: gs.py Проект: wbez/debates
def send_api_request(kwargs, retry=False):
    """
    Prompt user for confirmation, erase credentials and reauthenticate
    Returns the authomatic response
    """
    # Refresh the credentials scope and retry
    if retry:
        message = 'Forbidden response. Want to update the credentials & retry?'
        answer = prompt(message, default="No")
        if answer.lower() not in ('y', 'yes', 'buzz off', 'screw you'):
            logger.info('Ok so no retry...bye')
            exit()
        path = os.path.expanduser(app_config.GOOGLE_OAUTH_CREDENTIALS_PATH)
        os.remove(path)
        kwargs['credentials'] = check_credentials()
    logger.debug('API Request: %s ' % kwargs)
    resp = app_config.authomatic.access(**kwargs)
    logger.debug('API Response: %s ' % resp.content)
    return resp
Пример #15
0
 def wrapper(*args, **kwargs):
     headers = kwargs['headers']
     response_body = get_body('index.html')
     if 'Authorization' not in headers:
         return "HTTP/1.1 401 Unauthorized", [
             'WWW-Authenticate: Basic realm="Log in"'
         ], response_body
     else:
         try:
             credentials = base64.b64decode(headers['Authorization'].split(
                 ' ')[1]).decode("utf-8").split(':')
             if not check_credentials(credentials):
                 return "HTTP/1.1 401 Unauthorized", [
                     'WWW-Authenticate: Basic realm="Log in"'
                 ], response_body
         except Exception as error:
             return "HTTP/1.1 400 Bad Request", [
                 'WWW-Authenticate: Basic realm="Log in "'
             ], ''
     return view(*args, **kwargs)
Пример #16
0
Файл: gs.py Проект: wbez/debates
def get_gas_project_id(name):
    """
    gets the project id from the supplied name
    """
    url = "%s and title='%s'" % (GS_QUERY_URL, name)
    kwargs = {'credentials': check_credentials(), 'url': url, 'method': 'GET'}

    resp = send_api_request(kwargs)

    if resp.status == 200:
        return extract_id(resp)
    elif resp.status == 403:
        resp = send_api_request(kwargs, retry=True)
        if resp.status == 200:
            return extract_id(resp)
        else:
            logger.error('Error (%s).' % resp.status)
    else:
        logger.error('Error (%s).' % resp.status)
    return None
Пример #17
0
def create(name=None, folderid=None, folder=None, src='code'):
    """
    Create a new google apps script project
    """
    require('settings', provided_by=['production', 'development'])

    if not name:
        if app_config.DEPLOYMENT_TARGET == 'production':
            name = app_config.GAS_PROJECT_NAME
        else:
            name = '%s_%s' % (app_config.DEPLOYMENT_TARGET,
                              app_config.GAS_PROJECT_NAME)

    id = get_gas_project_id(name, quiet=True)

    if id:
        logger.error("Found project with the same name, use upsert to update")
        exit()

    # Get existing project files in drive
    dest_folder_id = None

    files_to_upload = [
        f for f in glob.glob('%s/*' % src) if f.split('.')[-1] in EXTS.keys()
    ]

    if not folderid:
        if folder:
            dest_folder_id = get_folder_id(folder)
        else:
            dest_folder_id = get_folder_id(app_config.DEPLOYMENT_TARGET)
        if not dest_folder_id:
            logger.error('Did not find the given folder, create it first')
            exit()
        logger.info('folder id: %s' % dest_folder_id)
    else:
        dest_folder_id = folderid

    payload = {"files": []}

    for file_path in files_to_upload:
        key = os.path.basename(file_path)
        file_name, ext = key.split('.')
        try:
            file_type = EXTS[ext]
        except KeyError:
            continue

        file_to_upload = {}

        with open(file_path) as fp:
            file_contents = fp.read()

        file_to_upload.update({
            'name': file_name,
            'type': file_type,
            'source': file_contents
        })

        payload['files'].append(file_to_upload)

    logger.info('Uploading %s files... ' % len(payload['files']))

    # Prepare API request
    kwargs = {
        'credentials': check_credentials(),
        'url': '%s?convert=true' % (UPLOAD_URL_TPL),
        'method': 'POST',
        'headers': {
            'Content-Type': 'application/vnd.google-apps.script+json'
        },
        'body': json.dumps(payload),
    }

    resp = send_api_request(kwargs)

    if resp.status == 200:
        id = resp.data['id']
        if not id:
            logger.error('did not get an id for the new project')
            exit()
        src_folder_id = resp.data['parents'][0]['id']
        success = update_metadata(id, name, src_folder_id, dest_folder_id)
        if success:
            logger.info('Done.')
        else:
            logger.error('Project created but could not update metadata')
    else:
        if resp.status == 403:
            resp = send_api_request(kwargs, retry=True)
            if resp.status == 200:
                logger.info('Done.')
                exit()
        logger.error('Error (%s).' % resp.status)
Пример #18
0
import sys
from utils import check_credentials
from agent import Supervisor
from store import save_to_file
from constants import AGENTS_FILE_PATH

if __name__ == '__main__':
    if check_credentials(sys.argv):
        print('Welcome!!!')
        agent = Supervisor.create_agent()
        save_to_file(AGENTS_FILE_PATH, Supervisor.agent_data())

    else:
        print('Wrong credentials, try again...')
Пример #19
0
Файл: gs.py Проект: wbez/debates
def execute_setup(name=None,
                  doc_id=None,
                  log_id=None,
                  cspan=None,
                  cspan_server=None):
    """
    execute script setup
    params:
    name - script name
    document_id - associated google doc
    log_id - associated google logs spreadsheet
    cspan - False to use Verb8tm, True to use CSPAN
    cspan_server - url that will serve the CSPAN captions
    """

    require('settings', provided_by=['production', 'staging', 'development'])
    # Get secrets
    secrets = app_config.get_secrets()
    verb8tm_srt_url = secrets.get('VERB8TM_SRT_API', app_config.PROJECT_SLUG)
    verb8tm_timestamp_url = secrets.get('VERB8TM_TIMESTAMP_API',
                                        app_config.PROJECT_SLUG)
    if cspan:
        pass
    else:
        cspan = str(app_config.CSPAN)

    ec2_public_dns_tpl = 'http://ec2-%s.compute-1.amazonaws.com:5000/'
    if cspan_server:
        pass
    else:
        if len(app_config.SERVERS):
            ip = app_config.SERVERS[0]
            ip = ip.replace('.', '-')
            cspan_server = ec2_public_dns_tpl % (ip)
        else:
            logger.error("no servers found, please specify a cspan_server")
            exit()

    # Get the script id from the script name and deployment target
    # prioritize passed in parameters
    if not name:
        name = '%s_%s' % (app_config.DEPLOYMENT_TARGET,
                          app_config.SCRIPT_PROJECT_NAME)

    script_id = get_gas_project_id(name)

    URL_PREFIX = 'https://script.googleapis.com/v1/scripts/'

    # url
    url = '%s%s:run' % (URL_PREFIX, script_id)

    # Compose payload we pass documentID and logID to setup script properties
    payload = {
        'function':
        'setup',
        'parameters': [
            verb8tm_srt_url, verb8tm_timestamp_url, cspan_server, cspan,
            app_config.TRANSCRIPT_GDOC_KEY, app_config.GAS_LOG_KEY
        ]
    }

    kwargs = {
        'credentials': check_credentials(),
        'url': url,
        'method': 'POST',
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': json.dumps(payload)
    }

    resp = send_api_request(kwargs)

    if resp.status == 200:
        return True
    else:
        print resp.status
        exit()
    return False
Пример #20
0
Файл: gs.py Проект: wbez/debates
def upsert(name=None, src='google_apps_scripts'):
    """
    Upload project files to drive,
    pass the id of the project and a source path
    """
    require('settings', provided_by=['production', 'staging', 'development'])

    if not name:
        name = '%s_%s' % (app_config.DEPLOYMENT_TARGET,
                          app_config.SCRIPT_PROJECT_NAME)

    id = get_gas_project_id(name)

    if not id:
        exit()

    existing_files = get_project_files(name)

    files_to_upload = [
        f for f in glob.glob('%s/*' % src) if f.split('.')[-1] in EXTS.keys()
    ]

    payload = {"files": []}

    for file_path in files_to_upload:
        key = os.path.basename(file_path)
        file_name, ext = key.split('.')
        try:
            file_type = EXTS[ext]
        except KeyError:
            continue

        try:
            file_to_upload = {'id': existing_files[key]['id']}
            logger.info(' - Replace %s (id=%s) with %s.' %
                        (key, existing_files[key]['id'], key))
        except KeyError:
            logger.info(' - New file %s found.' % key)
            logger.info("No existing file found for %s." % key)
            file_to_upload = {}

        with open(file_path) as fp:
            file_contents = fp.read()

        file_to_upload.update({
            'name': file_name,
            'type': file_type,
            'source': file_contents
        })

        payload['files'].append(file_to_upload)

    logger.info('Uploading %s files... ' % len(payload['files']))

    # Prepare API request
    kwargs = {
        'credentials': check_credentials(),
        'url': '%s/%s' % (UPLOAD_URL_TPL, id),
        'method': 'PUT',
        'headers': {
            'Content-Type': 'application/vnd.google-apps.script+json'
        },
        'body': json.dumps(payload),
    }

    resp = send_api_request(kwargs)

    if resp.status == 200:
        logger.info('Done.')
    else:
        if resp.status == 403:
            resp = send_api_request(kwargs, retry=True)
            if resp.status == 200:
                logger.info('Done.')
                exit()
        logger.error('Error (%s).' % resp.status)
Пример #21
0
Файл: gs.py Проект: wbez/debates
def create(name=None, folderid=None, folder=None, src='google_apps_scripts'):
    """
    Create a new google apps script project
    """

    # Get existing project files in drive
    dest_folder_id = None
    name = name if name else app_config.PROJECT_SLUG

    files_to_upload = [
        f for f in glob.glob('%s/*' % src) if f.split('.')[-1] in EXTS.keys()
    ]

    if not folderid:

        if folder:
            dest_folder_id = get_folder_id(folder)
            if not dest_folder_id:
                logger.error(
                    'Did not find the given folder, you need to create it first'
                )
                exit()
            print dest_folder_id
    else:
        dest_folder_id = folderid

    payload = {"files": []}

    for file_path in files_to_upload:
        key = os.path.basename(file_path)
        file_name, ext = key.split('.')
        try:
            file_type = EXTS[ext]
        except KeyError:
            continue

        file_to_upload = {}

        with open(file_path) as fp:
            file_contents = fp.read()

        file_to_upload.update({
            'name': file_name,
            'type': file_type,
            'source': file_contents
        })

        payload['files'].append(file_to_upload)

    logger.info('Uploading %s files... ' % len(payload['files']))

    # Prepare API request
    kwargs = {
        'credentials': check_credentials(),
        'url': '%s?convert=true' % (UPLOAD_URL_TPL),
        'method': 'POST',
        'headers': {
            'Content-Type': 'application/vnd.google-apps.script+json'
        },
        'body': json.dumps(payload),
    }

    resp = send_api_request(kwargs)

    if resp.status == 200:
        id = resp.data['id']
        if not id:
            logger.error('did not get an id for the new project')
            exit()
        src_folder_id = resp.data['parents'][0]['id']
        success = update_metadata(id, name, src_folder_id, dest_folder_id)
        logger.info('Done.')
    else:
        if resp.status == 403:
            resp = send_api_request(kwargs, retry=True)
            if resp.status == 200:
                logger.info('Done.')
                exit()
        logger.error('Error (%s).' % resp.status)