Skip to content

Commit

Permalink
Add support for 2FA authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
ov1d1u committed Sep 22, 2024
1 parent da00765 commit 75638f9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
40 changes: 37 additions & 3 deletions custom_components/telegram_earthquake_alert/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.const import CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.exceptions import HomeAssistantError
Expand Down Expand Up @@ -55,11 +55,15 @@ async def _sign_in_and_get_next_step(self) -> ConfigFlowResult:
try:
result = await self.telegram_client.sign_in(
self.phone_number,
password=self.password,
code=self.verification_code
code=self.verification_code,
password=self.password
)
except SendCodeUnavailableError:
raise HomeAssistantError("invalid_verification_code")
except SessionPasswordNeededError:
return await self.async_step_password()
except HomeAssistantError as e:
raise HomeAssistantError(e)

if isinstance(result, SentCode):
return await self.async_step_verification_code()
Expand Down Expand Up @@ -130,6 +134,36 @@ async def async_step_verification_code(
errors=errors
)

async def async_step_password(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the password step."""
errors: dict[str, str] = {}
if user_input is not None:
self.password = user_input[CONF_PASSWORD]

# Set verification_code to None as we're logging in
# with a password, now
self.verification_code = None

try:
return await self._sign_in_and_get_next_step()
except HomeAssistantError as e:
errors["base"] = str(e)
except Exception as e:
_LOGGER.exception(e)
errors["base"] = "unknown"

return self.async_show_form(
step_id="password",
data_schema=vol.Schema(
{
vol.Required(CONF_PASSWORD): str
}
),
errors=errors
)

async def async_step_select_dialogs(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/telegram_earthquake_alert/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"requirements": [
"Telethon==1.36.0"
],
"version": "0.0.1"
"version": "0.0.2"
}
10 changes: 9 additions & 1 deletion custom_components/telegram_earthquake_alert/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"config": {
"error": {
"invalid_verification_code": "Invalid verification code",
"api_id_invalid": "Invalid API id and API hash combination"
"api_id_invalid": "Invalid API id and API hash combination",
"2fa_password_required": "Two-steps verification is enabled and a password is required"
},
"abort": {
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
Expand Down Expand Up @@ -31,6 +32,13 @@
"description": "Enter the verification code sent to your phone",
"title": "Enter verification code"
},
"password": {
"data": {
"password": "Password"
},
"description": "Enter your two factor authentication password",
"title": "Password required"
},
"reconfigure": {
"data": {
"dialogs": "Dialogs to monitor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@
"reconfigure_successful": "Re-configuration was successful"
},
"error": {
"2fa_password_required": "Two-steps verification is enabled and a password is required",
"api_id_invalid": "Invalid API id and API hash combination",
"invalid_verification_code": "Invalid verification code"
},
"step": {
"password": {
"data": {
"password": "Password"
},
"description": "Enter your two factor authentication password",
"title": "Password required"
},
"reconfigure": {
"data": {
"dialogs": "Dialogs to monitor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"config": {
"error": {
"invalid_verification_code": "Cod de verificare invalid",
"api_id_invalid": "Combinația API id și API hash este invalidă"
"api_id_invalid": "Combinația API id și API hash este invalidă",
"2fa_password_required": "Verificarea în doi pași este activată și o parolă este necesară"
},
"step": {
"select_dialogs": {
Expand All @@ -28,6 +29,13 @@
"description": "Introduceți codul de verificare pe care l-ați primit pe telefon",
"title": "Introducere cod de verificare"
},
"password": {
"data": {
"password": "Parolă"
},
"description": "Introduceți parola pentru autentificarea în doi pași",
"title": "Parolă necesară"
},
"reconfigure": {
"data": {
"dialogs": "Conversații de monitorizat"
Expand Down

0 comments on commit 75638f9

Please sign in to comment.