22 #include "../../core/esp3doutput.h"
23 #include "../../core/settings_esp3d.h"
25 #if defined (AUTHENTICATION_FEATURE)
26 #if defined (HTTP_FEATURE)
27 #if defined (ARDUINO_ARCH_ESP32)
28 #include <WebServer.h>
29 #endif //ARDUINO_ARCH_ESP32
30 #if defined (ARDUINO_ARCH_ESP8266)
31 #include <ESP8266WebServer.h>
32 #endif //ARDUINO_ARCH_ESP8266
33 Authwebserver * AuthenticationService::_webserver =
nullptr;
35 #endif //AUTHENTICATION_FEATURE
37 #if defined (AUTHENTICATION_FEATURE)
38 String AuthenticationService::_adminpwd=
"";
39 String AuthenticationService::_userpwd=
"";
40 #if defined (HTTP_FEATURE)
41 uint32_t AuthenticationService::_sessionTimeout = 360000;
42 auth_ip * AuthenticationService::_head =
nullptr;
43 uint8_t AuthenticationService::_current_nb_ip = 0;
45 #endif //AUTHENTICATION_FEATURE
47 #define MAX_AUTH_IP 10
55 #if defined (HTTP_FEATURE) && defined (AUTHENTICATION_FEATURE)
57 #endif //HTTP_FEATURE && AUTHENTICATION_FEATURE
63 #ifdef AUTHENTICATION_FEATURE
74 #if defined (HTTP_FEATURE)
76 if (_webserver->hasHeader (
"Authorization") ) {
86 if (_webserver->hasHeader (
"Cookie") ) {
88 String cookie = _webserver->header (
"Cookie");
89 int pos = cookie.indexOf (
"ESPSESSIONID=");
91 int pos2 = cookie.indexOf (
";", pos);
92 String sessionID = cookie.substring (pos + strlen (
"ESPSESSIONID="), pos2);
93 IPAddress ip = _webserver->client().remoteIP();
95 auth_type = ResetAuthIP (ip, sessionID.c_str() );
100 #endif //HTTP_FEATURE
106 #endif //AUTHENTICATION_FEATURE
108 #ifdef AUTHENTICATION_FEATURE
110 #if defined (HTTP_FEATURE)
111 uint32_t AuthenticationService::setSessionTimeout(uint32_t timeout)
114 _sessionTimeout = timeout;
116 return _sessionTimeout;
118 uint32_t AuthenticationService::getSessionTimeout()
120 return _sessionTimeout;
122 #endif //HTTP_FEATURE
124 bool AuthenticationService::begin(Authwebserver * webserver)
128 #if defined (HTTP_FEATURE)
129 _webserver = webserver;
130 #endif //HTTP_FEATURE
133 void AuthenticationService::end()
135 #if defined (HTTP_FEATURE)
136 _webserver =
nullptr;
138 #endif //HTTP_FEATURE
141 void AuthenticationService::update()
147 void AuthenticationService::handle()
152 bool AuthenticationService::isadmin (
const char *pwd)
154 if (strcmp(_adminpwd.c_str(), pwd) !=0 ) {
162 bool AuthenticationService::isuser (
const char *pwd)
165 if (strcmp(_userpwd.c_str(), pwd)!=0) {
167 return isadmin (pwd);
173 #if defined (HTTP_FEATURE)
175 bool AuthenticationService::AddAuthIP (auth_ip * item)
187 char * AuthenticationService::create_session_ID()
189 static char sessionID[17];
191 for (
int i = 0; i < 17; i++) {
195 uint32_t now = millis();
197 IPAddress remoteIP = _webserver->client().remoteIP();
199 if (0 > sprintf (sessionID,
"%02X%02X%02X%02X%02X%02X%02X%02X", remoteIP[0], remoteIP[1], remoteIP[2], remoteIP[3], (uint8_t) ( (now >> 0) & 0xff), (uint8_t) ( (now >> 8) & 0xff), (uint8_t) ( (now >> 16) & 0xff), (uint8_t) ( (now >> 24) & 0xff) ) ) {
200 strcpy (sessionID,
"NONE");
205 bool AuthenticationService::ClearAllSessions()
208 auth_ip * current = _head;
209 _head = _head->_next;
218 bool AuthenticationService::ClearCurrentSession()
220 String cookie = _webserver->header(
"Cookie");
221 int pos = cookie.indexOf(
"ESPSESSIONID=");
224 int pos2 = cookie.indexOf(
";",pos);
225 sessionID = cookie.substring(pos+strlen(
"ESPSESSIONID="),pos2);
227 return ClearAuthIP(_webserver->client().remoteIP(), sessionID.c_str());
230 bool AuthenticationService::CreateSession(
level_authenticate_type auth_level,
const char * username,
const char* session_ID)
232 auth_ip * current_auth =
new auth_ip;
233 current_auth->level = auth_level;
234 current_auth->ip=_webserver->client().remoteIP();
235 strcpy(current_auth->sessionID, session_ID);
236 strcpy(current_auth->userID,username);
237 current_auth->last_time=millis();
238 #ifndef ALLOW_MULTIPLE_SESSIONS
241 #endif //ALLOW_MULTIPLE_SESSIONS
242 if (AddAuthIP(current_auth)) {
250 bool AuthenticationService::ClearAuthIP (IPAddress ip,
const char * sessionID)
252 auth_ip * current = _head;
253 auth_ip * previous = NULL;
256 if ( (ip == current->ip) && (strcmp (sessionID, current->sessionID) == 0) ) {
259 if (current == _head) {
260 _head = current->_next;
265 previous->_next = current->_next;
268 current = previous->_next;
272 current = current->_next;
279 auth_ip * AuthenticationService::GetAuth (IPAddress ip,
const char * sessionID)
281 auth_ip * current = _head;
286 if (ip == current->ip) {
287 if (strcmp (sessionID, current->sessionID) == 0) {
293 current = current->_next;
301 auth_ip * current = _head;
302 auth_ip * previous = NULL;
308 if ( (((millis() - current->last_time) > _sessionTimeout) && (_sessionTimeout!=0)) || ((ip != current->ip) && (_sessionTimeout==0)) ) {
310 if (current == _head) {
311 _head = current->_next;
316 previous->_next = current->_next;
319 current = previous->_next;
322 if (ip == current->ip) {
323 if (strcmp (sessionID, current->sessionID) == 0) {
325 current->last_time = millis();
330 current = current->_next;
335 #endif //HTTP_FEATURE
338 #endif //AUTHENTICATION_FEATURE