Rick Watson
5 years ago
24 changed files with 250 additions and 216 deletions
-
2interface/src/authentication/Authentication.js
-
88interface/src/components/RestComponent.js
-
5interface/src/containers/WiFiNetworkScanner.js
-
2src/APSettingsService.cpp
-
4src/APSettingsService.h
-
6src/ArduinoJsonJWT.cpp
-
2src/ArduinoJsonJWT.h
-
52src/AsyncAuthJsonWebHandler.h
-
4src/AuthenticationService.cpp
-
3src/AuthenticationService.h
-
2src/NTPSettingsService.cpp
-
4src/NTPSettingsService.h
-
2src/OTASettingsService.cpp
-
4src/OTASettingsService.h
-
48src/SecurityManager.cpp
-
47src/SecurityManager.h
-
35src/SecuritySettingsService.cpp
-
26src/SecuritySettingsService.h
-
90src/SettingsService.h
-
10src/WiFiScanner.cpp
-
3src/WiFiScanner.h
-
2src/WiFiSettingsService.cpp
-
4src/WiFiSettingsService.h
-
21src/main.cpp
@ -1,52 +0,0 @@ |
|||
#ifndef AsyncAuthJsonWebHandler_H_ |
|||
#define AsyncAuthJsonWebHandler_H_ |
|||
|
|||
#include <ESPAsyncWebServer.h> |
|||
#include <AsyncJsonWebHandler.h> |
|||
#include <ArduinoJson.h> |
|||
#include <SecurityManager.h> |
|||
|
|||
typedef std::function<void(AsyncWebServerRequest *request, JsonDocument &jsonDocument, Authentication &authentication)> AuthenticationJsonRequestCallback; |
|||
|
|||
/** |
|||
* Extends AsyncJsonWebHandler with a wrapper which verifies the user is authenticated. |
|||
* |
|||
* TODO - Extend with role checking support, possibly with a callback to verify the user. |
|||
*/ |
|||
class AsyncAuthJsonWebHandler: public AsyncJsonWebHandler { |
|||
|
|||
private: |
|||
SecurityManager *_securityManager; |
|||
using AsyncJsonWebHandler::onRequest; |
|||
|
|||
public: |
|||
|
|||
AsyncAuthJsonWebHandler() : |
|||
AsyncJsonWebHandler(), _securityManager(NULL) {} |
|||
|
|||
~AsyncAuthJsonWebHandler() {} |
|||
|
|||
void setSecurityManager(SecurityManager *securityManager) { |
|||
_securityManager = securityManager; |
|||
} |
|||
|
|||
void onRequest(AuthenticationJsonRequestCallback callback) { |
|||
AsyncJsonWebHandler::onRequest([this, callback](AsyncWebServerRequest *request, JsonDocument &jsonDocument) { |
|||
if(!_securityManager) { |
|||
Serial.print("Security manager not configured for endpoint: "); |
|||
Serial.println(_uri); |
|||
request->send(500); |
|||
return; |
|||
} |
|||
Authentication authentication = _securityManager->authenticateRequest(request); |
|||
if (!authentication.isAuthenticated()) { |
|||
request->send(401); |
|||
return; |
|||
} |
|||
callback(request, jsonDocument, authentication); |
|||
}); |
|||
} |
|||
|
|||
}; |
|||
|
|||
#endif // end AsyncAuthJsonWebHandler_H_ |
@ -0,0 +1,35 @@ |
|||
#include <SecuritySettingsService.h>
|
|||
|
|||
SecuritySettingsService::SecuritySettingsService(AsyncWebServer* server, FS* fs) : AdminSettingsService(server, fs, this, SECURITY_SETTINGS_PATH, SECURITY_SETTINGS_FILE), SecurityManager() {} |
|||
SecuritySettingsService::~SecuritySettingsService() {} |
|||
|
|||
void SecuritySettingsService::readFromJsonObject(JsonObject& root) { |
|||
// secret
|
|||
_jwtHandler.setSecret(root["jwt_secret"] | DEFAULT_JWT_SECRET); |
|||
|
|||
// users
|
|||
_users.clear(); |
|||
if (root["users"].is<JsonArray>()) { |
|||
for (JsonVariant user : root["users"].as<JsonArray>()) { |
|||
_users.push_back(User(user["username"], user["password"], user["admin"])); |
|||
} |
|||
} |
|||
} |
|||
|
|||
void SecuritySettingsService::writeToJsonObject(JsonObject& root) { |
|||
// secret
|
|||
root["jwt_secret"] = _jwtHandler.getSecret(); |
|||
|
|||
// users
|
|||
JsonArray users = root.createNestedArray("users"); |
|||
for (User _user : _users) { |
|||
JsonObject user = users.createNestedObject(); |
|||
user["username"] = _user.getUsername(); |
|||
user["password"] = _user.getPassword(); |
|||
user["admin"] = _user.isAdmin(); |
|||
} |
|||
} |
|||
|
|||
void SecuritySettingsService::begin() { |
|||
readFromFS(); |
|||
} |
@ -0,0 +1,26 @@ |
|||
#ifndef SecuritySettingsService_h |
|||
#define SecuritySettingsService_h |
|||
|
|||
#include <SettingsService.h> |
|||
#include <SecurityManager.h> |
|||
|
|||
#define SECURITY_SETTINGS_FILE "/config/securitySettings.json" |
|||
#define SECURITY_SETTINGS_PATH "/rest/securitySettings" |
|||
|
|||
class SecuritySettingsService : public AdminSettingsService, public SecurityManager { |
|||
|
|||
public: |
|||
|
|||
SecuritySettingsService(AsyncWebServer* server, FS* fs); |
|||
~SecuritySettingsService(); |
|||
|
|||
void begin(); |
|||
|
|||
protected: |
|||
|
|||
void readFromJsonObject(JsonObject& root); |
|||
void writeToJsonObject(JsonObject& root); |
|||
|
|||
}; |
|||
|
|||
#endif // end SecuritySettingsService_h |
Write
Preview
Loading…
Cancel
Save
Reference in new issue