arduino-esp32/libraries/WebServer/examples/HttpAuthCallback/HttpAuthCallback.ino
Dirk-Willem van Gulik e581717bf3
Web server simplifications and handers (#7429)
* First stab ad simplyfing webserver auth and adding a handler.

* Tweaks after testing against docs and latest Library tree

* Add documentatin for callback handler

* Bodge to allow things to compile without the dependencies

* Remove dependency on sodium to make it compile with 4.4

* Fix hex conversion

* Move some common HEX functions into a static HEX class, remove those from MD5 and add some examples. This allows for the cleanup of various to/from HEX routines elsewhere.

* Remove some duplicated code

* Add simplfiied HEXBuilder under MD5Bulder to CMakefile.

* Update for 3.0.0 and QoL improvements

* Remove examples that depend on external libraries

* Skip H2 testing

* Formatting improvements

* Move builders examples to Utilities folder

* Fix indentation

* Add HashBuilder abstract class

* Add SHA1Builder

* Fix comment

* Fix whitespace

* Fix crashes and improve log messages

* Fix indentation for webserver

---------

Co-authored-by: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com>
2024-01-16 15:49:46 +02:00

64 lines
1.5 KiB
C++

#include <WiFi.h>
#include <ESPmDNS.h>
#include <ArduinoOTA.h>
#include <WebServer.h>
const char* ssid = "........";
const char* password = "........";
WebServer server(80);
typedef struct credentials_t {
String username;
String password;
} credentials_t;
credentials_t passwdfile[] = {
{ "admin", "esp32" },
{ "fred", "41234123" },
{ "charlie", "sdfsd" },
{ "alice", "vambdnkuhj" },
{ "bob", "svcdbjhws12" },
};
const size_t N_CREDENTIALS = sizeof(passwdfile) / sizeof(credentials_t);
String * credentialsHandler(HTTPAuthMethod mode, String username, String params[])
{
for (int i = 0; i < N_CREDENTIALS; i++) {
if (username == passwdfile[i].username)
return new String(passwdfile[i].password);
}
return NULL;
}
void setup() {
Serial.begin(115200);
while (!Serial) { delay(10); }
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Connect Failed! Rebooting...");
delay(1000);
ESP.restart();
}
ArduinoOTA.begin();
server.on("/", []() {
if (!server.authenticate(&credentialsHandler)) {
server.requestAuthentication();
return;
}
server.send(200, "text/plain", "Login OK");
});
server.begin();
Serial.print("Open http://");
Serial.print(WiFi.localIP());
Serial.println("/ in your browser to see it working");
}
void loop() {
ArduinoOTA.handle();
server.handleClient();
delay(2);//allow the cpu to switch to other tasks
}