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.

102 lines
2.6 KiB

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