From 0b69f7231585cd8d110efeb08a992647b1bbb6a3 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Wed, 11 May 2022 01:38:05 +0200 Subject: [PATCH] Enable api transport encryption for new projects (#3142) * Enable api transport encryption for new projects * Format --- esphome/dashboard/dashboard.py | 3 +++ esphome/wizard.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/esphome/dashboard/dashboard.py b/esphome/dashboard/dashboard.py index b78d22cf7c..1fadac968d 100644 --- a/esphome/dashboard/dashboard.py +++ b/esphome/dashboard/dashboard.py @@ -1,5 +1,6 @@ # pylint: disable=wrong-import-position +import base64 import codecs import collections import functools @@ -378,6 +379,8 @@ class WizardRequestHandler(BaseHandler): if k in ("name", "platform", "board", "ssid", "psk", "password") } kwargs["ota_password"] = secrets.token_hex(16) + noise_psk = secrets.token_bytes(32) + kwargs["api_encryption_key"] = base64.b64encode(noise_psk).decode() destination = settings.rel_path(f"{kwargs['name']}.yaml") wizard.wizard_write(path=destination, **kwargs) self.set_status(200) diff --git a/esphome/wizard.py b/esphome/wizard.py index 34930ff66f..469219300b 100644 --- a/esphome/wizard.py +++ b/esphome/wizard.py @@ -111,6 +111,8 @@ def wizard_file(**kwargs): # Configure API if "password" in kwargs: config += f" password: \"{kwargs['password']}\"\n" + if "api_encryption_key" in kwargs: + config += f" encryption:\n key: \"{kwargs['api_encryption_key']}\"\n" # Configure OTA config += "\nota:\n"