mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Use HassKey in auth (#127573)
This commit is contained in:
parent
c3e37ef9a0
commit
7e6c106869
@ -159,6 +159,7 @@ from homeassistant.helpers.config_entry_oauth2_flow import OAuth2AuthorizeCallba
|
|||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
from homeassistant.util.hass_dict import HassKey
|
||||||
|
|
||||||
from . import indieauth, login_flow, mfa_setup_flow
|
from . import indieauth, login_flow, mfa_setup_flow
|
||||||
|
|
||||||
@ -166,7 +167,7 @@ DOMAIN = "auth"
|
|||||||
|
|
||||||
type StoreResultType = Callable[[str, Credentials], str]
|
type StoreResultType = Callable[[str, Credentials], str]
|
||||||
type RetrieveResultType = Callable[[str, str], Credentials | None]
|
type RetrieveResultType = Callable[[str, str], Credentials | None]
|
||||||
|
DATA_STORE: HassKey[StoreResultType] = HassKey(DOMAIN)
|
||||||
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
|
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
|
||||||
|
|
||||||
DELETE_CURRENT_TOKEN_DELAY = 2
|
DELETE_CURRENT_TOKEN_DELAY = 2
|
||||||
@ -177,14 +178,14 @@ def create_auth_code(
|
|||||||
hass: HomeAssistant, client_id: str, credential: Credentials
|
hass: HomeAssistant, client_id: str, credential: Credentials
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Create an authorization code to fetch tokens."""
|
"""Create an authorization code to fetch tokens."""
|
||||||
return cast(StoreResultType, hass.data[DOMAIN])(client_id, credential)
|
return hass.data[DATA_STORE](client_id, credential)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Component to allow users to login."""
|
"""Component to allow users to login."""
|
||||||
store_result, retrieve_result = _create_auth_code_store()
|
store_result, retrieve_result = _create_auth_code_store()
|
||||||
|
|
||||||
hass.data[DOMAIN] = store_result
|
hass.data[DATA_STORE] = store_result
|
||||||
|
|
||||||
hass.http.register_view(TokenView(retrieve_result))
|
hass.http.register_view(TokenView(retrieve_result))
|
||||||
hass.http.register_view(RevokeTokenView())
|
hass.http.register_view(RevokeTokenView())
|
||||||
|
@ -12,6 +12,7 @@ from homeassistant import data_entry_flow
|
|||||||
from homeassistant.components import websocket_api
|
from homeassistant.components import websocket_api
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.util.hass_dict import HassKey
|
||||||
|
|
||||||
WS_TYPE_SETUP_MFA = "auth/setup_mfa"
|
WS_TYPE_SETUP_MFA = "auth/setup_mfa"
|
||||||
SCHEMA_WS_SETUP_MFA = vol.All(
|
SCHEMA_WS_SETUP_MFA = vol.All(
|
||||||
@ -31,7 +32,7 @@ SCHEMA_WS_DEPOSE_MFA = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
|
|||||||
{vol.Required("type"): WS_TYPE_DEPOSE_MFA, vol.Required("mfa_module_id"): str}
|
{vol.Required("type"): WS_TYPE_DEPOSE_MFA, vol.Required("mfa_module_id"): str}
|
||||||
)
|
)
|
||||||
|
|
||||||
DATA_SETUP_FLOW_MGR = "auth_mfa_setup_flow_manager"
|
DATA_SETUP_FLOW_MGR: HassKey[MfaFlowManager] = HassKey("auth_mfa_setup_flow_manager")
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -89,7 +90,7 @@ def websocket_setup_mfa(
|
|||||||
|
|
||||||
async def async_setup_flow(msg: dict[str, Any]) -> None:
|
async def async_setup_flow(msg: dict[str, Any]) -> None:
|
||||||
"""Return a setup flow for mfa auth module."""
|
"""Return a setup flow for mfa auth module."""
|
||||||
flow_manager: MfaFlowManager = hass.data[DATA_SETUP_FLOW_MGR]
|
flow_manager = hass.data[DATA_SETUP_FLOW_MGR]
|
||||||
|
|
||||||
if (flow_id := msg.get("flow_id")) is not None:
|
if (flow_id := msg.get("flow_id")) is not None:
|
||||||
result = await flow_manager.async_configure(flow_id, msg.get("user_input"))
|
result = await flow_manager.async_configure(flow_id, msg.get("user_input"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user