Prompt to reauth when the august password is changed or token expires (#40103)
* Prompt to reauth when the august password is changed or token expires * augment missing config flow coverage * augment test coverage * Adjust test * Update homeassistant/components/august/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * block until patch complete Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
540b925659
commit
5ea04d64f6
9 files changed
+379
-100
No files matched your search
@@ -3,13 +3,18 @@ import asyncio
|
||||
import itertools
|
||||
import logging
|
||||
|
||||
from aiohttp import ClientError
|
||||
from aiohttp import ClientError, ClientResponseError
|
||||
from august.authenticator import ValidationResult
|
||||
from august.exceptions import AugustApiAIOHTTPError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
|
||||
from homeassistant.const import (
|
||||
CONF_PASSWORD,
|
||||
CONF_TIMEOUT,
|
||||
CONF_USERNAME,
|
||||
HTTP_UNAUTHORIZED,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
@@ -29,7 +34,7 @@ from .const import (
|
||||
MIN_TIME_BETWEEN_DETAIL_UPDATES,
|
||||
VERIFICATION_CODE_KEY,
|
||||
)
|
||||
from .exceptions import InvalidAuth, RequireValidation
|
||||
from .exceptions import CannotConnect, InvalidAuth, RequireValidation
|
||||
from .gateway import AugustGateway
|
||||
from .subscriber import AugustSubscriberMixin
|
||||
|
||||
@@ -113,10 +118,7 @@ async def async_setup_august(hass, config_entry, august_gateway):
|
||||
await august_gateway.async_authenticate()
|
||||
except RequireValidation:
|
||||
await async_request_validation(hass, config_entry, august_gateway)
|
||||
return False
|
||||
except InvalidAuth:
|
||||
_LOGGER.error("Password is no longer valid. Please set up August again")
|
||||
return False
|
||||
raise
|
||||
|
||||
# We still use the configurator to get a new 2fa code
|
||||
# when needed since config_flow doesn't have a way
|
||||
@@ -171,8 +173,30 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
try:
|
||||
await august_gateway.async_setup(entry.data)
|
||||
return await async_setup_august(hass, entry, august_gateway)
|
||||
except asyncio.TimeoutError as err:
|
||||
except ClientResponseError as err:
|
||||
if err.status == HTTP_UNAUTHORIZED:
|
||||
_async_start_reauth(hass, entry)
|
||||
return False
|
||||
|
||||
raise ConfigEntryNotReady from err
|
||||
except InvalidAuth:
|
||||
_async_start_reauth(hass, entry)
|
||||
return False
|
||||
except RequireValidation:
|
||||
return False
|
||||
except (CannotConnect, asyncio.TimeoutError) as err:
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
|
||||
def _async_start_reauth(hass: HomeAssistant, entry: ConfigEntry):
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": "reauth"},
|
||||
data=entry.data,
|
||||
)
|
||||
)
|
||||
_LOGGER.error("Password is no longer valid. Please reauthenticate")
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
|
||||
@@ -4,7 +4,7 @@ import logging
|
||||
from august.authenticator import ValidationResult
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries, core
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
|
||||
|
||||
from .const import (
|
||||
@@ -19,18 +19,8 @@ from .gateway import AugustGateway
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DATA_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_LOGIN_METHOD, default="phone"): vol.In(LOGIN_METHODS),
|
||||
vol.Required(CONF_USERNAME): str,
|
||||
vol.Required(CONF_PASSWORD): str,
|
||||
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): vol.Coerce(int),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_validate_input(
|
||||
hass: core.HomeAssistant,
|
||||
data,
|
||||
august_gateway,
|
||||
):
|
||||
@@ -79,6 +69,7 @@ class AugustConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Store an AugustGateway()."""
|
||||
self._august_gateway = None
|
||||
self.user_auth_details = {}
|
||||
self._needs_reset = False
|
||||
super().__init__()
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
@@ -87,30 +78,45 @@ class AugustConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
self._august_gateway = AugustGateway(self.hass)
|
||||
errors = {}
|
||||
if user_input is not None:
|
||||
await self._august_gateway.async_setup(user_input)
|
||||
combined_inputs = {**self.user_auth_details, **user_input}
|
||||
await self._august_gateway.async_setup(combined_inputs)
|
||||
if self._needs_reset:
|
||||
self._needs_reset = False
|
||||
await self._august_gateway.async_reset_authentication()
|
||||
|
||||
try:
|
||||
info = await async_validate_input(
|
||||
self.hass,
|
||||
user_input,
|
||||
combined_inputs,
|
||||
self._august_gateway,
|
||||
)
|
||||
await self.async_set_unique_id(user_input[CONF_USERNAME])
|
||||
return self.async_create_entry(title=info["title"], data=info["data"])
|
||||
except CannotConnect:
|
||||
errors["base"] = "cannot_connect"
|
||||
except InvalidAuth:
|
||||
errors["base"] = "invalid_auth"
|
||||
except RequireValidation:
|
||||
self.user_auth_details = user_input
|
||||
self.user_auth_details.update(user_input)
|
||||
|
||||
return await self.async_step_validation()
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
|
||||
if not errors:
|
||||
self.user_auth_details.update(user_input)
|
||||
|
||||
existing_entry = await self.async_set_unique_id(
|
||||
combined_inputs[CONF_USERNAME]
|
||||
)
|
||||
if existing_entry:
|
||||
self.hass.config_entries.async_update_entry(
|
||||
existing_entry, data=info["data"]
|
||||
)
|
||||
await self.hass.config_entries.async_reload(existing_entry.entry_id)
|
||||
return self.async_abort(reason="reauth_successful")
|
||||
return self.async_create_entry(title=info["title"], data=info["data"])
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
||||
step_id="user", data_schema=self._async_build_schema(), errors=errors
|
||||
)
|
||||
|
||||
async def async_step_validation(self, user_input=None):
|
||||
@@ -135,3 +141,23 @@ class AugustConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
return await self.async_step_user(user_input)
|
||||
|
||||
async def async_step_reauth(self, data):
|
||||
"""Handle configuration by re-auth."""
|
||||
self.user_auth_details = dict(data)
|
||||
self._needs_reset = True
|
||||
return await self.async_step_user()
|
||||
|
||||
def _async_build_schema(self):
|
||||
"""Generate the config flow schema."""
|
||||
base_schema = {
|
||||
vol.Required(CONF_LOGIN_METHOD, default="phone"): vol.In(LOGIN_METHODS),
|
||||
vol.Required(CONF_USERNAME): str,
|
||||
vol.Required(CONF_PASSWORD): str,
|
||||
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): vol.Coerce(int),
|
||||
}
|
||||
for key in self.user_auth_details:
|
||||
if key == CONF_PASSWORD or key not in base_schema:
|
||||
continue
|
||||
del base_schema[key]
|
||||
return vol.Schema(base_schema)
|
||||
@@ -2,12 +2,18 @@
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
|
||||
from aiohttp import ClientError
|
||||
from aiohttp import ClientError, ClientResponseError
|
||||
from august.api_async import ApiAsync
|
||||
from august.authenticator_async import AuthenticationState, AuthenticatorAsync
|
||||
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
|
||||
from homeassistant.const import (
|
||||
CONF_PASSWORD,
|
||||
CONF_TIMEOUT,
|
||||
CONF_USERNAME,
|
||||
HTTP_UNAUTHORIZED,
|
||||
)
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
|
||||
from .const import (
|
||||
@@ -32,29 +38,14 @@ class AugustGateway:
|
||||
self._access_token_cache_file = None
|
||||
self._hass = hass
|
||||
self._config = None
|
||||
self._api = None
|
||||
self._authenticator = None
|
||||
self._authentication = None
|
||||
|
||||
@property
|
||||
def authenticator(self):
|
||||
"""August authentication object from py-august."""
|
||||
return self._authenticator
|
||||
|
||||
@property
|
||||
def authentication(self):
|
||||
"""August authentication object from py-august."""
|
||||
return self._authentication
|
||||
self.api = None
|
||||
self.authenticator = None
|
||||
self.authentication = None
|
||||
|
||||
@property
|
||||
def access_token(self):
|
||||
"""Access token for the api."""
|
||||
return self._authentication.access_token
|
||||
|
||||
@property
|
||||
def api(self):
|
||||
"""August api object from py-august."""
|
||||
return self._api
|
||||
return self.authentication.access_token
|
||||
|
||||
def config_entry(self):
|
||||
"""Config entry."""
|
||||
@@ -78,12 +69,12 @@ class AugustGateway:
|
||||
)
|
||||
self._config = conf
|
||||
|
||||
self._api = ApiAsync(
|
||||
self.api = ApiAsync(
|
||||
self._aiohttp_session, timeout=self._config.get(CONF_TIMEOUT)
|
||||
)
|
||||
|
||||
self._authenticator = AuthenticatorAsync(
|
||||
self._api,
|
||||
self.authenticator = AuthenticatorAsync(
|
||||
self.api,
|
||||
self._config[CONF_LOGIN_METHOD],
|
||||
self._config[CONF_USERNAME],
|
||||
self._config[CONF_PASSWORD],
|
||||
@@ -93,30 +84,47 @@ class AugustGateway:
|
||||
),
|
||||
)
|
||||
|
||||
await self._authenticator.async_setup_authentication()
|
||||
await self.authenticator.async_setup_authentication()
|
||||
|
||||
async def async_authenticate(self):
|
||||
"""Authenticate with the details provided to setup."""
|
||||
self._authentication = None
|
||||
self.authentication = None
|
||||
try:
|
||||
self._authentication = await self.authenticator.async_authenticate()
|
||||
self.authentication = await self.authenticator.async_authenticate()
|
||||
if self.authentication.state == AuthenticationState.AUTHENTICATED:
|
||||
# Call the locks api to verify we are actually
|
||||
# authenticated because we can be authenticated
|
||||
# by have no access
|
||||
await self.api.async_get_operable_locks(self.access_token)
|
||||
except ClientResponseError as ex:
|
||||
if ex.status == HTTP_UNAUTHORIZED:
|
||||
raise InvalidAuth from ex
|
||||
|
||||
raise CannotConnect from ex
|
||||
except ClientError as ex:
|
||||
_LOGGER.error("Unable to connect to August service: %s", str(ex))
|
||||
raise CannotConnect from ex
|
||||
|
||||
if self._authentication.state == AuthenticationState.BAD_PASSWORD:
|
||||
if self.authentication.state == AuthenticationState.BAD_PASSWORD:
|
||||
raise InvalidAuth
|
||||
|
||||
if self._authentication.state == AuthenticationState.REQUIRES_VALIDATION:
|
||||
if self.authentication.state == AuthenticationState.REQUIRES_VALIDATION:
|
||||
raise RequireValidation
|
||||
|
||||
if self._authentication.state != AuthenticationState.AUTHENTICATED:
|
||||
_LOGGER.error(
|
||||
"Unknown authentication state: %s", self._authentication.state
|
||||
)
|
||||
if self.authentication.state != AuthenticationState.AUTHENTICATED:
|
||||
_LOGGER.error("Unknown authentication state: %s", self.authentication.state)
|
||||
raise InvalidAuth
|
||||
|
||||
return self._authentication
|
||||
return self.authentication
|
||||
|
||||
async def async_reset_authentication(self):
|
||||
"""Remove the cache file."""
|
||||
await self._hass.async_add_executor_job(self._reset_authentication)
|
||||
|
||||
def _reset_authentication(self):
|
||||
"""Remove the cache file."""
|
||||
if os.path.exists(self._access_token_cache_file):
|
||||
os.unlink(self._access_token_cache_file)
|
||||
|
||||
async def async_refresh_access_token_if_needed(self):
|
||||
"""Refresh the august access token if needed."""
|
||||
@@ -130,4 +138,4 @@ class AugustGateway:
|
||||
self.authentication.access_token_expires,
|
||||
refreshed_authentication.access_token_expires,
|
||||
)
|
||||
self._authentication = refreshed_authentication
|
||||
self.authentication = refreshed_authentication
|
||||
@@ -6,7 +6,8 @@
|
||||
"invalid_auth": "Invalid authentication"
|
||||
},
|
||||
"abort": {
|
||||
"already_configured": "Account is already configured"
|
||||
"already_configured": "Account is already configured",
|
||||
"reauth_successful": "Re-authentication was successful"
|
||||
},
|
||||
"step": {
|
||||
"validation": {
|
||||
@@ -28,4 +29,4 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,32 @@
|
||||
{
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "Account is already configured"
|
||||
"config": {
|
||||
"error": {
|
||||
"unknown": "Unexpected error",
|
||||
"cannot_connect": "Failed to connect, please try again",
|
||||
"invalid_auth": "Invalid authentication"
|
||||
},
|
||||
"abort": {
|
||||
"already_configured": "Account is already configured",
|
||||
"reauth_successful": "Re-authentication was successful"
|
||||
},
|
||||
"step": {
|
||||
"validation": {
|
||||
"title": "Two factor authentication",
|
||||
"data": {
|
||||
"code": "Verification code"
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "Failed to connect, please try again",
|
||||
"invalid_auth": "Invalid authentication",
|
||||
"unknown": "Unexpected error"
|
||||
"description": "Please check your {login_method} ({username}) and enter the verification code below"
|
||||
},
|
||||
"user": {
|
||||
"description": "If the Login Method is 'email', Username is the email address. If the Login Method is 'phone', Username is the phone number in the format '+NNNNNNNNN'.",
|
||||
"data": {
|
||||
"timeout": "Timeout (seconds)",
|
||||
"password": "[%key:common::config_flow::data::password%]",
|
||||
"username": "[%key:common::config_flow::data::username%]",
|
||||
"login_method": "Login Method"
|
||||
},
|
||||
"step": {
|
||||
"user": {
|
||||
"data": {
|
||||
"login_method": "Login Method",
|
||||
"password": "Password",
|
||||
"timeout": "Timeout (seconds)",
|
||||
"username": "Username"
|
||||
},
|
||||
"description": "If the Login Method is 'email', Username is the email address. If the Login Method is 'phone', Username is the phone number in the format '+NNNNNNNNN'.",
|
||||
"title": "Setup an August account"
|
||||
},
|
||||
"validation": {
|
||||
"data": {
|
||||
"code": "Verification code"
|
||||
},
|
||||
"description": "Please check your {login_method} ({username}) and enter the verification code below",
|
||||
"title": "Two factor authentication"
|
||||
}
|
||||
}
|
||||
"title": "Setup an August account"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user