From 59080a3a6fa3befd25dc6c8f7502f8da90631afb Mon Sep 17 00:00:00 2001 From: "Mr. Bubbles" Date: Tue, 25 Jun 2024 08:00:19 +0200 Subject: [PATCH] Strip whitespace characters from token in One-Time-Passwort (OTP) integration (#120380) --- homeassistant/components/otp/config_flow.py | 2 ++ tests/components/otp/test_config_flow.py | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/otp/config_flow.py b/homeassistant/components/otp/config_flow.py index 15d04c910ad..6aa4532683a 100644 --- a/homeassistant/components/otp/config_flow.py +++ b/homeassistant/components/otp/config_flow.py @@ -4,6 +4,7 @@ from __future__ import annotations import binascii import logging +from re import sub from typing import Any import pyotp @@ -47,6 +48,7 @@ class TOTPConfigFlow(ConfigFlow, domain=DOMAIN): errors: dict[str, str] = {} if user_input is not None: if user_input.get(CONF_TOKEN) and not user_input.get(CONF_NEW_TOKEN): + user_input[CONF_TOKEN] = sub(r"\s+", "", user_input[CONF_TOKEN]) try: await self.hass.async_add_executor_job( pyotp.TOTP(user_input[CONF_TOKEN]).now diff --git a/tests/components/otp/test_config_flow.py b/tests/components/otp/test_config_flow.py index eefb1a6f4e0..f9fac433ff9 100644 --- a/tests/components/otp/test_config_flow.py +++ b/tests/components/otp/test_config_flow.py @@ -12,6 +12,10 @@ from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType TEST_DATA = { + CONF_NAME: "OTP Sensor", + CONF_TOKEN: "2FX5 FBSY RE6V EC2F SHBQ CRKO 2GND VZ52", +} +TEST_DATA_RESULT = { CONF_NAME: "OTP Sensor", CONF_TOKEN: "2FX5FBSYRE6VEC2FSHBQCRKO2GNDVZ52", } @@ -41,7 +45,11 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: result["flow_id"], TEST_DATA, ) - await hass.async_block_till_done() + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "OTP Sensor" + assert result["data"] == TEST_DATA_RESULT + assert len(mock_setup_entry.mock_calls) == 1 @pytest.mark.parametrize( @@ -85,7 +93,7 @@ async def test_errors_and_recover( assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "OTP Sensor" - assert result["data"] == TEST_DATA + assert result["data"] == TEST_DATA_RESULT assert len(mock_setup_entry.mock_calls) == 1 @@ -96,13 +104,13 @@ async def test_flow_import(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_IMPORT}, - data=TEST_DATA, + data=TEST_DATA_RESULT, ) await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "OTP Sensor" - assert result["data"] == TEST_DATA + assert result["data"] == TEST_DATA_RESULT @pytest.mark.usefixtures("mock_pyotp") @@ -134,7 +142,7 @@ async def test_generate_new_token( assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "OTP Sensor" - assert result["data"] == TEST_DATA + assert result["data"] == TEST_DATA_RESULT assert len(mock_setup_entry.mock_calls) == 1 @@ -181,5 +189,5 @@ async def test_generate_new_token_errors( assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "OTP Sensor" - assert result["data"] == TEST_DATA + assert result["data"] == TEST_DATA_RESULT assert len(mock_setup_entry.mock_calls) == 1