Skip to content

Commit

Permalink
Add Support for Multi-Auth (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtruong-dbx authored Jul 21, 2021
1 parent fa606b8 commit 10d92da
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
11 changes: 7 additions & 4 deletions dropbox/dropbox_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ def __init__(self,
refresh will request all available scopes for application
"""

if not (oauth2_access_token or oauth2_refresh_token):
raise BadInputException('OAuth2 access token or refresh token must be set')
if not (oauth2_access_token or oauth2_refresh_token or (app_key and app_secret)):
raise BadInputException(
'OAuth2 access token or refresh token or app key/secret must be set'
)

if headers is not None and not isinstance(headers, dict):
raise BadInputException('Expected dict, got {}'.format(headers))
Expand Down Expand Up @@ -544,11 +546,12 @@ def request_json_string(self,
url = self._get_route_url(fq_hostname, func_name)

headers = {'User-Agent': self._user_agent}
if auth_type == USER_AUTH or auth_type == TEAM_AUTH:
auth_types = auth_type.replace(' ', '').split(',')
if (USER_AUTH in auth_types or TEAM_AUTH in auth_types) and self._oauth2_access_token:
headers['Authorization'] = 'Bearer %s' % self._oauth2_access_token
if self._headers:
headers.update(self._headers)
elif auth_type == APP_AUTH:
elif APP_AUTH in auth_types:
if self._app_key is None or self._app_secret is None:
raise BadInputException(
'Client id and client secret are required for routes with app auth')
Expand Down
29 changes: 25 additions & 4 deletions test/integration/test_dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from dropbox.files import (
DeleteResult,
ListFolderError,
PathOrLink,
SharedLinkFileInfo,
)
from dropbox.common import (
PathRoot,
Expand All @@ -41,7 +43,7 @@
def _value_from_env_or_die(env_name='DROPBOX_TOKEN'):
value = os.environ.get(env_name)
if value is None:
print('Set {} environment variable to a valid token.'.format(env_name),
print('Set {} environment variable to a valid value.'.format(env_name),
file=sys.stderr)
sys.exit(1)
return value
Expand Down Expand Up @@ -72,7 +74,12 @@ def dbx_team_from_env():
def dbx_app_auth_from_env():
app_key = _value_from_env_or_die("DROPBOX_APP_KEY")
app_secret = _value_from_env_or_die("DROPBOX_APP_SECRET")
return Dropbox(oauth2_access_token="foo", app_key=app_key, app_secret=app_secret)
return Dropbox(app_key=app_key, app_secret=app_secret)


@pytest.fixture()
def dbx_share_url_from_env():
return _value_from_env_or_die("DROPBOX_SHARED_LINK")


MALFORMED_TOKEN = 'asdf'
Expand All @@ -82,9 +89,10 @@ def dbx_app_auth_from_env():
DUMMY_PAYLOAD = string.ascii_letters.encode('ascii')


@pytest.mark.usefixtures("dbx_from_env", "refresh_dbx_from_env", "dbx_app_auth_from_env")
@pytest.mark.usefixtures(
"dbx_from_env", "refresh_dbx_from_env", "dbx_app_auth_from_env", "dbx_share_url_from_env"
)
class TestDropbox:

def test_default_oauth2_urls(self):
flow_obj = DropboxOAuth2Flow('dummy_app_key', 'dummy_app_secret',
'http://localhost/dummy', 'dummy_session', 'dbx-auth-csrf-token')
Expand Down Expand Up @@ -115,6 +123,19 @@ def test_bad_auth(self):
invalid_token_dbx.files_list_folder('')
assert cm.value.error.is_invalid_access_token()

def test_multi_auth(self, dbx_from_env, dbx_app_auth_from_env, dbx_share_url_from_env):
# Test for user (with oauth token)
preview_result, resp = dbx_from_env.files_get_thumbnail_v2(
PathOrLink.link(SharedLinkFileInfo(url=dbx_share_url_from_env))
)
assert resp.status_code == 200

# Test for app (with app key and secret)
preview_result, resp = dbx_from_env.files_get_thumbnail_v2(
PathOrLink.link(SharedLinkFileInfo(url=dbx_share_url_from_env))
)
assert resp.status_code == 200

def test_refresh(self, refresh_dbx_from_env):
refresh_dbx_from_env.users_get_current_account()

Expand Down
3 changes: 1 addition & 2 deletions test/unit/test_dropbox_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ def test_Dropbox_with_only_refresh(self, session_instance):
session=session_instance)

def test_Dropbox_with_only_app_key_and_secret(self, session_instance):
with pytest.raises(BadInputException):
Dropbox(app_key=APP_KEY, app_secret=APP_SECRET)
Dropbox(app_key=APP_KEY, app_secret=APP_SECRET)

def test_check_refresh_with_legacy_token(self, session_instance):
dbx = Dropbox(oauth2_access_token=ACCESS_TOKEN, session=session_instance)
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ passenv =
DROPBOX_TEAM_TOKEN
DROPBOX_TOKEN
DROPBOX_WEB_HOST
DROPBOX_SHARED_LINK

deps =
pip
Expand Down

0 comments on commit 10d92da

Please sign in to comment.