@@ -80,6 +80,8 @@ void WebSocketsServer::begin(void) {
80
80
client->cIsUpgrade = false ;
81
81
client->cIsWebsocket = false ;
82
82
83
+ client->base64Authorization = " " ;
84
+
83
85
client->cWsRXsize = 0 ;
84
86
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
85
87
client->cHttpLine = " " ;
@@ -262,6 +264,32 @@ void WebSocketsServer::disconnect(uint8_t num) {
262
264
}
263
265
}
264
266
267
+
268
+
269
+ /* *
270
+ * set the Authorizatio for the http request
271
+ * @param user const char *
272
+ * @param password const char *
273
+ */
274
+ void WebSocketsServer::setAuthorization (const char * user, const char * password) {
275
+ if (user && password) {
276
+ String auth = user;
277
+ auth += " :" ;
278
+ auth += password;
279
+ _base64Authorization = base64_encode ((uint8_t *)auth.c_str (), auth.length ());
280
+ }
281
+ }
282
+
283
+ /* *
284
+ * set the Authorizatio for the http request
285
+ * @param auth const char * base64
286
+ */
287
+ void WebSocketsServer::setAuthorization (const char * auth) {
288
+ if (auth) {
289
+ _base64Authorization = auth;
290
+ }
291
+ }
292
+
265
293
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
266
294
/* *
267
295
* get an IP for a client
@@ -564,6 +592,8 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
564
592
client->cProtocol = headerValue;
565
593
} else if (headerName.equalsIgnoreCase (" Sec-WebSocket-Extensions" )) {
566
594
client->cExtensions = headerValue;
595
+ } else if (headerName.equalsIgnoreCase (" Authorization" )) {
596
+ client->base64Authorization = headerValue;
567
597
}
568
598
} else {
569
599
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] Header error (%s)\n " , headerLine->c_str ());
@@ -583,6 +613,7 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
583
613
DEBUG_WEBSOCKETS (" [WS-Server][%d][handleHeader] - cProtocol: %s\n " , client->num , client->cProtocol .c_str ());
584
614
DEBUG_WEBSOCKETS (" [WS-Server][%d][handleHeader] - cExtensions: %s\n " , client->num , client->cExtensions .c_str ());
585
615
DEBUG_WEBSOCKETS (" [WS-Server][%d][handleHeader] - cVersion: %d\n " , client->num , client->cVersion );
616
+ DEBUG_WEBSOCKETS (" [WS-Server][%d][handleHeader] - base64Authorization: %s\n " , client->num , client->base64Authorization );
586
617
587
618
bool ok = (client->cIsUpgrade && client->cIsWebsocket );
588
619
@@ -598,6 +629,20 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
598
629
}
599
630
}
600
631
632
+ if (_base64Authorization.length () > 0 ) {
633
+ if (client->base64Authorization .length () > 0 ) {
634
+ String auth = " Basic " ;
635
+ auth += _base64Authorization;
636
+ if (auth != client->base64Authorization ) {
637
+ DEBUG_WEBSOCKETS (" [WS-Server][%d][handleHeader] HTTP Authorization failed!\n " , client->num );
638
+ handleAuthorizationFailed (client);
639
+ return ;
640
+ }
641
+ } else {
642
+ ok = false ;
643
+ }
644
+ }
645
+
601
646
if (ok) {
602
647
603
648
DEBUG_WEBSOCKETS (" [WS-Server][%d][handleHeader] Websocket connection incoming.\n " , client->num );
0 commit comments