Skip to content

Commit 4a3cd1f

Browse files
Feldtcguldner
andauthored
fix: keep the client's scope when refreshing after an expired token (#616)
When an API call returns 401 expired_access_token, the retry loop in request_json_string_with_retry refreshed the access token without the scope given to Dropbox(scope=...). The token request then had no "scope" field, so the new token carried every scope of the original grant. Pass self._scope, as check_and_refresh_access_token already does. Co-authored-by: Feldt <204218226+Feldt@users.noreply.github.com> Co-authored-by: Cody <codyguldner@dropbox.com>
1 parent 1147e50 commit 4a3cd1f

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎dropbox/dropbox_client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def request_json_string_with_retry(
564564
"ExpiredCredentials status_code=%s: Refreshing and Retrying",
565565
e.status_code,
566566
)
567-
self.refresh_access_token()
567+
self.refresh_access_token(scope=self._scope)
568568
has_refreshed = True
569569
else:
570570
raise

‎test/unit/test_dropbox_unit.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# Tests OAuth Flow
1515
from dropbox import DropboxOAuth2Flow, session, Dropbox, create_session
16+
from dropbox import auth
1617
from dropbox.base import DropboxBase
1718
from dropbox.common import PathRoot
1819
from dropbox.content_hash import content_hash
@@ -787,6 +788,29 @@ def test_refresh_raises_after_exhausting_retries(self, server_error_session_inst
787788
# Initial attempt + 2 retries.
788789
assert server_error_session_instance.post.call_count == 3
789790

791+
def test_refresh_on_expired_access_token_uses_client_scope(self, session_instance, mocker):
792+
# A 401 expired_access_token triggers a refresh, which must request the
793+
# scope the client was configured with, like check_and_refresh_access_token.
794+
dbx = Dropbox(
795+
oauth2_access_token=ACCESS_TOKEN,
796+
oauth2_refresh_token=REFRESH_TOKEN,
797+
oauth2_access_token_expiration=EXPIRATION,
798+
app_key=APP_KEY,
799+
app_secret=APP_SECRET,
800+
scope=SCOPE_LIST,
801+
session=session_instance,
802+
)
803+
expired = AuthError("request-id", auth.AuthError.expired_access_token)
804+
request = mocker.patch.object(dbx, "request_json_string", side_effect=[expired, "{}"])
805+
806+
result = dbx.request_json_string_with_retry("api", "test", "rpc", "{}", USER_AUTH, None)
807+
808+
assert result == "{}"
809+
assert request.call_count == 2
810+
session_instance.post.assert_called_once()
811+
token_body = session_instance.post.call_args.kwargs["data"]
812+
assert token_body.get("scope") == " ".join(SCOPE_LIST)
813+
790814

791815
class TestAutoContentHash:
792816
def _fake_file_metadata(self, size, file_content_hash):

0 commit comments

Comments
 (0)