Strip whitespace characters from token in One-Time-Passwort (OTP) integration (#120380)

This commit is contained in:
Mr. Bubbles 2024-06-25 08:00:19 +02:00 committed by GitHub
parent f1ddf80dff
commit 59080a3a6f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 6 deletions

View File

@ -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

View File

@ -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