WebServer.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <WebServer.h>
  2. #include <PathVariableHandler.h>
  3. void WebServer::onAuthenticated(const String &uri, THandlerFunction handler) {
  4. THandlerFunction authHandler = [this, handler]() {
  5. if (this->validateAuthentiation()) {
  6. handler();
  7. }
  8. };
  9. ESP8266WebServer::on(uri, authHandler);
  10. }
  11. void WebServer::onAuthenticated(const String &uri, HTTPMethod method, THandlerFunction handler) {
  12. THandlerFunction authHandler = [this, handler]() {
  13. if (this->validateAuthentiation()) {
  14. handler();
  15. }
  16. };
  17. ESP8266WebServer::on(uri, method, authHandler);
  18. }
  19. void WebServer::onAuthenticated(const String &uri, HTTPMethod method, THandlerFunction handler, THandlerFunction ufn) {
  20. THandlerFunction authHandler = [this, handler]() {
  21. if (this->validateAuthentiation()) {
  22. handler();
  23. }
  24. };
  25. ESP8266WebServer::on(uri, method, authHandler, ufn);
  26. }
  27. void WebServer::onPattern(const String& pattern, const HTTPMethod method, PathVariableHandler::TPathVariableHandlerFn handler) {
  28. addHandler(new PathVariableHandler(pattern.c_str(), method, handler));
  29. }
  30. void WebServer::onPatternAuthenticated(const String& pattern, const HTTPMethod method, PathVariableHandler::TPathVariableHandlerFn fn) {
  31. PathVariableHandler::TPathVariableHandlerFn authHandler = [this, fn](UrlTokenBindings* bindings) {
  32. if (this->validateAuthentiation()) {
  33. fn(bindings);
  34. }
  35. };
  36. addHandler(new PathVariableHandler(pattern.c_str(), method, authHandler));
  37. }
  38. void WebServer::requireAuthentication(const String& username, const String& password) {
  39. this->username = String(username);
  40. this->password = String(password);
  41. this->authEnabled = true;
  42. }
  43. void WebServer::disableAuthentication() {
  44. this->authEnabled = false;
  45. }
  46. bool WebServer::validateAuthentiation() {
  47. if (this->authEnabled &&
  48. !authenticate(this->username.c_str(), this->password.c_str())) {
  49. requestAuthentication();
  50. return false;
  51. }
  52. return true;
  53. }