mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2026-01-17 15:41:15 +08:00
feat: simplify oauth logic
This commit is contained in:
parent
417514a22f
commit
235961d630
@ -426,14 +426,12 @@ class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
cloud_server=self._cloud_server,
|
cloud_server=self._cloud_server,
|
||||||
uuid=self._uuid,
|
uuid=self._uuid,
|
||||||
loop=self._main_loop)
|
loop=self._main_loop)
|
||||||
state = hashlib.sha1(
|
|
||||||
f'd=ha.{self._uuid}'.encode('utf-8')).hexdigest()
|
|
||||||
self.hass.data[DOMAIN][self._virtual_did]['oauth_state'] = state
|
|
||||||
self._cc_oauth_auth_url = miot_oauth.gen_auth_url(
|
self._cc_oauth_auth_url = miot_oauth.gen_auth_url(
|
||||||
redirect_url=self._oauth_redirect_url_full, state=state)
|
redirect_url=self._oauth_redirect_url_full)
|
||||||
|
self.hass.data[DOMAIN][self._virtual_did]['oauth_state'] = (
|
||||||
|
miot_oauth.state)
|
||||||
_LOGGER.info(
|
_LOGGER.info(
|
||||||
'async_step_oauth, oauth_url: %s',
|
'async_step_oauth, oauth_url: %s', self._cc_oauth_auth_url)
|
||||||
self._cc_oauth_auth_url)
|
|
||||||
webhook_async_unregister(
|
webhook_async_unregister(
|
||||||
self.hass, webhook_id=self._virtual_did)
|
self.hass, webhook_id=self._virtual_did)
|
||||||
webhook_async_register(
|
webhook_async_register(
|
||||||
@ -1150,17 +1148,12 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
async def async_step_oauth(self, user_input=None):
|
async def async_step_oauth(self, user_input=None):
|
||||||
try:
|
try:
|
||||||
if self._cc_task_oauth is None:
|
if self._cc_task_oauth is None:
|
||||||
state = hashlib.sha1(
|
|
||||||
f'd=ha.{self._entry_data["uuid"]}'.encode('utf-8')
|
|
||||||
).hexdigest()
|
|
||||||
self.hass.data[DOMAIN][self._virtual_did]['oauth_state'] = state
|
|
||||||
self._miot_oauth.set_redirect_url(
|
|
||||||
redirect_url=self._oauth_redirect_url_full)
|
|
||||||
self._cc_oauth_auth_url = self._miot_oauth.gen_auth_url(
|
self._cc_oauth_auth_url = self._miot_oauth.gen_auth_url(
|
||||||
redirect_url=self._oauth_redirect_url_full, state=state)
|
redirect_url=self._oauth_redirect_url_full)
|
||||||
|
self.hass.data[DOMAIN][self._virtual_did]['oauth_state'] = (
|
||||||
|
self._miot_oauth.state)
|
||||||
_LOGGER.info(
|
_LOGGER.info(
|
||||||
'async_step_oauth, oauth_url: %s',
|
'async_step_oauth, oauth_url: %s', self._cc_oauth_auth_url)
|
||||||
self._cc_oauth_auth_url)
|
|
||||||
webhook_async_unregister(
|
webhook_async_unregister(
|
||||||
self.hass, webhook_id=self._virtual_did)
|
self.hass, webhook_id=self._virtual_did)
|
||||||
webhook_async_register(
|
webhook_async_register(
|
||||||
|
|||||||
@ -77,6 +77,7 @@ class MIoTOauthClient:
|
|||||||
_client_id: int
|
_client_id: int
|
||||||
_redirect_url: str
|
_redirect_url: str
|
||||||
_device_id: str
|
_device_id: str
|
||||||
|
_state: str
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, client_id: str, redirect_url: str, cloud_server: str,
|
self, client_id: str, redirect_url: str, cloud_server: str,
|
||||||
@ -99,8 +100,14 @@ class MIoTOauthClient:
|
|||||||
else:
|
else:
|
||||||
self._oauth_host = f'{cloud_server}.{DEFAULT_OAUTH2_API_HOST}'
|
self._oauth_host = f'{cloud_server}.{DEFAULT_OAUTH2_API_HOST}'
|
||||||
self._device_id = f'ha.{uuid}'
|
self._device_id = f'ha.{uuid}'
|
||||||
|
self._state = hashlib.sha1(
|
||||||
|
f'd={self._device_id}'.encode('utf-8')).hexdigest()
|
||||||
self._session = aiohttp.ClientSession(loop=self._main_loop)
|
self._session = aiohttp.ClientSession(loop=self._main_loop)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self) -> str:
|
||||||
|
return self.state
|
||||||
|
|
||||||
async def deinit_async(self) -> None:
|
async def deinit_async(self) -> None:
|
||||||
if self._session and not self._session.closed:
|
if self._session and not self._session.closed:
|
||||||
await self._session.close()
|
await self._session.close()
|
||||||
@ -138,8 +145,7 @@ class MIoTOauthClient:
|
|||||||
'client_id': self._client_id,
|
'client_id': self._client_id,
|
||||||
'response_type': 'code',
|
'response_type': 'code',
|
||||||
'device_id': self._device_id,
|
'device_id': self._device_id,
|
||||||
'state': hashlib.sha1(
|
'state': self._state
|
||||||
f'd={self._device_id}'.encode('utf-8')).hexdigest()
|
|
||||||
}
|
}
|
||||||
if state:
|
if state:
|
||||||
params['state'] = state
|
params['state'] = state
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user