Skip to content

Commit bda06d6

Browse files
author
Me No Dev
committed
Add HTTP Basic Auth to WebServer and libb64 (base64) to core
1 parent 4c81c2b commit bda06d6

File tree

9 files changed

+387
-4
lines changed

9 files changed

+387
-4
lines changed

cores/esp8266/libb64/AUTHORS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
libb64: Base64 Encoding/Decoding Routines
2+
======================================
3+
4+
Authors:
5+
-------
6+
7+
Chris Venter [email protected] http://rocketpod.blogspot.com

cores/esp8266/libb64/LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Copyright-Only Dedication (based on United States law)
2+
or Public Domain Certification
3+
4+
The person or persons who have associated work with this document (the
5+
"Dedicator" or "Certifier") hereby either (a) certifies that, to the best of
6+
his knowledge, the work of authorship identified is in the public domain of the
7+
country from which the work is published, or (b) hereby dedicates whatever
8+
copyright the dedicators holds in the work of authorship identified below (the
9+
"Work") to the public domain. A certifier, moreover, dedicates any copyright
10+
interest he may have in the associated work, and for these purposes, is
11+
described as a "dedicator" below.
12+
13+
A certifier has taken reasonable steps to verify the copyright status of this
14+
work. Certifier recognizes that his good faith efforts may not shield him from
15+
liability if in fact the work certified is not in the public domain.
16+
17+
Dedicator makes this dedication for the benefit of the public at large and to
18+
the detriment of the Dedicator's heirs and successors. Dedicator intends this
19+
dedication to be an overt act of relinquishment in perpetuity of all present
20+
and future rights under copyright law, whether vested or contingent, in the
21+
Work. Dedicator understands that such relinquishment of all rights includes
22+
the relinquishment of all rights to enforce (by lawsuit or otherwise) those
23+
copyrights in the Work.
24+
25+
Dedicator recognizes that, once placed in the public domain, the Work may be
26+
freely reproduced, distributed, transmitted, used, modified, built upon, or
27+
otherwise exploited by anyone for any purpose, commercial or non-commercial,
28+
and in any way, including by methods that have not yet been invented or
29+
conceived.

cores/esp8266/libb64/cdecode.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
cdecoder.c - c source to a base64 decoding algorithm implementation
3+
4+
This is part of the libb64 project, and has been placed in the public domain.
5+
For details, see http://sourceforge.net/projects/libb64
6+
*/
7+
8+
#include "cdecode.h"
9+
10+
int base64_decode_value(char value_in){
11+
static const char decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,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,-1,-1,-1,-1,-1,-1,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};
12+
static const char decoding_size = sizeof(decoding);
13+
value_in -= 43;
14+
if (value_in < 0 || value_in > decoding_size) return -1;
15+
return decoding[(int)value_in];
16+
}
17+
18+
void base64_init_decodestate(base64_decodestate* state_in){
19+
state_in->step = step_a;
20+
state_in->plainchar = 0;
21+
}
22+
23+
int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in){
24+
const char* codechar = code_in;
25+
char* plainchar = plaintext_out;
26+
char fragment;
27+
28+
*plainchar = state_in->plainchar;
29+
30+
switch (state_in->step){
31+
while (1){
32+
case step_a:
33+
do {
34+
if (codechar == code_in+length_in){
35+
state_in->step = step_a;
36+
state_in->plainchar = *plainchar;
37+
return plainchar - plaintext_out;
38+
}
39+
fragment = (char)base64_decode_value(*codechar++);
40+
} while (fragment < 0);
41+
*plainchar = (fragment & 0x03f) << 2;
42+
case step_b:
43+
do {
44+
if (codechar == code_in+length_in){
45+
state_in->step = step_b;
46+
state_in->plainchar = *plainchar;
47+
return plainchar - plaintext_out;
48+
}
49+
fragment = (char)base64_decode_value(*codechar++);
50+
} while (fragment < 0);
51+
*plainchar++ |= (fragment & 0x030) >> 4;
52+
*plainchar = (fragment & 0x00f) << 4;
53+
case step_c:
54+
do {
55+
if (codechar == code_in+length_in){
56+
state_in->step = step_c;
57+
state_in->plainchar = *plainchar;
58+
return plainchar - plaintext_out;
59+
}
60+
fragment = (char)base64_decode_value(*codechar++);
61+
} while (fragment < 0);
62+
*plainchar++ |= (fragment & 0x03c) >> 2;
63+
*plainchar = (fragment & 0x003) << 6;
64+
case step_d:
65+
do {
66+
if (codechar == code_in+length_in){
67+
state_in->step = step_d;
68+
state_in->plainchar = *plainchar;
69+
return plainchar - plaintext_out;
70+
}
71+
fragment = (char)base64_decode_value(*codechar++);
72+
} while (fragment < 0);
73+
*plainchar++ |= (fragment & 0x03f);
74+
}
75+
}
76+
/* control should not reach here */
77+
return plainchar - plaintext_out;
78+
}
79+
80+
int base64_decode_chars(const char* code_in, const int length_in, char* plaintext_out){
81+
base64_decodestate _state;
82+
base64_init_decodestate(&_state);
83+
int len = base64_decode_block(code_in, length_in, plaintext_out, &_state);
84+
if(len > 0) plaintext_out[len] = 0;
85+
return len;
86+
}

