watchforstock / evohome-client

Python client to access the Evohome web service
Apache License 2.0
88 stars 52 forks source link

handle unauthorized access_tokens #99

Closed zxdavb closed 4 years ago

zxdavb commented 4 years ago

Under certain circumstances, 'bad' (albeit valid) access tokens will be presented to the library, and the consequent failure will not be handled appropriately.

Either the access_token is 'bad', or it was 'good', but is now expired with a 'bad' access_token_expires.

These 'certain' circumstances may be contrived, but I have seen this error with users of Home Assistant.

Specifically (note the corrupted access token):

    client = evohomeasync2.EvohomeClient(
        username,
        password,
        refresh_token=refresh_token,
        access_token=f"{access_token}AA",
        access_token_expires=access_token_expires,
    )

Will cause (note this is from the async version of evohome-client, but the issue is the same):

Traceback (most recent call last):

    ...

  File "/home/homeassistant/lib/python3.6/site-packages/homeassistant/components/evohome/__init__.py", line 172, in init_client
    await client.login()
  File "/home/homeassistant/lib/python3.6/site-packages/evohomeasync2/__init__.py", line 73, in login
    await self.user_account()
  File "/home/homeassistant/lib/python3.6/site-packages/evohomeasync2/__init__.py", line 196, in user_account
    response.raise_for_status()
  File "/home/homeassistant/lib/python3.6/site-packages/aiohttp/client_reqrep.py", line 942, in raise_for_status
    headers=self.headers)
aiohttp.client_exceptions.ClientResponseError: 401, message='Unauthorized'

This is a proposed solution.

zxdavb commented 4 years ago

Without the fix, we have:

(venv) dbonnes@vm-builder:~/clients/evohome-client$ git checkout master
    ...
(venv) dbonnes@vm-builder:~/clients/evohome-client$ python
Python 3.7.5rc1 (default, Oct  8 2019, 16:47:45)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import evohomeclient2
>>> from datetime import datetime as dt
>>>
>>> access_token = "vHzIs8HvIGB ...
>>> access_token_expires = "2019-10-12 13:49:51"
>>>
>>> username = "REDACTED"
>>> password = "REDACTED"
>>>
>>> client = evohomeclient2.EvohomeClient(
...     username,
...     password,
...     access_token=f"{access_token}AA",
...     access_token_expires=dt.strptime(access_token_expires, "%Y-%m-%d %H:%M:%S"),
... )
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "/home/dbonnes/clients/evohome-client/evohomeclient2/__init__.py", line 78, in __init__
    self._login()
  File "/home/dbonnes/clients/evohome-client/evohomeclient2/__init__.py", line 81, in _login
    self.user_account()
  File "/home/dbonnes/clients/evohome-client/evohomeclient2/__init__.py", line 199, in user_account
    response.raise_for_status()
  File "/home/dbonnes/clients/evohome-client/venv/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://tccna.honeywell.com/WebAPI/emea/api/v1/userAccount
>>>

With the fix, we have (note the WARNING):

(venv) dbonnes@vm-builder:~/clients/evohome-client$ python
Python 3.7.5rc1 (default, Oct  8 2019, 16:47:45)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import evohomeclient2
>>> from datetime import datetime as dt
>>>
>>> access_token = "vHzIs8HvIGB ...
>>> access_token_expires = "2019-10-12 13:49:51"
>>>
>>> username = "REDACTED"
>>> password = "REDACTED"
>>>
>>> client = evohomeclient2.EvohomeClient(
...     username,
...     password,
...     access_token=f"{access_token}AA",
...     access_token_expires=dt.strptime(access_token_expires, "%Y-%m-%d %H:%M:%S"),
... )
WARNING:evohomeclient2:Unauthorized access_token (will try re-authenticating).
>>>
>>> client.installation()
[{'locationInfo': {'locationId': ...
>>>
zxdavb commented 4 years ago

Thanks @watchforstock - will you tag this as 0.3.4?

watchforstock commented 4 years ago

@zxdavb Indeed, now at https://pypi.org/project/evohomeclient/0.3.4/