2018-11-10 08:45:05 -05:00
|
|
|
/*******************************************************************************
|
|
|
|
* RetroShare JSON API *
|
2019-04-11 20:53:39 -04:00
|
|
|
* *
|
|
|
|
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
|
2018-11-10 08:45:05 -05:00
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
2019-04-11 20:53:39 -04:00
|
|
|
* it under the terms of the GNU Affero General Public License as *
|
2018-11-10 08:45:05 -05:00
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
2019-04-11 20:53:39 -04:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Affero General Public License for more details. *
|
2018-11-10 08:45:05 -05:00
|
|
|
* *
|
2019-04-11 20:53:39 -04:00
|
|
|
* You should have received a copy of the GNU Affero General Public License *
|
2019-04-14 06:34:01 -04:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
2018-11-10 08:45:05 -05:00
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2018-09-19 15:28:26 -04:00
|
|
|
#pragma once
|
2018-06-23 11:13:38 -04:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
#include <restbed>
|
2018-09-19 15:28:26 -04:00
|
|
|
#include <cstdint>
|
2018-09-25 16:33:35 -04:00
|
|
|
#include <map>
|
2018-06-23 11:13:38 -04:00
|
|
|
|
2018-09-19 15:28:26 -04:00
|
|
|
#include "util/rsthreads.h"
|
|
|
|
#include "pqi/p3cfgmgr.h"
|
|
|
|
#include "rsitems/rsitem.h"
|
|
|
|
#include "jsonapi/jsonapiitems.h"
|
2019-11-15 18:02:02 -05:00
|
|
|
#include "jsonapi/restbedservice.h"
|
|
|
|
#include "retroshare/rsjsonapi.h"
|
2018-06-23 11:13:38 -04:00
|
|
|
#include "util/rsthreads.h"
|
|
|
|
|
|
|
|
namespace rb = restbed;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple usage
|
|
|
|
* \code{.cpp}
|
|
|
|
* JsonApiServer jas(9092);
|
2018-09-19 15:28:26 -04:00
|
|
|
* jsonApiServer = &jas;
|
2018-06-23 11:13:38 -04:00
|
|
|
* jas.start("JsonApiServer");
|
|
|
|
* \endcode
|
2018-09-19 15:28:26 -04:00
|
|
|
* Uses p3Config to securely store persistent JSON API authorization tokens
|
2018-06-23 11:13:38 -04:00
|
|
|
*/
|
2019-11-15 18:02:02 -05:00
|
|
|
class JsonApiServer : public p3Config, public RestbedService, public RsJsonAPI
|
2018-06-23 11:13:38 -04:00
|
|
|
{
|
2019-11-15 18:02:02 -05:00
|
|
|
public:
|
|
|
|
JsonApiServer() ;
|
|
|
|
virtual ~JsonApiServer() = default;
|
2018-06-23 11:13:38 -04:00
|
|
|
|
2019-11-15 18:02:02 -05:00
|
|
|
// public API
|
2018-09-19 15:28:26 -04:00
|
|
|
|
2019-11-15 18:02:02 -05:00
|
|
|
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;
|
2019-11-12 16:32:18 -05:00
|
|
|
|
2019-11-17 16:01:49 -05:00
|
|
|
virtual void connectToConfigManager(p3ConfigMgr *cfgmgr);
|
|
|
|
|
2019-11-15 18:02:02 -05:00
|
|
|
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;
|
2018-09-19 15:28:26 -04:00
|
|
|
|
2019-11-16 08:30:36 -05:00
|
|
|
// private API. These methods may be moved to RsJsonAPI so as to be visible in http mode, if needed.
|
2019-11-11 16:48:35 -05:00
|
|
|
|
2018-09-19 15:28:26 -04:00
|
|
|
/**
|
|
|
|
* @brief Get decoded version of the given encoded token
|
|
|
|
* @jsonapi{development,unauthenticated}
|
2019-11-11 16:48:35 -05:00
|
|
|
* @param[in] radix64_token encoded
|
2018-09-19 15:28:26 -04:00
|
|
|
* @return token decoded
|
|
|
|
*/
|
2019-11-11 16:48:35 -05:00
|
|
|
static std::string decodeToken(const std::string& radix64_token);
|
2018-09-19 15:28:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get encoded version of the given decoded token
|
|
|
|
* @jsonapi{development,unauthenticated}
|
2019-11-11 16:48:35 -05:00
|
|
|
* @param[in] clear_token decoded
|
2018-09-19 15:28:26 -04:00
|
|
|
* @return token encoded
|
|
|
|
*/
|
2019-11-11 16:48:35 -05:00
|
|
|
static std::string encodeToken(const std::string& clear_token);
|
2018-09-19 15:28:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 );
|
|
|
|
|
2019-11-12 16:32:18 -05:00
|
|
|
|
2019-11-15 18:02:02 -05:00
|
|
|
// /!\ 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 );
|
|
|
|
|
2019-11-12 16:32:18 -05:00
|
|
|
protected:
|
2018-09-19 15:28:26 -04:00
|
|
|
/// @see RsSingleJobThread
|
|
|
|
virtual void run();
|
2019-11-15 18:02:02 -05:00
|
|
|
virtual std::vector<std::shared_ptr<rb::Resource> > getResources() const;
|
2018-06-28 09:04:06 -04:00
|
|
|
|
2018-06-23 11:13:38 -04:00
|
|
|
private:
|
2019-11-12 16:32:18 -05:00
|
|
|
|
2018-09-19 15:28:26 -04:00
|
|
|
/// @see p3Config::setupSerialiser
|
|
|
|
virtual RsSerialiser* setupSerialiser();
|
|
|
|
|
|
|
|
/// @see p3Config::saveList
|
|
|
|
virtual bool saveList(bool &cleanup, std::list<RsItem *>& saveItems);
|
|
|
|
|
|
|
|
/// @see p3Config::loadList
|
|
|
|
virtual bool loadList(std::list<RsItem *>& loadList);
|
|
|
|
|
|
|
|
/// @see p3Config::saveDone
|
|
|
|
virtual void saveDone();
|
|
|
|
|
2019-11-12 16:32:18 -05:00
|
|
|
uint16_t mPort;
|
|
|
|
std::string mBindAddress;
|
2018-06-28 09:04:06 -04:00
|
|
|
rb::Service mService;
|
2018-09-19 15:28:26 -04:00
|
|
|
|
|
|
|
/// Called when new JSON API auth token is requested to be authorized
|
2019-11-15 18:02:02 -05:00
|
|
|
/// The callback supplies the password to be used to make the token
|
|
|
|
///
|
|
|
|
std::function<bool(const std::string&,std::string& passwd)> mNewAccessRequestCallback;
|
2018-09-19 15:28:26 -04:00
|
|
|
|
|
|
|
/// Encrypted persistent storage for authorized JSON API tokens
|
|
|
|
JsonApiServerAuthTokenStorage mAuthTokenStorage;
|
|
|
|
RsMutex configMutex;
|
2018-09-25 16:33:35 -04:00
|
|
|
|
|
|
|
static const std::multimap<std::string, std::string> corsHeaders;
|
|
|
|
static const std::multimap<std::string, std::string> corsOptionsHeaders;
|
|
|
|
static void handleCorsOptions(const std::shared_ptr<rb::Session> session);
|
|
|
|
|
|
|
|
static bool checkRsServicePtrReady(
|
2019-04-11 20:53:39 -04:00
|
|
|
const void* serviceInstance, const std::string& serviceName,
|
2018-09-25 16:33:35 -04:00
|
|
|
RsGenericSerializer::SerializeContext& ctx,
|
2019-11-15 18:02:02 -05:00
|
|
|
const std::shared_ptr<rb::Session> session );
|
2019-04-11 20:53:39 -04:00
|
|
|
|
|
|
|
static inline bool checkRsServicePtrReady(
|
|
|
|
const std::shared_ptr<const void> serviceInstance,
|
|
|
|
const std::string& serviceName,
|
|
|
|
RsGenericSerializer::SerializeContext& ctx,
|
2019-11-15 18:02:02 -05:00
|
|
|
const std::shared_ptr<rb::Session> session )
|
2019-04-11 20:53:39 -04:00
|
|
|
{
|
|
|
|
return checkRsServicePtrReady(
|
|
|
|
serviceInstance.get(), serviceName, ctx, session );
|
|
|
|
}
|
2019-11-12 16:32:18 -05:00
|
|
|
|
2019-11-15 18:02:02 -05:00
|
|
|
std::vector<std::shared_ptr<rb::Resource> > _resources;
|
2018-06-23 11:13:38 -04:00
|
|
|
};
|
|
|
|
|