JSON API authorize token in createLocation

Solve uncoherent 401 behaviour at first use without login
This commit is contained in:
Gioacchino Mazzurco 2018-10-19 15:10:15 +02:00
parent 945775e9f5
commit 9e2c4ce49e
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
3 changed files with 54 additions and 6 deletions

View File

@ -118,6 +118,54 @@ JsonApiServer::JsonApiServer(uint16_t port, const std::string& bindAddress,
mNewAccessRequestCallback(newAccessRequestCallback), mNewAccessRequestCallback(newAccessRequestCallback),
configMutex("JsonApiServer config") configMutex("JsonApiServer config")
{ {
registerHandler("/rsLoginHelper/createLocation",
[this](const std::shared_ptr<rb::Session> session)
{
size_t reqSize = session->get_request()->get_header("Content-Length", 0);
session->fetch( reqSize, [this](
const std::shared_ptr<rb::Session> 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", registerHandler("/rsLoginHelper/attemptLogin",
[this](const std::shared_ptr<rb::Session> session) [this](const std::shared_ptr<rb::Session> session)
{ {

View File

@ -292,20 +292,20 @@ struct RsLoginHelper
/** /**
* @brief Creates a new RetroShare location, and log in once is created * @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 * @param[inout] location provide input information to generate the location
* and storage to output the data of the generated location * and storage to output the data of the generated location
* @param[in] password to protect and unlock the associated PGP key * @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] makeHidden pass true to create an hidden location. UNTESTED!
* @param[in] makeAutoTor pass true to create an automatically configured * @param[in] makeAutoTor pass true to create an automatically configured
* Tor hidden location. UNTESTED! * Tor hidden location. UNTESTED!
* @param[out] errorMessage if some error occurred human readable error
* message
* @return true if success, false otherwise * @return true if success, false otherwise
*/ */
bool createLocation( RsLoginHelper::Location& location, bool createLocation( RsLoginHelper::Location& location,
const std::string& password, bool makeHidden, const std::string& password, std::string& errorMessage,
bool makeAutoTor, std::string& errorMessage ); bool makeHidden = false, bool makeAutoTor = false );
/** /**
* @brief Check if RetroShare is already logged in, this usually return true * @brief Check if RetroShare is already logged in, this usually return true

View File

@ -1952,7 +1952,7 @@ void RsLoginHelper::getLocations(std::vector<RsLoginHelper::Location>& store)
bool RsLoginHelper::createLocation( bool RsLoginHelper::createLocation(
RsLoginHelper::Location& l, const std::string& password, 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); if(isLoggedIn()) return (errorMessage="Already Running", false);