Layer: L6 Presentation ยท Build flags: PC_ENABLE_AUTH (on by default)
What this example teaches
This is BasicAuth with one change: pass digest=true to the authenticated on() overload and the route is protected with HTTP Digest (RFC 7616, SHA-256, qop="auth") instead of Basic. With Digest the password never crosses the wire - only a salted hash of it does - and unauthenticated requests get a 401 plus a WWW-Authenticate: Digest challenge automatically.
One extra argument. The signature is the same as Basic auth, with a trailing true:
@ HTTP_GET
Safe, idempotent read.
The handler is reached only after the client computes a correct digest response from the server's nonce - so the server never sees, stores, or transmits the password in the clear.
Testing caveat (Windows curl). curl --digest on Windows routes Digest through SSPI/SChannel, which rejects this SHA-256 / qop="auth" challenge (SEC_E_QOP_NOT_SUPPORTED) and never sends credentials. That is a Windows-curl limitation, not a server bug - the challenge is standard RFC 7616 and works with a browser, wget, or Linux/macOS curl.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--lib="." examples/L6-Presentation/DigestAuth/DigestAuth.ino
curl --digest -u admin:s3cret http://<ip>/secret # Linux/macOS curl, wget, or a browser
Annotated source
The complete sketch (DigestAuth.ino), reproduced verbatim with added explanatory comments:
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
void handle_secret(uint8_t slot_id,
HttpReq *req)
{
(void)req;
server.
send(slot_id, 200,
"text/plain",
"authenticated: top secret payload");
}
void setup()
{
Serial.begin(115200);
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
int32_t result = server.
begin(80);
if (result < 0)
{
Serial.printf("begin() failed (error %d)\n", result);
return;
}
Serial.println("Server started on port 80");
}
void loop()
{
}
Single-port HTTP server with deterministic, zero-allocation execution.
void send(uint8_t slot_id, int code, const char *content_type, const char *payload)
Send an HTTP response with a body and close the connection.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void on(const char *path, HttpMethod method, Handler callback)
Register a route handler.
void handle()
Drive the server - call every Arduino loop() iteration.
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
Fully-parsed HTTP/1.1 request.