Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/jsonapi/jsonapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,65 @@ JsonApiServer::JsonApiServer(): configMutex("JsonApiServer config"),
} );
}, false);

registerHandler("/rsLoginHelper/attemptLogin",
[this](const std::shared_ptr<rb::Session> session)
{
auto reqSize = session->get_request()->get_header("Content-Length", 0);
session->fetch( static_cast<size_t>(reqSize), [this](
const std::shared_ptr<rb::Session> session,
const rb::Bytes& body )
{
INITIALIZE_API_CALL_JSON_CONTEXT;

RsPeerId account;
std::string password;

// JSON API only
std::string apiUser;
std::string apiPass;

// deserialize input parameters from JSON
{
RsGenericSerializer::SerializeContext& ctx(cReq);
RsGenericSerializer::SerializeJob j(RsGenericSerializer::FROM_JSON);
RS_SERIAL_PROCESS(account);
RS_SERIAL_PROCESS(password);

// JSON API only
RS_SERIAL_PROCESS(apiUser);
RS_SERIAL_PROCESS(apiPass);
}

RsInit::LoadCertificateStatus retval;

if(!apiUser.empty() && badApiCredientalsFormat(apiUser, apiPass))
{
retval = RsInit::LoadCertificateStatus::ERR_UNKNOWN;
}
else
{
// Call retroshare C++ API
retval = rsLoginHelper->attemptLogin(account, password);

// If login succeeded and custom API credentials were provided, authorize them!
if(retval == RsInit::OK && !apiUser.empty())
{
authorizeUser(apiUser, apiPass);
}
}

// serialize out parameters and return value to JSON
{
RsGenericSerializer::SerializeContext& ctx(cAns);
RsGenericSerializer::SerializeJob j(RsGenericSerializer::TO_JSON);
RS_SERIAL_PROCESS(retval);
}

// return them to the API caller
DEFAULT_API_CALL_JSON_RETURN(rb::OK);
} );
}, false);

registerHandler("/rsControl/rsGlobalShutDown",
[](const std::shared_ptr<rb::Session> session)
{
Expand Down
11 changes: 10 additions & 1 deletion src/retroshare/rsinit.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,18 @@ class RsLoginHelper

/**
* @brief Normal way to attempt login
* @jsonapi{development,unauthenticated}
* @jsonapi{development,manualwrapper}
* unauthenticated
* @param[in] account Id of the account to which attempt login
* @param[in] password Password for the given account
* param[in] apiUser (JSON API only) string containing username for JSON API
* so it can be later used to authenticate JSON API calls. It is passed
* down to @see RsJsonApi::authorizeUser under the hood.
* param[in] apiPass (JSON API only) string containing password for JSON API
* so it can be later used to authenticate JSON API calls. It is passed
* down to @see RsJsonApi::authorizeUser under the hood.
* To improve security we strongly advise to not use the same as the
* password used for the PGP key.
* @return RsInit::OK if login attempt success, error code otherwhise
*/
RsInit::LoadCertificateStatus attemptLogin(
Expand Down
11 changes: 11 additions & 0 deletions src/rsserver/rsinit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,17 @@ int RsServer::StartupRetroShare()
//
mPluginsManager->loadPlugins(programatically_inserted_plugins) ;

#ifdef RS_JSONAPI
if (rsJsonApi)
{
p3ConfigMgr *cfgmgr = dynamic_cast<p3ConfigMgr*>(mConfigMgr);
if (cfgmgr != nullptr)
{
rsJsonApi->connectToConfigManager(*cfgmgr);
}
}
#endif

/**** Reputation system ****/

p3GxsReputation *mReputations = new p3GxsReputation(mLinkMgr) ;
Expand Down
Loading