Exemplo n.º 1
0
def googledrive_folders(node_addon, user_addon, **kwargs):
    """ Returns all the subsequent folders under the folder id passed """
    node = kwargs.get('node') or kwargs['project']

    path = request.args.get('path', '')
    folder_id = request.args.get('folderId', 'root')

    try:
        access_token = user_addon.fetch_access_token()
    except exceptions.ExpiredAuthError:
        raise HTTPError(403)

    client = GoogleDriveClient(access_token)

    if folder_id == 'root':
        about = client.about()

        return [{
            'path': '/',
            'kind': rubeus.FOLDER,
            'id': about['rootFolderId'],
            'name': '/ (Full Google Drive)',
            'urls': {
                'folders': node.api_url_for('googledrive_folders', folderId=about['rootFolderId'])
            }
        }]

    contents = [
        to_hgrid(item, node, path=path)
        for item in client.folders(folder_id)
    ]

    return contents
Exemplo n.º 2
0
def googledrive_folder_list(node_addon, **kwargs):
    """ Returns all the subsequent folders under the folder id passed.
        Not easily generalizable due to `path` kwarg.
    """
    node = kwargs.get('node') or node_addon.owner

    path = request.args.get('path', '')
    folder_id = request.args.get('folderId', 'root')

    try:
        access_token = node_addon.fetch_access_token()
    except InvalidGrantError:
        raise HTTPError(403)

    client = GoogleDriveClient(access_token)

    if folder_id == 'root':
        about = client.about()

        return [{
            'path': '/',
            'kind': 'folder',
            'id': about['rootFolderId'],
            'name': '/ (Full Google Drive)',
            'urls': {
                'folders':
                node.api_url_for('googledrive_folder_list',
                                 folderId=about['rootFolderId'])
            }
        }]

    contents = [
        to_hgrid(item, node, path=path) for item in client.folders(folder_id)
    ]
    return contents
Exemplo n.º 3
0
def googledrive_folders(node_addon, **kwargs):
    """ Returns all the subsequent folders under the folder id passed """
    node = kwargs.get('node') or kwargs['project']

    path = request.args.get('path', '')
    folder_id = request.args.get('folderId', 'root')

    try:
        access_token = node_addon.fetch_access_token()
    except InvalidAuthError:
        raise HTTPError(403)

    client = GoogleDriveClient(access_token)

    if folder_id == 'root':
        about = client.about()

        return [{
            'path': '/',
            'kind': rubeus.FOLDER,
            'id': about['rootFolderId'],
            'name': '/ (Full Google Drive)',
            'urls': {
                'folders':
                node.api_url_for('googledrive_folders',
                                 folderId=about['rootFolderId'])
            }
        }]

    contents = [
        to_hgrid(item, node, path=path) for item in client.folders(folder_id)
    ]
    return contents
Exemplo n.º 4
0
def googledrive_folder_list(node_addon, **kwargs):
    """ Returns all the subsequent folders under the folder id passed.
        Not easily generalizable due to `path` kwarg.
    """
    node = kwargs.get('node') or node_addon.owner

    path = request.args.get('path', '')
    folder_id = request.args.get('folderId', 'root')

    try:
        access_token = node_addon.fetch_access_token()
    except InvalidAuthError:
        raise HTTPError(403)

    client = GoogleDriveClient(access_token)

    if folder_id == 'root':
        about = client.about()

        return [{
            'path': '/',
            'kind': 'folder',
            'id': about['rootFolderId'],
            'name': '/ (Full Google Drive)',
            'urls': {
                'folders': node.api_url_for('googledrive_folder_list', folderId=about['rootFolderId'])
            }
        }]

    contents = [
        to_hgrid(item, node, path=path)
        for item in client.folders(folder_id)
    ]
    return contents
Exemplo n.º 5
0
class GoogleDriveProvider(ExternalProvider):
    name = 'Google Drive'
    short_name = 'googledrive'

    client_id = drive_settings.CLIENT_ID
    client_secret = drive_settings.CLIENT_SECRET

    auth_url_base = '{}{}'.format(
        drive_settings.OAUTH_BASE_URL,
        'auth?access_type=offline&approval_prompt=force')
    callback_url = '{}{}'.format(drive_settings.API_BASE_URL,
                                 'oauth2/v3/token')
    auto_refresh_url = callback_url
    refresh_time = drive_settings.REFRESH_TIME

    default_scopes = drive_settings.OAUTH_SCOPE
    _auth_client = GoogleAuthClient()
    _drive_client = GoogleDriveClient()

    def handle_callback(self, response):
        client = self._auth_client
        info = client.userinfo(response['access_token'])
        return {
            'provider_id': info['sub'],
            'display_name': info['name'],
            'profile_url': info.get('profile', None)
        }

    def fetch_access_token(self, force_refresh=False):
        self.refresh_oauth_key(force=force_refresh)
        return self.account.oauth_key
