diff --git a/CMakeLists.txt b/CMakeLists.txt index fe15290..8115ac7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,7 +68,7 @@ add_executable(owsub src/APIServers.cpp src/Daemon.cpp src/Daemon.h src/Dashboard.h src/Dashboard.cpp - src/StorageService.cpp src/StorageService.h src/RESTAPI/RESTAPI_internetConnection_handler.cpp src/RESTAPI/RESTAPI_internetConnection_handler.h src/RESTAPI/RESTAPI_homeDeviceMode_handler.cpp src/RESTAPI/RESTAPI_homeDeviceMode_handler.h src/RESTAPI/RESTAPI_ipReservations_handler.cpp src/RESTAPI/RESTAPI_ipReservations_handler.h src/RESTAPI/RESTAPI_wifiNetworks_handler.cpp src/RESTAPI/RESTAPI_wifiNetworks_handler.h src/RESTAPI/RESTAPI_wiredClients_handler.cpp src/RESTAPI/RESTAPI_wiredClients_handler.h src/RESTAPI/RESTAPI_wifiClients_handler.cpp src/RESTAPI/RESTAPI_wifiClients_handler.h src/RESTAPI/RESTAPI_subscriberDevices_handler.cpp src/RESTAPI/RESTAPI_subscriberDevices_handler.h) + src/StorageService.cpp src/StorageService.h src/RESTAPI/RESTAPI_internetConnection_handler.cpp src/RESTAPI/RESTAPI_internetConnection_handler.h src/RESTAPI/RESTAPI_homeDeviceMode_handler.cpp src/RESTAPI/RESTAPI_homeDeviceMode_handler.h src/RESTAPI/RESTAPI_ipReservations_handler.cpp src/RESTAPI/RESTAPI_ipReservations_handler.h src/RESTAPI/RESTAPI_wifiNetworks_handler.cpp src/RESTAPI/RESTAPI_wifiNetworks_handler.h src/RESTAPI/RESTAPI_wiredClients_handler.cpp src/RESTAPI/RESTAPI_wiredClients_handler.h src/RESTAPI/RESTAPI_wifiClients_handler.cpp src/RESTAPI/RESTAPI_wifiClients_handler.h src/RESTAPI/RESTAPI_subscriberDevices_handler.cpp src/RESTAPI/RESTAPI_subscriberDevices_handler.h src/RESTObjects/RESTAPI_SubObjects.cpp src/RESTObjects/RESTAPI_SubObjects.h) target_link_libraries(owsub PUBLIC ${Poco_LIBRARIES} ${MySQL_LIBRARIES} diff --git a/build b/build index eb13855..e3f1e9b 100644 --- a/build +++ b/build @@ -1 +1 @@ -79 \ No newline at end of file +80 \ No newline at end of file diff --git a/openapi/userportal.yaml b/openapi/userportal.yaml index 8855f4a..1b9ebf6 100644 --- a/openapi/userportal.yaml +++ b/openapi/userportal.yaml @@ -78,13 +78,19 @@ components: type: integer schemas: - HomeDeviceModeAutomatic: + HomeDeviceMode: type: object properties: type: type: string enum: + - bridge + - manual - automatic + default: automatic + enableLEDS: + type: boolean + default: true subnet: type: string format: ipv4 @@ -98,34 +104,6 @@ components: type: string format: ipv4 - HomeDeviceModeManual: - type: object - properties: - type: - type: string - enum: - - manual - - HomeDeviceModeBridge: - type: object - properties: - type: - type: string - enum: - - bridge - - HomeDeviceMode: - type: object - properties: - enableLEDS: - type: boolean - default: true - type: - oneOf: - - $ref: '#/components/schemas/HomeDeviceModeAutomatic' - - $ref: '#/components/schemas/HomeDeviceModeManual' - - $ref: '#/components/schemas/HomeDeviceModeBridge' - IPReservation: type: object properties: @@ -145,43 +123,26 @@ components: type: boolean custom: type: boolean - customSettings: - type: object - properties: - primary: - type: string - format: ipv4 - seconfary: - type: string - format: ipv4 + primary: + type: string + format: ipv4 + seconfary: + type: string + format: ipv4 - InternetConnectionAutomatic: + InternetConnection: type: object properties: - connection: - type: string - enum: - - automatic - - InternetConnectionPPPoE: - type: object - properties: - connection: + type: type: string enum: + - manual - pppoe + - automatic username: type: string password: type: string - - InternetConnectionManual: - type: object - properties: - connection: - type: string - enum: - - manual ipAddress: type: string format: ipv4 @@ -198,15 +159,6 @@ components: type: string format: ipv4 - InternetConnection: - type: object - properties: - type: - oneOf: - - $ref: '#/components/schemas/InternetConnectionAutomatic' - - $ref: '#/components/schemas/InternetConnectionPPPoE' - - $ref: '#/components/schemas/InternetConnectionManual' - WifiNetwork: type: object properties: @@ -221,7 +173,7 @@ components: type: string encryption: type: string - band: + bands: type: array items: type: string diff --git a/src/RESTAPI/RESTAPI_homeDeviceMode_handler.h b/src/RESTAPI/RESTAPI_homeDeviceMode_handler.h index d68f4dd..62d8fd4 100644 --- a/src/RESTAPI/RESTAPI_homeDeviceMode_handler.h +++ b/src/RESTAPI/RESTAPI_homeDeviceMode_handler.h @@ -21,6 +21,14 @@ namespace OpenWifi { static const std::list PathName() { return std::list{"/api/v1/homeDeviceMode"}; }; + inline bool RoleIsAuthorized(std::string & Reason) { + if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } + void DoGet() final; void DoPost() final {}; void DoPut() final; diff --git a/src/RESTAPI/RESTAPI_internetConnection_handler.h b/src/RESTAPI/RESTAPI_internetConnection_handler.h index e008b90..7efa533 100644 --- a/src/RESTAPI/RESTAPI_internetConnection_handler.h +++ b/src/RESTAPI/RESTAPI_internetConnection_handler.h @@ -21,6 +21,14 @@ namespace OpenWifi { static const std::list PathName() { return std::list{"/api/v1/internetConnection"}; }; + inline bool RoleIsAuthorized(std::string & Reason) { + if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } + void DoGet() final; void DoPost() final {}; void DoPut() final; diff --git a/src/RESTAPI/RESTAPI_ipReservations_handler.h b/src/RESTAPI/RESTAPI_ipReservations_handler.h index 328450a..81e36aa 100644 --- a/src/RESTAPI/RESTAPI_ipReservations_handler.h +++ b/src/RESTAPI/RESTAPI_ipReservations_handler.h @@ -21,6 +21,14 @@ namespace OpenWifi { static const std::list PathName() { return std::list{"/api/v1/ipReservations"}; }; + inline bool RoleIsAuthorized(std::string & Reason) { + if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } + void DoGet() final; void DoPost() final {}; void DoPut() final; diff --git a/src/RESTAPI/RESTAPI_subscriberDevices_handler.h b/src/RESTAPI/RESTAPI_subscriberDevices_handler.h index c8c6be0..651bd95 100644 --- a/src/RESTAPI/RESTAPI_subscriberDevices_handler.h +++ b/src/RESTAPI/RESTAPI_subscriberDevices_handler.h @@ -21,6 +21,14 @@ namespace OpenWifi { static const std::list PathName() { return std::list{"/api/v1/subscriberDevices"}; }; + inline bool RoleIsAuthorized(std::string & Reason) { + if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } + void DoGet() final; void DoPost() final {}; void DoPut() final; diff --git a/src/RESTAPI/RESTAPI_wifiClients_handler.h b/src/RESTAPI/RESTAPI_wifiClients_handler.h index 887b758..12d6ebb 100644 --- a/src/RESTAPI/RESTAPI_wifiClients_handler.h +++ b/src/RESTAPI/RESTAPI_wifiClients_handler.h @@ -20,6 +20,14 @@ namespace OpenWifi { static const std::list PathName() { return std::list{"/api/v1/wifiClients"}; }; + inline bool RoleIsAuthorized(std::string & Reason) { + if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } + void DoGet() final; void DoPost() final {}; void DoPut() final {}; diff --git a/src/RESTAPI/RESTAPI_wifiNetworks_handler.h b/src/RESTAPI/RESTAPI_wifiNetworks_handler.h index 57758bd..69eec65 100644 --- a/src/RESTAPI/RESTAPI_wifiNetworks_handler.h +++ b/src/RESTAPI/RESTAPI_wifiNetworks_handler.h @@ -21,6 +21,14 @@ namespace OpenWifi { static const std::list PathName() { return std::list{"/api/v1/wifiNetworks"}; }; + inline bool RoleIsAuthorized(std::string & Reason) { + if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } + void DoGet() final; void DoPost() final {}; void DoPut() final; diff --git a/src/RESTAPI/RESTAPI_wiredClients_handler.h b/src/RESTAPI/RESTAPI_wiredClients_handler.h index 4652a2e..2989565 100644 --- a/src/RESTAPI/RESTAPI_wiredClients_handler.h +++ b/src/RESTAPI/RESTAPI_wiredClients_handler.h @@ -20,6 +20,14 @@ namespace OpenWifi { static const std::list PathName() { return std::list{"/api/v1/wiredClients"}; }; + inline bool RoleIsAuthorized(std::string & Reason) { + if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } + void DoGet() final; void DoPost() final {}; void DoPut() final {}; diff --git a/src/RESTObjects/RESTAPI_SubObjects.cpp b/src/RESTObjects/RESTAPI_SubObjects.cpp new file mode 100644 index 0000000..9a1701d --- /dev/null +++ b/src/RESTObjects/RESTAPI_SubObjects.cpp @@ -0,0 +1,286 @@ +// +// Created by stephane bourque on 2021-10-27. +// + +#include "RESTAPI_SubObjects.h" +#include "framework/MicroService.h" + +using OpenWifi::RESTAPI_utils::field_to_json; +using OpenWifi::RESTAPI_utils::field_from_json; + + +namespace OpenWifi { + + void HomeDeviceMode::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "enableLEDS", enableLEDS); + field_to_json(Obj, "type", type); + field_to_json(Obj, "subnet", subnet); + field_to_json(Obj, "subnetMask", subnetMask); + field_to_json(Obj, "startIP", startIP); + field_to_json(Obj, "endIP", endIP); + } + + bool HomeDeviceMode::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "enableLEDS", enableLEDS); + field_from_json(Obj, "type", type); + field_from_json(Obj, "subnet", subnet); + field_from_json(Obj, "subnetMask", subnetMask); + field_from_json(Obj, "startIP", startIP); + field_from_json(Obj, "endIP", endIP); + return true; + } catch (...) { + } + return false; + } + + void IPReservation::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "nickname", nickname); + field_to_json(Obj, "ipAddress", ipAddress); + field_to_json(Obj, "macAddress", macAddress); + } + + bool IPReservation::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "nickname", nickname); + field_from_json(Obj, "ipAddress", ipAddress); + field_from_json(Obj, "macAddress", macAddress); + return true; + } catch (...) { + } + return false; + } + + void DnsConfiguration::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "ISP", ISP); + field_to_json(Obj, "custom", custom); + field_to_json(Obj, "primary", primary); + field_to_json(Obj, "secondary", secondary); + } + + bool DnsConfiguration::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "ISP", ISP); + field_from_json(Obj, "custom", custom); + field_from_json(Obj, "primary", primary); + field_from_json(Obj, "secondary", secondary); + return true; + } catch (...) { + } + return false; + } + + void InternetConnection::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "type", type); + field_to_json(Obj, "username", username); + field_to_json(Obj, "password", password); + field_to_json(Obj, "ipAddress", ipAddress); + field_to_json(Obj, "subNetMask", subNetMask); + field_to_json(Obj, "defaultGateway", defaultGateway); + field_to_json(Obj, "primaryDns", primaryDns); + field_to_json(Obj, "secondaryDns", secondaryDns); + } + + bool InternetConnection::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "type", type); + field_from_json(Obj, "username", username); + field_from_json(Obj, "password", password); + field_from_json(Obj, "ipAddress", ipAddress); + field_from_json(Obj, "subNetMask", subNetMask); + field_from_json(Obj, "defaultGateway", defaultGateway); + field_from_json(Obj, "primaryDns", primaryDns); + field_from_json(Obj, "secondaryDns", secondaryDns); + return true; + } catch (...) { + } + return false; + } + + void WifiNetwork::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "type", type); + field_to_json(Obj, "name", name); + field_to_json(Obj, "password", password); + field_to_json(Obj, "encryption", encryption); + field_to_json(Obj, "bands", bands); + } + + bool WifiNetwork::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "type", type); + field_from_json(Obj, "name", name); + field_from_json(Obj, "password", password); + field_from_json(Obj, "encryption", encryption); + field_from_json(Obj, "bands", bands); + return true; + } catch (...) { + } + return false; + } + + void WifiNetworkList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "wifiNetworks", wifiNetworks); + } + + bool WifiNetworkList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "wifiNetworks", wifiNetworks); + return true; + } catch (...) { + } + return false; + } + + void AccessTime::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "day", day); + field_to_json(Obj, "rangeList", rangeList); + } + + bool AccessTime::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "day", day); + field_from_json(Obj, "rangeList", rangeList); + return true; + } catch (...) { + } + return false; + } + + void AccessTimes::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "schedule", schedule); + } + + bool AccessTimes::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "schedule", schedule); + return true; + } catch (...) { + } + return false; + } + + void SubscriberDevice::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "macAddress", macAddress); + field_to_json(Obj, "manufacturer", manufacturer); + field_to_json(Obj, "firstContact", firstContact); + field_to_json(Obj, "lastContact", lastContact); + field_to_json(Obj, "group", group); + field_to_json(Obj, "icon", icon); + field_to_json(Obj, "suspended", suspended); + field_to_json(Obj, "ip", ip); + field_to_json(Obj, "schedule", schedule); + } + + bool SubscriberDevice::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "macAddress", macAddress); + field_from_json(Obj, "manufacturer", manufacturer); + field_from_json(Obj, "firstContact", firstContact); + field_from_json(Obj, "lastContact", lastContact); + field_from_json(Obj, "group", group); + field_from_json(Obj, "icon", icon); + field_from_json(Obj, "suspended", suspended); + field_from_json(Obj, "ip", ip); + field_from_json(Obj, "schedule", schedule); + return true; + } catch (...) { + } + return false; + } + + void SubscriberDeviceList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "devices", devices); + } + + bool SubscriberDeviceList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "devices", devices); + return true; + } catch (...) { + } + return false; + } + + void Association::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "name", name); + field_to_json(Obj, "macAddress", macAddress); + field_to_json(Obj, "rssi", rssi); + field_to_json(Obj, "power", power); + field_to_json(Obj, "ipv4", ipv4); + field_to_json(Obj, "ipv6", ipv6); + field_to_json(Obj, "tx", tx); + field_to_json(Obj, "rx", rx); + } + + bool Association::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "name", name); + field_from_json(Obj, "macAddress", macAddress); + field_from_json(Obj, "rssi", rssi); + field_from_json(Obj, "power", power); + field_from_json(Obj, "ipv4", ipv4); + field_from_json(Obj, "ipv6", ipv6); + field_from_json(Obj, "tx", tx); + field_from_json(Obj, "rx", rx); + return true; + } catch (...) { + } + return false; + } + + void AssociationList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "associations", associations); + } + + bool AssociationList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "associations", associations); + return true; + } catch (...) { + } + return false; + } + + void Client::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "macAddress", macAddress); + field_to_json(Obj, "speed", speed); + field_to_json(Obj, "mode", mode); + field_to_json(Obj, "ipv4", ipv4); + field_to_json(Obj, "ipv6", ipv6); + field_to_json(Obj, "tx", tx); + field_to_json(Obj, "rx", rx); + } + + bool Client::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "macAddress", macAddress); + field_from_json(Obj, "speed", speed); + field_from_json(Obj, "mode", mode); + field_from_json(Obj, "ipv4", ipv4); + field_from_json(Obj, "ipv6", ipv6); + field_from_json(Obj, "tx", tx); + field_from_json(Obj, "rx", rx); + return true; + } catch (...) { + } + return false; + } + + void ClientList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "clients", clients); + } + + bool ClientList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "clients", clients); + return true; + } catch (...) { + } + return false; + } + +} \ No newline at end of file diff --git a/src/RESTObjects/RESTAPI_SubObjects.h b/src/RESTObjects/RESTAPI_SubObjects.h new file mode 100644 index 0000000..0f73428 --- /dev/null +++ b/src/RESTObjects/RESTAPI_SubObjects.h @@ -0,0 +1,159 @@ +// +// Created by stephane bourque on 2021-10-27. +// + +#ifndef OWSUB_RESTAPI_SUBOBJECTS_H +#define OWSUB_RESTAPI_SUBOBJECTS_H + +#include + +#include "Poco/JSON/Object.h" + +namespace OpenWifi { + + struct HomeDeviceMode { + bool enableLEDS = true; + std::string type; // bridge, manual, automatic + std::string subnet; + std::string subnetMask; + std::string startIP; + std::string endIP; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct IPReservation { + std::string nickname; + std::string ipAddress; + std::string macAddress; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct DnsConfiguration { + bool ISP; + bool custom; + std::string primary; + std::string secondary; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct InternetConnection { + std::string type; // automatic, pppoe, manual + std::string username; + std::string password; + std::string ipAddress; + std::string subNetMask; + std::string defaultGateway; + std::string primaryDns; + std::string secondaryDns; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct WifiNetwork { + std::string type; // main, guest + std::string name; + std::string password; + std::string encryption; + std::vector bands; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct WifiNetworkList { + std::vector wifiNetworks; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct AccessTime { + std::string day; + std::vector rangeList; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct AccessTimes { + std::vector schedule; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct SubscriberDevice { + std::string name; + std::string description; + std::string macAddress; + std::string manufacturer; + uint64_t firstContact; + uint64_t lastContact; + std::string group; + std::string icon; + bool suspended; + std::string ip; + std::vector schedule; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct SubscriberDeviceList { + std::vector devices; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct Association { + std::string name; + std::string macAddress; + int rssi; + int power; + std::string ipv4; + std::string ipv6; + uint64_t tx; + uint64_t rx; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct AssociationList { + std::vector associations; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct Client { + std::string macAddress; + std::string speed; + std::string mode; + std::string ipv4; + std::string ipv6; + uint64_t tx; + uint64_t rx; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ClientList { + std::vector clients; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + +} + +#endif //OWSUB_RESTAPI_SUBOBJECTS_H diff --git a/src/framework/MicroService.h b/src/framework/MicroService.h index b144c37..c6f6893 100644 --- a/src/framework/MicroService.h +++ b/src/framework/MicroService.h @@ -204,6 +204,10 @@ namespace OpenWifi::RESTAPI_utils { Obj.set(Field, Arr); } + inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, int Value) { + Obj.set(Field, Value); + } + template void field_to_json(Poco::JSON::Object &Obj, const char *Field, const T &Value) { Poco::JSON::Object Answer; Value.to_json(Answer); @@ -222,6 +226,12 @@ namespace OpenWifi::RESTAPI_utils { } } + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, int &Value) { + if(Obj->isObject(Field)) { + Value = Obj->get(Field); + } + } + template void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, T &Value) { if(Obj->isObject(Field)) { Poco::JSON::Object::Ptr A = Obj->getObject(Field); @@ -1262,6 +1272,10 @@ namespace OpenWifi { RESTAPIHandler(BindingMap map, Poco::Logger &l, std::vector Methods, RESTAPI_GenericServer & Server, bool Internal=false, bool AlwaysAuthorize=true) : Bindings_(std::move(map)), Logger_(l), Methods_(std::move(Methods)), Server_(Server), Internal_(Internal), AlwaysAuthorize_(AlwaysAuthorize) {} + inline bool RoleIsAuthorized(const std::string & Path, const std::string & Method, std::string & Reason) { + return true; + } + inline void handleRequest(Poco::Net::HTTPServerRequest &RequestIn, Poco::Net::HTTPServerResponse &ResponseIn) final { try { @@ -1271,8 +1285,15 @@ namespace OpenWifi { if (!ContinueProcessing()) return; - if (AlwaysAuthorize_ && !IsAuthorized()) + if (AlwaysAuthorize_ && !IsAuthorized()) { return; + } + + std::string Reason; + if(!RoleIsAuthorized(RequestIn.getURI(), Request->getMethod(), Reason)) { + UnAuthorized(Reason); + return; + } ParseParameters(); if (Request->getMethod() == Poco::Net::HTTPRequest::HTTP_GET) @@ -3084,7 +3105,7 @@ namespace OpenWifi { Internal) {} static const std::list PathName() { return std::list{"/api/v1/system"};} - inline void DoGet() { + inline void DoGet() { std::string Arg; if(HasParameter("command",Arg) && Arg=="info") { Poco::JSON::Object Answer;