1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-25 05:03:52 +01:00

Basic Auth for web_server component (#674)

* Basic auth

* Test

* Linter fix

* Make username/password strict strings

Reason: passwords only consisting of digits (012345) will be silently converted (to "12345")


Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
Nikolay Vasilchuk
2019-10-13 15:27:44 +03:00
committed by Otto Winter
parent 1a763ae974
commit b2388b6fe7
5 changed files with 30 additions and 1 deletions

View File

@@ -29,6 +29,11 @@ struct UrlMatch {
class WebServer : public Controller, public Component, public AsyncWebHandler {
public:
WebServer(web_server_base::WebServerBase *base) : base_(base) {}
void set_username(const char *username) { username_ = username; }
void set_password(const char *password) { password_ = password; }
/** Set the URL to the CSS <link> that's sent to each client. Defaults to
* https://esphome.io/_static/webserver-v1.min.css
*
@@ -56,6 +61,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
/// Handle an index request under '/'.
void handle_index_request(AsyncWebServerRequest *request);
bool using_auth() { return username_ != nullptr && password_ != nullptr; }
#ifdef USE_SENSOR
void on_sensor_update(sensor::Sensor *obj, float state) override;
/// Handle a sensor request under '/sensor/<id>'.
@@ -125,6 +132,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
protected:
web_server_base::WebServerBase *base_;
AsyncEventSource events_{"/events"};
const char *username_{nullptr};
const char *password_{nullptr};
const char *css_url_{nullptr};
const char *js_url_{nullptr};
};