4
4
#include < WebServer.h>
5
5
#include " mbedtls/sha1.h"
6
6
7
-
8
- // / We have two options - we either come in with a bearer
7
+ // We have two options - we either come in with a bearer
9
8
// token - i.e. a special header or API token; or we
10
9
// get a normal HTTP style basic auth prompt.
11
10
//
18
17
19
18
// Create the secet token SHA1 with:
20
19
// echo -n SecritToken | openssl sha1
21
- //
20
+
22
21
String secret_token_hex = " d2cce6b472959484a21c3194080c609b8a2c910b" ;
23
22
24
23
// Wifi credentials
25
- //
24
+
26
25
const char * ssid = " ........" ;
27
26
const char * password = " ........" ;
28
27
@@ -36,20 +35,17 @@ WebServer server(80);
36
35
// The sha1 of 'esp32' (without the trailing \0) expressed as 20
37
36
// bytes of hex. Created by for example 'echo -n esp32 | openssl sha1'
38
37
// or http://www.sha1-online.com.
39
- //
40
38
const char * www_username_hex = " hexadmin" ;
41
39
const char * www_password_hex = " 8cb124f8c277c16ec0b2ee00569fd151a08e342b" ;
42
40
43
41
// The same; but now expressed as a base64 string (e.g. as commonly used
44
42
// by webservers). Created by ` echo -n esp32 | openssl sha1 -binary | openssl base64`
45
- //
46
43
const char * www_username_base64 = " base64admin" ;
47
44
const char * www_password_base64 = " jLEk+MJ3wW7Asu4AVp/RUaCONCs=" ;
48
45
49
46
static unsigned char _bearer[20 ];
50
47
String * check_bearer_or_auth (HTTPAuthMethod mode, String authReq, String params[]) {
51
48
// we expect authReq to be "bearer some-secret"
52
- //
53
49
String lcAuthReq = authReq;
54
50
lcAuthReq.toLowerCase ();
55
51
if (mode == OTHER_AUTH && (lcAuthReq.startsWith (" bearer " ))) {
@@ -64,7 +60,6 @@ String * check_bearer_or_auth(HTTPAuthMethod mode, String authReq, String params
64
60
};
65
61
66
62
// that failed - so do a normal auth
67
- //
68
63
return server.authenticateBasicSHA1 (www_username_hex, www_password_hex) ?
69
64
new String (params[0 ]) : NULL ;
70
65
};
@@ -82,9 +77,8 @@ void setup() {
82
77
ArduinoOTA.begin ();
83
78
84
79
// Convert token to a convenient binary representation.
85
- //
86
80
size_t len = HEXBuilder::hex2bytes (_bearer,sizeof (_bearer),secret_token_hex);
87
- if (len != 20 )
81
+ if (len != 20 )
88
82
Serial.println (" Bearer token does not look like a valid SHA1 hex string ?!" );
89
83
90
84
server.on (" /" , []() {
0 commit comments