ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
DigestAuth - HTTP Digest authentication (password never on the wire)

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:

// on(path, method, handler, realm, user, pass, digest=true)
server.on("/secret", HttpMethod::HTTP_GET, handle_secret, "demo", "admin", "s3cret", 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:

// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
PC server;
// GET /secret - only reached after successful Digest authentication.
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);
init_wifi_physical(SSID, PASSWORD);
Serial.print("Connecting to WiFi");
while (!wifi_ready())
{
delay(250);
Serial.print('.');
}
uint32_t ip = pc_net_egress_ip(); // library egress IP (network byte order), no Arduino WiFi
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
// The trailing `true` selects Digest instead of Basic for this route.
// on(path, method, handler, realm, user, pass, digest=true)
server.on("/secret", HttpMethod::HTTP_GET, handle_secret, "demo", "admin", "s3cret", true);
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()
{
server.handle();
}
Single-port HTTP server with deterministic, zero-allocation execution.
Definition protocore.h:348
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.
Definition physical.cpp:41
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
Definition physical.cpp:77
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Definition physical.cpp:45
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
Fully-parsed HTTP/1.1 request.