Skip to content

Commit

Permalink
Change auth urls for China & USA
Browse files Browse the repository at this point in the history
  • Loading branch information
rikroe committed Oct 31, 2020
1 parent 7da2fc2 commit d931c00
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 21 deletions.
18 changes: 8 additions & 10 deletions bimmer_connected/account.py
Expand Up @@ -19,7 +19,7 @@

from bimmer_connected.country_selector import Regions, get_server_url, get_gcdm_oauth_endpoint
from bimmer_connected.vehicle import ConnectedDriveVehicle
from bimmer_connected.const import AUTH_URL, AUTH_URL_LEGACY, VEHICLES_URL, ERROR_CODE_MAPPING
from bimmer_connected.const import AUTH_URL, VEHICLES_URL, ERROR_CODE_MAPPING

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -68,11 +68,15 @@ def _get_oauth_token(self) -> None:
return

_LOGGER.debug('getting new oauth token')
url = AUTH_URL.format(
gcdm_oauth_endpoint=get_gcdm_oauth_endpoint(self._region)
)

headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": "124",
"Connection": "Keep-Alive",
"Host": "customer.bmwgroup.com" if self._region == Regions.REST_OF_WORLD else self.server_url,
"Host": urllib.parse.urlparse(url).netloc,
"Accept-Encoding": "gzip",
"Authorization": "Basic ZDc2NmI1MzctYTY1NC00Y2JkLWEzZGMtMGNhNTY3MmQ3ZjhkOjE1"
"ZjY5N2Y2LWE1ZDUtNGNhZC05OWQ5LTNhMTViYzdmMzk3Mw==",
Expand All @@ -89,14 +93,8 @@ def _get_oauth_token(self) -> None:
}

data = urllib.parse.urlencode(values)
if self._region == Regions.REST_OF_WORLD:
url = AUTH_URL.format(
gcdm_oauth_endpoint=get_gcdm_oauth_endpoint(self._region)
)
expected_response_code = 200
else:
url = AUTH_URL_LEGACY.format(server=self.server_url)
expected_response_code = 200

expected_response_code = 200
try:
response = self.send_request(url, data=data, headers=headers, allow_redirects=False,
expected_response=expected_response_code, post=True)
Expand Down
3 changes: 1 addition & 2 deletions bimmer_connected/const.py
@@ -1,7 +1,6 @@
"""URLs for different services and error code mapping."""

AUTH_URL = 'https://customer.bmwgroup.com/{gcdm_oauth_endpoint}/token'
AUTH_URL_LEGACY = 'https://{server}/gcdm/oauth/token'
AUTH_URL = 'https://{gcdm_oauth_endpoint}/oauth/token'
BASE_URL = 'https://{server}/webapi/v1'

VEHICLES_URL = BASE_URL + '/user/vehicles'
Expand Down
6 changes: 3 additions & 3 deletions bimmer_connected/country_selector.py
Expand Up @@ -22,9 +22,9 @@ class Regions(Enum):

#: Mapping from regions to servers
_GCDM_OAUTH_ENDPOINTS = {
Regions.NORTH_AMERICA: 'gcdm/usa/oauth',
Regions.REST_OF_WORLD: 'gcdm/oauth',
Regions.CHINA: 'gcdm/oauth'
Regions.NORTH_AMERICA: 'customer.bmwgroup.com/gcdm/usa',
Regions.REST_OF_WORLD: 'customer.bmwgroup.com/gcdm',
Regions.CHINA: 'customer.bmwgroup.cn/gcdm'
}


Expand Down
6 changes: 1 addition & 5 deletions test/__init__.py
Expand Up @@ -132,14 +132,10 @@ def __init__(self) -> None:
"""Constructor."""
self.last_request = []
self.responses = [
MockResponse('https://.+/gcdm/oauth/token',
MockResponse('https://.+/gcdm/.*/?oauth/token',
headers=_AUTH_RESPONSE_HEADERS,
data_files=['G31_NBTevo/auth_response.json'],
status_code=200),
MockResponse('https://.+/gcdm/(.+/)?oauth/authenticate',
headers=_AUTH_RESPONSE_HEADERS,
data_files=['G31_NBTevo/auth_response.json'],
status_code=302),
MockResponse('https://.+/webapi/v1/user/vehicles$',
data_files=['vehicles.json']),
]
Expand Down
2 changes: 1 addition & 1 deletion test/test_account.py
Expand Up @@ -38,7 +38,7 @@ def test_us_header(self):
with mock.patch('bimmer_connected.account.requests', new=backend_mock):
ConnectedDriveAccount(TEST_USERNAME, TEST_PASSWORD, Regions.NORTH_AMERICA)
request = [r for r in backend_mock.last_request if 'oauth' in r.url][0]
self.assertEqual('b2vapi.bmwgroup.us', request.headers['Host'])
self.assertEqual('customer.bmwgroup.com', request.headers['Host'])

def test_anonymize_data(self):
"""Test anonymization function."""
Expand Down

0 comments on commit d931c00

Please sign in to comment.