Mark auth voluptuous schema fields as required (#57003)

This commit is contained in:
Paulus Schoutsen 2021-10-03 22:02:30 -07:00 committed by GitHub
parent 79b10c43d8
commit 2d374d65b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 35 additions and 25 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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)}
),
)

View File

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