Leosac  0.8.0
Open Source Access Control
WSServer.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "LeosacFwd.hpp"
23 #include "Messages.hpp"
24 #include "Service.hpp"
25 #include "WebSockFwd.hpp"
26 #include "api/APIAuth.hpp"
27 #include "api/APISession.hpp"
29 #include "api/MethodHandler.hpp"
30 #include "core/APIStatusCode.hpp"
31 #include "core/audit/AuditFwd.hpp"
32 #include "tools/db/db_fwd.hpp"
33 #include <boost/optional.hpp>
34 #include <set>
35 #include <type_traits>
36 #include <websocketpp/config/asio_no_tls.hpp>
37 #include <websocketpp/server.hpp>
38 #include <zmqpp/zmqpp.hpp>
39 
40 namespace Leosac
41 {
42 namespace Module
43 {
44 namespace WebSockAPI
45 {
46 using json = nlohmann::json;
47 
61 class WSServer
62 {
63  public:
68  WSServer(WebSockAPIModule &module, DBPtr database);
69  ~WSServer();
70 
71  using Server = websocketpp::server<websocketpp::config::asio>;
72  using ConnectionAPIMap = std::map<websocketpp::connection_hdl, APIPtr,
73  std::owner_less<websocketpp::connection_hdl>>;
74 
75  void run(const std::string &interface, uint16_t port);
76 
78 
87  void start_shutdown();
88 
95  bool register_asio_handler(const Service::WSHandler &handler,
96  const std::string &name);
97 
98 
108  void register_crud_handler_external(const std::string &resource_name,
116  void unregister_handler(const std::string &name);
117 
121  APIAuth &auth();
122 
126  DBPtr db();
127 
132 
137 
142  void clear_user_sessions(Auth::UserPtr user, APIPtr exception);
143 
144  private:
145  void on_open(websocketpp::connection_hdl hdl);
146 
147  void on_close(websocketpp::connection_hdl hdl);
148 
150 
166  void on_message(websocketpp::connection_hdl hdl, Server::message_ptr msg);
167 
176  boost::optional<ServerMessage> handle_request(APIPtr api_handle,
177  const ClientMessage &msg,
179 
183  ClientMessage parse_request(const json &in);
184 
190  void send_message(websocketpp::connection_hdl hdl, const ServerMessage &msg);
191 
204  void register_crud_handler(const std::string &resource_name,
206 
214  boost::optional<json> dispatch_request(APIPtr api_handle,
215  const ClientMessage &in,
217 
222  bool has_handler(const std::string &name) const;
223 
229  void finalize_audit(const Audit::IWSAPICallPtr &audit, ServerMessage &msg);
230 
233 
237  std::map<std::string, json (APISession::*)(const json &)> handlers_;
238 
239  std::map<std::string, MethodHandler::Factory> individual_handlers_;
240 
241  std::map<std::string, CRUDResourceHandler::Factory> crud_handlers_;
242 
246  std::map<std::string, Service::WSHandler> asio_handlers_;
247 
252 
259 
264  std::unique_ptr<boost::asio::io_service::work> work_;
265 };
266 }
267 }
268 }
Leosac::Module::WebSockAPI::WSServer::dispatch_request
boost::optional< json > dispatch_request(APIPtr api_handle, const ClientMessage &in, Audit::IAuditEntryPtr)
Dispatch the request from a client, so that it is processed by the appropriate handler.
Definition: WSServer.cpp:269
Leosac::Module::WebSockAPI::WSServer::WSServer
WSServer(WebSockAPIModule &module, DBPtr database)
Definition: WSServer.cpp:78
Leosac::Module::WebSockAPI::WSServer::individual_handlers_
std::map< std::string, MethodHandler::Factory > individual_handlers_
Definition: WSServer.hpp:239
CRUDResourceHandler.hpp
Leosac::Module::WebSockAPI::WSServer::on_open
void on_open(websocketpp::connection_hdl hdl)
Definition: WSServer.cpp:143
Leosac::Module::WebSockAPI::ServerMessage
A message sent by the server to a client.
Definition: Messages.hpp:38
json
nlohmann::json json
Definition: WSServer.cpp:76
Leosac::Module::WebSockAPI::WSServer::register_crud_handler_external
void register_crud_handler_external(const std::string &resource_name, CRUDResourceHandler::Factory factory)
Register a CRUD handler from an external thread.
Definition: WSServer.cpp:526
Leosac::Module::WebSockAPI::WSServer::crud_handlers_
std::map< std::string, CRUDResourceHandler::Factory > crud_handlers_
Definition: WSServer.hpp:241
LeosacFwd.hpp
Leosac::Module::WebSockAPI::WSServer::unregister_handler
void unregister_handler(const std::string &name)
Remove an Asio based handler.
Definition: WSServer.cpp:510
Leosac::Module::WebSockAPI::WSServer::run
void run(const std::string &interface, uint16_t port)
Definition: WSServer.cpp:223
Leosac::Auth::UserPtr
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
Leosac::Module::WebSockAPI::WSServer::auth
APIAuth & auth()
Retrieve the authentication helper.
Definition: WSServer.cpp:264
Leosac::Module::WebSockAPI::APIAuth
This class is responsible for providing an API to manage authentication for Websocket client.
Definition: APIAuth.hpp:41
Leosac::Module::WebSockAPI::ClientMessage
A message sent by a client to Leosac.
Definition: Messages.hpp:54
Leosac::Module::WebSockAPI::WSServer::clear_user_sessions
void clear_user_sessions(Auth::UserPtr user, APIPtr exception)
Deauthenticate all the connections of user, except the exception APISession.
Definition: WSServer.cpp:410
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Module::WebSockAPI::CRUDResourceHandler::Factory
CRUDResourceHandlerUPtr(*)(RequestContext) Factory
Definition: CRUDResourceHandler.hpp:90
Leosac::Module::WebSockAPI::WSServer::send_message
void send_message(websocketpp::connection_hdl hdl, const ServerMessage &msg)
Send a message over a connection.
Definition: WSServer.cpp:347
Leosac::Module::WebSockAPI::WSServer::Server
websocketpp::server< websocketpp::config::asio > Server
Definition: WSServer.hpp:71
Leosac::Audit::IAuditEntryPtr
std::shared_ptr< IAuditEntry > IAuditEntryPtr
Definition: AuditFwd.hpp:40
Leosac::Module::WebSockAPI::WSServer::on_message
void on_message(websocketpp::connection_hdl hdl, Server::message_ptr msg)
A websocket message has been received.
Definition: WSServer.cpp:156
Leosac::Module::WebSockAPI::WSServer::asio_handlers_
std::map< std::string, Service::WSHandler > asio_handlers_
Handlers registered through the WebSockAPI::Service object.
Definition: WSServer.hpp:246
Leosac::Module::WebSockAPI::WebSockAPIModule
A module that provide a websocket interface to Leosac.
Definition: WebSockAPI.hpp:46
Leosac::Module::WebSockAPI::WSServer::ConnectionAPIMap
std::map< websocketpp::connection_hdl, APIPtr, std::owner_less< websocketpp::connection_hdl > > ConnectionAPIMap
Definition: WSServer.hpp:73
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Messages.hpp
Leosac::Module::WebSockAPI::WSServer::connection_session_
ConnectionAPIMap connection_session_
Definition: WSServer.hpp:231
Leosac::Module::WebSockAPI::WSServer::module_
WebSockAPIModule & module_
A reference to the module.
Definition: WSServer.hpp:258
Leosac::Module::WebSockAPI::WSServer::finalize_audit
void finalize_audit(const Audit::IWSAPICallPtr &audit, ServerMessage &msg)
Extract values from the msg and finalizes the audit object with them.
Definition: WSServer.cpp:465
Leosac::Module::WebSockAPI::WSServer::handle_request
boost::optional< ServerMessage > handle_request(APIPtr api_handle, const ClientMessage &msg, Audit::IAuditEntryPtr)
Handle a request.
Definition: WSServer.cpp:383
Leosac::DBServicePtr
std::shared_ptr< DBService > DBServicePtr
Definition: db_fwd.hpp:34
Leosac::Module::WebSockAPI::WSServer::register_asio_handler
bool register_asio_handler(const Service::WSHandler &handler, const std::string &name)
This function block the calling thread until the WebSocket thread has processed the handler registrat...
Definition: WSServer.cpp:492
Leosac::Module::WebSockAPI::Service::WSHandler
std::function< boost::optional< json >(const RequestContext &)> WSHandler
Definition: Service.hpp:53
Leosac::Module::WebSockAPI::WSServer::has_handler
bool has_handler(const std::string &name) const
Returns true if an handler named name already exists.
Definition: WSServer.cpp:459
Leosac::Module::WebSockAPI::WSServer::~WSServer
~WSServer()
Definition: WSServer.cpp:137
Leosac::Module::WebSockAPI::WSServer
The implementation class that runs the websocket server.
Definition: WSServer.hpp:61
APIStatusCode.hpp
Service.hpp
Leosac::Audit::IWSAPICallPtr
std::shared_ptr< IWSAPICall > IWSAPICallPtr
Definition: AuditFwd.hpp:52
Leosac::Module::WebSockAPI::WSServer::on_close
void on_close(websocketpp::connection_hdl hdl)
Definition: WSServer.cpp:150
APIAuth.hpp
Leosac::Module::WebSockAPI::WSServer::srv_
Server srv_
Definition: WSServer.hpp:77
WebSockFwd.hpp
Leosac::Module::WebSockAPI::WSServer::core_utils
CoreUtilsPtr core_utils()
Retrieve the CoreUtils pointer.
Definition: WSServer.cpp:342
APISession.hpp
Leosac::Module::WebSockAPI::WSServer::dbsrv_
DBServicePtr dbsrv_
Database service object.
Definition: WSServer.hpp:251
Leosac::Module::WebSockAPI::WSServer::auth_
APIAuth auth_
Definition: WSServer.hpp:232
Leosac::Module::WebSockAPI::WSServer::db
DBPtr db()
Retrieve database handle.
Definition: WSServer.cpp:337
Leosac::Module::WebSockAPI::APIPtr
std::shared_ptr< APISession > APIPtr
Definition: WebSockFwd.hpp:33
db_fwd.hpp
Leosac::Module::WebSockAPI::WSServer::parse_request
ClientMessage parse_request(const json &in)
Create a ClientMessage object from a json request.
Definition: WSServer.cpp:361
Leosac::Module::WebSockAPI::json
nlohmann::json json
Definition: AccessOverview.hpp:30
Leosac::Module::WebSockAPI::WSServer::start_shutdown
void start_shutdown()
Start the process of shutting down the server.
Definition: WSServer.cpp:243
Leosac::Module::WebSockAPI::WSServer::work_
std::unique_ptr< boost::asio::io_service::work > work_
Work used to keep the io_service alive while someone has a reference to (WS) Service object.
Definition: WSServer.hpp:264
Leosac::Module::WebSockAPI::APISession
This is the application-level object that provide the API.
Definition: APISession.hpp:42
Leosac::Module::WebSockAPI::WSServer::dbsrv
DBServicePtr dbsrv()
Retrieve database service pointer.
Definition: WSServer.cpp:454
AuditFwd.hpp
Leosac::Module::WebSockAPI::WSServer::attempt_unregister_ws_service
void attempt_unregister_ws_service()
Definition: WSServer.cpp:539
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
Leosac::Module::WebSockAPI::WSServer::register_crud_handler
void register_crud_handler(const std::string &resource_name, CRUDResourceHandler::Factory factory)
An internal helper function to register a CRUD resource handler.
Definition: WSServer.cpp:442
Leosac::Module::WebSockAPI::WSServer::handlers_
std::map< std::string, json(APISession::*)(const json &)> handlers_
This maps (string) command name to API method.
Definition: WSServer.hpp:237
MethodHandler.hpp