mirror of
https://github.com/esphome/esphome.git
synced 2025-08-04 09:27:47 +00:00
[web_server] Conditionally compile authentication code to save flash memory (#10022)
This commit is contained in:
parent
a75f73dbf0
commit
494a1a216c
@ -298,6 +298,7 @@ async def to_code(config):
|
|||||||
if config[CONF_ENABLE_PRIVATE_NETWORK_ACCESS]:
|
if config[CONF_ENABLE_PRIVATE_NETWORK_ACCESS]:
|
||||||
cg.add_define("USE_WEBSERVER_PRIVATE_NETWORK_ACCESS")
|
cg.add_define("USE_WEBSERVER_PRIVATE_NETWORK_ACCESS")
|
||||||
if CONF_AUTH in config:
|
if CONF_AUTH in config:
|
||||||
|
cg.add_define("USE_WEBSERVER_AUTH")
|
||||||
cg.add(paren.set_auth_username(config[CONF_AUTH][CONF_USERNAME]))
|
cg.add(paren.set_auth_username(config[CONF_AUTH][CONF_USERNAME]))
|
||||||
cg.add(paren.set_auth_password(config[CONF_AUTH][CONF_PASSWORD]))
|
cg.add(paren.set_auth_password(config[CONF_AUTH][CONF_PASSWORD]))
|
||||||
if CONF_CSS_INCLUDE in config:
|
if CONF_CSS_INCLUDE in config:
|
||||||
|
@ -14,9 +14,11 @@ WebServerBase *global_web_server_base = nullptr; // NOLINT(cppcoreguidelines-av
|
|||||||
void WebServerBase::add_handler(AsyncWebHandler *handler) {
|
void WebServerBase::add_handler(AsyncWebHandler *handler) {
|
||||||
// remove all handlers
|
// remove all handlers
|
||||||
|
|
||||||
|
#ifdef USE_WEBSERVER_AUTH
|
||||||
if (!credentials_.username.empty()) {
|
if (!credentials_.username.empty()) {
|
||||||
handler = new internal::AuthMiddlewareHandler(handler, &credentials_);
|
handler = new internal::AuthMiddlewareHandler(handler, &credentials_);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
this->handlers_.push_back(handler);
|
this->handlers_.push_back(handler);
|
||||||
if (this->server_ != nullptr) {
|
if (this->server_ != nullptr) {
|
||||||
this->server_->addHandler(handler);
|
this->server_->addHandler(handler);
|
||||||
|
@ -41,6 +41,7 @@ class MiddlewareHandler : public AsyncWebHandler {
|
|||||||
AsyncWebHandler *next_;
|
AsyncWebHandler *next_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifdef USE_WEBSERVER_AUTH
|
||||||
struct Credentials {
|
struct Credentials {
|
||||||
std::string username;
|
std::string username;
|
||||||
std::string password;
|
std::string password;
|
||||||
@ -79,6 +80,7 @@ class AuthMiddlewareHandler : public MiddlewareHandler {
|
|||||||
protected:
|
protected:
|
||||||
Credentials *credentials_;
|
Credentials *credentials_;
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
@ -108,8 +110,10 @@ class WebServerBase : public Component {
|
|||||||
std::shared_ptr<AsyncWebServer> get_server() const { return server_; }
|
std::shared_ptr<AsyncWebServer> get_server() const { return server_; }
|
||||||
float get_setup_priority() const override;
|
float get_setup_priority() const override;
|
||||||
|
|
||||||
|
#ifdef USE_WEBSERVER_AUTH
|
||||||
void set_auth_username(std::string auth_username) { credentials_.username = std::move(auth_username); }
|
void set_auth_username(std::string auth_username) { credentials_.username = std::move(auth_username); }
|
||||||
void set_auth_password(std::string auth_password) { credentials_.password = std::move(auth_password); }
|
void set_auth_password(std::string auth_password) { credentials_.password = std::move(auth_password); }
|
||||||
|
#endif
|
||||||
|
|
||||||
void add_handler(AsyncWebHandler *handler);
|
void add_handler(AsyncWebHandler *handler);
|
||||||
|
|
||||||
@ -121,7 +125,9 @@ class WebServerBase : public Component {
|
|||||||
uint16_t port_{80};
|
uint16_t port_{80};
|
||||||
std::shared_ptr<AsyncWebServer> server_{nullptr};
|
std::shared_ptr<AsyncWebServer> server_{nullptr};
|
||||||
std::vector<AsyncWebHandler *> handlers_;
|
std::vector<AsyncWebHandler *> handlers_;
|
||||||
|
#ifdef USE_WEBSERVER_AUTH
|
||||||
internal::Credentials credentials_;
|
internal::Credentials credentials_;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace web_server_base
|
} // namespace web_server_base
|
||||||
|
@ -223,6 +223,7 @@ void AsyncWebServerRequest::init_response_(AsyncWebServerResponse *rsp, int code
|
|||||||
this->rsp_ = rsp;
|
this->rsp_ = rsp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef USE_WEBSERVER_AUTH
|
||||||
bool AsyncWebServerRequest::authenticate(const char *username, const char *password) const {
|
bool AsyncWebServerRequest::authenticate(const char *username, const char *password) const {
|
||||||
if (username == nullptr || password == nullptr || *username == 0) {
|
if (username == nullptr || password == nullptr || *username == 0) {
|
||||||
return true;
|
return true;
|
||||||
@ -261,6 +262,7 @@ void AsyncWebServerRequest::requestAuthentication(const char *realm) const {
|
|||||||
httpd_resp_set_hdr(*this, "WWW-Authenticate", auth_val.c_str());
|
httpd_resp_set_hdr(*this, "WWW-Authenticate", auth_val.c_str());
|
||||||
httpd_resp_send_err(*this, HTTPD_401_UNAUTHORIZED, nullptr);
|
httpd_resp_send_err(*this, HTTPD_401_UNAUTHORIZED, nullptr);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
AsyncWebParameter *AsyncWebServerRequest::getParam(const std::string &name) {
|
AsyncWebParameter *AsyncWebServerRequest::getParam(const std::string &name) {
|
||||||
auto find = this->params_.find(name);
|
auto find = this->params_.find(name);
|
||||||
|
@ -115,9 +115,11 @@ class AsyncWebServerRequest {
|
|||||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||||
size_t contentLength() const { return this->req_->content_len; }
|
size_t contentLength() const { return this->req_->content_len; }
|
||||||
|
|
||||||
|
#ifdef USE_WEBSERVER_AUTH
|
||||||
bool authenticate(const char *username, const char *password) const;
|
bool authenticate(const char *username, const char *password) const;
|
||||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||||
void requestAuthentication(const char *realm = nullptr) const;
|
void requestAuthentication(const char *realm = nullptr) const;
|
||||||
|
#endif
|
||||||
|
|
||||||
void redirect(const std::string &url);
|
void redirect(const std::string &url);
|
||||||
|
|
||||||
|
@ -163,6 +163,7 @@
|
|||||||
#define USE_SPI
|
#define USE_SPI
|
||||||
#define USE_VOICE_ASSISTANT
|
#define USE_VOICE_ASSISTANT
|
||||||
#define USE_WEBSERVER
|
#define USE_WEBSERVER
|
||||||
|
#define USE_WEBSERVER_AUTH
|
||||||
#define USE_WEBSERVER_OTA
|
#define USE_WEBSERVER_OTA
|
||||||
#define USE_WEBSERVER_PORT 80 // NOLINT
|
#define USE_WEBSERVER_PORT 80 // NOLINT
|
||||||
#define USE_WEBSERVER_SORTING
|
#define USE_WEBSERVER_SORTING
|
||||||
@ -210,6 +211,7 @@
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
#define USE_WEBSERVER
|
#define USE_WEBSERVER
|
||||||
|
#define USE_WEBSERVER_AUTH
|
||||||
#define USE_WEBSERVER_PORT 80 // NOLINT
|
#define USE_WEBSERVER_PORT 80 // NOLINT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -226,6 +228,7 @@
|
|||||||
#define USE_SOCKET_IMPL_LWIP_SOCKETS
|
#define USE_SOCKET_IMPL_LWIP_SOCKETS
|
||||||
#define USE_SOCKET_SELECT_SUPPORT
|
#define USE_SOCKET_SELECT_SUPPORT
|
||||||
#define USE_WEBSERVER
|
#define USE_WEBSERVER
|
||||||
|
#define USE_WEBSERVER_AUTH
|
||||||
#define USE_WEBSERVER_PORT 80 // NOLINT
|
#define USE_WEBSERVER_PORT 80 // NOLINT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1 +1,6 @@
|
|||||||
<<: !include common_v2.yaml
|
<<: !include common_v2.yaml
|
||||||
|
|
||||||
|
web_server:
|
||||||
|
auth:
|
||||||
|
username: admin
|
||||||
|
password: password
|
||||||
|
Loading…
x
Reference in New Issue
Block a user