esp8266-react-framework/src/SecurityManager.cpp

95 lines
2.9 KiB
C++
Raw Normal View History

2019-04-29 23:30:43 +00:00
#include <SecurityManager.h>
SecurityManager::SecurityManager(AsyncWebServer* server, FS* fs) : SettingsService(server, fs, USERS_PATH, SECURITY_SETTINGS_FILE) {}
2019-04-29 23:30:43 +00:00
SecurityManager::~SecurityManager() {}
void SecurityManager::readFromJsonObject(JsonObject& root) {
// secret
2019-04-29 23:30:43 +00:00
_jwtSecret = root["jwt_secret"] | DEFAULT_JWT_SECRET;
// users
_users.clear();
2019-04-29 23:30:43 +00:00
if (root["users"].is<JsonArray>()) {
for (JsonVariant user : root["users"].as<JsonArray>()) {
2019-05-25 08:45:49 +00:00
_users.push_back(User(user["username"], user["password"], user["admin"]));
2019-04-29 23:30:43 +00:00
}
}
}
2019-04-29 23:30:43 +00:00
void SecurityManager::writeToJsonObject(JsonObject& root) {
// secret
root["jwt_secret"] = _jwtSecret;
// users
JsonArray users = root.createNestedArray("users");
for (User _user : _users) {
JsonObject user = users.createNestedObject();
user["username"] = _user.getUsername();
user["password"] = _user.getPassword();
2019-05-25 08:45:49 +00:00
user["admin"] = _user.isAdmin();
}
}
2019-04-29 23:30:43 +00:00
void SecurityManager::begin() {
2019-05-02 23:31:20 +00:00
// read config
readFromFS();
2019-05-02 23:31:20 +00:00
// configure secret
2019-05-06 14:50:19 +00:00
jwtHandler.setSecret(_jwtSecret);
2019-04-29 23:30:43 +00:00
}
2019-05-18 18:35:27 +00:00
Authentication SecurityManager::authenticateRequest(AsyncWebServerRequest *request) {
AsyncWebHeader* authorizationHeader = request->getHeader(AUTHORIZATION_HEADER);
if (authorizationHeader) {
String value = authorizationHeader->value();
value.startsWith(AUTHORIZATION_HEADER_PREFIX);
value = value.substring(AUTHORIZATION_HEADER_PREFIX_LEN);
return authenticateJWT(value);
}
return Authentication();
}
Authentication SecurityManager::authenticateJWT(String jwt) {
DynamicJsonDocument payloadDocument(MAX_JWT_SIZE);
jwtHandler.parseJWT(jwt, payloadDocument);
if (payloadDocument.is<JsonObject>()) {
JsonObject parsedPayload = payloadDocument.as<JsonObject>();
String username = parsedPayload["username"];
for (User _user : _users) {
2019-05-18 18:35:27 +00:00
if (_user.getUsername() == username && validatePayload(parsedPayload, &_user)){
return Authentication(_user);
}
}
}
2019-05-18 18:35:27 +00:00
return Authentication();
2019-04-29 23:30:43 +00:00
}
Authentication SecurityManager::authenticate(String username, String password) {
for (User _user : _users) {
if (_user.getUsername() == username && _user.getPassword() == password){
2019-05-18 18:35:27 +00:00
return Authentication(_user);
}
}
2019-05-18 18:35:27 +00:00
return Authentication();
}
inline void populateJWTPayload(JsonObject &payload, User *user) {
payload["username"] = user->getUsername();
2019-05-25 08:45:49 +00:00
payload["admin"] = user -> isAdmin();
2019-05-18 18:35:27 +00:00
}
boolean SecurityManager::validatePayload(JsonObject &parsedPayload, User *user) {
DynamicJsonDocument _jsonDocument(MAX_JWT_SIZE);
JsonObject payload = _jsonDocument.to<JsonObject>();
populateJWTPayload(payload, user);
return payload == parsedPayload;
2019-04-29 23:30:43 +00:00
}
2019-05-18 18:35:27 +00:00
String SecurityManager::generateJWT(User *user) {
DynamicJsonDocument _jsonDocument(MAX_JWT_SIZE);
2019-05-18 18:35:27 +00:00
JsonObject payload = _jsonDocument.to<JsonObject>();
populateJWTPayload(payload, user);
return jwtHandler.buildJWT(payload);
2019-04-29 23:30:43 +00:00
}