SimpleAuthentification.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. const char* ssid = "........";
  5. const char* password = "........";
  6. ESP8266WebServer server(80);
  7. //Check if header is present and correct
  8. bool is_authentified(){
  9. Serial.println("Enter is_authentified");
  10. if (server.hasHeader("Cookie")){
  11. Serial.print("Found cookie: ");
  12. String cookie = server.header("Cookie");
  13. Serial.println(cookie);
  14. if (cookie.indexOf("ESPSESSIONID=1") != -1) {
  15. Serial.println("Authentification Successful");
  16. return true;
  17. }
  18. }
  19. Serial.println("Authentification Failed");
  20. return false;
  21. }
  22. //login page, also called for disconnect
  23. void handleLogin(){
  24. String msg;
  25. if (server.hasHeader("Cookie")){
  26. Serial.print("Found cookie: ");
  27. String cookie = server.header("Cookie");
  28. Serial.println(cookie);
  29. }
  30. if (server.hasArg("DISCONNECT")){
  31. Serial.println("Disconnection");
  32. String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=0\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
  33. server.sendContent(header);
  34. return;
  35. }
  36. if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")){
  37. if (server.arg("USERNAME") == "admin" && server.arg("PASSWORD") == "admin" ){
  38. String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=1\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
  39. server.sendContent(header);
  40. Serial.println("Log in Successful");
  41. return;
  42. }
  43. msg = "Wrong username/password! try again.";
  44. Serial.println("Log in Failed");
  45. }
  46. String content = "<html><body><form action='/login' method='POST'>To log in, please use : admin/admin<br>";
  47. content += "User:<input type='text' name='USERNAME' placeholder='user name'><br>";
  48. content += "Password:<input type='password' name='PASSWORD' placeholder='password'><br>";
  49. content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>";
  50. content += "You also can go <a href='/inline'>here</a></body></html>";
  51. server.send(200, "text/html", content);
  52. }
  53. //root page can be accessed only if authentification is ok
  54. void handleRoot(){
  55. Serial.println("Enter handleRoot");
  56. String header;
  57. if (!is_authentified()){
  58. String header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
  59. server.sendContent(header);
  60. return;
  61. }
  62. String content = "<html><body><H2>hello, you successfully connected to esp8266!</H2><br>";
  63. if (server.hasHeader("User-Agent")){
  64. content += "the user agent used is : " + server.header("User-Agent") + "<br><br>";
  65. }
  66. content += "You can access this page until you <a href=\"/login?DISCONNECT=YES\">disconnect</a></body></html>";
  67. server.send(200, "text/html", content);
  68. }
  69. //no need authentification
  70. void handleNotFound(){
  71. String message = "File Not Found\n\n";
  72. message += "URI: ";
  73. message += server.uri();
  74. message += "\nMethod: ";
  75. message += (server.method() == HTTP_GET)?"GET":"POST";
  76. message += "\nArguments: ";
  77. message += server.args();
  78. message += "\n";
  79. for (uint8_t i=0; i<server.args(); i++){
  80. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  81. }
  82. server.send(404, "text/plain", message);
  83. }
  84. void setup(void){
  85. Serial.begin(115200);
  86. WiFi.begin(ssid, password);
  87. Serial.println("");
  88. // Wait for connection
  89. while (WiFi.status() != WL_CONNECTED) {
  90. delay(500);
  91. Serial.print(".");
  92. }
  93. Serial.println("");
  94. Serial.print("Connected to ");
  95. Serial.println(ssid);
  96. Serial.print("IP address: ");
  97. Serial.println(WiFi.localIP());
  98. server.on("/", handleRoot);
  99. server.on("/login", handleLogin);
  100. server.on("/inline", [](){
  101. server.send(200, "text/plain", "this works without need of authentification");
  102. });
  103. server.onNotFound(handleNotFound);
  104. //here the list of headers to be recorded
  105. const char * headerkeys[] = {"User-Agent","Cookie"} ;
  106. size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
  107. //ask server to track these headers
  108. server.collectHeaders(headerkeys, headerkeyssize );
  109. server.begin();
  110. Serial.println("HTTP server started");
  111. }
  112. void loop(void){
  113. server.handleClient();
  114. }