From d931c0033de5269705a74599c0dc81ae1cc40ccc Mon Sep 17 00:00:00 2001 From: rikroe Date: Sat, 31 Oct 2020 16:00:49 +0100 Subject: [PATCH] Change auth urls for China & USA --- bimmer_connected/account.py | 18 ++++++++---------- bimmer_connected/const.py | 3 +-- bimmer_connected/country_selector.py | 6 +++--- test/__init__.py | 6 +----- test/test_account.py | 2 +- 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/bimmer_connected/account.py b/bimmer_connected/account.py index fe04a9d0..bd08063a 100644 --- a/bimmer_connected/account.py +++ b/bimmer_connected/account.py @@ -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__) @@ -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==", @@ -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) diff --git a/bimmer_connected/const.py b/bimmer_connected/const.py index b5cb3225..0fa9ee0f 100644 --- a/bimmer_connected/const.py +++ b/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' diff --git a/bimmer_connected/country_selector.py b/bimmer_connected/country_selector.py index 515ec863..3de9679a 100644 --- a/bimmer_connected/country_selector.py +++ b/bimmer_connected/country_selector.py @@ -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' } diff --git a/test/__init__.py b/test/__init__.py index e6da3935..30a1916d 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -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']), ] diff --git a/test/test_account.py b/test/test_account.py index 1b158209..787b1d70 100644 --- a/test/test_account.py +++ b/test/test_account.py @@ -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."""