mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Strip whitespace characters from token in One-Time-Passwort (OTP) integration (#120380)
This commit is contained in:
parent
f1ddf80dff
commit
59080a3a6f
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import binascii
|
import binascii
|
||||||
import logging
|
import logging
|
||||||
|
from re import sub
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import pyotp
|
import pyotp
|
||||||
@ -47,6 +48,7 @@ class TOTPConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
if user_input.get(CONF_TOKEN) and not user_input.get(CONF_NEW_TOKEN):
|
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:
|
try:
|
||||||
await self.hass.async_add_executor_job(
|
await self.hass.async_add_executor_job(
|
||||||
pyotp.TOTP(user_input[CONF_TOKEN]).now
|
pyotp.TOTP(user_input[CONF_TOKEN]).now
|
||||||
|
@ -15,6 +15,10 @@ TEST_DATA = {
|
|||||||
CONF_NAME: "OTP Sensor",
|
CONF_NAME: "OTP Sensor",
|
||||||
CONF_TOKEN: "2FX5 FBSY RE6V EC2F SHBQ CRKO 2GND VZ52",
|
CONF_TOKEN: "2FX5 FBSY RE6V EC2F SHBQ CRKO 2GND VZ52",
|
||||||
}
|
}
|
||||||
|
TEST_DATA_RESULT = {
|
||||||
|
CONF_NAME: "OTP Sensor",
|
||||||
|
CONF_TOKEN: "2FX5FBSYRE6VEC2FSHBQCRKO2GNDVZ52",
|
||||||
|
}
|
||||||
|
|
||||||
TEST_DATA_2 = {
|
TEST_DATA_2 = {
|
||||||
CONF_NAME: "OTP Sensor",
|
CONF_NAME: "OTP Sensor",
|
||||||
@ -41,7 +45,11 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
|||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
TEST_DATA,
|
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(
|
@pytest.mark.parametrize(
|
||||||
@ -85,7 +93,7 @@ async def test_errors_and_recover(
|
|||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "OTP Sensor"
|
assert result["title"] == "OTP Sensor"
|
||||||
assert result["data"] == TEST_DATA
|
assert result["data"] == TEST_DATA_RESULT
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
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(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_IMPORT},
|
context={"source": SOURCE_IMPORT},
|
||||||
data=TEST_DATA,
|
data=TEST_DATA_RESULT,
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "OTP Sensor"
|
assert result["title"] == "OTP Sensor"
|
||||||
assert result["data"] == TEST_DATA
|
assert result["data"] == TEST_DATA_RESULT
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_pyotp")
|
@pytest.mark.usefixtures("mock_pyotp")
|
||||||
@ -134,7 +142,7 @@ async def test_generate_new_token(
|
|||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "OTP Sensor"
|
assert result["title"] == "OTP Sensor"
|
||||||
assert result["data"] == TEST_DATA
|
assert result["data"] == TEST_DATA_RESULT
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
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["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "OTP Sensor"
|
assert result["title"] == "OTP Sensor"
|
||||||
assert result["data"] == TEST_DATA
|
assert result["data"] == TEST_DATA_RESULT
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user