mirror of
				https://github.com/Telecominfraproject/wlan-cloud-ucentralsec.git
				synced 2025-10-29 18:02:29 +00:00 
			
		
		
		
	Adding proper security logging.
This commit is contained in:
		| @@ -74,7 +74,10 @@ add_executable( owsec | ||||
|                 src/RESTAPI_avatarHandler.cpp src/RESTAPI_avatarHandler.h | ||||
|                 src/storage_avatar.cpp src/storage_avatar.h src/storage_users.h | ||||
|                 src/OpenWifiTypes.h src/RESTAPI_email_handler.cpp src/RESTAPI_email_handler.h | ||||
|         src/storage_tokens.cpp) | ||||
|                 src/storage_tokens.cpp | ||||
|                 src/RESTAPI_GenericServer.h src/RESTAPI_GenericServer.cpp | ||||
|                 src/RESTAPI_errors.h | ||||
|                 ) | ||||
|  | ||||
| if(NOT SMALL_BUILD) | ||||
|     target_link_libraries(owsec PUBLIC | ||||
|   | ||||
| @@ -10,7 +10,7 @@ | ||||
| namespace OpenWifi { | ||||
|     class RESTAPI_AssetServer : public RESTAPIHandler { | ||||
|     public: | ||||
|         RESTAPI_AssetServer(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) | ||||
|         RESTAPI_AssetServer(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer &Server, bool Internal) | ||||
|                 : RESTAPIHandler(bindings, L, | ||||
|                                  std::vector<std::string> | ||||
|                                          {Poco::Net::HTTPRequest::HTTP_POST, | ||||
| @@ -18,6 +18,7 @@ namespace OpenWifi { | ||||
|                                           Poco::Net::HTTPRequest::HTTP_PUT, | ||||
|                                           Poco::Net::HTTPRequest::HTTP_DELETE, | ||||
|                                           Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
|                                           Server, | ||||
|                                           Internal) {} | ||||
|         static const std::list<const char *> PathName() { return std::list<const char *>{"/wwwassets/{id}" , | ||||
|                                                                                          "/favicon.ico"}; }; | ||||
|   | ||||
							
								
								
									
										5
									
								
								src/RESTAPI_GenericServer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								src/RESTAPI_GenericServer.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | ||||
| // | ||||
| // Created by stephane bourque on 2021-09-15. | ||||
| // | ||||
|  | ||||
| #include "RESTAPI_GenericServer.h" | ||||
							
								
								
									
										78
									
								
								src/RESTAPI_GenericServer.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								src/RESTAPI_GenericServer.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,78 @@ | ||||
| // | ||||
| // Created by stephane bourque on 2021-09-15. | ||||
| // | ||||
|  | ||||
| #ifndef OWPROV_RESTAPI_GENERICSERVER_H | ||||
| #define OWPROV_RESTAPI_GENERICSERVER_H | ||||
|  | ||||
| #include <vector> | ||||
| #include <string> | ||||
|  | ||||
| #include "Daemon.h" | ||||
| #include "Poco/StringTokenizer.h" | ||||
| #include "Poco/Net/HTTPRequest.h" | ||||
|  | ||||
| namespace OpenWifi { | ||||
|  | ||||
|     class RESTAPI_GenericServer { | ||||
|     public: | ||||
|  | ||||
|         enum { | ||||
|             LOG_GET=0, | ||||
|             LOG_DELETE, | ||||
|             LOG_PUT, | ||||
|             LOG_POST | ||||
|         }; | ||||
|  | ||||
|         void inline SetFlags(bool External, const std::string &Methods) { | ||||
|             Poco::StringTokenizer   Tokens(Methods,","); | ||||
|             auto Offset = (External ? 0 : 4); | ||||
|             for(const auto &i:Tokens) { | ||||
|                 if(Poco::icompare(i,Poco::Net::HTTPRequest::HTTP_DELETE)==0) | ||||
|                     LogFlags_[Offset+LOG_DELETE]=true; | ||||
|                 else if(Poco::icompare(i,Poco::Net::HTTPRequest::HTTP_PUT)==0) | ||||
|                     LogFlags_[Offset+LOG_PUT]=true; | ||||
|                 else if(Poco::icompare(i,Poco::Net::HTTPRequest::HTTP_POST)==0) | ||||
|                     LogFlags_[Offset+LOG_POST]=true; | ||||
|                 else if(Poco::icompare(i,Poco::Net::HTTPRequest::HTTP_GET)==0) | ||||
|                     LogFlags_[Offset+LOG_GET]=true; | ||||
|             } | ||||
|         } | ||||
|         inline void InitLogging() { | ||||
|             std::string Public = Daemon()->ConfigGetString("apilogging.public.methods","PUT,POST,DELETE"); | ||||
|             SetFlags(true, Public); | ||||
|             std::string Private = Daemon()->ConfigGetString("apilogging.private.methods","PUT,POST,DELETE"); | ||||
|             SetFlags(false, Private); | ||||
|  | ||||
|             std::string PublicBadTokens = Daemon()->ConfigGetString("apilogging.public.badtokens.methods",""); | ||||
|             LogBadTokens_[0] = (Poco::icompare(PublicBadTokens,"true")==0); | ||||
|             std::string PrivateBadTokens = Daemon()->ConfigGetString("apilogging.private.badtokens.methods",""); | ||||
|             LogBadTokens_[1] = (Poco::icompare(PrivateBadTokens,"true")==0); | ||||
|         } | ||||
|  | ||||
|         [[nodiscard]] inline bool LogIt(const std::string &Method, bool External) const { | ||||
|             auto Offset = (External ? 0 : 4); | ||||
|             if(Method == Poco::Net::HTTPRequest::HTTP_GET) | ||||
|                 return LogFlags_[Offset+LOG_GET]; | ||||
|             if(Method == Poco::Net::HTTPRequest::HTTP_POST) | ||||
|                 return LogFlags_[Offset+LOG_POST]; | ||||
|             if(Method == Poco::Net::HTTPRequest::HTTP_PUT) | ||||
|                 return LogFlags_[Offset+LOG_PUT]; | ||||
|             if(Method == Poco::Net::HTTPRequest::HTTP_DELETE) | ||||
|                 return LogFlags_[Offset+LOG_DELETE]; | ||||
|             return false; | ||||
|         }; | ||||
|  | ||||
|         [[nodiscard]] inline bool LogBadTokens(bool External) const { | ||||
|             return LogBadTokens_[ (External ? 0 : 1) ]; | ||||
|         }; | ||||
|  | ||||
|     private: | ||||
|         std::array<bool,8>       LogFlags_{false}; | ||||
|         std::array<bool,2>       LogBadTokens_{false}; | ||||
|     }; | ||||
|  | ||||
| } | ||||
|  | ||||
|  | ||||
| #endif //OWPROV_RESTAPI_GENERICSERVER_H | ||||
| @@ -25,6 +25,7 @@ namespace OpenWifi { | ||||
|  | ||||
|     int RESTAPI_InternalServer::Start() { | ||||
|         Logger_.information("Starting."); | ||||
|         Server_.InitLogging(); | ||||
|  | ||||
|         for(const auto & Svr: ConfigServersList_) { | ||||
|             Logger_.information(Poco::format("Starting: %s:%s Keyfile:%s CertFile: %s", Svr.Address(), std::to_string(Svr.Port()), | ||||
| @@ -41,7 +42,7 @@ namespace OpenWifi { | ||||
|             Params->setMaxQueued(200); | ||||
|             Params->setKeepAlive(true); | ||||
|  | ||||
|             auto NewServer = std::make_unique<Poco::Net::HTTPServer>(new InternalRequestHandlerFactory, Pool_, Sock, Params); | ||||
|             auto NewServer = std::make_unique<Poco::Net::HTTPServer>(new InternalRequestHandlerFactory(Server_), Pool_, Sock, Params); | ||||
|             NewServer->start(); | ||||
|             RESTServers_.push_back(std::move(NewServer)); | ||||
|         } | ||||
| @@ -70,7 +71,7 @@ namespace OpenWifi { | ||||
|                 RESTAPI_system_command, | ||||
|                 RESTAPI_action_links, | ||||
|                 RESTAPI_validateToken_handler | ||||
|         >(Path,Bindings,Logger_); | ||||
|         >(Path,Bindings,Logger_, Server_); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -11,6 +11,7 @@ | ||||
| #include "Poco/Net/HTTPRequestHandlerFactory.h" | ||||
| #include "Poco/Net/HTTPServerRequest.h" | ||||
| #include "Poco/Net/NetException.h" | ||||
| #include "RESTAPI_GenericServer.h" | ||||
|  | ||||
| namespace OpenWifi { | ||||
|  | ||||
| @@ -31,19 +32,22 @@ namespace OpenWifi { | ||||
|         private: | ||||
|             static RESTAPI_InternalServer *instance_; | ||||
|             std::vector<std::unique_ptr<Poco::Net::HTTPServer>>   RESTServers_; | ||||
|             Poco::ThreadPool	Pool_; | ||||
|             Poco::ThreadPool	    Pool_; | ||||
|             RESTAPI_GenericServer   Server_; | ||||
|     }; | ||||
|  | ||||
|     inline RESTAPI_InternalServer * RESTAPI_InternalServer() { return RESTAPI_InternalServer::instance(); }; | ||||
|  | ||||
|     class InternalRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { | ||||
|         public: | ||||
|             InternalRequestHandlerFactory() : | ||||
|                     Logger_(RESTAPI_InternalServer()->Logger()){} | ||||
|         explicit InternalRequestHandlerFactory(RESTAPI_GenericServer & Server) : | ||||
|                     Logger_(RESTAPI_InternalServer()->Logger()), | ||||
|                     Server_(Server){} | ||||
|  | ||||
|             Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &request) override; | ||||
|         private: | ||||
|             Poco::Logger    & Logger_; | ||||
|             RESTAPI_GenericServer & Server_; | ||||
|     }; | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -18,13 +18,14 @@ | ||||
| namespace OpenWifi { | ||||
|     class RESTAPI_action_links : public RESTAPIHandler { | ||||
|     public: | ||||
|         RESTAPI_action_links(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) | ||||
|         RESTAPI_action_links(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer &Server, bool Internal) | ||||
|                 : RESTAPIHandler(bindings, L, | ||||
|                                  std::vector<std::string>{ | ||||
|                                                             Poco::Net::HTTPRequest::HTTP_GET, | ||||
|                                                             Poco::Net::HTTPRequest::HTTP_POST, | ||||
|                                                             Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
|                                                             Internal) {} | ||||
|              std::vector<std::string>{ | ||||
|                                         Poco::Net::HTTPRequest::HTTP_GET, | ||||
|                                         Poco::Net::HTTPRequest::HTTP_POST, | ||||
|                                         Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
|                                         Server, | ||||
|                                         Internal) {} | ||||
|         static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/actionLink"}; }; | ||||
|         void RequestResetPassword(std::string &Id); | ||||
|         void CompleteResetPassword(std::string &Id); | ||||
|   | ||||
| @@ -33,13 +33,14 @@ namespace OpenWifi { | ||||
|  | ||||
|     class RESTAPI_avatarHandler : public RESTAPIHandler { | ||||
|     public: | ||||
|         RESTAPI_avatarHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) | ||||
|         RESTAPI_avatarHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer &Server, bool Internal) | ||||
|                 : RESTAPIHandler(bindings, L, | ||||
|                                  std::vector<std::string>{ | ||||
|                                          Poco::Net::HTTPRequest::HTTP_GET, | ||||
|                                          Poco::Net::HTTPRequest::HTTP_POST, | ||||
|                                          Poco::Net::HTTPRequest::HTTP_DELETE, | ||||
|                                          Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
|                                          Server, | ||||
|                                          Internal) {} | ||||
|         static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/avatar/{id}"}; }; | ||||
|  | ||||
|   | ||||
| @@ -11,10 +11,11 @@ | ||||
| namespace OpenWifi { | ||||
|     class RESTAPI_email_handler : public RESTAPIHandler { | ||||
|     public: | ||||
|         RESTAPI_email_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) | ||||
|         RESTAPI_email_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer &Server, bool Internal) | ||||
|         : RESTAPIHandler(bindings, L, | ||||
|                          std::vector<std::string>{Poco::Net::HTTPRequest::HTTP_POST, | ||||
|                                                   Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
|                                                   Server, | ||||
|                                                   Internal) {} | ||||
|         static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/email"};} | ||||
|         void DoGet() final {}; | ||||
|   | ||||
							
								
								
									
										42
									
								
								src/RESTAPI_errors.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/RESTAPI_errors.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| // | ||||
| // Created by stephane bourque on 2021-09-12. | ||||
| // | ||||
|  | ||||
| #ifndef OWPROV_RESTAPI_ERRORS_H | ||||
| #define OWPROV_RESTAPI_ERRORS_H | ||||
|  | ||||
| namespace OpenWifi::RESTAPI::Errors { | ||||
|     static const std::string MissingUUID{"Missing UUID."}; | ||||
|     static const std::string MissingSerialNumber{"Missing Serial Number."}; | ||||
|     static const std::string InternalError{"Internal error. Please try later."}; | ||||
|     static const std::string InvalidJSONDocument{"Invalid JSON document."}; | ||||
|     static const std::string UnsupportedHTTPMethod{"Unsupported HTTP Method"}; | ||||
|     static const std::string StillInUse{"Element still in use."}; | ||||
|     static const std::string CouldNotBeDeleted{"Element could not be deleted."}; | ||||
|     static const std::string NameMustBeSet{"The name property must be set."}; | ||||
|     static const std::string ConfigBlockInvalid{"Configuration block type invalid."}; | ||||
|     static const std::string UnknownId{"Unknown management policy."}; | ||||
|     static const std::string InvalidDeviceTypes{"Unknown or invalid device type(s)."}; | ||||
|     static const std::string RecordNotCreated{"Record could not be created."}; | ||||
|     static const std::string RecordNotUpdated{"Record could not be updated."}; | ||||
|     static const std::string UnknownManagementPolicyUUID{"Unknown management policy UUID."}; | ||||
|     static const std::string CannotDeleteRoot{"Root Entity cannot be removed, only modified."}; | ||||
|     static const std::string MustCreateRootFirst{"Root entity must be created first."}; | ||||
|     static const std::string ParentUUIDMustExist{"Parent UUID must exist."}; | ||||
|     static const std::string ConfigurationMustExist{"Configuration must exist."}; | ||||
|     static const std::string MissingOrInvalidParameters{"Invalid or missing parameters."}; | ||||
|     static const std::string UnknownSerialNumber{"Unknown Serial Number."}; | ||||
|     static const std::string InvalidSerialNumber{"Invalid Serial Number."}; | ||||
|     static const std::string SerialNumberExists{"Serial Number already exists."}; | ||||
|     static const std::string ValidNonRootUUID{"Must be a non-root, and valid UUID."}; | ||||
|     static const std::string VenueMustExist{"Venue does not exist."}; | ||||
|     static const std::string NotBoth{"You cannot specify both Entity and Venue"}; | ||||
|     static const std::string EntityMustExist{"Entity must exist."}; | ||||
|     static const std::string ParentOrEntityMustBeSet{"Parent or Entity must be set."}; | ||||
|     static const std::string ContactMustExist{"Contact must exist."}; | ||||
|     static const std::string LocationMustExist{"Location must exist."}; | ||||
|     static const std::string OnlyWSSupported{"This endpoint only supports WebSocket."}; | ||||
|  | ||||
| } | ||||
|  | ||||
| #endif //OWPROV_RESTAPI_ERRORS_H | ||||
| @@ -17,6 +17,8 @@ | ||||
| #include "Poco/URI.h" | ||||
| #include "Poco/Net/OAuth20Credentials.h" | ||||
|  | ||||
| #include "RESTAPI_errors.h" | ||||
|  | ||||
| #ifdef	TIP_SECURITY_SERVICE | ||||
| #include "AuthService.h" | ||||
| #else | ||||
| @@ -52,11 +54,11 @@ namespace OpenWifi { | ||||
| 			else if (Request->getMethod() == Poco::Net::HTTPRequest::HTTP_PUT) | ||||
| 				DoPut(); | ||||
| 			else | ||||
| 				BadRequest("Unsupported HTTP Method"); | ||||
| 				BadRequest(RESTAPI::Errors::UnsupportedHTTPMethod); | ||||
| 			return; | ||||
| 		} catch (const Poco::Exception &E) { | ||||
| 			Logger_.log(E); | ||||
| 			BadRequest("Internal error."); | ||||
| 			BadRequest(RESTAPI::Errors::InternalError); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -355,7 +357,22 @@ namespace OpenWifi { | ||||
|  | ||||
| 	bool RESTAPIHandler::IsAuthorized() { | ||||
| 	    if(Internal_) { | ||||
| 	        return Daemon()->IsValidAPIKEY(*Request); | ||||
| 	        auto Allowed = Daemon()->IsValidAPIKEY(*Request); | ||||
| 	        if(!Allowed) { | ||||
| 	            if(Server_.LogBadTokens(false)) { | ||||
| 	                Logger_.debug(Poco::format("I-REQ-DENIED(%s): Method='%s' Path='%s", | ||||
|                                                Utils::FormatIPv6(Request->clientAddress().toString()), | ||||
|                                                Request->getMethod(), Request->getURI())); | ||||
| 	            } | ||||
| 	        } else { | ||||
| 	            auto Id = Request->get("X-INTERNAL-NAME", "unknown"); | ||||
| 	            if(Server_.LogIt(Request->getMethod(),true)) { | ||||
| 	                Logger_.debug(Poco::format("I-REQ-ALLOWED(%s): User='%s' Method='%s' Path='%s", | ||||
|                                                Utils::FormatIPv6(Request->clientAddress().toString()), Id, | ||||
|                                                Request->getMethod(), Request->getURI())); | ||||
| 	            } | ||||
| 	        } | ||||
|             return Allowed; | ||||
| 	    } else { | ||||
|             if (SessionToken_.empty()) { | ||||
|                 try { | ||||
| @@ -372,8 +389,18 @@ namespace OpenWifi { | ||||
| #else | ||||
|             if (AuthClient()->IsAuthorized(*Request, SessionToken_, UserInfo_)) { | ||||
| #endif | ||||
|                 if(Server_.LogIt(Request->getMethod(),true)) { | ||||
|                     Logger_.debug(Poco::format("X-REQ-ALLOWED(%s): User='%s' Method='%s' Path='%s", | ||||
|                          Utils::FormatIPv6(Request->clientAddress().toString()), UserInfo_.userinfo.email, | ||||
|                          Request->getMethod(), Request->getURI())); | ||||
|                 } | ||||
|                 return true; | ||||
|             } else { | ||||
|                 if(Server_.LogBadTokens(true)) { | ||||
|                     Logger_.debug(Poco::format("X-REQ-DENIED(%s): Method='%s' Path='%s", | ||||
|                          Utils::FormatIPv6(Request->clientAddress().toString()), | ||||
|                          Request->getMethod(), Request->getURI())); | ||||
|                 } | ||||
|                 UnAuthorized(); | ||||
|             } | ||||
|             return false; | ||||
|   | ||||
| @@ -26,6 +26,7 @@ | ||||
|  | ||||
| #include "RESTAPI_SecurityObjects.h" | ||||
| #include "RESTAPI_utils.h" | ||||
| #include "RESTAPI_GenericServer.h" | ||||
|  | ||||
| namespace OpenWifi { | ||||
|  | ||||
| @@ -92,8 +93,8 @@ namespace OpenWifi { | ||||
|  | ||||
| 		typedef std::map<std::string, std::string> BindingMap; | ||||
|  | ||||
| 		RESTAPIHandler(BindingMap map, Poco::Logger &l, std::vector<std::string> Methods, bool Internal=false, bool AlwaysAuthorize=true) | ||||
| 		: Bindings_(std::move(map)), Logger_(l), Methods_(std::move(Methods)), Internal_(Internal), AlwaysAuthorize_(AlwaysAuthorize) {} | ||||
| 		RESTAPIHandler(BindingMap map, Poco::Logger &l, std::vector<std::string> 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) {} | ||||
|  | ||||
| 		static bool ParseBindings(const std::string & Request, const std::list<const char *> & EndPoints, BindingMap &Keys); | ||||
| 		void PrintBindings(); | ||||
| @@ -173,12 +174,13 @@ namespace OpenWifi { | ||||
| 		Poco::Net::HTTPServerResponse   *Response= nullptr; | ||||
| 		bool                        AlwaysAuthorize_=true; | ||||
| 		Poco::JSON::Parser          IncomingParser_; | ||||
| 		RESTAPI_GenericServer       & Server_; | ||||
| 	}; | ||||
|  | ||||
| 	class RESTAPI_UnknownRequestHandler : public RESTAPIHandler { | ||||
| 	  public: | ||||
| 		RESTAPI_UnknownRequestHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L) | ||||
| 			: RESTAPIHandler(bindings, L, std::vector<std::string>{}) {} | ||||
| 		RESTAPI_UnknownRequestHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server) | ||||
| 			: RESTAPIHandler(bindings, L, std::vector<std::string>{}, Server) {} | ||||
|         inline void DoGet() override {}; | ||||
| 		inline void DoPost() override {}; | ||||
| 		inline void DoPut() override {}; | ||||
| @@ -197,30 +199,30 @@ namespace OpenWifi { | ||||
| 	} | ||||
|  | ||||
| 	template<typename T, typename... Args> | ||||
| 	RESTAPIHandler * RESTAPI_Router(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & Logger ) { | ||||
| 	RESTAPIHandler * RESTAPI_Router(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & Logger, RESTAPI_GenericServer & Server) { | ||||
| 		static_assert(test_has_PathName_method((T*)nullptr), "Class must have a static PathName() method."); | ||||
| 		if(RESTAPIHandler::ParseBindings(RequestedPath,T::PathName(),Bindings)) { | ||||
| 			return new T(Bindings, Logger, false); | ||||
| 			return new T(Bindings, Logger, Server, false); | ||||
| 		} | ||||
|  | ||||
| 		if constexpr (sizeof...(Args) == 0) { | ||||
| 			return new RESTAPI_UnknownRequestHandler(Bindings,Logger); | ||||
| 			return new RESTAPI_UnknownRequestHandler(Bindings,Logger, Server); | ||||
| 		} else { | ||||
| 			return RESTAPI_Router<Args...>(RequestedPath, Bindings, Logger); | ||||
| 			return RESTAPI_Router<Args...>(RequestedPath, Bindings, Logger, Server); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|     template<typename T, typename... Args> | ||||
|     RESTAPIHandler * RESTAPI_Router_I(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & Logger) { | ||||
|     RESTAPIHandler * RESTAPI_Router_I(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & Logger, RESTAPI_GenericServer & Server) { | ||||
|         static_assert(test_has_PathName_method((T*)nullptr), "Class must have a static PathName() method."); | ||||
|         if(RESTAPIHandler::ParseBindings(RequestedPath,T::PathName(),Bindings)) { | ||||
|             return new T(Bindings, Logger, true); | ||||
|             return new T(Bindings, Logger, Server, true); | ||||
|         } | ||||
|  | ||||
|         if constexpr (sizeof...(Args) == 0) { | ||||
|             return new RESTAPI_UnknownRequestHandler(Bindings,Logger); | ||||
|             return new RESTAPI_UnknownRequestHandler(Bindings,Logger, Server); | ||||
|         } else { | ||||
|             return RESTAPI_Router_I<Args...>(RequestedPath, Bindings, Logger); | ||||
|             return RESTAPI_Router_I<Args...>(RequestedPath, Bindings, Logger, Server); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -14,12 +14,13 @@ | ||||
| namespace OpenWifi { | ||||
| 	class RESTAPI_oauth2Handler : public RESTAPIHandler { | ||||
| 	  public: | ||||
| 		RESTAPI_oauth2Handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) | ||||
| 	    RESTAPI_oauth2Handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer &Server, bool Internal) | ||||
| 			: RESTAPIHandler(bindings, L, | ||||
| 							 std::vector<std::string>{Poco::Net::HTTPRequest::HTTP_POST, | ||||
| 													  Poco::Net::HTTPRequest::HTTP_DELETE, | ||||
|                                                       Poco::Net::HTTPRequest::HTTP_GET, | ||||
| 													  Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
| 													  Server, | ||||
| 													  Internal, false) {} | ||||
| 		static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/oauth2/{token}","/api/v1/oauth2"}; }; | ||||
| 		void DoGet() final; | ||||
|   | ||||
| @@ -30,6 +30,7 @@ namespace OpenWifi { | ||||
|  | ||||
|     int RESTAPI_Server::Start() { | ||||
|         Logger_.information("Starting."); | ||||
|         Server_.InitLogging(); | ||||
|  | ||||
|         AsserDir_ = Daemon()->ConfigPath("openwifi.restapi.wwwassets"); | ||||
|         AccessPolicy_ = Daemon()->ConfigGetString("openwifi.document.policy.access", "/wwwassets/access_policy.html"); | ||||
| @@ -50,7 +51,7 @@ namespace OpenWifi { | ||||
|             Params->setMaxQueued(200); | ||||
| 			Params->setKeepAlive(true); | ||||
|  | ||||
|             auto NewServer = std::make_unique<Poco::Net::HTTPServer>(new RequestHandlerFactory, Pool_, Sock, Params); | ||||
|             auto NewServer = std::make_unique<Poco::Net::HTTPServer>(new RequestHandlerFactory(Server_), Pool_, Sock, Params); | ||||
|             NewServer->start(); | ||||
|             RESTServers_.push_back(std::move(NewServer)); | ||||
|         } | ||||
| @@ -75,7 +76,7 @@ namespace OpenWifi { | ||||
|                 RESTAPI_action_links, | ||||
|                 RESTAPI_avatarHandler, | ||||
|                 RESTAPI_email_handler | ||||
|                 >(Path,Bindings,Logger_); | ||||
|                 >(Path,Bindings,Logger_,Server_); | ||||
|     } | ||||
|  | ||||
|     void RESTAPI_Server::Stop() { | ||||
|   | ||||
| @@ -15,6 +15,7 @@ | ||||
| #include "Poco/Net/HTTPRequestHandlerFactory.h" | ||||
| #include "Poco/Net/HTTPServerRequest.h" | ||||
| #include "Poco/Net/NetException.h" | ||||
| #include "RESTAPI_GenericServer.h" | ||||
|  | ||||
| namespace OpenWifi { | ||||
|  | ||||
| @@ -40,6 +41,7 @@ namespace OpenWifi { | ||||
| 		std::string         AsserDir_; | ||||
| 		std::string         PasswordPolicy_; | ||||
| 		std::string         AccessPolicy_; | ||||
| 		RESTAPI_GenericServer   Server_; | ||||
|  | ||||
|         RESTAPI_Server() noexcept: | ||||
|             SubSystemServer("RESTAPIServer", "REST-SRV", "openwifi.restapi") | ||||
| @@ -51,12 +53,14 @@ namespace OpenWifi { | ||||
|  | ||||
|     class RequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { | ||||
|         public: | ||||
|             RequestHandlerFactory() : | ||||
|                 Logger_(RESTAPI_Server()->Logger()){} | ||||
|         RequestHandlerFactory(RESTAPI_GenericServer &Server) : | ||||
|                 Logger_(RESTAPI_Server()->Logger()), | ||||
|                 Server_(Server){} | ||||
|  | ||||
|             Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &request) override; | ||||
|         private: | ||||
|             Poco::Logger    & Logger_; | ||||
|             RESTAPI_GenericServer   &Server_; | ||||
|     }; | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -9,10 +9,11 @@ | ||||
| namespace OpenWifi { | ||||
|     class RESTAPI_systemEndpoints_handler : public RESTAPIHandler { | ||||
|     public: | ||||
|         RESTAPI_systemEndpoints_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) | ||||
|         RESTAPI_systemEndpoints_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer &Server, bool Internal) | ||||
|                 : RESTAPIHandler(bindings, L, | ||||
|                                  std::vector<std::string>{Poco::Net::HTTPRequest::HTTP_GET, | ||||
|                                                           Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
|                                                           Server, | ||||
|                                                           Internal) {} | ||||
|         static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/systemEndpoints"}; }; | ||||
|         void DoGet() final; | ||||
|   | ||||
| @@ -12,6 +12,7 @@ | ||||
|  | ||||
| #include "Daemon.h" | ||||
| #include "RESTAPI_protocol.h" | ||||
| #include "RESTAPI_errors.h" | ||||
|  | ||||
| namespace OpenWifi { | ||||
| 	void RESTAPI_system_command::DoPost() { | ||||
|   | ||||
| @@ -14,11 +14,12 @@ | ||||
| namespace OpenWifi { | ||||
| class RESTAPI_system_command : public RESTAPIHandler { | ||||
|   public: | ||||
| 	RESTAPI_system_command(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) | ||||
|     RESTAPI_system_command(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal) | ||||
| 		: RESTAPIHandler(bindings, L, | ||||
| 						 std::vector<std::string>{Poco::Net::HTTPRequest::HTTP_POST, | ||||
| 														  Poco::Net::HTTPRequest::HTTP_GET, | ||||
| 														  Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
| 														  Server, | ||||
| 						 Internal) {} | ||||
| 	static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/system"};} | ||||
|  | ||||
|   | ||||
| @@ -10,7 +10,7 @@ | ||||
| namespace OpenWifi { | ||||
|     class RESTAPI_user_handler : public RESTAPIHandler { | ||||
|     public: | ||||
|         RESTAPI_user_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) | ||||
|         RESTAPI_user_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer &Server, bool Internal) | ||||
|                 : RESTAPIHandler(bindings, L, | ||||
|                                  std::vector<std::string> | ||||
|                                          {Poco::Net::HTTPRequest::HTTP_POST, | ||||
| @@ -18,6 +18,7 @@ namespace OpenWifi { | ||||
|                                           Poco::Net::HTTPRequest::HTTP_PUT, | ||||
|                                           Poco::Net::HTTPRequest::HTTP_DELETE, | ||||
|                                           Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
|                                           Server, | ||||
|                                           Internal) {} | ||||
|         static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/user/{id}"}; }; | ||||
|         void DoGet() final; | ||||
|   | ||||
| @@ -10,11 +10,12 @@ | ||||
| namespace OpenWifi { | ||||
|     class RESTAPI_users_handler : public RESTAPIHandler { | ||||
|     public: | ||||
|         RESTAPI_users_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) | ||||
|         RESTAPI_users_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer &Server, bool Internal) | ||||
|                 : RESTAPIHandler(bindings, L, | ||||
|                                  std::vector<std::string> | ||||
|                                  {Poco::Net::HTTPRequest::HTTP_GET, | ||||
|                                   Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
|                                   Server, | ||||
|                                   Internal) {} | ||||
|         static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/users"}; }; | ||||
|         void DoGet() final; | ||||
|   | ||||
| @@ -10,11 +10,12 @@ | ||||
| namespace OpenWifi { | ||||
|     class RESTAPI_validateToken_handler : public RESTAPIHandler { | ||||
|     public: | ||||
|         RESTAPI_validateToken_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, bool Internal) | ||||
|         RESTAPI_validateToken_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer &Server, bool Internal) | ||||
|                 : RESTAPIHandler(bindings, L, | ||||
|                                  std::vector<std::string> | ||||
|                                          {Poco::Net::HTTPRequest::HTTP_GET, | ||||
|                                           Poco::Net::HTTPRequest::HTTP_OPTIONS}, | ||||
|                                           Server, | ||||
|                                           Internal) {}; | ||||
|         static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/validateToken"}; }; | ||||
|         void DoGet() final; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 stephb9959
					stephb9959