cores/esp8266/libb64/cdecode.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
cdecode.h - c header for a base64 decoding algorithm
3+
4+
This is part of the libb64 project, and has been placed in the public domain.
5+
For details, see http://sourceforge.net/projects/libb64
6+
*/
7+
8+
#ifndef BASE64_CDECODE_H
9+
#define BASE64_CDECODE_H
10+
11+
#define base64_decode_expected_len(n) ((n * 3) / 4)
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
typedef enum {
18+
step_a, step_b, step_c, step_d
19+
} base64_decodestep;
20+
21+
typedef struct {
22+
base64_decodestep step;
23+
char plainchar;
24+
} base64_decodestate;
25+
26+
void base64_init_decodestate(base64_decodestate* state_in);
27+
28+
int base64_decode_value(char value_in);
29+
30+
int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in);
31+
32+
int base64_decode_chars(const char* code_in, const int length_in, char* plaintext_out);
33+
34+
#ifdef __cplusplus
35+
} // extern "C"
36+
#endif
37+
38+
#endif /* BASE64_CDECODE_H */

cores/esp8266/libb64/cencode.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
cencoder.c - c source to a base64 encoding algorithm implementation
3+
4+
This is part of the libb64 project, and has been placed in the public domain.
5+
For details, see http://sourceforge.net/projects/libb64
6+
*/
7+
8+
#include "cencode.h"
9+
10+
const int CHARS_PER_LINE = 72;
11+
12+
void base64_init_encodestate(base64_encodestate* state_in){
13+
state_in->step = step_A;
14+
state_in->result = 0;
15+
state_in->stepcount = 0;
16+
}
17+
18+
char base64_encode_value(char value_in){
19+
static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
20+
if (value_in > 63) return '=';
21+
return encoding[(int)value_in];
22+
}
23+
24+
int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in){
25+
const char* plainchar = plaintext_in;
26+
const char* const plaintextend = plaintext_in + length_in;
27+
char* codechar = code_out;
28+
char result;
29+
char fragment;
30+
31+
result = state_in->result;
32+
33+
switch (state_in->step){
34+
while (1){
35+
case step_A:
36+
if (plainchar == plaintextend){
37+
state_in->result = result;
38+
state_in->step = step_A;
39+
return codechar - code_out;
40+
}
41+
fragment = *plainchar++;
42+
result = (fragment & 0x0fc) >> 2;
43+
*codechar++ = base64_encode_value(result);
44+
result = (fragment & 0x003) << 4;
45+
case step_B:
46+
if (plainchar == plaintextend){
47+
state_in->result = result;
48+
state_in->step = step_B;
49+
return codechar - code_out;
50+
}
51+
fragment = *plainchar++;
52+
result |= (fragment & 0x0f0) >> 4;
53+
*codechar++ = base64_encode_value(result);
54+
result = (fragment & 0x00f) << 2;
55+
case step_C:
56+
if (plainchar == plaintextend){
57+
state_in->result = result;
58+
state_in->step = step_C;
59+
return codechar - code_out;
60+
}
61+
fragment = *plainchar++;
62+
result |= (fragment & 0x0c0) >> 6;
63+
*codechar++ = base64_encode_value(result);
64+
result = (fragment & 0x03f) >> 0;
65+
*codechar++ = base64_encode_value(result);
66+
67+
++(state_in->stepcount);
68+
if (state_in->stepcount == CHARS_PER_LINE/4){
69+
*codechar++ = '\n';
70+
state_in->stepcount = 0;
71+
}
72+
}
73+
}
74+
/* control should not reach here */
75+
return codechar - code_out;
76+
}
77+
78+
int base64_encode_blockend(char* code_out, base64_encodestate* state_in){
79+
char* codechar = code_out;
80+
81+
switch (state_in->step){
82+
case step_B:
83+
*codechar++ = base64_encode_value(state_in->result);
84+
*codechar++ = '=';
85+
*codechar++ = '=';
86+
break;
87+
case step_C:
88+
*codechar++ = base64_encode_value(state_in->result);
89+
*codechar++ = '=';
90+
break;
91+
case step_A:
92+
break;
93+
}
94+
*codechar = 0x00;
95+
96+
return codechar - code_out;
97+
}
98+
99+
int base64_encode_chars(const char* plaintext_in, int length_in, char* code_out){
100+
base64_encodestate _state;
101+
base64_init_encodestate(&_state);
102+
int len = base64_encode_block(plaintext_in, length_in, code_out, &_state);
103+
return len + base64_encode_blockend((code_out + len), &_state);
104+
}

