-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathESP8266_WebServerBasicAuth.ino
72 lines (65 loc) · 1.94 KB
/
ESP8266_WebServerBasicAuth.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <ESP8266WiFi.h>
const char *ssid = "MY_SSID"; // TODO
const char *password = "MY_PASSWORD"; // TODO
// https://ddg.co/?q=base64+encode+MY_USER%3AMY_PASSWORD
const char *storedCreds = "MY_BASE64_ENCODED_CREDENTIALS"; // TODO
const int maxCredsLen = 64;
const int port = 80;
WiFiServer server(port);
void setup() {
Serial.begin(115200);
Serial.print("\nConnecting to network ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
Serial.print("Connected to network, local IP = ");
Serial.println(WiFi.localIP());
server.begin();
}
void readStringToEndOfLine(WiFiClient client, char *s, int maxLen) {
memset(s, '\0', maxLen); // fill string with '\0'
int i = 0;
int ch = client.read();
while (i < (maxLen - 1) && ch != -1 && ch != '\r' && ch != '\n') {
s[i] = ch;
i++;
ch = client.read();
}
}
void send200Response(WiFiClient client) {
client.print("HTTP/1.1 200 OK\r\n");
client.print("Content-Length: 0\r\n");
client.print("Connection: close\r\n");
client.print("\r\n");
}
void send401Response(WiFiClient client) {
client.print("HTTP/1.1 401 Unauthorized\r\n");
client.print("WWW-Authenticate: Basic\r\n");
client.print("Content-Length: 0\r\n");
client.print("Connection: close\r\n");
client.print("\r\n");
}
void loop() {
WiFiClient client = server.available();
if (client && client.connected()) {
Serial.println(client.remoteIP());
if (client.find("Authorization: Basic ")) {
char creds[maxCredsLen];
readStringToEndOfLine(client, creds, maxCredsLen);
Serial.println(creds);
client.find("\r\n\r\n"); // Ignore further request headers
if (strcmp(storedCreds, creds) == 0) {
send200Response(client);
} else {
send401Response(client);
}
} else {
send401Response(client);
}
delay(1); // Give Web browser time to receive data
client.stop();
}
}