Prepare for merging

This commit is contained in:
Gioacchino Mazzurco 2019-11-27 18:44:10 +01:00
parent b1860d8682
commit 1d4ca64dee
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
27 changed files with 1442 additions and 992 deletions

View file

@ -4,14 +4,13 @@
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* it under the terms of the GNU Affero General Public License version 3 as *
* published by the Free Software Foundation. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
@ -25,12 +24,13 @@
#include <cstdint>
#include <map>
#include <set>
#include <functional>
#include <vector>
#include "util/rsthreads.h"
#include "pqi/p3cfgmgr.h"
#include "rsitems/rsitem.h"
#include "jsonapi/jsonapiitems.h"
#include "jsonapi/restbedservice.h"
#include "retroshare/rsjsonapi.h"
#include "util/rsthreads.h"
@ -39,96 +39,89 @@ namespace rb = restbed;
class JsonApiResourceProvider
{
public:
JsonApiResourceProvider() {}
virtual ~JsonApiResourceProvider() = default;
virtual ~JsonApiResourceProvider() = default;
virtual std::string getName() const =0;
virtual std::vector<std::shared_ptr<rb::Resource> > getResources() const =0;
virtual std::vector<std::shared_ptr<rb::Resource>> getResources() const = 0;
inline bool operator< (const JsonApiResourceProvider& rp) const
{ return this < &rp; }
};
/**
* Simple usage
* \code{.cpp}
* JsonApiServer jas(9092);
* jsonApiServer = &jas;
* jas.start("JsonApiServer");
* \endcode
* Uses p3Config to securely store persistent JSON API authorization tokens
*/
class JsonApiServer : public p3Config, public RestbedService, public RsJsonAPI
class JsonApiServer : public p3Config, public RsThread, public RsJsonApi
{
public:
JsonApiServer() ;
virtual ~JsonApiServer() = default;
JsonApiServer();
~JsonApiServer() override = default;
// implements RestbedService
std::vector<std::shared_ptr<rb::Resource>> getResources() const;
virtual std::vector<std::shared_ptr<rb::Resource> > getResources() const override ;
/// @see RsJsonApi
bool restart() override;
// RsJsonAPI public API
/// @see RsJsonApi
bool fullstop() override;
bool restart() override { return RestbedService::restart(); }
bool stop() override { return RestbedService::fullstop();}
bool isRunning() override { return RestbedService::isRunning(); }
/// @see RsJsonApi
inline bool isRunning() override { return RsThread::isRunning(); }
void setListeningPort(uint16_t port) override { return RestbedService::setListeningPort(port); }
void setBindingAddress(const std::string& bind_address) override { return RestbedService::setBindAddress(bind_address); }
uint16_t listeningPort() const override { return RestbedService::listeningPort() ; }
/// @see RsJsonApi
void setListeningPort(uint16_t port) override;
virtual void connectToConfigManager(p3ConfigMgr *cfgmgr);
/// @see RsJsonApi
void setBindingAddress(const std::string& bindAddress) override;
virtual bool authorizeUser(const std::string& alphanumeric_user,const std::string& alphanumeric_passwd) override;
virtual std::map<std::string,std::string> getAuthorizedTokens() override;
/// @see RsJsonApi
std::string getBindingAddress() const override;
/// @see RsJsonApi
uint16_t listeningPort() const override;
/// @see RsJsonApi
void connectToConfigManager(p3ConfigMgr& cfgmgr) override;
/// @see RsJsonApi
virtual bool authorizeUser(
const std::string& alphanumeric_user,
const std::string& alphanumeric_passwd ) override;
/// @see RsJsonApi
std::map<std::string,std::string> getAuthorizedTokens() override;
/// @see RsJsonApi
bool revokeAuthToken(const std::string& user) override;
/// @see RsJsonApi
bool isAuthTokenValid(const std::string& token) override;
bool requestNewTokenAutorization(const std::string& user) override;
void registerResourceProvider(const JsonApiResourceProvider *);
void unregisterResourceProvider(const JsonApiResourceProvider *);
bool hasResourceProvider(const JsonApiResourceProvider *);
/// @see RsJsonAPI
bool requestNewTokenAutorization(
const std::string& user, const std::string& password ) override;
// private API. These methods may be moved to RsJsonAPI so as to be visible in http mode, if needed.
/// @see RsJsonApi
void registerResourceProvider(const JsonApiResourceProvider&) override;
/// @see RsJsonApi
void unregisterResourceProvider(const JsonApiResourceProvider&) override;
/// @see RsJsonApi
bool hasResourceProvider(const JsonApiResourceProvider&) override;
/**
* @brief Get decoded version of the given encoded token
* @jsonapi{development,unauthenticated}
* @param[in] radix64_token encoded
* @return token decoded
*/
static std::string decodeToken(const std::string& radix64_token);
/**
* @brief Get encoded version of the given decoded token
* @jsonapi{development,unauthenticated}
* @param[in] clear_token decoded
* @return token encoded
*/
static std::string encodeToken(const std::string& clear_token);
/**
* @brief Write version information to given paramethers
* @jsonapi{development,unauthenticated}
* @param[out] major storage
* @param[out] minor storage
* @param[out] mini storage
* @param[out] extra storage
* @param[out] human storage
*/
static void version( uint32_t& major, uint32_t& minor, uint32_t& mini,
std::string& extra, std::string&human );
// /!\ These methods shouldn't be accessible through http!
/**
* @param[in] path Path itno which publish the API call
* Register an unique handler for a resource path
* @param[in] path Path into 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
* autenticated or not.
*/
void registerHandler(
const std::string& path,
@ -139,29 +132,29 @@ public:
* @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 );
void setNewAccessRequestCallback(
const std::function<bool(const std::string&, const std::string&)>&
callback );
private:
/// @see p3Config::setupSerialiser
virtual RsSerialiser* setupSerialiser();
RsSerialiser* setupSerialiser() override;
/// @see p3Config::saveList
virtual bool saveList(bool &cleanup, std::list<RsItem *>& saveItems);
bool saveList(bool &cleanup, std::list<RsItem *>& saveItems) override;
/// @see p3Config::loadList
virtual bool loadList(std::list<RsItem *>& loadList);
bool loadList(std::list<RsItem *>& loadList) override;
/// @see p3Config::saveDone
virtual void saveDone();
void saveDone() override;
uint16_t mPort;
std::string mBindAddress;
/// Called when new JSON API auth token is requested to be authorized
/// The callback supplies the password to be used to make the token
///
std::function<bool(const std::string&,std::string& passwd)> mNewAccessRequestCallback;
std::function<bool(const std::string&, const std::string& passwd)>
mNewAccessRequestCallback;
/// Encrypted persistent storage for authorized JSON API tokens
JsonApiServerAuthTokenStorage mAuthTokenStorage;
@ -182,10 +175,21 @@ private:
RsGenericSerializer::SerializeContext& ctx,
const std::shared_ptr<rb::Session> session )
{
return checkRsServicePtrReady( serviceInstance.get(), serviceName, ctx, session );
return checkRsServicePtrReady(
serviceInstance.get(), serviceName, ctx, session );
}
std::vector<std::shared_ptr<rb::Resource> > mResources;
std::set<const JsonApiResourceProvider *> mResourceProviders;
std::vector<std::shared_ptr<rb::Resource>> mResources;
std::set<
std::reference_wrapper<const JsonApiResourceProvider>,
std::less<const JsonApiResourceProvider> > mResourceProviders;
/// @see RsThread
void runloop() override;
std::shared_ptr<restbed::Service> mService;
uint16_t mListeningPort;
std::string mBindingAddress;
};