cores/esp8266/libb64/cencode.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
cencode.h - c header for a base64 encoding algorithm
3+
4+
This is part of the libb64 project, and has been placed in the public domain.
5+
For details, see http://sourceforge.net/projects/libb64
6+
*/
7+
8+
#ifndef BASE64_CENCODE_H
9+
#define BASE64_CENCODE_H
10+
11+
#define base64_encode_expected_len(n) ((((4 * n) / 3) + 3) & ~3)
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
typedef enum {
18+
step_A, step_B, step_C
19+
} base64_encodestep;
20+
21+
typedef struct {
22+
base64_encodestep step;
23+
char result;
24+
int stepcount;
25+
} base64_encodestate;
26+
27+
void base64_init_encodestate(base64_encodestate* state_in);
28+
29+
char base64_encode_value(char value_in);
30+
31+
int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in);
32+
33+
int base64_encode_blockend(char* code_out, base64_encodestate* state_in);
34+
35+
int base64_encode_chars(const char* plaintext_in, int length_in, char* code_out);
36+
37+
#ifdef __cplusplus
38+
} // extern "C"
39+
#endif
40+
41+
#endif /* BASE64_CENCODE_H */
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <ESP8266WiFi.h>
2+
#include <ESP8266mDNS.h>
3+
#include <ArduinoOTA.h>
4+
#include <ESP8266WebServer.h>
5+
6+
const char* ssid = "........";
7+
const char* password = "........";
8+
9+
ESP8266WebServer server(80);
10+
11+
const char* www_username = "admin";
12+
const char* www_password = "esp8266";
13+
14+
void setup() {
15+
Serial.begin(115200);
16+
WiFi.mode(WIFI_STA);
17+
WiFi.begin(ssid, password);
18+
if(WiFi.waitForConnectResult() != WL_CONNECTED) {
19+
Serial.println("WiFi Connect Failed! Rebooting...");
20+
delay(1000);
21+
ESP.restart();
22+
}
23+
ArduinoOTA.begin();
24+
25+
server.on("/", [](){
26+
if(!server.authenticate(www_username, www_password))
27+
return server.requestAuthentication();
28+
server.send(200, "text/plain", "Login OK");
29+
});
30+
server.begin();
31+
32+
Serial.print("Open http://");
33+
Serial.print(WiFi.localIP());
34+
Serial.println("/ in your browser to see it working");
35+
}
36+
37+
void loop() {
38+
ArduinoOTA.handle();
39+
server.handleClient();
40+
}

0 commit comments

Comments
 (0)