From 2d374d65b65b27f3678a7d829031cc9407a4ecc6 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 3 Oct 2021 22:02:30 -0700 Subject: [PATCH] Mark auth voluptuous schema fields as required (#57003) --- homeassistant/auth/mfa_modules/insecure_example.py | 4 ++-- homeassistant/auth/mfa_modules/notify.py | 2 +- homeassistant/auth/mfa_modules/totp.py | 2 +- homeassistant/auth/providers/command_line.py | 14 ++++++++------ homeassistant/auth/providers/homeassistant.py | 14 ++++++++------ homeassistant/auth/providers/insecure_example.py | 14 ++++++++------ .../auth/providers/legacy_api_password.py | 4 +++- homeassistant/auth/providers/trusted_networks.py | 4 +++- tests/components/auth/test_mfa_setup_flow.py | 2 +- 9 files changed, 35 insertions(+), 25 deletions(-) diff --git a/homeassistant/auth/mfa_modules/insecure_example.py b/homeassistant/auth/mfa_modules/insecure_example.py index 1d40339417b..a50b762b121 100644 --- a/homeassistant/auth/mfa_modules/insecure_example.py +++ b/homeassistant/auth/mfa_modules/insecure_example.py @@ -38,12 +38,12 @@ class InsecureExampleModule(MultiFactorAuthModule): @property def input_schema(self) -> vol.Schema: """Validate login flow input data.""" - return vol.Schema({"pin": str}) + return vol.Schema({vol.Required("pin"): str}) @property def setup_schema(self) -> vol.Schema: """Validate async_setup_user input data.""" - return vol.Schema({"pin": str}) + return vol.Schema({vol.Required("pin"): str}) async def async_setup_flow(self, user_id: str) -> SetupFlow: """Return a data entry flow handler for setup module. diff --git a/homeassistant/auth/mfa_modules/notify.py b/homeassistant/auth/mfa_modules/notify.py index 7d5cf0b0641..ec5d5b7cd03 100644 --- a/homeassistant/auth/mfa_modules/notify.py +++ b/homeassistant/auth/mfa_modules/notify.py @@ -110,7 +110,7 @@ class NotifyAuthModule(MultiFactorAuthModule): @property def input_schema(self) -> vol.Schema: """Validate login flow input data.""" - return vol.Schema({INPUT_FIELD_CODE: str}) + return vol.Schema({vol.Required(INPUT_FIELD_CODE): str}) async def _async_load(self) -> None: """Load stored data.""" diff --git a/homeassistant/auth/mfa_modules/totp.py b/homeassistant/auth/mfa_modules/totp.py index 5ff2c01c755..0ff7e1147b1 100644 --- a/homeassistant/auth/mfa_modules/totp.py +++ b/homeassistant/auth/mfa_modules/totp.py @@ -84,7 +84,7 @@ class TotpAuthModule(MultiFactorAuthModule): @property def input_schema(self) -> vol.Schema: """Validate login flow input data.""" - return vol.Schema({INPUT_FIELD_CODE: str}) + return vol.Schema({vol.Required(INPUT_FIELD_CODE): str}) async def _async_load(self) -> None: """Load stored data.""" diff --git a/homeassistant/auth/providers/command_line.py b/homeassistant/auth/providers/command_line.py index 6d1a1627fd5..81a6b6d78e5 100644 --- a/homeassistant/auth/providers/command_line.py +++ b/homeassistant/auth/providers/command_line.py @@ -2,7 +2,6 @@ from __future__ import annotations import asyncio -import collections from collections.abc import Mapping import logging import os @@ -148,10 +147,13 @@ class CommandLineLoginFlow(LoginFlow): user_input.pop("password") return await self.async_finish(user_input) - schema: dict[str, type] = collections.OrderedDict() - schema["username"] = str - schema["password"] = str - return self.async_show_form( - step_id="init", data_schema=vol.Schema(schema), errors=errors + step_id="init", + data_schema=vol.Schema( + { + vol.Required("username"): str, + vol.Required("password"): str, + } + ), + errors=errors, ) diff --git a/homeassistant/auth/providers/homeassistant.py b/homeassistant/auth/providers/homeassistant.py index 6ac9fac03e5..1ffed6f87fd 100644 --- a/homeassistant/auth/providers/homeassistant.py +++ b/homeassistant/auth/providers/homeassistant.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio import base64 -from collections import OrderedDict from collections.abc import Mapping import logging from typing import Any, cast @@ -335,10 +334,13 @@ class HassLoginFlow(LoginFlow): user_input.pop("password") return await self.async_finish(user_input) - schema: dict[str, type] = OrderedDict() - schema["username"] = str - schema["password"] = str - return self.async_show_form( - step_id="init", data_schema=vol.Schema(schema), errors=errors + step_id="init", + data_schema=vol.Schema( + { + vol.Required("username"): str, + vol.Required("password"): str, + } + ), + errors=errors, ) diff --git a/homeassistant/auth/providers/insecure_example.py b/homeassistant/auth/providers/insecure_example.py index fb390b65b0d..9ad6da27ce3 100644 --- a/homeassistant/auth/providers/insecure_example.py +++ b/homeassistant/auth/providers/insecure_example.py @@ -1,7 +1,6 @@ """Example auth provider.""" from __future__ import annotations -from collections import OrderedDict from collections.abc import Mapping import hmac from typing import Any, cast @@ -117,10 +116,13 @@ class ExampleLoginFlow(LoginFlow): user_input.pop("password") return await self.async_finish(user_input) - schema: dict[str, type] = OrderedDict() - schema["username"] = str - schema["password"] = str - return self.async_show_form( - step_id="init", data_schema=vol.Schema(schema), errors=errors + step_id="init", + data_schema=vol.Schema( + { + vol.Required("username"): str, + vol.Required("password"): str, + } + ), + errors=errors, ) diff --git a/homeassistant/auth/providers/legacy_api_password.py b/homeassistant/auth/providers/legacy_api_password.py index af24506210b..2cb113b8b8c 100644 --- a/homeassistant/auth/providers/legacy_api_password.py +++ b/homeassistant/auth/providers/legacy_api_password.py @@ -102,5 +102,7 @@ class LegacyLoginFlow(LoginFlow): return await self.async_finish({}) return self.async_show_form( - step_id="init", data_schema=vol.Schema({"password": str}), errors=errors + step_id="init", + data_schema=vol.Schema({vol.Required("password"): str}), + errors=errors, ) diff --git a/homeassistant/auth/providers/trusted_networks.py b/homeassistant/auth/providers/trusted_networks.py index a9ee6a48335..0f2b287a227 100644 --- a/homeassistant/auth/providers/trusted_networks.py +++ b/homeassistant/auth/providers/trusted_networks.py @@ -244,5 +244,7 @@ class TrustedNetworksLoginFlow(LoginFlow): return self.async_show_form( step_id="init", - data_schema=vol.Schema({"user": vol.In(self._available_users)}), + data_schema=vol.Schema( + {vol.Required("user"): vol.In(self._available_users)} + ), ) diff --git a/tests/components/auth/test_mfa_setup_flow.py b/tests/components/auth/test_mfa_setup_flow.py index 3569d7d5233..edf45742dbd 100644 --- a/tests/components/auth/test_mfa_setup_flow.py +++ b/tests/components/auth/test_mfa_setup_flow.py @@ -67,7 +67,7 @@ async def test_ws_setup_depose_mfa(hass, hass_ws_client): assert flow["type"] == data_entry_flow.RESULT_TYPE_FORM assert flow["handler"] == "example_module" assert flow["step_id"] == "init" - assert flow["data_schema"][0] == {"type": "string", "name": "pin"} + assert flow["data_schema"][0] == {"type": "string", "name": "pin", "required": True} await client.send_json( {