2019-05-29 22:48:16 +00:00
|
|
|
#ifndef SecuritySettingsService_h
|
|
|
|
#define SecuritySettingsService_h
|
|
|
|
|
2019-08-10 11:35:26 +00:00
|
|
|
#include <AdminSettingsService.h>
|
2019-05-29 22:48:16 +00:00
|
|
|
#include <SecurityManager.h>
|
|
|
|
|
2019-12-29 17:54:12 +00:00
|
|
|
#define DEFAULT_ADMIN_USERNAME "admin"
|
|
|
|
#define DEFAULT_GUEST_USERNAME "guest"
|
|
|
|
|
2019-05-29 22:48:16 +00:00
|
|
|
#define SECURITY_SETTINGS_FILE "/config/securitySettings.json"
|
|
|
|
#define SECURITY_SETTINGS_PATH "/rest/securitySettings"
|
|
|
|
|
2020-02-01 08:44:26 +00:00
|
|
|
class SecuritySettings {
|
|
|
|
public:
|
|
|
|
String jwtSecret;
|
|
|
|
std::list<User> users;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SecuritySettingsService : public AdminSettingsService<SecuritySettings>, public SecurityManager {
|
2019-12-03 23:16:06 +00:00
|
|
|
public:
|
|
|
|
SecuritySettingsService(AsyncWebServer* server, FS* fs);
|
|
|
|
~SecuritySettingsService();
|
2019-05-29 22:48:16 +00:00
|
|
|
|
2020-02-01 08:44:26 +00:00
|
|
|
// Functions to implement SecurityManager
|
|
|
|
Authentication authenticate(String username, String password);
|
|
|
|
Authentication authenticateRequest(AsyncWebServerRequest* request);
|
|
|
|
String generateJWT(User* user);
|
|
|
|
ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest, AuthenticationPredicate predicate);
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
protected:
|
|
|
|
void readFromJsonObject(JsonObject& root);
|
|
|
|
void writeToJsonObject(JsonObject& root);
|
2020-02-01 08:44:26 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ArduinoJsonJWT _jwtHandler = ArduinoJsonJWT(DEFAULT_JWT_SECRET);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup the user by JWT
|
|
|
|
*/
|
|
|
|
Authentication authenticateJWT(String jwt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify the payload is correct
|
|
|
|
*/
|
|
|
|
boolean validatePayload(JsonObject& parsedPayload, User* user);
|
2019-05-29 22:48:16 +00:00
|
|
|
};
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
#endif // end SecuritySettingsService_h
|