auth in config mode

This commit is contained in:
Wolfgang Hottgenroth 2018-02-02 16:21:52 +01:00
parent 616099f699
commit 2e34cfe5b5
6 changed files with 95 additions and 9 deletions

View File

@ -7,6 +7,8 @@ from Cheetah.Template import Template
configItems = [
{"label":"_", "key":"magic", "type":"I", "default": ""},
{"label":"Config Username", "key":"confUser", "type":"C", "length":32, "default":"admin"},
{"label":"Config Password", "key":"confPasswd", "type":"C", "length":32, "default":"geheim123"},
{"label":"Wifi SSID", "key":"wifiSsid", "type":"C", "length":32, "default":"test"},
{"label":"Wifi Key", "key":"wifiKey", "type":"C", "length":64, "default":"geheim"},
{"label":"MQTT Broker", "key":"mqttBroker", "type":"C", "length":64, "default":"broker.hottis.de"},
@ -25,8 +27,17 @@ configItems = [
magic = 0xC0DE0002
appName = "ESP8266 based TouchSwitch"
confWifiSsid = "espconfig"
h_file = Template(file="configuration_h.tmpl", searchList=[{"magic":magic, "configItems":configItems}])
params = {
"magic":magic,
"appName":appName,
"confWifiSsid":confWifiSsid,
"configItems":configItems
}
h_file = Template(file="configuration_h.tmpl", searchList=[params])
open('configuration.h','w').write(str(h_file))
c_file = Template(file="configuration_c.tmpl", searchList=[{"magic":magic, "configItems":configItems}])
c_file = Template(file="configuration_c.tmpl", searchList=[params])
open('configuration.cpp','w').write(str(c_file))

View File

@ -12,11 +12,22 @@
tConfigBlock configBlock;
const uint32_t MAGIC = $magic;
const char* CONFIG_SSID = "$confWifiSsid";
extern ESP8266WebServer webServer;
bool configSaved = false;
static bool checkAuthentication() {
return webServer.authenticate(configBlock.confUser, configBlock.confPasswd);
}
void configServeIndex() {
if (! checkAuthentication()) {
return webServer.requestAuthentication();
}
bool configValid = (configBlock.magic == MAGIC);
if (! configValid) {
@ -35,10 +46,10 @@ void configServeIndex() {
"<!doctype html"
"<html lang=\"en\">"
" <head>"
" <title>ESP8266 Thermometer Configuration Page</title>"
" <title>$appName</title>"
" </head>"
" <body>"
" <h1>ESP8266 Configuration Page</h1>";
" <h1>$appName - ESP8266 Configuration Page</h1>";
if (configSaved) {
configSaved = false;
@ -92,6 +103,10 @@ void configServeIndex() {
}
void configServeGetConfiguration() {
if (! checkAuthentication()) {
return webServer.requestAuthentication();
}
String arg;
#for $configItem in $configItems

View File

@ -10,6 +10,7 @@ typedef struct {
extern const uint32_t MAGIC;
extern tConfigBlock configBlock;
extern const char* CONFIG_SSID;
void configServeIndex();
void configServeGetConfiguration();

View File

@ -10,14 +10,27 @@
tConfigBlock configBlock;
const uint32_t MAGIC = 3235774466;
const char* CONFIG_SSID = "espconfig";
extern ESP8266WebServer webServer;
bool configSaved = false;
static bool checkAuthentication() {
return webServer.authenticate(configBlock.confUser, configBlock.confPasswd);
}
void configServeIndex() {
if (! checkAuthentication()) {
return webServer.requestAuthentication();
}
bool configValid = (configBlock.magic == MAGIC);
if (! configValid) {
strcpy(configBlock.confUser, "admin");
strcpy(configBlock.confPasswd, "geheim123");
strcpy(configBlock.wifiSsid, "test");
strcpy(configBlock.wifiKey, "geheim");
strcpy(configBlock.mqttBroker, "broker.hottis.de");
@ -38,10 +51,10 @@ void configServeIndex() {
"<!doctype html"
"<html lang=\"en\">"
" <head>"
" <title>ESP8266 Thermometer Configuration Page</title>"
" <title>ESP8266 based TouchSwitch</title>"
" </head>"
" <body>"
" <h1>ESP8266 Configuration Page</h1>";
" <h1>ESP8266 based TouchSwitch - ESP8266 Configuration Page</h1>";
if (configSaved) {
configSaved = false;
@ -53,6 +66,36 @@ void configServeIndex() {
" <table>"
" <tr>"
" <td>"
" <label for\"confUser\">Config Username</label>"
" </td><td>"
" <input type=\"text\" name=\"confUser\" id=\"confUser\" ";
buffer += " size=\"32\" ";
buffer += " value=\"";
buffer += configBlock.confUser;
buffer += "\"";
buffer +=
" />"
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"confPasswd\">Config Password</label>"
" </td><td>"
" <input type=\"text\" name=\"confPasswd\" id=\"confPasswd\" ";
buffer += " size=\"32\" ";
buffer += " value=\"";
buffer += configBlock.confPasswd;
buffer += "\"";
buffer +=
" />"
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"wifiSsid\">Wifi SSID</label>"
" </td><td>"
" <input type=\"text\" name=\"wifiSsid\" id=\"wifiSsid\" ";
@ -275,8 +318,16 @@ void configServeIndex() {
}
void configServeGetConfiguration() {
if (! checkAuthentication()) {
return webServer.requestAuthentication();
}
String arg;
arg = webServer.arg("confUser");
strcpy(configBlock.confUser, arg.c_str());
arg = webServer.arg("confPasswd");
strcpy(configBlock.confPasswd, arg.c_str());
arg = webServer.arg("wifiSsid");
strcpy(configBlock.wifiSsid, arg.c_str());
arg = webServer.arg("wifiKey");
@ -329,6 +380,14 @@ void showConfiguration() {
Serial.print(configBlock.magic);
Serial.println(">");
Serial.print("confUser = <");
Serial.print(configBlock.confUser);
Serial.println(">");
Serial.print("confPasswd = <");
Serial.print(configBlock.confPasswd);
Serial.println(">");
Serial.print("wifiSsid = <");
Serial.print(configBlock.wifiSsid);
Serial.println(">");

View File

@ -1,5 +1,7 @@
typedef struct {
uint32_t magic;
char confUser[32];
char confPasswd[32];
char wifiSsid[32];
char wifiKey[64];
char mqttBroker[64];
@ -18,6 +20,7 @@ typedef struct {
extern const uint32_t MAGIC;
extern tConfigBlock configBlock;
extern const char* CONFIG_SSID;
void configServeIndex();
void configServeGetConfiguration();

View File

@ -17,13 +17,10 @@
#include "configuration.h"
const char* CONFIG_SSID = "espTherm";
ESP8266WebServer webServer(80);
void configServeNotFound() {
webServer.send(404, "text/plain", "page not found");
#ifdef DEBUG