Layer: L7 Application ยท Build flags: PC_ENABLE_OIDC
What this example teaches
OpenID Connect lets an external identity provider (Google, Auth0, Azure AD, ...) vouch for a user with a signed ID token. A client presents Authorization: Bearer <id_token> and the device verifies the RS256 signature against the issuer's JWKS public key and checks iss / aud / exp, then serves the request as the authenticated subject. GET /whoami returns the subject on a valid token, 401 otherwise.
Verify the token, then trust its claims:
pc_oidc_claims claims;
int rc = pc_oidc_verify(token, strlen(token), JWKS, ISSUER, AUDIENCE, now, &claims);
if (rc != pc_oidc_result::PC_OIDC_OK) { }
The full Authorization header lives in req->authorization. ID tokens exceed the normal header-value cap, so they get their own field; the handler steps past the Bearer scheme itself:
const char *hdr = req->authorization;
if (!hdr || strncasecmp(hdr, "Bearer ", 7) != 0) { }
const char *token = hdr + 7;
Production notes. Fetch the JWKS from the issuer's discovery document (<issuer>/.well-known/openid-configuration -> jwks_uri) over HTTPS off the request hot path and cache it (re-fetch on an unknown kid); the demo embeds it. Use a real NTP clock for now; the demo hardcodes a time so the bundled test token validates.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_OIDC=1" \
--lib="." examples/L7-Application/OidcAuth/OidcAuth.ino
curl -H "Authorization: Bearer $ID_TOKEN" http://<ip>/whoami
# 200 {"sub":"...","email":"..."} on a valid token, else 401 {"error":<code>}
Annotated source
The complete sketch (OidcAuth.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_OIDC 1
#include <string.h>
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static const char *JWKS = "{\"keys\":[{\"kty\":\"RSA\",\"kid\":\"your-kid\",\"alg\":\"RS256\","
"\"n\":\"<base64url-modulus>\",\"e\":\"AQAB\"}]}";
static const char *ISSUER = "https://issuer.example";
static const char *AUDIENCE = "your-client-id";
void setup()
{
delay(250);
Serial.print("IP: ");
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
const char *hdr = req->authorization;
if (!hdr || strncasecmp(hdr, "Bearer ", 7) != 0)
{
server.
send(
id, 401,
"application/json",
"{\"error\":\"missing token\"}");
return;
}
const char *token = hdr + 7;
uint32_t now = 1700000100;
pc_oidc_claims claims;
int rc = pc_oidc_verify(token, strlen(token), JWKS, ISSUER, AUDIENCE, now, &claims);
if (rc != pc_oidc_result::PC_OIDC_OK)
{
char b[40];
snprintf(b, sizeof(b), "{\"error\":%d}", rc);
server.
send(
id, 401,
"application/json", b);
return;
}
char b[192];
snprintf(b, sizeof(b), "{\"sub\":\"%s\",\"email\":\"%s\"}", claims.sub, claims.email);
server.
send(
id, 200,
"application/json", b);
});
}
void loop()
{
}
Single-port HTTP server with deterministic, zero-allocation execution.
void add_response_header(uint8_t slot_id, const char *name, const char *value)
Queue a custom response header for the next send on this slot.
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.
OpenID Connect ID-token verification, RS256 (PC_ENABLE_OIDC).
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.
@ HTTP_GET
Safe, idempotent read.
Fully-parsed HTTP/1.1 request.