add HTTP Basic Authorization to WS Client and Server

see: #55
This commit is contained in:
Markus Sattler
2016-02-17 17:56:03 +01:00
parent 2d87bfa3d6
commit c57a4c19ab
6 changed files with 106 additions and 2 deletions

View File

@ -78,6 +78,9 @@ public:
void disconnect(void);
void disconnect(uint8_t num);
void setAuthorization(const char * user, const char * password);
void setAuthorization(const char * auth);
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
IPAddress remoteIP(uint8_t num);
#endif
@ -86,6 +89,7 @@ protected:
uint16_t _port;
String _origin;
String _protocol;
String _base64Authorization; ///< Base64 encoded Auth request
WEBSOCKETS_NETWORK_SERVER_CLASS * _server;
@ -109,8 +113,8 @@ protected:
/**
* called if a non Websocket connection is comming in.
* Note: can be overrided
* called if a non Websocket connection is coming in.
* Note: can be override
* @param client WSclient_t * ptr to the client struct
*/
virtual void handleNonWebsocketConnection(WSclient_t * client) {
@ -126,6 +130,25 @@ protected:
clientDisconnect(client);
}
/**
* called if a non Authorization connection is coming in.
* Note: can be override
* @param client WSclient_t * ptr to the client struct
*/
virtual void handleAuthorizationFailed(WSclient_t *client) {
client->tcp->write("HTTP/1.1 401 Unauthorized\r\n"
"Server: arduino-WebSocket-Server\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 45\r\n"
"Connection: close\r\n"
"Sec-WebSocket-Version: 13\r\n"
"WWW-Authenticate: Basic realm=\"WebSocket Server\""
"\r\n"
"This Websocket server requires Authorization!");
clientDisconnect(client);
}
/**
* called for sending a Event to the app
* @param num uint8_t