Add 2SA authentication to Synology DSM (#34101)
Co-Authored-By: J. Nick Koston <nick@koston.org> Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
1 parent
4484afc0d6
commit
30744bea9c
9 files changed
+134
-12
No files matched your search
@@ -5,10 +5,17 @@
|
||||
},
|
||||
"error": {
|
||||
"login": "Login error: please check your username & password",
|
||||
"missing_data": "Missing data: please retry later or an other configuration"
|
||||
"missing_data": "Missing data: please retry later or an other configuration",
|
||||
"otp_failed": "Two-step authentication failed, retry with a new pass code"
|
||||
},
|
||||
"flow_title": "Synology DSM {name} ({host})",
|
||||
"step": {
|
||||
"2sa": {
|
||||
"data": {
|
||||
"otp_code": "Code"
|
||||
},
|
||||
"title": "Synology DSM: two-step authentication"
|
||||
},
|
||||
"link": {
|
||||
"data": {
|
||||
"api_version": "DSM version",
|
||||
|
||||
@@ -71,8 +71,11 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
||||
unit = hass.config.units.temperature_unit
|
||||
use_ssl = entry.data[CONF_SSL]
|
||||
api_version = entry.data.get(CONF_API_VERSION, DEFAULT_DSM_VERSION)
|
||||
device_token = entry.data.get("device_token")
|
||||
|
||||
api = SynoApi(hass, host, port, username, password, unit, use_ssl, api_version)
|
||||
api = SynoApi(
|
||||
hass, host, port, username, password, unit, use_ssl, device_token, api_version
|
||||
)
|
||||
|
||||
await api.async_setup()
|
||||
|
||||
@@ -105,6 +108,7 @@ class SynoApi:
|
||||
password: str,
|
||||
temp_unit: str,
|
||||
use_ssl: bool,
|
||||
device_token: str,
|
||||
api_version: int,
|
||||
):
|
||||
"""Initialize the API wrapper class."""
|
||||
@@ -114,6 +118,7 @@ class SynoApi:
|
||||
self._username = username
|
||||
self._password = password
|
||||
self._use_ssl = use_ssl
|
||||
self._device_token = device_token
|
||||
self._api_version = api_version
|
||||
self.temp_unit = temp_unit
|
||||
|
||||
@@ -137,6 +142,7 @@ class SynoApi:
|
||||
self._username,
|
||||
self._password,
|
||||
self._use_ssl,
|
||||
device_token=self._device_token,
|
||||
dsm_version=self._api_version,
|
||||
)
|
||||
|
||||
|
||||
@@ -3,6 +3,11 @@ import logging
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from synology_dsm import SynologyDSM
|
||||
from synology_dsm.exceptions import (
|
||||
SynologyDSMLogin2SAFailedException,
|
||||
SynologyDSMLogin2SARequiredException,
|
||||
SynologyDSMLoginInvalidException,
|
||||
)
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries, exceptions
|
||||
@@ -29,6 +34,8 @@ from .const import DOMAIN # pylint: disable=unused-import
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_OTP_CODE = "otp_code"
|
||||
|
||||
|
||||
def _discovery_schema_with_defaults(discovery_info):
|
||||
return vol.Schema(_ordered_shared_schema(discovery_info))
|
||||
@@ -66,6 +73,7 @@ class SynologyDSMFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the synology_dsm config flow."""
|
||||
self.saved_user_input = {}
|
||||
self.discovered_conf = {}
|
||||
|
||||
async def _show_setup_form(self, user_input=None, errors=None):
|
||||
@@ -104,6 +112,7 @@ class SynologyDSMFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
password = user_input[CONF_PASSWORD]
|
||||
use_ssl = user_input.get(CONF_SSL, DEFAULT_SSL)
|
||||
api_version = user_input.get(CONF_API_VERSION, DEFAULT_DSM_VERSION)
|
||||
otp_code = user_input.get(CONF_OTP_CODE)
|
||||
|
||||
if not port:
|
||||
if use_ssl is True:
|
||||
@@ -117,9 +126,15 @@ class SynologyDSMFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
try:
|
||||
serial = await self.hass.async_add_executor_job(
|
||||
_login_and_fetch_syno_info, api
|
||||
_login_and_fetch_syno_info, api, otp_code
|
||||
)
|
||||
except InvalidAuth:
|
||||
except SynologyDSMLogin2SARequiredException:
|
||||
return await self.async_step_2sa(user_input)
|
||||
except SynologyDSMLogin2SAFailedException:
|
||||
errors[CONF_OTP_CODE] = "otp_failed"
|
||||
user_input[CONF_OTP_CODE] = None
|
||||
return await self.async_step_2sa(user_input, errors)
|
||||
except (SynologyDSMLoginInvalidException, InvalidAuth):
|
||||
errors[CONF_USERNAME] = "login"
|
||||
except InvalidData:
|
||||
errors["base"] = "missing_data"
|
||||
@@ -139,6 +154,8 @@ class SynologyDSMFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
CONF_PASSWORD: password,
|
||||
CONF_API_VERSION: api_version,
|
||||
}
|
||||
if otp_code:
|
||||
config_data["device_token"] = api.device_token
|
||||
if user_input.get(CONF_DISKS):
|
||||
config_data[CONF_DISKS] = user_input[CONF_DISKS]
|
||||
if user_input.get(CONF_VOLUMES):
|
||||
@@ -168,10 +185,27 @@ class SynologyDSMFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Import a config entry."""
|
||||
return await self.async_step_user(user_input)
|
||||
|
||||
async def async_step_link(self, user_input=None):
|
||||
async def async_step_link(self, user_input):
|
||||
"""Link a config entry from discovery."""
|
||||
return await self.async_step_user(user_input)
|
||||
|
||||
async def async_step_2sa(self, user_input, errors=None):
|
||||
"""Enter 2SA code to anthenticate."""
|
||||
if not self.saved_user_input:
|
||||
self.saved_user_input = user_input
|
||||
|
||||
if not user_input.get(CONF_OTP_CODE):
|
||||
return self.async_show_form(
|
||||
step_id="2sa",
|
||||
data_schema=vol.Schema({vol.Required(CONF_OTP_CODE): str}),
|
||||
errors=errors or {},
|
||||
)
|
||||
|
||||
user_input = {**self.saved_user_input, **user_input}
|
||||
self.saved_user_input = {}
|
||||
|
||||
return await self.async_step_user(user_input)
|
||||
|
||||
def _host_already_configured(self, hostname):
|
||||
"""See if we already have a host matching user input configured."""
|
||||
existing_hosts = {
|
||||
@@ -180,9 +214,9 @@ class SynologyDSMFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
return hostname in existing_hosts
|
||||
|
||||
|
||||
def _login_and_fetch_syno_info(api):
|
||||
def _login_and_fetch_syno_info(api, otp_code):
|
||||
"""Login to the NAS and fetch basic data."""
|
||||
if not api.login():
|
||||
if not api.login(otp_code):
|
||||
raise InvalidAuth
|
||||
|
||||
# These do i/o
|
||||
|
||||
@@ -6,9 +6,9 @@ from homeassistant.const import (
|
||||
)
|
||||
|
||||
DOMAIN = "synology_dsm"
|
||||
BASE_NAME = "Synology"
|
||||
|
||||
CONF_VOLUMES = "volumes"
|
||||
BASE_NAME = "Synology"
|
||||
DEFAULT_SSL = True
|
||||
DEFAULT_PORT = 5000
|
||||
DEFAULT_PORT_SSL = 5001
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"domain": "synology_dsm",
|
||||
"name": "Synology DSM",
|
||||
"documentation": "https://www.home-assistant.io/integrations/synology_dsm",
|
||||
"requirements": ["python-synology==0.5.0"],
|
||||
"requirements": ["python-synology==0.6.0"],
|
||||
"codeowners": ["@ProtoThis", "@Quentame"],
|
||||
"config_flow": true,
|
||||
"ssdp": [
|
||||
|
||||
@@ -13,6 +13,12 @@
|
||||
"password": "Password"
|
||||
}
|
||||
},
|
||||
"2sa": {
|
||||
"title": "Synology DSM: two-step authentication",
|
||||
"data": {
|
||||
"otp_code": "Code"
|
||||
}
|
||||
},
|
||||
"link": {
|
||||
"title": "Synology DSM",
|
||||
"description": "Do you want to setup {name} ({host})?",
|
||||
@@ -27,7 +33,8 @@
|
||||
},
|
||||
"error": {
|
||||
"login": "Login error: please check your username & password",
|
||||
"missing_data": "Missing data: please retry later or an other configuration"
|
||||
"missing_data": "Missing data: please retry later or an other configuration",
|
||||
"otp_failed": "Two-step authentication failed, retry with a new pass code"
|
||||
},
|
||||
"abort": { "already_configured": "Host already configured" }
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user