Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.6 KiB

  1. #ifndef SecurityManager_h
  2. #define SecurityManager_h
  3. #include <ArduinoJsonJWT.h>
  4. #include <ESPAsyncWebServer.h>
  5. #include <ESPUtils.h>
  6. #include <AsyncJson.h>
  7. #include <list>
  8. #ifndef FACTORY_JWT_SECRET
  9. #define FACTORY_JWT_SECRET ESPUtils::defaultDeviceValue()
  10. #endif
  11. #define ACCESS_TOKEN_PARAMATER "access_token"
  12. #define AUTHORIZATION_HEADER "Authorization"
  13. #define AUTHORIZATION_HEADER_PREFIX "Bearer "
  14. #define AUTHORIZATION_HEADER_PREFIX_LEN 7
  15. #define MAX_JWT_SIZE 128
  16. class User {
  17. public:
  18. String username;
  19. String password;
  20. bool admin;
  21. public:
  22. User(String username, String password, bool admin) : username(username), password(password), admin(admin) {
  23. }
  24. };
  25. class Authentication {
  26. public:
  27. User* user;
  28. boolean authenticated;
  29. public:
  30. Authentication(User& user) : user(new User(user)), authenticated(true) {
  31. }
  32. Authentication() : user(nullptr), authenticated(false) {
  33. }
  34. ~Authentication() {
  35. delete (user);
  36. }
  37. };
  38. typedef std::function<boolean(Authentication& authentication)> AuthenticationPredicate;
  39. class AuthenticationPredicates {
  40. public:
  41. static bool NONE_REQUIRED(Authentication& authentication) {
  42. return true;
  43. };
  44. static bool IS_AUTHENTICATED(Authentication& authentication) {
  45. return authentication.authenticated;
  46. };
  47. static bool IS_ADMIN(Authentication& authentication) {
  48. return authentication.authenticated && authentication.user->admin;
  49. };
  50. };
  51. class SecurityManager {
  52. public:
  53. /*
  54. * Authenticate, returning the user if found
  55. */
  56. virtual Authentication authenticate(const String& username, const String& password) = 0;
  57. /*
  58. * Check the request header for the Authorization token
  59. */
  60. virtual Authentication authenticateRequest(AsyncWebServerRequest* request) = 0;
  61. /*
  62. * Generate a JWT for the user provided
  63. */
  64. virtual String generateJWT(User* user) = 0;
  65. /**
  66. * Filter a request with the provided predicate, only returning true if the predicate matches.
  67. */
  68. virtual ArRequestFilterFunction filterRequest(AuthenticationPredicate predicate) = 0;
  69. /**
  70. * Wrap the provided request to provide validation against an AuthenticationPredicate.
  71. */
  72. virtual ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest,
  73. AuthenticationPredicate predicate) = 0;
  74. /**
  75. * Wrap the provided json request callback to provide validation against an AuthenticationPredicate.
  76. */
  77. virtual ArJsonRequestHandlerFunction wrapCallback(ArJsonRequestHandlerFunction callback,
  78. AuthenticationPredicate predicate) = 0;
  79. };
  80. #endif // end SecurityManager_h