Ejemplo n.º 1
0
 def __init__(
         self,
         client_id,
         client_secret,
         store_tokens=None,
         box_device_id='0',
         box_device_name='',
         access_token=None,
         refresh_token=None,
         network_layer=None,
         refresh_lock=None,
 ):
     """
     :param client_id:
         Box API key used for identifying the application the user is authenticating with.
     :type client_id:
         `unicode`
     :param client_secret:
         Box API secret used for making OAuth2 requests.
     :type client_secret:
         `unicode`
     :param store_tokens:
         Optional callback for getting access to tokens for storing them.
     :type store_tokens:
         `callable`
     :param box_device_id:
         Optional unique ID of this device. Used for applications that want to support device-pinning.
     :type box_device_id:
         `unicode`
     :param box_device_name:
         Optional human readable name for this device.
     :type box_device_name:
         `unicode`
     :param access_token:
         Access token to use for auth until it expires.
     :type access_token:
         `unicode`
     :param refresh_token:
         Refresh token to use for auth until it expires or is used.
     :type refresh_token:
         `unicode`
     :param network_layer:
         If specified, use it to make network requests. If not, the default network implementation will be used.
     :type network_layer:
         :class:`Network`
     :param refresh_lock:
         Lock used to synchronize token refresh. If not specified, then a :class:`threading.Lock` will be used.
     :type refresh_lock:
         Context Manager
     """
     self._client_id = client_id
     self._client_secret = client_secret
     self._store_tokens_callback = store_tokens
     self._access_token = access_token
     self._refresh_token = refresh_token
     self._network_layer = network_layer if network_layer else DefaultNetwork()
     self._refresh_lock = refresh_lock or Lock()
     self._box_device_id = box_device_id
     self._box_device_name = box_device_name
     self._closed = False
Ejemplo n.º 2
0
def test_default_network_retry_after_sleeps(delay):
    default_network = DefaultNetwork()
    retry_call = Mock()
    mock_sleep = Mock()
    with patch('boxsdk.network.default_network.time.sleep', mock_sleep):
        default_network.retry_after(delay, retry_call, DEFAULT, kwarg=DEFAULT)
    mock_sleep.assert_called_once_with(delay)
    retry_call.assert_called_once_with(DEFAULT, kwarg=DEFAULT)
Ejemplo n.º 3
0
def test_default_network_request(mock_request, http_verb, test_url, access_token, generic_successful_response):
    # pylint:disable=redefined-outer-name
    default_network = DefaultNetwork()
    mock_request.return_value = generic_successful_response
    response = default_network.request(http_verb, test_url, access_token, custom_kwarg='test')
    mock_request.assert_called_once_with(http_verb, test_url, custom_kwarg='test')
    assert response.response_as_stream == generic_successful_response.raw
    assert response.access_token_used == access_token
Ejemplo n.º 4
0
def test_network_response_constructor(make_network_request_and_assert_response):
    assert DefaultNetwork().network_response_constructor is DefaultNetworkResponse

    class DefaultNetworkResponseSubclass(DefaultNetworkResponse):
        pass

    class DefaultNetworkSubclass(DefaultNetwork):
        @property
        def network_response_constructor(self):
            return DefaultNetworkResponseSubclass

    network = DefaultNetworkSubclass()
    response = make_network_request_and_assert_response(network)
    assert isinstance(response, DefaultNetworkResponseSubclass)
Ejemplo n.º 5
0
def network_layer():
    return DefaultNetwork()
Ejemplo n.º 6
0
def check_update_for_user(user):
    logger.info('Updating user')

    # Get modules info from IVLE
    logger.info('Getting IVLE modules')
    modules = get_ivle_modules(user)

    # Get all module workbins
    logger.info('Getting IVLE file structure')
    ivle_file_structure = get_ivle_file_structure(user, modules)

    if user.dropbox_token:
        # Get Dropbox info
        logger.info('Updating dropbox')
        dbx = dropbox.Dropbox(user.dropbox_token)

        # Update Dropbox using IVLE file structure
        sync_dropbox(user, dbx, modules, ivle_file_structure)

    if user.box_access_token and user.box_refresh_token:
        # TODO: I'm sure this will work 99% of the time. But need to confirm.
        oauth = OAuth2(client_id=BOX_CLIENT_ID,
                       client_secret=BOX_CLIENT_SECRET,
                       access_token=user.box_access_token,
                       refresh_token=user.box_refresh_token)

        # Immediately regenerate a new token for the next use
        new_access_token, new_refresh_token = oauth.refresh(
            access_token_to_refresh=user.box_access_token)
        user.box_access_token = new_access_token
        user.box_refresh_token = new_refresh_token
        user.save()

        # This consumes our old tokens
        box = Client(oauth, DefaultNetwork())
        sync_box(user, box, modules, ivle_file_structure)

    if user.gdrive_token:

        logger.info('Updating google drive for ' + user.ivle_user_id)
        # configure sdk
        gauth = GoogleAuth('api/client_secrets.yaml')

        # To replace refresh token with actual value, NEED TRY CATCH HERE
        tf = tempfile.NamedTemporaryFile('r+')
        with open("api/credentials.json", "r+") as jsonFile:
            data = json.load(jsonFile)
            data["refresh_token"] = user.gdrive_token
            data["client_secret"] = GDRIVE_CLIENT_SECRET
            data["client_id"] = GDRIVE_CLIENT_ID
            json.dump(data, tf)

        tf.seek(0)

        # Try to load saved client credentials
        gauth.LoadCredentialsFile(tf.name)
        if gauth.credentials is None:
            # Authenticate if they're not there
            logger.error('Authentication failed for user for google drive')
            # gauth.LocalWebserverAuth()

        else:
            # Refresh them
            logger.info('Refreshing Gdrive token')
            gauth.Refresh()

        drive = GoogleDrive(gauth)
        sync_gdrive(user, drive, modules, ivle_file_structure)
        logger.info('Sync done for Gdrive')

    if user.onedrive_access_token:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

        client = get_onedrive_client(loop)
        attach_onedrive_session(client, user)
        client.auth_provider.refresh_token()

        # Save the new tokens after refreshing
        user.onedrive_access_token = client.auth_provider._session.access_token
        user.onedrive_refresh_token = client.auth_provider._session.refresh_token
        user.save()

        sync_onedrive(user, client, modules, ivle_file_structure)
        loop.close()
Ejemplo n.º 7
0
def network():
    return DefaultNetwork()
Ejemplo n.º 8
0
def test_default_network_request(make_network_request_and_assert_response):
    # pylint:disable=redefined-outer-name
    default_network = DefaultNetwork()
    make_network_request_and_assert_response(default_network, custom_kwargs='test')