From 9e2c4ce49e845a9d6505fe6b5bd5db8af22b7156 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Fri, 19 Oct 2018 15:10:15 +0200 Subject: [PATCH] JSON API authorize token in createLocation Solve uncoherent 401 behaviour at first use without login --- libretroshare/src/jsonapi/jsonapi.cpp | 48 +++++++++++++++++++++++++++ libretroshare/src/retroshare/rsinit.h | 10 +++--- libretroshare/src/rsserver/rsinit.cc | 2 +- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/libretroshare/src/jsonapi/jsonapi.cpp b/libretroshare/src/jsonapi/jsonapi.cpp index 0a293de93..ac398f92f 100644 --- a/libretroshare/src/jsonapi/jsonapi.cpp +++ b/libretroshare/src/jsonapi/jsonapi.cpp @@ -118,6 +118,54 @@ JsonApiServer::JsonApiServer(uint16_t port, const std::string& bindAddress, mNewAccessRequestCallback(newAccessRequestCallback), configMutex("JsonApiServer config") { + registerHandler("/rsLoginHelper/createLocation", + [this](const std::shared_ptr session) + { + size_t reqSize = session->get_request()->get_header("Content-Length", 0); + session->fetch( reqSize, [this]( + const std::shared_ptr session, + const rb::Bytes& body ) + { + INITIALIZE_API_CALL_JSON_CONTEXT; + + RsLoginHelper::Location location; + std::string password; + std::string errorMessage; + bool makeHidden = false; + bool makeAutoTor = false; + + // deserialize input parameters from JSON + { + RsGenericSerializer::SerializeContext& ctx(cReq); + RsGenericSerializer::SerializeJob j(RsGenericSerializer::FROM_JSON); + RS_SERIAL_PROCESS(location); + RS_SERIAL_PROCESS(password); + RS_SERIAL_PROCESS(makeHidden); + RS_SERIAL_PROCESS(makeAutoTor); + } + + // call retroshare C++ API + bool retval = rsLoginHelper->createLocation( + location, password, errorMessage, makeHidden, + makeAutoTor ); + + if(retval) + authorizeToken(location.mLocationId.toStdString()+":"+password); + + // serialize out parameters and return value to JSON + { + RsGenericSerializer::SerializeContext& ctx(cAns); + RsGenericSerializer::SerializeJob j(RsGenericSerializer::TO_JSON); + RS_SERIAL_PROCESS(location); + RS_SERIAL_PROCESS(errorMessage); + RS_SERIAL_PROCESS(retval); + } + + // return them to the API caller + DEFAULT_API_CALL_JSON_RETURN(rb::OK); + } ); + }, false); + registerHandler("/rsLoginHelper/attemptLogin", [this](const std::shared_ptr session) { diff --git a/libretroshare/src/retroshare/rsinit.h b/libretroshare/src/retroshare/rsinit.h index 5dfade3f8..f81bfaddf 100644 --- a/libretroshare/src/retroshare/rsinit.h +++ b/libretroshare/src/retroshare/rsinit.h @@ -292,20 +292,20 @@ struct RsLoginHelper /** * @brief Creates a new RetroShare location, and log in once is created - * @jsonapi{development,unauthenticated} + * @jsonapi{development,manualwrapper} * @param[inout] location provide input information to generate the location * and storage to output the data of the generated location * @param[in] password to protect and unlock the associated PGP key + * @param[out] errorMessage if some error occurred human readable error + * message * @param[in] makeHidden pass true to create an hidden location. UNTESTED! * @param[in] makeAutoTor pass true to create an automatically configured * Tor hidden location. UNTESTED! - * @param[out] errorMessage if some error occurred human readable error - * message * @return true if success, false otherwise */ bool createLocation( RsLoginHelper::Location& location, - const std::string& password, bool makeHidden, - bool makeAutoTor, std::string& errorMessage ); + const std::string& password, std::string& errorMessage, + bool makeHidden = false, bool makeAutoTor = false ); /** * @brief Check if RetroShare is already logged in, this usually return true diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index fe2f4c991..e3f1f141e 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -1952,7 +1952,7 @@ void RsLoginHelper::getLocations(std::vector& store) bool RsLoginHelper::createLocation( RsLoginHelper::Location& l, const std::string& password, - bool makeHidden, bool makeAutoTor, std::string& errorMessage ) + std::string& errorMessage, bool makeHidden, bool makeAutoTor ) { if(isLoggedIn()) return (errorMessage="Already Running", false);