Skip to content

Commit

Permalink
Remove token from config_store on error
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneNulschDE committed Mar 7, 2024
1 parent ba0e43f commit e2411c1
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions custom_components/mbapi2020/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import uuid

from aiohttp import ClientSession
from aiohttp.client_exceptions import ClientError

from custom_components.mbapi2020.errors import MbapiError, MBAuthError
from custom_components.mbapi2020.errors import MBAuthError
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
Expand Down Expand Up @@ -83,7 +82,7 @@ async def request_pin(self, email: str, nonce: str):
_LOGGER.info("PIN preflight request 1")
headers = self._get_header()
url = f"{helper.Rest_url(self._region)}/v1/config"
r = await self._async_request("get", url, headers=headers)
await self._async_request("get", url, headers=headers)

_LOGGER.info("PIN request")
url = f"{helper.Rest_url(self._region)}/v1/login"
Expand All @@ -98,7 +97,7 @@ async def async_refresh_access_token(self, refresh_token: str, is_retry: bool =
_LOGGER.info("Auth token refresh preflight request 1")
headers = self._get_header()
url = f"{helper.Rest_url(self._region)}/v1/config"
r = await self._async_request("get", url, headers=headers)
await self._async_request("get", url, headers=headers)

url = f"{helper.Login_Base_Url(self._region)}/as/token.oauth2"
data = f"grant_type=refresh_token&refresh_token={refresh_token}"
Expand All @@ -115,10 +114,8 @@ async def async_refresh_access_token(self, refresh_token: str, is_retry: bool =
if is_retry:
if self._config_entry and self._config_entry.data:
new_config_entry_data = deepcopy(dict(self._config_entry.data))
new_config_entry_data["token"] = None
changed = self._hass.config_entries.async_update_entry(
self._config_entry, data=new_config_entry_data
)
new_config_entry_data.pop("token", None)
self._hass.config_entries.async_update_entry(self._config_entry, data=new_config_entry_data)
raise err

if token_info is not None:
Expand Down Expand Up @@ -166,31 +163,29 @@ async def async_get_cached_token(self):
if self.token:
token_info = self.token
token_type = "instance"
else:
if not os.path.exists(self._config_entry_token_path):
if os.path.exists(self._old_token_path):
_LOGGER.debug("async_get_cached_token: token migration - start copy")
shutil.copyfile(self._old_token_path, self._config_entry_token_path)
os.remove(self._old_token_path)

token_file = open(self._config_entry_token_path)
token_info_string = token_file.read()
token_file.close()
token_info = json.loads(token_info_string)
token_type = "old_file"
else:
if self._config_entry and self._config_entry.data and "token" in self._config_entry.data:
token_info = self._config_entry.data["token"]
token_type = "config_entry"
else:
_LOGGER.warning("No token information - reauth required")
return None
else:
elif not os.path.exists(self._config_entry_token_path):
if os.path.exists(self._old_token_path):
_LOGGER.debug("async_get_cached_token: token migration - start copy")
shutil.copyfile(self._old_token_path, self._config_entry_token_path)
os.remove(self._old_token_path)

token_file = open(self._config_entry_token_path)
token_info_string = token_file.read()
token_file.close()
token_info = json.loads(token_info_string)
token_type = "new_file"
token_type = "old_file"
elif self._config_entry and self._config_entry.data and "token" in self._config_entry.data:
token_info = self._config_entry.data["token"]
token_type = "config_entry"
else:
_LOGGER.warning("No token information - reauth required")
return None
else:
token_file = open(self._config_entry_token_path)
token_info_string = token_file.read()
token_file.close()
token_info = json.loads(token_info_string)
token_type = "new_file"

if self.is_token_expired(token_info):
async with self._get_token_lock:
Expand Down Expand Up @@ -233,7 +228,7 @@ def _save_token_info(self, token_info):

new_config_entry_data = deepcopy(dict(self._config_entry.data))
new_config_entry_data["token"] = token_info
changed = self._hass.config_entries.async_update_entry(self._config_entry, data=new_config_entry_data)
self._hass.config_entries.async_update_entry(self._config_entry, data=new_config_entry_data)

try:
with open(self._config_entry_token_path, "w") as token_file:
Expand Down

0 comments on commit e2411c1

Please sign in to comment.