2016-11-25 21:04:06 +00:00
|
|
|
"""Authentication for HTTP component."""
|
|
|
|
import asyncio
|
2017-09-28 07:49:35 +00:00
|
|
|
import base64
|
2016-11-25 21:04:06 +00:00
|
|
|
import hmac
|
|
|
|
import logging
|
|
|
|
|
2017-09-28 07:49:35 +00:00
|
|
|
from aiohttp import hdrs
|
|
|
|
|
2016-11-25 21:04:06 +00:00
|
|
|
from homeassistant.const import HTTP_HEADER_HA_AUTH
|
|
|
|
from .util import get_real_ip
|
|
|
|
from .const import KEY_TRUSTED_NETWORKS, KEY_AUTHENTICATED
|
|
|
|
|
|
|
|
DATA_API_PASSWORD = 'api_password'
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def auth_middleware(app, handler):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Authenticate as middleware."""
|
2016-11-25 21:04:06 +00:00
|
|
|
# If no password set, just always set authenticated=True
|
|
|
|
if app['hass'].http.api_password is None:
|
|
|
|
@asyncio.coroutine
|
|
|
|
def no_auth_middleware_handler(request):
|
|
|
|
"""Auth middleware to approve all requests."""
|
|
|
|
request[KEY_AUTHENTICATED] = True
|
|
|
|
return handler(request)
|
|
|
|
|
|
|
|
return no_auth_middleware_handler
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def auth_middleware_handler(request):
|
|
|
|
"""Auth middleware to check authentication."""
|
|
|
|
# Auth code verbose on purpose
|
|
|
|
authenticated = False
|
|
|
|
|
2016-11-27 02:23:28 +00:00
|
|
|
if (HTTP_HEADER_HA_AUTH in request.headers and
|
2017-04-30 05:04:49 +00:00
|
|
|
validate_password(
|
|
|
|
request, request.headers[HTTP_HEADER_HA_AUTH])):
|
2016-11-25 21:04:06 +00:00
|
|
|
# A valid auth header has been set
|
|
|
|
authenticated = True
|
|
|
|
|
2017-05-26 20:12:17 +00:00
|
|
|
elif (DATA_API_PASSWORD in request.query and
|
|
|
|
validate_password(request, request.query[DATA_API_PASSWORD])):
|
2016-11-25 21:04:06 +00:00
|
|
|
authenticated = True
|
|
|
|
|
2017-09-28 07:49:35 +00:00
|
|
|
elif (hdrs.AUTHORIZATION in request.headers and
|
|
|
|
validate_authorization_header(request)):
|
|
|
|
authenticated = True
|
|
|
|
|
2016-11-25 21:04:06 +00:00
|
|
|
elif is_trusted_ip(request):
|
|
|
|
authenticated = True
|
|
|
|
|
|
|
|
request[KEY_AUTHENTICATED] = authenticated
|
|
|
|
|
|
|
|
return handler(request)
|
|
|
|
|
|
|
|
return auth_middleware_handler
|
|
|
|
|
|
|
|
|
|
|
|
def is_trusted_ip(request):
|
|
|
|
"""Test if request is from a trusted ip."""
|
|
|
|
ip_addr = get_real_ip(request)
|
|
|
|
|
|
|
|
return ip_addr and any(
|
|
|
|
ip_addr in trusted_network for trusted_network
|
|
|
|
in request.app[KEY_TRUSTED_NETWORKS])
|
2016-11-27 02:23:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_password(request, api_password):
|
|
|
|
"""Test if password is valid."""
|
2017-04-30 05:04:49 +00:00
|
|
|
return hmac.compare_digest(
|
|
|
|
api_password, request.app['hass'].http.api_password)
|
2017-09-28 07:49:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_authorization_header(request):
|
|
|
|
"""Test an authorization header if valid password."""
|
|
|
|
if hdrs.AUTHORIZATION not in request.headers:
|
|
|
|
return False
|
|
|
|
|
|
|
|
auth_type, auth = request.headers.get(hdrs.AUTHORIZATION).split(' ', 1)
|
|
|
|
|
|
|
|
if auth_type != 'Basic':
|
|
|
|
return False
|
|
|
|
|
|
|
|
decoded = base64.b64decode(auth).decode('utf-8')
|
|
|
|
username, password = decoded.split(':', 1)
|
|
|
|
|
|
|
|
if username != 'homeassistant':
|
|
|
|
return False
|
|
|
|
|
|
|
|
return validate_password(request, password)
|