clean-up code

This commit is contained in:
proddy
2020-07-31 13:59:40 +02:00
parent 4fc6797f4e
commit 09895bb461
10 changed files with 200 additions and 313 deletions

View File

@@ -37,7 +37,6 @@ static bool __output_pins[256];
static int __output_level[256];
int main(int argc __attribute__((unused)), char * argv[] __attribute__((unused))) {
memset(__output_pins, 0, sizeof(__output_pins));
memset(__output_level, 0, sizeof(__output_level));

View File

@@ -3,16 +3,11 @@
#define ASYNC_JSON_H_
#include <ArduinoJson.h>
#include <ESPAsyncWebServer.h>
// #include <Print.h>
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
constexpr const char * JSON_MIMETYPE = "application/json";
/*
* Json Response
* */
class ChunkPrint : public Print {
private:
uint8_t * _destination;
@@ -47,7 +42,6 @@ class ChunkPrint : public Print {
class AsyncJsonResponse {
protected:
DynamicJsonDocument _jsonBuffer;
JsonVariant _root;
@@ -71,7 +65,7 @@ class AsyncJsonResponse {
return _isValid;
}
size_t setLength() {
return 0;
return 0;
}
size_t getSize() {
@@ -79,9 +73,6 @@ return 0;
}
size_t _fillBuffer(uint8_t * data, size_t len) {
// ChunkPrint dest(data, 0, len);
// serializeJson(_root, dest);
return len;
}
};

View File

@@ -24,7 +24,6 @@ struct AsyncMqttClientMessageProperties {
bool retain;
};
namespace AsyncMqttClientInternals {
typedef std::function<void(bool sessionPresent)> OnConnectUserCallback;

View File

@@ -95,8 +95,6 @@ class EMSESPSettingsService {
void begin();
private:
// HttpEndpoint<EMSESPSettings> _httpEndpoint;
// FSPersistence<EMSESPSettings> _fsPersistence;
};
#endif

View File

@@ -131,7 +131,6 @@ class AsyncWebServer {
}
void on(const char * uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest){};
};

View File

@@ -24,46 +24,13 @@ class FSPersistence {
}
void readFromFS() {
/*
File settingsFile = _fs->open(_filePath, "r");
if (settingsFile) {
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
DeserializationError error = deserializeJson(jsonDocument, settingsFile);
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
JsonObject jsonObject = jsonDocument.as<JsonObject>();
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
settingsFile.close();
return;
}
settingsFile.close();
}
*/
// If we reach here we have not been successful in loading the config,
// hard-coded emergency defaults are now applied.
applyDefaults();
}
bool writeToFS() {
// create and populate a new json object
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
JsonObject jsonObject = jsonDocument.to<JsonObject>();
_statefulService->read(jsonObject, _stateReader);
// serialize it to filesystem
/*
File settingsFile = _fs->open(_filePath, "w");
// failed to open file, return false
if (!settingsFile) {
return false;
}
// serialize the data to the file
serializeJson(jsonDocument, settingsFile);
settingsFile.close();
*/
return true;
}
@@ -90,8 +57,6 @@ class FSPersistence {
update_handler_id_t _updateHandlerId;
protected:
// We assume the updater supplies sensible defaults if an empty object
// is supplied, this virtual function allows that to be changed.
virtual void applyDefaults() {
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
JsonObject jsonObject = jsonDocument.as<JsonObject>();

View File

@@ -22,12 +22,6 @@ class HttpGetEndpoint {
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
/*
server->on(servicePath.c_str(),
HTTP_GET,
securityManager->wrapRequest(std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1),
authenticationPredicate));
*/
}
HttpGetEndpoint(JsonStateReader<T> stateReader,
@@ -36,9 +30,6 @@ class HttpGetEndpoint {
const String& servicePath,
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
/*
server->on(servicePath.c_str(), HTTP_GET, std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1));
*/
}
protected:
@@ -47,12 +38,6 @@ class HttpGetEndpoint {
size_t _bufferSize;
void fetchSettings(AsyncWebServerRequest* request) {
// AsyncJsonResponse* response = new AsyncJsonResponse(false, _bufferSize);
// JsonObject jsonObject = response->getRoot().to<JsonObject>();
// _statefulService->read(jsonObject, _stateReader);
// response->setLength();
// request->send(response);
}
};
@@ -70,17 +55,7 @@ class HttpPostEndpoint {
_stateReader(stateReader),
_stateUpdater(stateUpdater),
_statefulService(statefulService),
/*
_updateHandler(
servicePath,
securityManager->wrapCallback(
std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2),
authenticationPredicate),
bufferSize),
*/
_bufferSize(bufferSize) {
//_updateHandler.setMethod(HTTP_POST);
// server->addHandler(&_updateHandler);
}
HttpPostEndpoint(JsonStateReader<T> stateReader,
@@ -92,42 +67,26 @@ class HttpPostEndpoint {
_stateReader(stateReader),
_stateUpdater(stateUpdater),
_statefulService(statefulService),
/*
_updateHandler(servicePath,
std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2),
bufferSize),
*/
_bufferSize(bufferSize) {
// _updateHandler.setMethod(HTTP_POST);
// server->addHandler(&_updateHandler);
}
protected:
JsonStateReader<T> _stateReader;
JsonStateUpdater<T> _stateUpdater;
StatefulService<T>* _statefulService;
//AsyncCallbackJsonWebHandler _updateHandler;
size_t _bufferSize;
void updateSettings(AsyncWebServerRequest* request, JsonVariant& json) {
if (!json.is<JsonObject>()) {
// request->send(400);
return;
}
JsonObject jsonObject = json.as<JsonObject>();
StateUpdateResult outcome = _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
if (outcome == StateUpdateResult::ERROR) {
// request->send(400);
return;
}
if (outcome == StateUpdateResult::CHANGED) {
// request->onDisconnect([this]() { _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); });
}
// AsyncJsonResponse* response = new AsyncJsonResponse(false, _bufferSize);
// jsonObject = response->getRoot().to<JsonObject>();
// _statefulService->read(jsonObject, _stateReader);
// response->setLength();
// request->send(response);
}
};

View File

@@ -4,7 +4,6 @@
#include <Arduino.h>
#include <Features.h>
#include <ESPAsyncWebServer.h>
// #include <ESPUtils.h>
#include <AsyncJson.h>
#include <list>
@@ -27,36 +26,43 @@ class User {
bool admin;
public:
User(String username, String password, bool admin) : username(username), password(password), admin(admin) {
User(String username, String password, bool admin)
: username(username)
, password(password)
, admin(admin) {
}
};
class Authentication {
public:
User* user;
User * user;
boolean authenticated;
public:
Authentication(User& user) : user(new User(user)), authenticated(true) {
Authentication(User & user)
: user(new User(user))
, authenticated(true) {
}
Authentication() : user(nullptr), authenticated(false) {
Authentication()
: user(nullptr)
, authenticated(false) {
}
~Authentication() {
delete (user);
}
};
typedef std::function<boolean(Authentication& authentication)> AuthenticationPredicate;
typedef std::function<boolean(Authentication & authentication)> AuthenticationPredicate;
class AuthenticationPredicates {
public:
static bool NONE_REQUIRED(Authentication& authentication) {
static bool NONE_REQUIRED(Authentication & authentication) {
return true;
};
static bool IS_AUTHENTICATED(Authentication& authentication) {
static bool IS_AUTHENTICATED(Authentication & authentication) {
return authentication.authenticated;
};
static bool IS_ADMIN(Authentication& authentication) {
static bool IS_ADMIN(Authentication & authentication) {
return authentication.authenticated && authentication.user->admin;
};
};
@@ -64,39 +70,14 @@ class AuthenticationPredicates {
class SecurityManager {
public:
#if FT_ENABLED(FT_SECURITY)
/*
* Authenticate, returning the user if found
*/
virtual Authentication authenticate(const String& username, const String& password) = 0;
/*
* Generate a JWT for the user provided
*/
virtual String generateJWT(User* user) = 0;
virtual Authentication authenticate(const String & username, const String & password) = 0;
virtual String generateJWT(User * user) = 0;
#endif
/*
* Check the request header for the Authorization token
*/
virtual Authentication authenticateRequest(AsyncWebServerRequest* request) = 0;
/**
* Filter a request with the provided predicate, only returning true if the predicate matches.
*/
virtual Authentication authenticateRequest(AsyncWebServerRequest * request) = 0;
virtual ArRequestFilterFunction filterRequest(AuthenticationPredicate predicate) = 0;
/**
* Wrap the provided request to provide validation against an AuthenticationPredicate.
*/
virtual ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest,
AuthenticationPredicate predicate) = 0;
/**
* Wrap the provided json request callback to provide validation against an AuthenticationPredicate.
*/
virtual ArJsonRequestHandlerFunction wrapCallback(ArJsonRequestHandlerFunction onRequest,
AuthenticationPredicate predicate) = 0;
virtual ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest, AuthenticationPredicate predicate) = 0;
virtual ArJsonRequestHandlerFunction wrapCallback(ArJsonRequestHandlerFunction onRequest, AuthenticationPredicate predicate) = 0;
};
#endif // end SecurityManager_h

View File

@@ -34,7 +34,7 @@ class SecuritySettings {
String jwtSecret;
std::list<User> users;
static void read(SecuritySettings& settings, JsonObject& root) {
static void read(SecuritySettings & settings, JsonObject & root) {
// secret
root["jwt_secret"] = settings.jwtSecret;
@@ -48,7 +48,7 @@ class SecuritySettings {
}
}
static StateUpdateResult update(JsonObject& root, SecuritySettings& settings) {
static StateUpdateResult update(JsonObject & root, SecuritySettings & settings) {
// secret
settings.jwtSecret = root["jwt_secret"] | FACTORY_JWT_SECRET;
@@ -68,14 +68,14 @@ class SecuritySettings {
class SecuritySettingsService : public StatefulService<SecuritySettings>, public SecurityManager {
public:
SecuritySettingsService(AsyncWebServer* server, FS* fs);
SecuritySettingsService(AsyncWebServer * server, FS * fs);
void begin();
// Functions to implement SecurityManager
Authentication authenticate(const String& username, const String& password);
Authentication authenticateRequest(AsyncWebServerRequest* request);
String generateJWT(User* user);
Authentication authenticate(const String & username, const String & password);
Authentication authenticateRequest(AsyncWebServerRequest * request);
String generateJWT(User * user);
ArRequestFilterFunction filterRequest(AuthenticationPredicate predicate);
ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest, AuthenticationPredicate predicate);
ArJsonRequestHandlerFunction wrapCallback(ArJsonRequestHandlerFunction callback, AuthenticationPredicate predicate);
@@ -86,27 +86,19 @@ class SecuritySettingsService : public StatefulService<SecuritySettings>, public
ArduinoJsonJWT _jwtHandler;
void configureJWTHandler();
/*
* Lookup the user by JWT
*/
Authentication authenticateJWT(String& jwt);
/*
* Verify the payload is correct
*/
boolean validatePayload(JsonObject& parsedPayload, User* user);
Authentication authenticateJWT(String & jwt);
boolean validatePayload(JsonObject & parsedPayload, User * user);
};
#else
class SecuritySettingsService : public SecurityManager {
public:
SecuritySettingsService(AsyncWebServer* server, FS* fs);
SecuritySettingsService(AsyncWebServer * server, FS * fs);
~SecuritySettingsService();
// minimal set of functions to support framework with security settings disabled
Authentication authenticateRequest(AsyncWebServerRequest* request);
Authentication authenticateRequest(AsyncWebServerRequest * request);
ArRequestFilterFunction filterRequest(AuthenticationPredicate predicate);
ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest, AuthenticationPredicate predicate);
ArJsonRequestHandlerFunction wrapCallback(ArJsonRequestHandlerFunction onRequest, AuthenticationPredicate predicate);

View File

@@ -18,21 +18,23 @@ enum class StateUpdateResult {
};
template <typename T>
using JsonStateUpdater = std::function<StateUpdateResult(JsonObject& root, T& settings)>;
using JsonStateUpdater = std::function<StateUpdateResult(JsonObject & root, T & settings)>;
template <typename T>
using JsonStateReader = std::function<void(T& settings, JsonObject& root)>;
using JsonStateReader = std::function<void(T & settings, JsonObject & root)>;
typedef size_t update_handler_id_t;
typedef std::function<void(const String& originId)> StateUpdateCallback;
typedef std::function<void(const String & originId)> StateUpdateCallback;
typedef struct StateUpdateHandlerInfo {
static update_handler_id_t currentUpdatedHandlerId;
update_handler_id_t _id;
StateUpdateCallback _cb;
bool _allowRemove;
StateUpdateHandlerInfo(StateUpdateCallback cb, bool allowRemove) :
_id(++currentUpdatedHandlerId), _cb(cb), _allowRemove(allowRemove){};
StateUpdateHandlerInfo(StateUpdateCallback cb, bool allowRemove)
: _id(++currentUpdatedHandlerId)
, _cb(cb)
, _allowRemove(allowRemove){};
} StateUpdateHandlerInfo_t;
template <class T>
@@ -40,11 +42,13 @@ class StatefulService {
public:
template <typename... Args>
#ifdef ESP32
StatefulService(Args&&... args) :
_state(std::forward<Args>(args)...), _accessMutex(xSemaphoreCreateRecursiveMutex()) {
StatefulService(Args &&... args)
: _state(std::forward<Args>(args)...)
, _accessMutex(xSemaphoreCreateRecursiveMutex()) {
}
#else
StatefulService(Args&&... args) : _state(std::forward<Args>(args)...) {
StatefulService(Args &&... args)
: _state(std::forward<Args>(args)...) {
}
#endif
@@ -67,7 +71,7 @@ class StatefulService {
}
}
StateUpdateResult update(std::function<StateUpdateResult(T&)> stateUpdater, const String& originId) {
StateUpdateResult update(std::function<StateUpdateResult(T &)> stateUpdater, const String & originId) {
beginTransaction();
StateUpdateResult result = stateUpdater(_state);
endTransaction();
@@ -77,14 +81,14 @@ class StatefulService {
return result;
}
StateUpdateResult updateWithoutPropagation(std::function<StateUpdateResult(T&)> stateUpdater) {
StateUpdateResult updateWithoutPropagation(std::function<StateUpdateResult(T &)> stateUpdater) {
beginTransaction();
StateUpdateResult result = stateUpdater(_state);
endTransaction();
return result;
}
StateUpdateResult update(JsonObject& jsonObject, JsonStateUpdater<T> stateUpdater, const String& originId) {
StateUpdateResult update(JsonObject & jsonObject, JsonStateUpdater<T> stateUpdater, const String & originId) {
beginTransaction();
StateUpdateResult result = stateUpdater(jsonObject, _state);
endTransaction();
@@ -94,27 +98,27 @@ class StatefulService {
return result;
}
StateUpdateResult updateWithoutPropagation(JsonObject& jsonObject, JsonStateUpdater<T> stateUpdater) {
StateUpdateResult updateWithoutPropagation(JsonObject & jsonObject, JsonStateUpdater<T> stateUpdater) {
beginTransaction();
StateUpdateResult result = stateUpdater(jsonObject, _state);
endTransaction();
return result;
}
void read(std::function<void(T&)> stateReader) {
void read(std::function<void(T &)> stateReader) {
beginTransaction();
stateReader(_state);
endTransaction();
}
void read(JsonObject& jsonObject, JsonStateReader<T> stateReader) {
void read(JsonObject & jsonObject, JsonStateReader<T> stateReader) {
beginTransaction();
stateReader(_state, jsonObject);
endTransaction();
}
void callUpdateHandlers(const String& originId) {
for (const StateUpdateHandlerInfo_t& updateHandler : _updateHandlers) {
void callUpdateHandlers(const String & originId) {
for (const StateUpdateHandlerInfo_t & updateHandler : _updateHandlers) {
updateHandler._cb(originId);
}
}