diff --git a/src/jsonapi/jsonapi.cpp b/src/jsonapi/jsonapi.cpp index 27eb0d7ae..bebbba042 100644 --- a/src/jsonapi/jsonapi.cpp +++ b/src/jsonapi/jsonapi.cpp @@ -283,6 +283,65 @@ JsonApiServer::JsonApiServer(): configMutex("JsonApiServer config"), } ); }, false); + registerHandler("/rsLoginHelper/attemptLogin", + [this](const std::shared_ptr session) + { + auto reqSize = session->get_request()->get_header("Content-Length", 0); + session->fetch( static_cast(reqSize), [this]( + const std::shared_ptr 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 session) { diff --git a/src/retroshare/rsinit.h b/src/retroshare/rsinit.h index 11c6d9190..eccc058d0 100644 --- a/src/retroshare/rsinit.h +++ b/src/retroshare/rsinit.h @@ -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( diff --git a/src/rsserver/rsinit.cc b/src/rsserver/rsinit.cc index c3b17c149..c98d94d61 100644 --- a/src/rsserver/rsinit.cc +++ b/src/rsserver/rsinit.cc @@ -1345,6 +1345,17 @@ int RsServer::StartupRetroShare() // mPluginsManager->loadPlugins(programatically_inserted_plugins) ; +#ifdef RS_JSONAPI + if (rsJsonApi) + { + p3ConfigMgr *cfgmgr = dynamic_cast(mConfigMgr); + if (cfgmgr != nullptr) + { + rsJsonApi->connectToConfigManager(*cfgmgr); + } + } +#endif + /**** Reputation system ****/ p3GxsReputation *mReputations = new p3GxsReputation(mLinkMgr) ;