Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable auth by default 馃檲 #16107

Merged
merged 4 commits into from Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion homeassistant/bootstrap.py
Expand Up @@ -87,9 +87,11 @@ async def async_from_config_dict(config: Dict[str, Any],
log_no_color)

core_config = config.get(core.DOMAIN, {})
has_api_password = bool((config.get('http') or {}).get('api_password'))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user has just http: in their config, the key exists but is None. The or solves both if key does not exist or when it's None.


try:
await conf_util.async_process_ha_core_config(hass, core_config)
await conf_util.async_process_ha_core_config(
hass, core_config, has_api_password)
except vol.Invalid as ex:
conf_util.async_log_exception(ex, 'homeassistant', core_config, hass)
return None
Expand Down
14 changes: 12 additions & 2 deletions homeassistant/config.py
Expand Up @@ -407,7 +407,8 @@ def _format_config_error(ex: vol.Invalid, domain: str, config: Dict) -> str:


async def async_process_ha_core_config(
hass: HomeAssistant, config: Dict) -> None:
hass: HomeAssistant, config: Dict,
has_api_password: bool = False) -> None:
"""Process the [homeassistant] section from the configuration.

This method is a coroutine.
Expand All @@ -416,9 +417,18 @@ async def async_process_ha_core_config(

# Only load auth during startup.
if not hasattr(hass, 'auth'):
auth_conf = config.get(CONF_AUTH_PROVIDERS)

if auth_conf is None:
auth_conf = [
{'type': 'homeassistant'}
]
if has_api_password:
auth_conf.append({'type': 'legacy_api_password'})

setattr(hass, 'auth', await auth.auth_manager_from_config(
hass,
config.get(CONF_AUTH_PROVIDERS, []),
auth_conf,
config.get(CONF_AUTH_MFA_MODULES, [])))

hac = hass.config
Expand Down
41 changes: 41 additions & 0 deletions tests/test_config.py
Expand Up @@ -812,6 +812,47 @@ async def test_auth_provider_config(hass):
await config_util.async_process_ha_core_config(hass, core_config)

assert len(hass.auth.auth_providers) == 2
assert hass.auth.auth_providers[0].type == 'homeassistant'
assert hass.auth.auth_providers[1].type == 'legacy_api_password'
assert hass.auth.active is True


async def test_auth_provider_config_default(hass):
"""Test loading default auth provider config."""
core_config = {
'latitude': 60,
'longitude': 50,
'elevation': 25,
'name': 'Huis',
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
'time_zone': 'GMT',
}
if hasattr(hass, 'auth'):
del hass.auth
await config_util.async_process_ha_core_config(hass, core_config)

assert len(hass.auth.auth_providers) == 1
assert hass.auth.auth_providers[0].type == 'homeassistant'
assert hass.auth.active is True


async def test_auth_provider_config_default_api_password(hass):
"""Test loading default auth provider config with api password."""
core_config = {
'latitude': 60,
'longitude': 50,
'elevation': 25,
'name': 'Huis',
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
'time_zone': 'GMT',
}
if hasattr(hass, 'auth'):
del hass.auth
await config_util.async_process_ha_core_config(hass, core_config, True)

assert len(hass.auth.auth_providers) == 2
assert hass.auth.auth_providers[0].type == 'homeassistant'
assert hass.auth.auth_providers[1].type == 'legacy_api_password'
assert hass.auth.active is True


Expand Down