Exemplo n.º 6
0
    def get_folders(self, **kwargs):
        node = self.owner

        #  Defaults exist when called by the API, but are `None`
        path = kwargs.get('path') or ''
        folder_id = kwargs.get('folder_id') or 'root'

        try:
            access_token = self.fetch_access_token()
        except exceptions.InvalidAuthError:
            raise HTTPError(403)

        client = GoogleDriveClient(access_token)
        if folder_id == 'root':
            about = client.about()

            return [{
                'addon': self.config.short_name,
                'path': '/',
                'kind': 'folder',
                'id': about['rootFolderId'],
                'name': '/ (Full Google Drive)',
                'urls': {
                    'folders':
                    api_v2_url('nodes/{}/addons/googledrive/folders/'.format(
                        self.owner._id),
                               params={
                                   'path': '/',
                                   'id': about['rootFolderId']
                               })
                }
            }]

        contents = [
            to_hgrid(item, node, path=path)
            for item in client.folders(folder_id)
        ]
        return contents
Exemplo n.º 7
0
    def get_folders(self, **kwargs):
        node = self.owner

        #  Defaults exist when called by the API, but are `None`
        path = kwargs.get('path') or ''
        folder_id = kwargs.get('folder_id') or 'root'

        try:
            access_token = self.fetch_access_token()
        except exceptions.InvalidAuthError:
            raise HTTPError(403)

        client = GoogleDriveClient(access_token)
        if folder_id == 'root':
            about = client.about()

            return [{
                'addon': self.config.short_name,
                'path': '/',
                'kind': 'folder',
                'id': about['rootFolderId'],
                'name': '/ (Full Google Drive)',
                'urls': {
                    'folders': api_v2_url('nodes/{}/addons/googledrive/folders/'.format(self.owner._id),
                        params={
                            'path': '/',
                            'id': about['rootFolderId']
                    })
                }
            }]

        contents = [
            to_hgrid(item, node, path=path)
            for item in client.folders(folder_id)
        ]
        return contents
Exemplo n.º 8
0
class GoogleDriveProvider(ExternalProvider):
    name = 'Google Drive'
    short_name = 'googledrive'

    client_id = drive_settings.CLIENT_ID
    client_secret = drive_settings.CLIENT_SECRET

    auth_url_base = '{}{}'.format(
        drive_settings.OAUTH_BASE_URL,
        'auth?access_type=offline&approval_prompt=force')
    callback_url = '{}{}'.format(drive_settings.API_BASE_URL,
                                 'oauth2/v3/token')

    default_scopes = drive_settings.OAUTH_SCOPE
    _auth_client = GoogleAuthClient()
    _drive_client = GoogleDriveClient()

    def handle_callback(self, response):
        client = self._auth_client
        info = client.userinfo(response['access_token'])
        return {
            'provider_id': info['sub'],
            'display_name': info['name'],
            'profile_url': info.get('profile', None)
        }

    def _refresh_token(self, access_token, refresh_token):
        """ Handles the actual request to refresh tokens

        :param str access_token: Access token (oauth key) associated with this account
        :param str refresh_token: Refresh token used to request a new access token
        :return dict token: New set of tokens
        """
        client = self._auth_client
        if refresh_token:
            token = client.refresh(access_token, refresh_token)
            return token
        else:
            return False

    def fetch_access_token(self, force_refresh=False):
        self.refresh_access_token(force=force_refresh)
        return self.account.oauth_key

    def refresh_access_token(self, force=False):
        """ If the token has expired or will soon, handles refreshing and the storage of new tokens

        :param bool force: Indicates whether or not to force the refreshing process, for the purpose of ensuring that authorization has not been unexpectedly removed.
        """
        if self._needs_refresh() or force:
            token = self._refresh_token(self.account.oauth_key,
                                        self.account.refresh_token)
            self.account.oauth_key = token['access_token']
            self.account.refresh_token = token['refresh_token']
            self.account.expires_at = datetime.utcfromtimestamp(
                token['expires_at'])
            self.account.save()

    def _needs_refresh(self):
        if self.account.expires_at is None:
            return False
        return (self.account.expires_at - datetime.utcnow()
                ).total_seconds() < drive_settings.REFRESH_TIME