Skip to content

Commit 7cdab3c

Browse files
Brent1LTBrent Bumann
andauthored
Refresh access token when using a very old short-lived access token with unknown expiration (dropbox#352)
Co-authored-by: Brent Bumann <bbumann@dropbox.com>
1 parent de474e1 commit 7cdab3c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

dropbox/dropbox_client.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,10 @@ def check_and_refresh_access_token(self):
358358
:return:
359359
"""
360360
can_refresh = self._oauth2_refresh_token and self._app_key
361-
needs_refresh = self._oauth2_access_token_expiration and \
362-
(datetime.utcnow() + timedelta(seconds=TOKEN_EXPIRATION_BUFFER)) >= \
363-
self._oauth2_access_token_expiration
361+
needs_refresh = self._oauth2_refresh_token and \
362+
(not self._oauth2_access_token_expiration or
363+
(datetime.utcnow() + timedelta(seconds=TOKEN_EXPIRATION_BUFFER)) >=
364+
self._oauth2_access_token_expiration)
364365
needs_token = not self._oauth2_access_token
365366
if (needs_refresh or needs_token) and can_refresh:
366367
self.refresh_access_token(scope=self._scope)

test/unit/test_dropbox_unit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,18 @@ def test_check_refresh_with_only_refresh(self, session_instance):
345345
dbx.check_and_refresh_access_token()
346346
assert session_instance.post.call_count == 1
347347

348+
def test_refresh_token_with_no_expiration(self, session_instance):
349+
# Test refresh token is refreshed when not given expiration
350+
dbx = Dropbox(oauth2_access_token=ACCESS_TOKEN,
351+
oauth2_refresh_token=REFRESH_TOKEN,
352+
oauth2_access_token_expiration=None,
353+
app_key=APP_KEY,
354+
app_secret=APP_SECRET,
355+
session=session_instance)
356+
dbx.check_and_refresh_access_token()
357+
assert session_instance.post.call_count == 1
358+
assert dbx._oauth2_access_token_expiration
359+
348360
def test_check_refresh_with_invalid_grant(self, invalid_grant_session_instance):
349361
dbx = Dropbox(oauth2_refresh_token=REFRESH_TOKEN,
350362
app_key=APP_KEY,

0 commit comments

Comments
 (0)