Skip to content

Commit ebfa77d

Browse files
karandeep-joharKarandeep Johar
andauthored
Add default timeout to oauth flow finish calls (#208)
* Add default timeout to oauth flow finish calls * lint fixes Co-authored-by: Karandeep Johar <kjohar@dropbox.com>
1 parent 7b111c5 commit ebfa77d

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

dropbox/dropbox.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
HOST_CONTENT,
4848
HOST_NOTIFY,
4949
pinned_session,
50+
DEFAULT_TIMEOUT
5051
)
5152

5253
PATH_ROOT_HEADER = 'Dropbox-API-Path-Root'
@@ -131,17 +132,14 @@ class _DropboxTransport(object):
131132
# the HTTP body.
132133
_ROUTE_STYLE_RPC = 'rpc'
133134

134-
# This is the default longest time we'll block on receiving data from the server
135-
_DEFAULT_TIMEOUT = 100
136-
137135
def __init__(self,
138136
oauth2_access_token=None,
139137
max_retries_on_error=4,
140138
max_retries_on_rate_limit=None,
141139
user_agent=None,
142140
session=None,
143141
headers=None,
144-
timeout=_DEFAULT_TIMEOUT,
142+
timeout=DEFAULT_TIMEOUT,
145143
oauth2_refresh_token=None,
146144
oauth2_access_token_expiration=None,
147145
app_key=None,
@@ -167,7 +165,7 @@ def __init__(self,
167165
client will wait for any single packet from the
168166
server. After the timeout the client will give up on
169167
connection. If `None`, client will wait forever. Defaults
170-
to 30 seconds.
168+
to 100 seconds.
171169
:param str oauth2_refresh_token: OAuth2 refresh token for refreshing access token
172170
:param datetime oauth2_access_token_expiration: Expiration for oauth2_access_token
173171
:param str app_key: application key of requesting application; used for token refresh
@@ -387,7 +385,7 @@ def refresh_access_token(self, host=API_HOST, scope=None):
387385
scope = " ".join(scope)
388386
body['scope'] = scope
389387

390-
timeout = self._DEFAULT_TIMEOUT
388+
timeout = DEFAULT_TIMEOUT
391389
if self._timeout:
392390
timeout = self._timeout
393391
res = self._session.post(url, data=body, timeout=timeout)

dropbox/oauth.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
API_HOST,
2424
WEB_HOST,
2525
pinned_session,
26+
DEFAULT_TIMEOUT,
2627
)
2728

2829
if six.PY3:
@@ -130,7 +131,7 @@ def __repr__(self):
130131
class DropboxOAuth2FlowBase(object):
131132

132133
def __init__(self, consumer_key, consumer_secret=None, locale=None, token_access_type='legacy',
133-
scope=None, include_granted_scopes=None, use_pkce=False):
134+
scope=None, include_granted_scopes=None, use_pkce=False, timeout=DEFAULT_TIMEOUT):
134135
if scope is not None and (len(scope) == 0 or not isinstance(scope, list)):
135136
raise BadInputException("Scope list must be of type list")
136137
if token_access_type is not None and token_access_type not in TOKEN_ACCESS_TYPES:
@@ -148,6 +149,7 @@ def __init__(self, consumer_key, consumer_secret=None, locale=None, token_access
148149
self.requests_session = pinned_session()
149150
self.scope = scope
150151
self.include_granted_scopes = include_granted_scopes
152+
self._timeout = timeout
151153

152154
if use_pkce:
153155
self.code_verifier = _generate_pkce_code_verifier()
@@ -195,7 +197,7 @@ def _finish(self, code, redirect_uri, code_verifier):
195197
if redirect_uri is not None:
196198
params['redirect_uri'] = redirect_uri
197199

198-
resp = self.requests_session.post(url, data=params)
200+
resp = self.requests_session.post(url, data=params, timeout=self._timeout)
199201
resp.raise_for_status()
200202

201203
d = resp.json()
@@ -285,7 +287,7 @@ class DropboxOAuth2FlowNoRedirect(DropboxOAuth2FlowBase):
285287
"""
286288

287289
def __init__(self, consumer_key, consumer_secret=None, locale=None, token_access_type='legacy',
288-
scope=None, include_granted_scopes=None, use_pkce=False): # noqa: E501;
290+
scope=None, include_granted_scopes=None, use_pkce=False, timeout=DEFAULT_TIMEOUT): # noqa: E501;
289291
"""
290292
Construct an instance.
291293
@@ -311,6 +313,11 @@ def __init__(self, consumer_key, consumer_secret=None, locale=None, token_access
311313
:param bool use_pkce: Whether or not to use Sha256 based PKCE. PKCE should be only use on
312314
client apps which doesn't call your server. It is less secure than non-PKCE flow but
313315
can be used if you are unable to safely retrieve your app secret
316+
:param Optional[float] timeout: Maximum duration in seconds that
317+
client will wait for any single packet from the
318+
server. After the timeout the client will give up on
319+
connection. If `None`, client will wait forever. Defaults
320+
to 100 seconds.
314321
"""
315322
super(DropboxOAuth2FlowNoRedirect, self).__init__(
316323
consumer_key=consumer_key,
@@ -320,6 +327,7 @@ def __init__(self, consumer_key, consumer_secret=None, locale=None, token_access
320327
scope=scope,
321328
include_granted_scopes=include_granted_scopes,
322329
use_pkce=use_pkce,
330+
timeout=timeout
323331
)
324332

325333
def start(self):
@@ -365,7 +373,7 @@ class DropboxOAuth2Flow(DropboxOAuth2FlowBase):
365373
def __init__(self, consumer_key, redirect_uri, session,
366374
csrf_token_session_key, consumer_secret=None, locale=None,
367375
token_access_type='legacy', scope=None,
368-
include_granted_scopes=None, use_pkce=False):
376+
include_granted_scopes=None, use_pkce=False, timeout=DEFAULT_TIMEOUT):
369377
"""
370378
Construct an instance.
371379
@@ -397,15 +405,23 @@ def __init__(self, consumer_key, redirect_uri, session,
397405
team - include team scopes in the grant
398406
Note: if this user has never linked the app, include_granted_scopes must be None
399407
:param bool use_pkce: Whether or not to use Sha256 based PKCE
408+
:param Optional[float] timeout: Maximum duration in seconds that
409+
client will wait for any single packet from the
410+
server. After the timeout the client will give up on
411+
connection. If `None`, client will wait forever. Defaults
412+
to 100 seconds.
400413
"""
414+
401415
super(DropboxOAuth2Flow, self).__init__(
402416
consumer_key=consumer_key,
403417
consumer_secret=consumer_secret,
404418
locale=locale,
405419
token_access_type=token_access_type,
406420
scope=scope,
407421
include_granted_scopes=include_granted_scopes,
408-
use_pkce=use_pkce)
422+
use_pkce=use_pkce,
423+
timeout=timeout
424+
)
409425
self.redirect_uri = redirect_uri
410426
self.session = session
411427
self.csrf_token_session_key = csrf_token_session_key

dropbox/session.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
API_NOTIFICATION_HOST = os.environ.get('DROPBOX_API_NOTIFY_HOST', HOST_NOTIFY + API_DOMAIN)
3030
WEB_HOST = os.environ.get('DROPBOX_WEB_HOST', HOST_WWW + WEB_DOMAIN)
3131

32+
# This is the default longest time we'll block on receiving data from the server
33+
DEFAULT_TIMEOUT = 100
34+
3235
_TRUSTED_CERT_FILE = pkg_resources.resource_filename(__name__, 'trusted-certs.crt')
3336

3437
# TODO(kelkabany): We probably only want to instantiate this once so that even

0 commit comments

Comments
 (0)