Skip to content

Commit 91d48fe

Browse files
mbogosianposita
authored andcommitted
Fix host for calls to /oauth2/authorize
1 parent 57404df commit 91d48fe

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

dropbox/oauth.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
import six
1616
import urllib
1717

18-
from .session import pinned_session
18+
from .session import (
19+
API_HOST,
20+
WEB_HOST,
21+
pinned_session,
22+
)
1923

2024
if six.PY3:
2125
url_path_quote = urllib.parse.quote # pylint: disable=no-member,useless-suppression
@@ -92,8 +96,6 @@ def __init__(self, consumer_key, consumer_secret, locale=None):
9296
self.locale = locale
9397
self.requests_session = pinned_session()
9498

95-
self._host = os.environ.get('DROPBOX_WEB_HOST', 'www.dropbox.com')
96-
9799
def _get_authorize_url(self, redirect_uri, state):
98100
params = dict(response_type='code',
99101
client_id=self.consumer_key)
@@ -102,7 +104,7 @@ def _get_authorize_url(self, redirect_uri, state):
102104
if state is not None:
103105
params['state'] = state
104106

105-
return self.build_url('/oauth2/authorize', params)
107+
return self.build_url('/oauth2/authorize', params, WEB_HOST)
106108

107109
def _finish(self, code, redirect_uri):
108110
url = self.build_url('/oauth2/token')
@@ -155,7 +157,7 @@ def build_path(self, target, params=None):
155157
else:
156158
return target_path
157159

158-
def build_url(self, target, params=None):
160+
def build_url(self, target, params=None, host=API_HOST):
159161
"""Build an API URL.
160162
161163
This method adds scheme and hostname to the path
@@ -166,7 +168,7 @@ def build_url(self, target, params=None):
166168
:return: The full API URL.
167169
:rtype: str
168170
"""
169-
return "https://%s%s" % (self._host, self.build_path(target, params))
171+
return "https://%s%s" % (host, self.build_path(target, params))
170172

171173

172174
class DropboxOAuth2FlowNoRedirect(DropboxOAuth2FlowBase):

dropbox/session.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import requests
66
from requests.adapters import HTTPAdapter
7-
from requests.packages.urllib3.poolmanager import PoolManager
7+
from urllib3.poolmanager import PoolManager
88

99

1010
_TRUSTED_CERT_FILE = pkg_resources.resource_filename(__name__, 'trusted-certs.crt')
@@ -52,7 +52,11 @@ def pinned_session(pool_maxsize=8):
5252
url_path_quote = urllib.parse.quote # pylint: disable=no-member,useless-suppression
5353
url_encode = urllib.parse.urlencode # pylint: disable=no-member,useless-suppression
5454

55-
DOMAIN = os.environ.get('DROPBOX_DOMAIN', '.dropboxapi.com')
55+
API_DOMAIN = os.environ.get('DROPBOX_API_DOMAIN',
56+
os.environ.get('DROPBOX_DOMAIN', '.dropboxapi.com'))
57+
58+
WEB_DOMAIN = os.environ.get('DROPBOX_WEB_DOMAIN',
59+
os.environ.get('DROPBOX_DOMAIN', '.dropbox.com'))
5660

5761
# Default short hostname for RPC-style routes.
5862
HOST_API = 'api'
@@ -66,10 +70,10 @@ def pinned_session(pool_maxsize=8):
6670
# Default short hostname for the Drobox website.
6771
HOST_WWW = 'www'
6872

69-
API_HOST = os.environ.get('DROPBOX_API_HOST', HOST_API + DOMAIN)
70-
API_CONTENT_HOST = os.environ.get('DROPBOX_API_CONTENT_HOST', HOST_CONTENT + DOMAIN)
71-
API_NOTIFICATION_HOST = os.environ.get('DROPBOX_API_NOTIFY_HOST', HOST_NOTIFY + DOMAIN)
72-
WEB_HOST = os.environ.get('DROPBOX_WEB_HOST', HOST_WWW + DOMAIN)
73+
API_HOST = os.environ.get('DROPBOX_API_HOST', HOST_API + API_DOMAIN)
74+
API_CONTENT_HOST = os.environ.get('DROPBOX_API_CONTENT_HOST', HOST_CONTENT + API_DOMAIN)
75+
API_NOTIFICATION_HOST = os.environ.get('DROPBOX_API_NOTIFY_HOST', HOST_NOTIFY + API_DOMAIN)
76+
WEB_HOST = os.environ.get('DROPBOX_WEB_HOST', HOST_WWW + WEB_DOMAIN)
7377

7478
class OAuthToken(object):
7579
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
version = eval(line.split('=', 1)[1].strip()) # pylint: disable=eval-used
2626

2727
install_reqs = [
28-
'requests >= 2.5.1, != 2.6.1',
28+
'requests >= 2.5.1, != 2.6.1, !=2.16.0, !=2.16.1',
2929
'six >= 1.3.0',
3030
'urllib3',
3131
]

test/test_dropbox.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import os
88
import posixpath
99
import random
10+
import re
11+
import six
1012
import string
1113
import sys
1214
import threading
@@ -19,8 +21,10 @@
1921

2022
from dropbox import (
2123
Dropbox,
24+
DropboxOAuth2Flow,
2225
DropboxTeam,
2326
client,
27+
session,
2428
)
2529
from dropbox.exceptions import (
2630
ApiError,
@@ -66,6 +70,25 @@ def wrapped(self, *args, **kwargs):
6670

6771
class TestDropbox(unittest.TestCase):
6872

73+
def test_default_oauth2_urls(self):
74+
flow_obj = DropboxOAuth2Flow('dummy_app_key', 'dummy_app_secret',
75+
'http://localhost/dummy', 'dummy_session', 'dbx-auth-csrf-token')
76+
77+
six.assertRegex(
78+
flow_obj._get_authorize_url('http://localhost/redirect', 'state'),
79+
r'^https://{}/oauth2/authorize\?'.format(re.escape(session.WEB_HOST)),
80+
)
81+
82+
self.assertEqual(
83+
flow_obj.build_url('/oauth2/authorize'),
84+
'https://{}/oauth2/authorize'.format(session.API_HOST),
85+
)
86+
87+
self.assertEqual(
88+
flow_obj.build_url('/oauth2/authorize', host=session.WEB_HOST),
89+
'https://{}/oauth2/authorize'.format(session.WEB_HOST),
90+
)
91+
6992
def test_bad_auth(self):
7093
# Test malformed token
7194
malformed_token_dbx = Dropbox(MALFORMED_TOKEN)

0 commit comments

Comments
 (0)