Remove direct authentication via trusted networks or API password (#27656)
* Remove direct authentication via trusted networks and API password * Fix tests
This commit is contained in:
1 parent
97478d1ef4
commit
3231e22ddf
33 files changed
+114
-423
No files matched your search
@@ -17,7 +17,6 @@ from homeassistant.const import (
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import homeassistant.util as hass_util
|
||||
from homeassistant.util import ssl as ssl_util
|
||||
from homeassistant.util.logging import HideSensitiveDataFilter
|
||||
|
||||
from .auth import setup_auth
|
||||
from .ban import setup_bans
|
||||
@@ -32,7 +31,6 @@ from .view import HomeAssistantView # noqa
|
||||
|
||||
DOMAIN = "http"
|
||||
|
||||
CONF_API_PASSWORD = "api_password"
|
||||
CONF_SERVER_HOST = "server_host"
|
||||
CONF_SERVER_PORT = "server_port"
|
||||
CONF_BASE_URL = "base_url"
|
||||
@@ -42,7 +40,6 @@ CONF_SSL_KEY = "ssl_key"
|
||||
CONF_CORS_ORIGINS = "cors_allowed_origins"
|
||||
CONF_USE_X_FORWARDED_FOR = "use_x_forwarded_for"
|
||||
CONF_TRUSTED_PROXIES = "trusted_proxies"
|
||||
CONF_TRUSTED_NETWORKS = "trusted_networks"
|
||||
CONF_LOGIN_ATTEMPTS_THRESHOLD = "login_attempts_threshold"
|
||||
CONF_IP_BAN_ENABLED = "ip_ban_enabled"
|
||||
CONF_SSL_PROFILE = "ssl_profile"
|
||||
@@ -59,37 +56,8 @@ DEFAULT_CORS = "https://cast.home-assistant.io"
|
||||
NO_LOGIN_ATTEMPT_THRESHOLD = -1
|
||||
|
||||
|
||||
def trusted_networks_deprecated(value):
|
||||
"""Warn user trusted_networks config is deprecated."""
|
||||
if not value:
|
||||
return value
|
||||
|
||||
_LOGGER.warning(
|
||||
"Configuring trusted_networks via the http integration has been"
|
||||
" deprecated. Use the trusted networks auth provider instead."
|
||||
" For instructions, see https://www.home-assistant.io/docs/"
|
||||
"authentication/providers/#trusted-networks"
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
def api_password_deprecated(value):
|
||||
"""Warn user api_password config is deprecated."""
|
||||
if not value:
|
||||
return value
|
||||
|
||||
_LOGGER.warning(
|
||||
"Configuring api_password via the http integration has been"
|
||||
" deprecated. Use the legacy api password auth provider instead."
|
||||
" For instructions, see https://www.home-assistant.io/docs/"
|
||||
"authentication/providers/#legacy-api-password"
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
HTTP_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_API_PASSWORD): vol.All(cv.string, api_password_deprecated),
|
||||
vol.Optional(CONF_SERVER_HOST, default=DEFAULT_SERVER_HOST): cv.string,
|
||||
vol.Optional(CONF_SERVER_PORT, default=SERVER_PORT): cv.port,
|
||||
vol.Optional(CONF_BASE_URL): cv.string,
|
||||
@@ -103,9 +71,6 @@ HTTP_SCHEMA = vol.Schema(
|
||||
vol.Inclusive(CONF_TRUSTED_PROXIES, "proxy"): vol.All(
|
||||
cv.ensure_list, [ip_network]
|
||||
),
|
||||
vol.Optional(CONF_TRUSTED_NETWORKS, default=[]): vol.All(
|
||||
cv.ensure_list, [ip_network], trusted_networks_deprecated
|
||||
),
|
||||
vol.Optional(
|
||||
CONF_LOGIN_ATTEMPTS_THRESHOLD, default=NO_LOGIN_ATTEMPT_THRESHOLD
|
||||
): vol.Any(cv.positive_int, NO_LOGIN_ATTEMPT_THRESHOLD),
|
||||
@@ -149,7 +114,6 @@ async def async_setup(hass, config):
|
||||
if conf is None:
|
||||
conf = HTTP_SCHEMA({})
|
||||
|
||||
api_password = conf.get(CONF_API_PASSWORD)
|
||||
server_host = conf[CONF_SERVER_HOST]
|
||||
server_port = conf[CONF_SERVER_PORT]
|
||||
ssl_certificate = conf.get(CONF_SSL_CERTIFICATE)
|
||||
@@ -162,11 +126,6 @@ async def async_setup(hass, config):
|
||||
login_threshold = conf[CONF_LOGIN_ATTEMPTS_THRESHOLD]
|
||||
ssl_profile = conf[CONF_SSL_PROFILE]
|
||||
|
||||
if api_password is not None:
|
||||
logging.getLogger("aiohttp.access").addFilter(
|
||||
HideSensitiveDataFilter(api_password)
|
||||
)
|
||||
|
||||
server = HomeAssistantHTTP(
|
||||
hass,
|
||||
server_host=server_host,
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
"""Authentication for HTTP component."""
|
||||
import base64
|
||||
import logging
|
||||
|
||||
from aiohttp import hdrs
|
||||
from aiohttp.web import middleware
|
||||
import jwt
|
||||
|
||||
from homeassistant.auth.providers import legacy_api_password
|
||||
from homeassistant.auth.util import generate_secret
|
||||
from homeassistant.const import HTTP_HEADER_HA_AUTH
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
@@ -52,16 +49,6 @@ def async_sign_path(hass, refresh_token_id, path, expiration):
|
||||
@callback
|
||||
def setup_auth(hass, app):
|
||||
"""Create auth middleware for the app."""
|
||||
old_auth_warning = set()
|
||||
|
||||
support_legacy = hass.auth.support_legacy
|
||||
if support_legacy:
|
||||
_LOGGER.warning("legacy_api_password support has been enabled.")
|
||||
|
||||
trusted_networks = []
|
||||
for prv in hass.auth.auth_providers:
|
||||
if prv.type == "trusted_networks":
|
||||
trusted_networks += prv.trusted_networks
|
||||
|
||||
async def async_validate_auth_header(request):
|
||||
"""
|
||||
@@ -75,40 +62,16 @@ def setup_auth(hass, app):
|
||||
# If no space in authorization header
|
||||
return False
|
||||
|
||||
if auth_type == "Bearer":
|
||||
refresh_token = await hass.auth.async_validate_access_token(auth_val)
|
||||
if refresh_token is None:
|
||||
return False
|
||||
if auth_type != "Bearer":
|
||||
return False
|
||||
|
||||
request[KEY_HASS_USER] = refresh_token.user
|
||||
return True
|
||||
refresh_token = await hass.auth.async_validate_access_token(auth_val)
|
||||
|
||||
if auth_type == "Basic" and support_legacy:
|
||||
decoded = base64.b64decode(auth_val).decode("utf-8")
|
||||
try:
|
||||
username, password = decoded.split(":", 1)
|
||||
except ValueError:
|
||||
# If no ':' in decoded
|
||||
return False
|
||||
if refresh_token is None:
|
||||
return False
|
||||
|
||||
if username != "homeassistant":
|
||||
return False
|
||||
|
||||
user = await legacy_api_password.async_validate_password(hass, password)
|
||||
if user is None:
|
||||
return False
|
||||
|
||||
request[KEY_HASS_USER] = user
|
||||
_LOGGER.info(
|
||||
"Basic auth with api_password is going to deprecate,"
|
||||
" please use a bearer token to access %s from %s",
|
||||
request.path,
|
||||
request[KEY_REAL_IP],
|
||||
)
|
||||
old_auth_warning.add(request.path)
|
||||
return True
|
||||
|
||||
return False
|
||||
request[KEY_HASS_USER] = refresh_token.user
|
||||
return True
|
||||
|
||||
async def async_validate_signed_request(request):
|
||||
"""Validate a signed request."""
|
||||
@@ -140,50 +103,16 @@ def setup_auth(hass, app):
|
||||
request[KEY_HASS_USER] = refresh_token.user
|
||||
return True
|
||||
|
||||
async def async_validate_trusted_networks(request):
|
||||
"""Test if request is from a trusted ip."""
|
||||
ip_addr = request[KEY_REAL_IP]
|
||||
|
||||
if not any(ip_addr in trusted_network for trusted_network in trusted_networks):
|
||||
return False
|
||||
|
||||
user = await hass.auth.async_get_owner()
|
||||
if user is None:
|
||||
return False
|
||||
|
||||
request[KEY_HASS_USER] = user
|
||||
return True
|
||||
|
||||
async def async_validate_legacy_api_password(request, password):
|
||||
"""Validate api_password."""
|
||||
user = await legacy_api_password.async_validate_password(hass, password)
|
||||
if user is None:
|
||||
return False
|
||||
|
||||
request[KEY_HASS_USER] = user
|
||||
return True
|
||||
|
||||
@middleware
|
||||
async def auth_middleware(request, handler):
|
||||
"""Authenticate as middleware."""
|
||||
authenticated = False
|
||||
|
||||
if HTTP_HEADER_HA_AUTH in request.headers or DATA_API_PASSWORD in request.query:
|
||||
if request.path not in old_auth_warning:
|
||||
_LOGGER.log(
|
||||
logging.INFO if support_legacy else logging.WARNING,
|
||||
"api_password is going to deprecate. You need to use a"
|
||||
" bearer token to access %s from %s",
|
||||
request.path,
|
||||
request[KEY_REAL_IP],
|
||||
)
|
||||
old_auth_warning.add(request.path)
|
||||
|
||||
if hdrs.AUTHORIZATION in request.headers and await async_validate_auth_header(
|
||||
request
|
||||
):
|
||||
# it included both use_auth and api_password Basic auth
|
||||
authenticated = True
|
||||
auth_type = "bearer token"
|
||||
|
||||
# We first start with a string check to avoid parsing query params
|
||||
# for every request.
|
||||
@@ -193,39 +122,15 @@ def setup_auth(hass, app):
|
||||
and await async_validate_signed_request(request)
|
||||
):
|
||||
authenticated = True
|
||||
auth_type = "signed request"
|
||||
|
||||
elif trusted_networks and await async_validate_trusted_networks(request):
|
||||
if request.path not in old_auth_warning:
|
||||
# When removing this, don't forget to remove the print logic
|
||||
# in http/view.py
|
||||
request["deprecate_warning_message"] = (
|
||||
"Access from trusted networks without auth token is "
|
||||
"going to be removed in Home Assistant 0.96. Configure "
|
||||
"the trusted networks auth provider or use long-lived "
|
||||
"access tokens to access {} from {}".format(
|
||||
request.path, request[KEY_REAL_IP]
|
||||
)
|
||||
)
|
||||
old_auth_warning.add(request.path)
|
||||
authenticated = True
|
||||
|
||||
elif (
|
||||
support_legacy
|
||||
and HTTP_HEADER_HA_AUTH in request.headers
|
||||
and await async_validate_legacy_api_password(
|
||||
request, request.headers[HTTP_HEADER_HA_AUTH]
|
||||
if authenticated:
|
||||
_LOGGER.debug(
|
||||
"Authenticated %s for %s using %s",
|
||||
request[KEY_REAL_IP],
|
||||
request.path,
|
||||
auth_type,
|
||||
)
|
||||
):
|
||||
authenticated = True
|
||||
|
||||
elif (
|
||||
support_legacy
|
||||
and DATA_API_PASSWORD in request.query
|
||||
and await async_validate_legacy_api_password(
|
||||
request, request.query[DATA_API_PASSWORD]
|
||||
)
|
||||
):
|
||||
authenticated = True
|
||||
|
||||
request[KEY_AUTHENTICATED] = authenticated
|
||||
return await handler(request)
|
||||
|
||||
@@ -3,7 +3,7 @@ import aiohttp_cors
|
||||
from aiohttp.web_urldispatcher import Resource, ResourceRoute, StaticResource
|
||||
from aiohttp.hdrs import ACCEPT, CONTENT_TYPE, ORIGIN, AUTHORIZATION
|
||||
|
||||
from homeassistant.const import HTTP_HEADER_HA_AUTH, HTTP_HEADER_X_REQUESTED_WITH
|
||||
from homeassistant.const import HTTP_HEADER_X_REQUESTED_WITH
|
||||
from homeassistant.core import callback
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ ALLOWED_CORS_HEADERS = [
|
||||
ACCEPT,
|
||||
HTTP_HEADER_X_REQUESTED_WITH,
|
||||
CONTENT_TYPE,
|
||||
HTTP_HEADER_HA_AUTH,
|
||||
AUTHORIZATION,
|
||||
]
|
||||
VALID_CORS_TYPES = (Resource, ResourceRoute, StaticResource)
|
||||
|
||||
@@ -17,7 +17,6 @@ from homeassistant.const import CONTENT_TYPE_JSON
|
||||
from homeassistant.core import Context, is_callback
|
||||
from homeassistant.helpers.json import JSONEncoder
|
||||
|
||||
from .ban import process_success_login
|
||||
from .const import KEY_AUTHENTICATED, KEY_HASS, KEY_REAL_IP
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -106,13 +105,8 @@ def request_handler_factory(view, handler):
|
||||
|
||||
authenticated = request.get(KEY_AUTHENTICATED, False)
|
||||
|
||||
if view.requires_auth:
|
||||
if authenticated:
|
||||
if "deprecate_warning_message" in request:
|
||||
_LOGGER.warning(request["deprecate_warning_message"])
|
||||
await process_success_login(request)
|
||||
else:
|
||||
raise HTTPUnauthorized()
|
||||
if view.requires_auth and not authenticated:
|
||||
raise HTTPUnauthorized()
|
||||
|
||||
_LOGGER.debug(
|
||||
"Serving %s to %s (auth: %s)",
|
||||
|
||||
Reference in new issue
Block a user