converted JsonAPI with public api and using RestbedService system

This commit is contained in:
csoler 2019-11-16 00:02:02 +01:00
parent 3ca22f0052
commit a304ec20ef
No known key found for this signature in database
GPG key ID: 7BCA522266C0804C
10 changed files with 198 additions and 206 deletions

View file

@ -29,18 +29,12 @@
#include "pqi/p3cfgmgr.h"
#include "rsitems/rsitem.h"
#include "jsonapi/jsonapiitems.h"
#include "jsonapi/restbedservice.h"
#include "retroshare/rsjsonapi.h"
#include "util/rsthreads.h"
namespace rb = restbed;
struct JsonApiServer;
/**
* Pointer to global instance of JsonApiServer
* @jsonapi{development}
*/
extern JsonApiServer* jsonApiServer;
/**
* Simple usage
* \code{.cpp}
@ -50,102 +44,27 @@ extern JsonApiServer* jsonApiServer;
* \endcode
* Uses p3Config to securely store persistent JSON API authorization tokens
*/
struct JsonApiServer : RsSingleJobThread, p3Config
class JsonApiServer : public p3Config, public RestbedService, public RsJsonAPI
{
static const uint16_t DEFAULT_PORT = 9092 ;
static const std::string DEFAULT_LISTENING_ADDRESS ;
public:
JsonApiServer() ;
virtual ~JsonApiServer() = default;
static JsonApiServer& instance() ;
// public API
/**
* @param[in] path Path itno which publish the API call
* @param[in] handler function which will be called to handle the requested
* @param[in] requiresAutentication specify if the API call must be
* autenticated or not
* path, the function must be declared like:
* \code{.cpp}
* void functionName(const shared_ptr<restbed::Session> session)
* \endcode
*/
void registerHandler(
const std::string& path,
const std::function<void(const std::shared_ptr<rb::Session>)>& handler,
bool requiresAutentication = true );
virtual bool restart() override { return RestbedService::restart();}
virtual bool stop() override { return RestbedService::stop();}
virtual void setListeningPort(uint16_t port) override { RestbedService::setListeningPort(port) ;}
virtual void setBindingAddress(const std::string& address) override { RestbedService::setBindAddress(address); }
virtual int status() const override;
/**
* @brief Set new access request callback
* @param callback function to call when a new JSON API access is requested
*/
void setNewAccessRequestCallback(
const std::function<bool(const std::string&)>& callback );
virtual bool authorizeUser(const std::string& alphanumeric_user,const std::string& alphanumeric_passwd) override;
virtual std::map<std::string,std::string> getAuthorizedTokens() override;
bool revokeAuthToken(const std::string& user) override;
bool isAuthTokenValid(const std::string& token) override;
bool requestNewTokenAutorization(const std::string& user) override;
/**
* @brief Shutdown the JSON API server
* Beware that this method shout down only the JSON API server instance not
*/
void shutdown();
/**
* @brief start
* Starts the json Api server.
*
* @param port port to listen to
* @param listen_address bind address to listen to
*/
void start( uint16_t port = DEFAULT_PORT,
const std::string& bindAddress = "127.0.0.1",
const std::function<bool(const std::string&)> newAccessRequestCallback = [](const std::string&){return false;});
/**
* @brief This function should be used by JSON API clients that aren't
* authenticated yet, to ask their token to be authorized, the success or
* failure will depend on mNewAccessRequestCallback return value, and it
* will likely need human user interaction in the process.
* @jsonapi{development,unauthenticated}
* @param[in] token token to autorize
* @return true if authorization succeded, false otherwise.
*/
bool requestNewTokenAutorization(const std::string& token);
/**
* @brief Check if given JSON API auth token is authorized
* @jsonapi{development}
* @param[in] token decoded
* @return tru if authorized, false otherwise
*/
bool isAuthTokenValid(const std::string& token);
/**
* @brief Get uthorized tokens
* @jsonapi{development}
* @return the set of authorized encoded tokens
*/
std::map<std::string,std::string> getAuthorizedTokens();
/**
* @brief Revoke given auth token
* @jsonapi{development}
* @param[in] user par of the decoded token
* @return true if the token has been revoked, false otherwise
*/
bool revokeAuthToken(const std::string& user);
/**
* @brief Add new auth token to the authorized set
* @jsonapi{development}
* @param[in] token token to autorize decoded, in format user:passwd where user and passwd are alphanumeric strings
* @return true if the token has been added to authorized, false if error occurred (e.g. token format is invalid)
*/
bool authorizeToken(const std::string& token);
/**
* @brief Add new auth (user,passwd) token to the authorized set, creating the token user:passwd internally.
* @jsonapi{development}
* @param[in] alphanumeric_user username to autorize decoded
* @param[in] alphanumeric_passwd passwd to autorize decoded
* @return true if the token has been added to authorized, false if error occurred
*/
bool authorizeUser(const std::string& alphanumeric_user,const std::string& alphanumeric_passwd);
// private API
/**
* @brief Get decoded version of the given encoded token
@ -176,23 +95,35 @@ struct JsonApiServer : RsSingleJobThread, p3Config
std::string& extra, std::string&human );
static void setConfigMgr(p3ConfigMgr *cfg) { _config_mgr = cfg; }
// /!\ These methods shouldn't be accessible through http!
/**
* @param[in] path Path itno which publish the API call
* @param[in] handler function which will be called to handle the requested
* @param[in] requiresAutentication specify if the API call must be
* autenticated or not
* path, the function must be declared like:
* \code{.cpp}
* void functionName(const shared_ptr<restbed::Session> session)
* \endcode
*/
void registerHandler(
const std::string& path,
const std::function<void(const std::shared_ptr<rb::Session>)>& handler,
bool requiresAutentication = true );
/**
* @brief Set new access request callback
* @param callback function to call when a new JSON API access is requested
*/
void setNewAccessRequestCallback(const std::function<bool(const std::string&,std::string&)>& callback );
protected:
/// @see RsSingleJobThread
virtual void run();
virtual std::vector<std::shared_ptr<rb::Resource> > getResources() const;
private:
/**
* @brief construct a JsonApiServer instance with given parameters
* @param[in] port listening port fpt the JSON API socket
* @param[in] bindAddress binding address for the JSON API socket
* @param newAccessRequestCallback called when a new auth token is asked to
* be authorized via JSON API, the auth token is passed as parameter, and
* the callback should return true if the new token get access granted and
* false otherwise, this usually requires user interacion to confirm access
*/
JsonApiServer( );
/// @see p3Config::setupSerialiser
virtual RsSerialiser* setupSerialiser();
@ -211,7 +142,9 @@ private:
rb::Service mService;
/// Called when new JSON API auth token is requested to be authorized
std::function<bool(const std::string&)> mNewAccessRequestCallback;
/// The callback supplies the password to be used to make the token
///
std::function<bool(const std::string&,std::string& passwd)> mNewAccessRequestCallback;
/// Encrypted persistent storage for authorized JSON API tokens
JsonApiServerAuthTokenStorage mAuthTokenStorage;
@ -224,19 +157,18 @@ private:
static bool checkRsServicePtrReady(
const void* serviceInstance, const std::string& serviceName,
RsGenericSerializer::SerializeContext& ctx,
const std::shared_ptr<restbed::Session> session );
const std::shared_ptr<rb::Session> session );
static inline bool checkRsServicePtrReady(
const std::shared_ptr<const void> serviceInstance,
const std::string& serviceName,
RsGenericSerializer::SerializeContext& ctx,
const std::shared_ptr<restbed::Session> session )
const std::shared_ptr<rb::Session> session )
{
return checkRsServicePtrReady(
serviceInstance.get(), serviceName, ctx, session );
}
JsonApiServer *_instance;
static p3ConfigMgr *_config_mgr;
std::vector<std::shared_ptr<rb::Resource> > _resources;
};