diff --git a/README.md b/README.md index 72144d7..6f64acc 100644 --- a/README.md +++ b/README.md @@ -564,6 +564,15 @@ server.serveStatic("/", SPIFFS, "/www/"); server.serveStatic("/", SPIFFS, "/www/").setDefaultFile("default.html"); ``` +### Serving static files with authentication + +```cpp +server + .serveStatic("/", SPIFFS, "/www/") + .setDefaultFile("default.html") + .setAuthentication("user", "pass"); +``` + ### Specifying Cache-Control header It is possible to specify Cache-Control header value to reduce the number of calls to the server once the client loaded the files. For more information on Cache-Control values see [Cache-Control](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9) @@ -757,6 +766,8 @@ ws.binary((uint32_t)client_id, flash_binary, 4); //send binary to all clients ws.binaryAll((char*)binary); ws.binaryAll((uint8_t*)binary, (size_t)len); +//HTTP Authenticate before switch to Websocket protocol +ws.setAuthentication("user", "pass"); //client methods AsyncWebSocketClient * client; @@ -798,6 +809,8 @@ void setup(){ // and set reconnect delay to 1 second client->send("hello!",NULL,millis(),1000); }); + //HTTP Basic authentication + events.setAuthentication("user", "pass"); server.addHandler(&events); // setup ...... } diff --git a/src/AsyncEventSource.cpp b/src/AsyncEventSource.cpp index 7b8cdca..51166b2 100644 --- a/src/AsyncEventSource.cpp +++ b/src/AsyncEventSource.cpp @@ -229,6 +229,8 @@ bool AsyncEventSource::canHandle(AsyncWebServerRequest *request){ } void AsyncEventSource::handleRequest(AsyncWebServerRequest *request){ + if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str())) + return request->requestAuthentication(); request->send(new AsyncEventSourceResponse(this)); } diff --git a/src/AsyncWebSocket.cpp b/src/AsyncWebSocket.cpp index b0c1a78..5c3cf5c 100644 --- a/src/AsyncWebSocket.cpp +++ b/src/AsyncWebSocket.cpp @@ -867,6 +867,9 @@ void AsyncWebSocket::handleRequest(AsyncWebServerRequest *request){ request->send(400); return; } + if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str())) + return request->requestAuthentication(); + AsyncWebHeader* version = request->getHeader(WS_STR_VERSION); if(version->value().toInt() != 13){ AsyncWebServerResponse *response = request->beginResponse(400); diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index 535e387..ed45f3b 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -290,9 +290,12 @@ class AsyncWebRewrite { class AsyncWebHandler { protected: ArRequestFilterFunction _filter; + String _username; + String _password; public: - AsyncWebHandler(){} + AsyncWebHandler():_username(""), _password(""){} AsyncWebHandler& setFilter(ArRequestFilterFunction fn) { _filter = fn; return *this; } + AsyncWebHandler& setAuthentication(const char *username, const char *password){ _username = String(username);_password = String(password);}; bool filter(AsyncWebServerRequest *request){ return _filter == NULL || _filter(request); } virtual ~AsyncWebHandler(){} virtual bool canHandle(AsyncWebServerRequest *request __attribute__((unused))){ diff --git a/src/WebHandlers.cpp b/src/WebHandlers.cpp index 2326664..9276d4f 100644 --- a/src/WebHandlers.cpp +++ b/src/WebHandlers.cpp @@ -67,6 +67,7 @@ AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(struct tm* last_mo strftime (result,30,"%a, %d %b %Y %H:%M:%S %Z", last_modified); return setLastModified((const char *)result); } + #ifdef ESP8266 AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(time_t last_modified){ return setLastModified((struct tm *)gmtime(&last_modified)); @@ -180,6 +181,8 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request) String filename = String((char*)request->_tempObject); free(request->_tempObject); request->_tempObject = NULL; + if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str())) + return request->requestAuthentication(); if (request->_tempFile == true) { String etag = String(request->_tempFile.size());