Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check cookies function #282

Open
wants to merge 7 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions custom_components/nest_protect/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self) -> None:

self._config_entry = None
self._default_account_type = "production"
self._cookies_latest = ""

async def async_validate_input(self, user_input: dict[str, Any]) -> list:
"""Validate user credentials."""
Expand Down Expand Up @@ -95,6 +96,8 @@ async def async_step_account_link(
) -> FlowResult:
"""Handle a flow initialized by the user."""
errors = {}
issue_token = ""
cookies = ""

if user_input:
try:
Expand All @@ -104,10 +107,15 @@ async def async_step_account_link(
)
user_input[CONF_ISSUE_TOKEN] = issue_token
user_input[CONF_COOKIES] = cookies

self.check_cookies(cookies)

except (TimeoutError, ClientError):
errors["base"] = "cannot_connect"
except BadCredentialsException:
errors["base"] = "invalid_auth"
except ExcessCookiesException:
errors["base"] = "cookies_unknown"
except Exception as exception: # pylint: disable=broad-except
errors["base"] = "unknown"
LOGGER.exception(exception)
Expand Down Expand Up @@ -142,8 +150,8 @@ async def async_step_account_link(
step_id="account_link",
data_schema=vol.Schema(
{
vol.Required(CONF_ISSUE_TOKEN): str,
vol.Required(CONF_COOKIES): str,
vol.Required(CONF_ISSUE_TOKEN, default=issue_token): str,
vol.Required(CONF_COOKIES, default=cookies): str,
}
),
errors=errors,
Expand All @@ -162,3 +170,36 @@ async def async_step_reauth(
self._default_account_type = self._config_entry.data[CONF_ACCOUNT_TYPE]

return await self.async_step_account_link(user_input)

def check_cookies(self, cookies):
"""Check for other cookies than five expected ones."""
# If there are other cookies, it may be an indication of cache not being cleared properly.

if cookies != self._cookies_latest:
self._cookies_latest = cookies

cookie_dict = {
k.strip(): v.strip()
for (k, v) in (item.split("=", 1) for item in cookies.split(";"))
}

for cookie_name in cookie_dict.keys():
if cookie_name.upper() not in (
cn.upper()
for cn in [
"NID",
"__Secure-3PSID",
"__Secure-3PAPISID",
"__Host-3PLSID",
"__Secure-3PSIDCC",
]
):
raise ExcessCookiesException

self._cookies_latest = ""


class ExcessCookiesException(Exception):
"""Raised when there are more then the expected cookies."""

pass
1 change: 1 addition & 0 deletions custom_components/nest_protect/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"cookies_unknown": "[%key:common::config_flow::error::cookies_unknown%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {
Expand Down
1 change: 1 addition & 0 deletions custom_components/nest_protect/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"error": {
"cannot_connect": "Failed to connect",
"invalid_auth": "Invalid authentication",
"cookies_unknown": "Authentication succeeded, but it seems as if the cache may not have been properly cleared which could lead to reduced session lifetime. We recommend to retrieve cookies using Incognito Mode. Alternatively you may continue as is by clicking send once again.",
"unknown": "Unexpected error"
},
"step": {
Expand Down