mirror of
https://github.com/scottchiefbaker/ESP-WebOTA.git
synced 2024-08-30 18:12:33 +00:00
Add an option to do digest auth
This commit is contained in:
parent
aefb6c1a91
commit
c965942b2d
@ -25,6 +25,12 @@ ESP8266WebServer OTAServer(9999);
|
|||||||
|
|
||||||
WebOTA webota;
|
WebOTA webota;
|
||||||
|
|
||||||
|
char WWW_USER[16] = "";
|
||||||
|
char WWW_PASSWORD[16] = "";
|
||||||
|
const char* WWW_REALM = "WebOTA";
|
||||||
|
// the Content of the HTML response in case of Unautherized Access Default:empty
|
||||||
|
String authFailResponse = "Auth Fail";
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
int WebOTA::init(const unsigned int port, const char *path) {
|
int WebOTA::init(const unsigned int port, const char *path) {
|
||||||
@ -63,6 +69,13 @@ int WebOTA::init() {
|
|||||||
return WebOTA::init(8080, "/webota");
|
return WebOTA::init(8080, "/webota");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebOTA::useAuth(const char* user, const char* password) {
|
||||||
|
strncpy(WWW_USER, user, sizeof(WWW_USER) - 1);
|
||||||
|
strncpy(WWW_PASSWORD, password, sizeof(WWW_PASSWORD) - 1);
|
||||||
|
|
||||||
|
//Serial.printf("Set auth '%s' / '%s' %d\n", user, password, len);
|
||||||
|
}
|
||||||
|
|
||||||
int WebOTA::handle() {
|
int WebOTA::handle() {
|
||||||
// If we haven't run the init yet run it
|
// If we haven't run the init yet run it
|
||||||
if (!this->init_has_run) {
|
if (!this->init_has_run) {
|
||||||
@ -242,6 +255,26 @@ String get_mac_address() {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int8_t check_auth(WebServer *server) {
|
||||||
|
// If we have a user and a password we check digest auth
|
||||||
|
bool use_auth = (strlen(WWW_USER) && strlen(WWW_PASSWORD));
|
||||||
|
if (!use_auth) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!server->authenticate(WWW_USER, WWW_PASSWORD)) {
|
||||||
|
//Basic Auth Method
|
||||||
|
//return server.requestAuthentication(BASIC_AUTH, WWW_REALM, authFailResponse);
|
||||||
|
|
||||||
|
// Digest Auth
|
||||||
|
server->requestAuthentication(DIGEST_AUTH, WWW_REALM, authFailResponse);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
int WebOTA::add_http_routes(ESP8266WebServer *server, const char *path) {
|
int WebOTA::add_http_routes(ESP8266WebServer *server, const char *path) {
|
||||||
#endif
|
#endif
|
||||||
@ -250,11 +283,15 @@ int WebOTA::add_http_routes(WebServer *server, const char *path) {
|
|||||||
#endif
|
#endif
|
||||||
// Index page
|
// Index page
|
||||||
server->on("/", HTTP_GET, [server]() {
|
server->on("/", HTTP_GET, [server]() {
|
||||||
|
check_auth(server);
|
||||||
|
|
||||||
server->send(200, "text/html", F("<h1>WebOTA</h1>"));
|
server->send(200, "text/html", F("<h1>WebOTA</h1>"));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Upload firmware page
|
// Upload firmware page
|
||||||
server->on(path, HTTP_GET, [server,this]() {
|
server->on(path, HTTP_GET, [server,this]() {
|
||||||
|
check_auth(server);
|
||||||
|
|
||||||
String html = "";
|
String html = "";
|
||||||
if (this->custom_html != NULL) {
|
if (this->custom_html != NULL) {
|
||||||
html = this->custom_html;
|
html = this->custom_html;
|
||||||
@ -284,6 +321,8 @@ int WebOTA::add_http_routes(WebServer *server, const char *path) {
|
|||||||
|
|
||||||
// Handling uploading firmware file
|
// Handling uploading firmware file
|
||||||
server->on(path, HTTP_POST, [server,this]() {
|
server->on(path, HTTP_POST, [server,this]() {
|
||||||
|
check_auth(server);
|
||||||
|
|
||||||
server->send(200, "text/plain", (Update.hasError()) ? "Update: fail\n" : "Update: OK!\n");
|
server->send(200, "text/plain", (Update.hasError()) ? "Update: fail\n" : "Update: OK!\n");
|
||||||
delay(500);
|
delay(500);
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
|
@ -28,6 +28,7 @@ class WebOTA {
|
|||||||
int handle();
|
int handle();
|
||||||
|
|
||||||
void set_custom_html(char const * const html);
|
void set_custom_html(char const * const html);
|
||||||
|
void useAuth(const char* user, const char* password);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool init_has_run;
|
bool init_has_run;
|
||||||
|
Loading…
Reference in New Issue
Block a user