Cloud Updates (#11404)

* Verify stored keys on startup

* Handle Google Assistant messages

* Fix tests

* Don't verify expiration when getting claims

* Remove email based check

* Lint

* Lint

* Lint
This commit is contained in:
Paulus Schoutsen authored and Pascal Vizeli committed 2018-01-03 19:16:59 +01:00
1 parent 86e1d0f952
commit f314b6cb6c
7 files changed
+178 -108

No files matched your search

+122 -38
View File
@@ -5,13 +5,17 @@ import json
import logging
import os
import aiohttp
import async_timeout
import voluptuous as vol
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, CONF_REGION, CONF_MODE)
from homeassistant.helpers import entityfilter
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util import dt as dt_util
from homeassistant.components.alexa import smart_home
from homeassistant.components.alexa import smart_home as alexa_sh
from homeassistant.components.google_assistant import smart_home as ga_sh
from . import http_api, iot
from .const import CONFIG_DIR, DOMAIN, SERVERS
@@ -21,7 +25,8 @@ REQUIREMENTS = ['warrant==0.6.1']
_LOGGER = logging.getLogger(__name__)
CONF_ALEXA = 'alexa'
CONF_ALEXA_FILTER = 'filter'
CONF_GOOGLE_ASSISTANT = 'google_assistant'
CONF_FILTER = 'filter'
CONF_COGNITO_CLIENT_ID = 'cognito_client_id'
CONF_RELAYER = 'relayer'
CONF_USER_POOL_ID = 'user_pool_id'
@@ -30,9 +35,9 @@ MODE_DEV = 'development'
DEFAULT_MODE = 'production'
DEPENDENCIES = ['http']
ALEXA_SCHEMA = vol.Schema({
ASSISTANT_SCHEMA = vol.Schema({
vol.Optional(
CONF_ALEXA_FILTER,
CONF_FILTER,
default=lambda: entityfilter.generate_filter([], [], [], [])
): entityfilter.FILTER_SCHEMA,
})
@@ -46,7 +51,8 @@ CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_USER_POOL_ID): str,
vol.Optional(CONF_REGION): str,
vol.Optional(CONF_RELAYER): str,
vol.Optional(CONF_ALEXA): ALEXA_SCHEMA
vol.Optional(CONF_ALEXA): ASSISTANT_SCHEMA,
vol.Optional(CONF_GOOGLE_ASSISTANT): ASSISTANT_SCHEMA,
}),
}, extra=vol.ALLOW_EXTRA)
@@ -60,17 +66,19 @@ def async_setup(hass, config):
kwargs = {CONF_MODE: DEFAULT_MODE}
if CONF_ALEXA not in kwargs:
kwargs[CONF_ALEXA] = ALEXA_SCHEMA({})
kwargs[CONF_ALEXA] = ASSISTANT_SCHEMA({})
kwargs[CONF_ALEXA] = smart_home.Config(**kwargs[CONF_ALEXA])
if CONF_GOOGLE_ASSISTANT not in kwargs:
kwargs[CONF_GOOGLE_ASSISTANT] = ASSISTANT_SCHEMA({})
kwargs[CONF_ALEXA] = alexa_sh.Config(**kwargs[CONF_ALEXA])
kwargs['gass_should_expose'] = kwargs.pop(CONF_GOOGLE_ASSISTANT)
cloud = hass.data[DOMAIN] = Cloud(hass, **kwargs)
@asyncio.coroutine
def init_cloud(event):
"""Initialize connection."""
yield from cloud.initialize()
success = yield from cloud.initialize()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, init_cloud)
if not success:
return False
yield from http_api.async_setup(hass)
return True
@@ -79,12 +87,16 @@ def async_setup(hass, config):
class Cloud:
"""Store the configuration of the cloud connection."""
def __init__(self, hass, mode, cognito_client_id=None, user_pool_id=None,
region=None, relayer=None, alexa=None):
def __init__(self, hass, mode, alexa, gass_should_expose,
cognito_client_id=None, user_pool_id=None, region=None,
relayer=None):
"""Create an instance of Cloud."""
self.hass = hass
self.mode = mode
self.alexa_config = alexa
self._gass_should_expose = gass_should_expose
self._gass_config = None
self.jwt_keyset = None
self.id_token = None
self.access_token = None
self.refresh_token = None
@@ -104,11 +116,6 @@ class Cloud:
self.region = info['region']
self.relayer = info['relayer']
@property
def cognito_email_based(self):
"""Return if cognito is email based."""
return not self.user_pool_id.endswith('GmV')
@property
def is_logged_in(self):
"""Get if cloud is logged in."""
@@ -128,37 +135,37 @@ class Cloud:
@property
def claims(self):
"""Get the claims from the id token."""
from jose import jwt
return jwt.get_unverified_claims(self.id_token)
"""Return the claims from the id token."""
return self._decode_claims(self.id_token)
@property
def user_info_path(self):
"""Get path to the stored auth."""
return self.path('{}_auth.json'.format(self.mode))
@property
def gass_config(self):
"""Return the Google Assistant config."""
if self._gass_config is None:
self._gass_config = ga_sh.Config(
should_expose=self._gass_should_expose,
agent_user_id=self.claims['cognito:username']
)
return self._gass_config
@asyncio.coroutine
def initialize(self):
"""Initialize and load cloud info."""
def load_config():
"""Load the configuration."""
# Ensure config dir exists
path = self.hass.config.path(CONFIG_DIR)
if not os.path.isdir(path):
os.mkdir(path)
jwt_success = yield from self._fetch_jwt_keyset()
user_info = self.user_info_path
if os.path.isfile(user_info):
with open(user_info, 'rt') as file:
info = json.loads(file.read())
self.id_token = info['id_token']
self.access_token = info['access_token']
self.refresh_token = info['refresh_token']
if not jwt_success:
return False
yield from self.hass.async_add_job(load_config)
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START,
self._start_cloud)
if self.id_token is not None:
yield from self.iot.connect()
return True
def path(self, *parts):
"""Get config path inside cloud dir.
@@ -175,6 +182,7 @@ class Cloud:
self.id_token = None
self.access_token = None
self.refresh_token = None
self._gass_config = None
yield from self.hass.async_add_job(
lambda: os.remove(self.user_info_path))
@@ -187,3 +195,79 @@ class Cloud:
'access_token': self.access_token,
'refresh_token': self.refresh_token,
}, indent=4))
def _start_cloud(self, event):
"""Start the cloud component."""
# Ensure config dir exists
path = self.hass.config.path(CONFIG_DIR)
if not os.path.isdir(path):
os.mkdir(path)
user_info = self.user_info_path
if not os.path.isfile(user_info):
return
with open(user_info, 'rt') as file:
info = json.loads(file.read())
# Validate tokens
try:
for token in 'id_token', 'access_token':
self._decode_claims(info[token])
except ValueError as err: # Raised when token is invalid
_LOGGER.warning('Found invalid token %s: %s', token, err)
return
self.id_token = info['id_token']
self.access_token = info['access_token']
self.refresh_token = info['refresh_token']
self.hass.add_job(self.iot.connect())
@asyncio.coroutine
def _fetch_jwt_keyset(self):
"""Fetch the JWT keyset for the Cognito instance."""
session = async_get_clientsession(self.hass)
url = ("https://cognito-idp.us-east-1.amazonaws.com/"
"{}/.well-known/jwks.json".format(self.user_pool_id))
try:
with async_timeout.timeout(10, loop=self.hass.loop):
req = yield from session.get(url)
self.jwt_keyset = yield from req.json()
return True
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
_LOGGER.error("Error fetching Cognito keyset: %s", err)
return False
def _decode_claims(self, token):
"""Decode the claims in a token."""
from jose import jwt, exceptions as jose_exceptions
try:
header = jwt.get_unverified_header(token)
except jose_exceptions.JWTError as err:
raise ValueError(str(err)) from None
kid = header.get("kid")
if kid is None:
raise ValueError('No kid in header')
# Locate the key for this kid
key = None
for key_dict in self.jwt_keyset["keys"]:
if key_dict["kid"] == kid:
key = key_dict
break
if not key:
raise ValueError(
"Unable to locate kid ({}) in keyset".format(kid))
try:
return jwt.decode(
token, key, audience=self.cognito_client_id, options={
'verify_exp': False,
})
except jose_exceptions.JWTError as err:
raise ValueError(str(err)) from None
+4 -23
View File
@@ -1,5 +1,4 @@
"""Package to communicate with the authentication API."""
import hashlib
import logging
@@ -58,11 +57,6 @@ def _map_aws_exception(err):
return ex(err.response['Error']['Message'])
def _generate_username(email):
"""Generate a username from an email address."""
return hashlib.sha512(email.encode('utf-8')).hexdigest()
def register(cloud, email, password):
"""Register a new account."""
from botocore.exceptions import ClientError
@@ -72,10 +66,7 @@ def register(cloud, email, password):
# https://github.com/capless/warrant/pull/82
cognito.add_base_attributes()
try:
if cloud.cognito_email_based:
cognito.register(email, password)
else:
cognito.register(_generate_username(email), password)
cognito.register(email, password)
except ClientError as err:
raise _map_aws_exception(err)
@@ -86,11 +77,7 @@ def confirm_register(cloud, confirmation_code, email):
cognito = _cognito(cloud)
try:
if cloud.cognito_email_based:
cognito.confirm_sign_up(confirmation_code, email)
else:
cognito.confirm_sign_up(confirmation_code,
_generate_username(email))
cognito.confirm_sign_up(confirmation_code, email)
except ClientError as err:
raise _map_aws_exception(err)
@@ -114,10 +101,7 @@ def forgot_password(cloud, email):
"""Initiate forgotten password flow."""
from botocore.exceptions import ClientError
if cloud.cognito_email_based:
cognito = _cognito(cloud, username=email)
else:
cognito = _cognito(cloud, username=_generate_username(email))
cognito = _cognito(cloud, username=email)
try:
cognito.initiate_forgot_password()
@@ -129,10 +113,7 @@ def confirm_forgot_password(cloud, confirmation_code, email, new_password):
"""Confirm forgotten password code and change password."""
from botocore.exceptions import ClientError
if cloud.cognito_email_based:
cognito = _cognito(cloud, username=email)
else:
cognito = _cognito(cloud, username=_generate_username(email))
cognito = _cognito(cloud, username=email)
try:
cognito.confirm_forgot_password(confirmation_code, new_password)
+1 -1
View File
@@ -252,6 +252,6 @@ def _account_data(cloud):
return {
'email': claims['email'],
'sub_exp': claims.get('custom:sub-exp'),
'sub_exp': claims['custom:sub-exp'],
'cloud': cloud.iot.state,
}
+14 -4
View File
@@ -5,7 +5,8 @@ import logging
from aiohttp import hdrs, client_exceptions, WSMsgType
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.components.alexa import smart_home
from homeassistant.components.alexa import smart_home as alexa
from homeassistant.components.google_assistant import smart_home as ga
from homeassistant.util.decorator import Registry
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from . import auth_api
@@ -204,9 +205,18 @@ def async_handle_message(hass, cloud, handler_name, payload):
@asyncio.coroutine
def async_handle_alexa(hass, cloud, payload):
"""Handle an incoming IoT message for Alexa."""
return (yield from smart_home.async_handle_message(hass,
cloud.alexa_config,
payload))
result = yield from alexa.async_handle_message(hass, cloud.alexa_config,
payload)
return result
@HANDLERS.register('google_assistant')
@asyncio.coroutine
def async_handle_google_assistant(hass, cloud, payload):
"""Handle an incoming IoT message for Google Assistant."""
result = yield from ga.async_handle_message(hass, cloud.gass_config,
payload)
return result
@HANDLERS.register('cloud')