Skip to content

Commit

Permalink
Deprecate legacy api auth provider (#104409)
Browse files Browse the repository at this point in the history
Co-authored-by: Franck Nijhof <git@frenck.dev>
  • Loading branch information
edenhaus and frenck committed Nov 27, 2023
1 parent 5550dcb commit cf9b0e8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 25 deletions.
23 changes: 21 additions & 2 deletions homeassistant/auth/providers/legacy_api_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,40 @@

import voluptuous as vol

from homeassistant.core import callback
from homeassistant.core import async_get_hass, callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue

from ..models import Credentials, UserMeta
from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow

AUTH_PROVIDER_TYPE = "legacy_api_password"
CONF_API_PASSWORD = "api_password"

CONFIG_SCHEMA = AUTH_PROVIDER_SCHEMA.extend(
_CONFIG_SCHEMA = AUTH_PROVIDER_SCHEMA.extend(
{vol.Required(CONF_API_PASSWORD): cv.string}, extra=vol.PREVENT_EXTRA
)


def _create_repair_and_validate(config: dict[str, Any]) -> dict[str, Any]:
async_create_issue(
async_get_hass(),
"auth",
"deprecated_legacy_api_password",
breaks_in_ha_version="2024.6.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_legacy_api_password",
)

return _CONFIG_SCHEMA(config) # type: ignore[no-any-return]


CONFIG_SCHEMA = _create_repair_and_validate


LEGACY_USER_NAME = "Legacy API password user"


Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/auth/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@
"invalid_code": "Invalid code, please try again."
}
}
},
"issues": {
"deprecated_legacy_api_password": {
"title": "The legacy API password is deprecated",
"description": "The legacy API password authentication provider is deprecated and will be removed. Please remove it from your YAML configuration and use the default Home Assistant authentication provider instead."
}
}
}
46 changes: 26 additions & 20 deletions script/hassfest/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,29 @@ def name_validator(value: dict[str, Any]) -> dict[str, Any]:
return vol.All(*validators)


def gen_issues_schema(config: Config, integration: Integration) -> dict[str, Any]:
"""Generate the issues schema."""
return {
str: vol.All(
cv.has_at_least_one_key("description", "fix_flow"),
vol.Schema(
{
vol.Required("title"): translation_value_validator,
vol.Exclusive(
"description", "fixable"
): translation_value_validator,
vol.Exclusive("fix_flow", "fixable"): gen_data_entry_schema(
config=config,
integration=integration,
flow_title=UNDEFINED,
require_step_title=False,
),
},
),
)
}


def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
"""Generate a strings schema."""
return vol.Schema(
Expand Down Expand Up @@ -266,25 +289,7 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
vol.Optional("application_credentials"): {
vol.Optional("description"): translation_value_validator,
},
vol.Optional("issues"): {
str: vol.All(
cv.has_at_least_one_key("description", "fix_flow"),
vol.Schema(
{
vol.Required("title"): translation_value_validator,
vol.Exclusive(
"description", "fixable"
): translation_value_validator,
vol.Exclusive("fix_flow", "fixable"): gen_data_entry_schema(
config=config,
integration=integration,
flow_title=UNDEFINED,
require_step_title=False,
),
},
),
)
},
vol.Optional("issues"): gen_issues_schema(config, integration),
vol.Optional("entity_component"): cv.schema_with_slug_keys(
{
vol.Optional("name"): str,
Expand Down Expand Up @@ -362,7 +367,8 @@ def gen_auth_schema(config: Config, integration: Integration) -> vol.Schema:
flow_title=REQUIRED,
require_step_title=True,
)
}
},
vol.Optional("issues"): gen_issues_schema(config, integration),
}
)

Expand Down
22 changes: 19 additions & 3 deletions tests/auth/providers/test_legacy_api_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
from homeassistant.auth import auth_store
from homeassistant.auth.providers import legacy_api_password
from homeassistant.core import HomeAssistant
import homeassistant.helpers.issue_registry as ir
from homeassistant.setup import async_setup_component

from tests.common import ensure_auth_manager_loaded

CONFIG = {"type": "legacy_api_password", "api_password": "test-password"}


@pytest.fixture
Expand All @@ -16,9 +22,7 @@ def store(hass):
@pytest.fixture
def provider(hass, store):
"""Mock provider."""
return legacy_api_password.LegacyApiPasswordAuthProvider(
hass, store, {"type": "legacy_api_password", "api_password": "test-password"}
)
return legacy_api_password.LegacyApiPasswordAuthProvider(hass, store, CONFIG)


@pytest.fixture
Expand Down Expand Up @@ -68,3 +72,15 @@ async def test_login_flow_works(hass: HomeAssistant, manager) -> None:
flow_id=result["flow_id"], user_input={"password": "test-password"}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY


async def test_create_repair_issue(hass: HomeAssistant):
"""Test legacy api password auth provider creates a reapir issue."""
hass.auth = await auth.auth_manager_from_config(hass, [CONFIG], [])
ensure_auth_manager_loaded(hass.auth)
await async_setup_component(hass, "auth", {})
issue_registry: ir.IssueRegistry = ir.async_get(hass)

assert issue_registry.async_get_issue(
domain="auth", issue_id="deprecated_legacy_api_password"
)

0 comments on commit cf9b0e8

Please sign in to comment.