From 0ff80baed3a566d07f48e15db958ff698c0e1ded Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 28 Jun 2018 12:06:43 +0200 Subject: [PATCH] Extend JSON API with reasonable way to create location --- libretroshare/src/retroshare/rsinit.h | 13 ++++++++++ libretroshare/src/rsserver/rsinit.cc | 35 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/libretroshare/src/retroshare/rsinit.h b/libretroshare/src/retroshare/rsinit.h index 4bd6bfc58..eb176870a 100644 --- a/libretroshare/src/retroshare/rsinit.h +++ b/libretroshare/src/retroshare/rsinit.h @@ -216,6 +216,19 @@ struct RsLoginHelper * @param[out] locations storage for the retrived locations */ void getLocations(std::vector& locations); + + /** + * @brief Creates a new RetroShare location + * @jsonapi{development} + * @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 + * @return true if success, false otherwise + */ + bool createLocation( RsLoginHelper::Location& location, + const std::string& password, + std::string& errorMessage ); }; #endif diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 685afca5b..6c064cd3b 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -1934,6 +1934,41 @@ void RsLoginHelper::getLocations(std::vector& store) } } +bool RsLoginHelper::createLocation( + RsLoginHelper::Location& l, const std::string& password, + std::string& errorMessage ) +{ + if(l.mLocationName.empty()) + { + errorMessage = "Location name is needed"; + return false; + } + + if(l.mPgpId.isNull() && l.mPpgName.empty()) + { + errorMessage = "Either PGP name or PGP id is needed"; + return false; + } + + if(l.mPgpId.isNull() && !RsAccounts::GeneratePGPCertificate( + l.mPpgName, "", password, l.mPgpId, 4096, errorMessage) ) + { + errorMessage = "Failure creating PGP key: " + errorMessage; + return false; + } + + if(!rsNotify->cachePgpPassphrase(password)) return false; + if(!rsNotify->setDisableAskPassword(true)) return false; + + bool ret = RsAccounts::GenerateSSLCertificate( + l.mPgpId, "", l.mLocationName, "", false, + RSRandom::random_alphaNumericString( + RsInit::getSslPwdLen() ), l.mLocationId, errorMessage ); + + rsNotify->setDisableAskPassword(false); + return ret; +} + void RsLoginHelper::Location::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx )