core/homeassistant/auth/mfa_modules/insecure_example.py

89 lines
2.6 KiB
Python
Raw Normal View History

"""Example auth module."""
2021-03-17 20:46:07 +00:00
from __future__ import annotations
from typing import Any
import voluptuous as vol
from homeassistant.core import HomeAssistant
2019-07-31 19:25:30 +00:00
from . import (
MULTI_FACTOR_AUTH_MODULE_SCHEMA,
MULTI_FACTOR_AUTH_MODULES,
MultiFactorAuthModule,
2019-07-31 19:25:30 +00:00
SetupFlow,
)
CONFIG_SCHEMA = MULTI_FACTOR_AUTH_MODULE_SCHEMA.extend(
{
vol.Required("data"): [
vol.Schema({vol.Required("user_id"): str, vol.Required("pin"): str})
]
},
extra=vol.PREVENT_EXTRA,
)
2019-07-31 19:25:30 +00:00
@MULTI_FACTOR_AUTH_MODULES.register("insecure_example")
class InsecureExampleModule(MultiFactorAuthModule):
"""Example auth module validate pin."""
2019-07-31 19:25:30 +00:00
DEFAULT_TITLE = "Insecure Personal Identify Number"
2021-03-17 20:46:07 +00:00
def __init__(self, hass: HomeAssistant, config: dict[str, Any]) -> None:
"""Initialize the user data store."""
super().__init__(hass, config)
2019-07-31 19:25:30 +00:00
self._data = config["data"]
@property
def input_schema(self) -> vol.Schema:
"""Validate login flow input data."""
return vol.Schema({vol.Required("pin"): str})
@property
def setup_schema(self) -> vol.Schema:
"""Validate async_setup_user input data."""
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.
Mfa module should extend SetupFlow
"""
return SetupFlow(self, self.setup_schema, user_id)
async def async_setup_user(self, user_id: str, setup_data: Any) -> Any:
"""Set up user to use mfa module."""
# data shall has been validate in caller
2019-07-31 19:25:30 +00:00
pin = setup_data["pin"]
for data in self._data:
2019-07-31 19:25:30 +00:00
if data["user_id"] == user_id:
# already setup, override
2019-07-31 19:25:30 +00:00
data["pin"] = pin
return
2019-07-31 19:25:30 +00:00
self._data.append({"user_id": user_id, "pin": pin})
async def async_depose_user(self, user_id: str) -> None:
"""Remove user from mfa module."""
found = None
for data in self._data:
2019-07-31 19:25:30 +00:00
if data["user_id"] == user_id:
found = data
break
if found:
self._data.remove(found)
async def async_is_user_setup(self, user_id: str) -> bool:
"""Return whether user is setup."""
return any(data["user_id"] == user_id for data in self._data)
2021-03-17 20:46:07 +00:00
async def async_validate(self, user_id: str, user_input: dict[str, Any]) -> bool:
"""Return True if validation passed."""
return any(
data["user_id"] == user_id and data["pin"] == user_input["pin"]
for data in self._data
)