diff --git a/homeassistant/components/http.py b/homeassistant/components/http.py index 7d8fd8620b4c8be9247a3008c0c58186b30e13e2..12647be68f7d36959201517831797ce3dffde9f1 100644 --- a/homeassistant/components/http.py +++ b/homeassistant/components/http.py @@ -5,6 +5,7 @@ For more details about the RESTful API, please refer to the documentation at https://home-assistant.io/developers/api/ """ import gzip +import hmac import json import logging import ssl @@ -200,12 +201,22 @@ class RequestHandler(SimpleHTTPRequestHandler): "Error parsing JSON", HTTP_UNPROCESSABLE_ENTITY) return - self.authenticated = (self.server.api_password is None or - self.headers.get(HTTP_HEADER_HA_AUTH) == - self.server.api_password or - data.get(DATA_API_PASSWORD) == - self.server.api_password or - self.verify_session()) + if self.verify_session(): + # The user has a valid session already + self.authenticated = True + elif self.server.api_password is None: + # No password is set, so everyone is authenticated + self.authenticated = True + elif hmac.compare_digest(self.headers.get(HTTP_HEADER_HA_AUTH, ''), + self.server.api_password): + # A valid auth header has been set + self.authenticated = True + elif hmac.compare_digest(data.get(DATA_API_PASSWORD, ''), + self.server.api_password): + # A valid password has been specified + self.authenticated = True + else: + self.authenticated = False if '_METHOD' in data: method = data.pop('_METHOD')