Add basic auth for server static (#143)

* Add basic auth for server static

* Also effect to Websocket

* Add http basic auth for event source & document to README.md
This commit is contained in:
Tuan PM 2017-03-06 02:02:33 +08:00 committed by Me No Dev
parent bab5457584
commit 9b66da2c92
5 changed files with 25 additions and 1 deletions

View File

@ -564,6 +564,15 @@ server.serveStatic("/", SPIFFS, "/www/");
server.serveStatic("/", SPIFFS, "/www/").setDefaultFile("default.html"); 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 ### 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 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) 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 //send binary to all clients
ws.binaryAll((char*)binary); ws.binaryAll((char*)binary);
ws.binaryAll((uint8_t*)binary, (size_t)len); ws.binaryAll((uint8_t*)binary, (size_t)len);
//HTTP Authenticate before switch to Websocket protocol
ws.setAuthentication("user", "pass");
//client methods //client methods
AsyncWebSocketClient * client; AsyncWebSocketClient * client;
@ -798,6 +809,8 @@ void setup(){
// and set reconnect delay to 1 second // and set reconnect delay to 1 second
client->send("hello!",NULL,millis(),1000); client->send("hello!",NULL,millis(),1000);
}); });
//HTTP Basic authentication
events.setAuthentication("user", "pass");
server.addHandler(&events); server.addHandler(&events);
// setup ...... // setup ......
} }

View File

@ -229,6 +229,8 @@ bool AsyncEventSource::canHandle(AsyncWebServerRequest *request){
} }
void AsyncEventSource::handleRequest(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)); request->send(new AsyncEventSourceResponse(this));
} }

View File

@ -867,6 +867,9 @@ void AsyncWebSocket::handleRequest(AsyncWebServerRequest *request){
request->send(400); request->send(400);
return; return;
} }
if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str()))
return request->requestAuthentication();
AsyncWebHeader* version = request->getHeader(WS_STR_VERSION); AsyncWebHeader* version = request->getHeader(WS_STR_VERSION);
if(version->value().toInt() != 13){ if(version->value().toInt() != 13){
AsyncWebServerResponse *response = request->beginResponse(400); AsyncWebServerResponse *response = request->beginResponse(400);

View File

@ -290,9 +290,12 @@ class AsyncWebRewrite {
class AsyncWebHandler { class AsyncWebHandler {
protected: protected:
ArRequestFilterFunction _filter; ArRequestFilterFunction _filter;
String _username;
String _password;
public: public:
AsyncWebHandler(){} AsyncWebHandler():_username(""), _password(""){}
AsyncWebHandler& setFilter(ArRequestFilterFunction fn) { _filter = fn; return *this; } 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); } bool filter(AsyncWebServerRequest *request){ return _filter == NULL || _filter(request); }
virtual ~AsyncWebHandler(){} virtual ~AsyncWebHandler(){}
virtual bool canHandle(AsyncWebServerRequest *request __attribute__((unused))){ virtual bool canHandle(AsyncWebServerRequest *request __attribute__((unused))){

View File

@ -67,6 +67,7 @@ AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(struct tm* last_mo
strftime (result,30,"%a, %d %b %Y %H:%M:%S %Z", last_modified); strftime (result,30,"%a, %d %b %Y %H:%M:%S %Z", last_modified);
return setLastModified((const char *)result); return setLastModified((const char *)result);
} }
#ifdef ESP8266 #ifdef ESP8266
AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(time_t last_modified){ AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(time_t last_modified){
return setLastModified((struct tm *)gmtime(&last_modified)); return setLastModified((struct tm *)gmtime(&last_modified));
@ -180,6 +181,8 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
String filename = String((char*)request->_tempObject); String filename = String((char*)request->_tempObject);
free(request->_tempObject); free(request->_tempObject);
request->_tempObject = NULL; request->_tempObject = NULL;
if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str()))
return request->requestAuthentication();
if (request->_tempFile == true) { if (request->_tempFile == true) {
String etag = String(request->_tempFile.size()); String etag = String(request->_tempFile.size());