lasselukkari / aWOT

Arduino web server library.
MIT License
283 stars 41 forks source link

How to use Basic Auth? #3

Closed MaximKeegan closed 8 years ago

MaximKeegan commented 8 years ago

How to build basic auth with aWOT?

lasselukkari commented 8 years ago
#include <ESP8266WiFi.h>
#include <aWOT.h>

const char* ssid = "wifi";
const char* password = "pass";
WiFiServer server(80);
WebApp app;
char authBuffer[200];

void secretCmd(Request &req, Response &res) {
  char * authHeader = req.header("Authorization");

  if (strcmp(authHeader, "Basic c3VwZXI6YWRtaW4=") != 0) { // super:admin in base64
    res.set("WWW-Authenticate", "Basic realm=\"Secret Area\"");
    return res.unauthorized();
  }

  P(secretPage) =
    "<html>\n"
    "<head>\n"
    "<title>Secret Area</title>\n"
    "</head>\n"
    "<body>\n"
    "<h1>Secret area</h1>\n"
    "</body>\n"
    "</html>";

  res.success("text/html");
  res.printP(secretPage);
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  Serial.println(WiFi.localIP());

  server.begin();

  app.readHeader("Authorization", authBuffer, 200);
  app.get("/secret", &secretCmd);
}

void loop() {
  WiFiClient client = server.available();

  if (client.available()) {
    app.process(&client);
  }

  delay(20);
}
lasselukkari commented 8 years ago

Example: https://github.com/lasselukkari/aWOT/commit/76e0274d96a191bb3b7047bb591d8c3df06b7379