Improve API to create locations

Improve on top of sehraf work, add documentation for API token params
Deprecate old version and disable it once RS 0.6.6 is released
New method take relevant paramethers explicitely instead of having them
  incapsualted inside a RsLoginHelper::Location struct
New method return a proper error_condition instead of boolean + string
RsLoginHelper::attemptLogin doesn't need manual wrapper anymore
This commit is contained in:
Gioacchino Mazzurco 2020-06-09 20:13:22 +02:00
parent 275bdba132
commit 4a34cfaf11
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
4 changed files with 194 additions and 22 deletions

View file

@ -114,6 +114,8 @@ RsLoginHelper* rsLoginHelper = nullptr;
RsAccounts* rsAccounts = nullptr;
const RsInitErrorCategory RsInitErrorCategory::instance;
RsConfigOptions::RsConfigOptions()
:
autoLogin(false),
@ -1950,6 +1952,47 @@ void RsLoginHelper::getLocations(std::vector<RsLoginHelper::Location>& store)
}
}
std::error_condition RsLoginHelper::createLocationV2(
RsPeerId& locationId, RsPgpId& pgpId,
const std::string& locationName, const std::string& pgpName,
const std::string& password )
{
if(isLoggedIn()) return RsInitErrorNum::ALREADY_LOGGED_IN;
if(locationName.empty()) return RsInitErrorNum::INVALID_LOCATION_NAME;
if(pgpId.isNull() && pgpName.empty())
return RsInitErrorNum::PGP_NAME_OR_ID_NEEDED;
std::string errorMessage;
if(pgpId.isNull() && !RsAccounts::GeneratePGPCertificate(
pgpName, "", password, pgpId, 4096, errorMessage ) )
{
RS_ERR("Failure creating PGP key: ", errorMessage);
return RsInitErrorNum::PGP_KEY_CREATION_FAILED;
}
std::string sslPassword =
RsRandom::random_alphaNumericString(RsInit::getSslPwdLen());
rsNotify->cachePgpPassphrase(password);
rsNotify->setDisableAskPassword(true);
bool ret = RsAccounts::createNewAccount(
pgpId, "", locationName, "", false, false, sslPassword,
locationId, errorMessage );
if(!ret)
{
RS_ERR("Failure creating SSL key: ", errorMessage);
return RsInitErrorNum::SSL_KEY_CREATION_FAILED;
}
RsInit::LoadPassword(sslPassword);
ret = (RsInit::OK == attemptLogin(locationId, password));
rsNotify->setDisableAskPassword(false);
return (ret ? std::error_condition() : RsInitErrorNum::LOGIN_FAILED);
}
#if !RS_VERSION_AT_LEAST(0,6,6)
bool RsLoginHelper::createLocation(
RsLoginHelper::Location& l, const std::string& password,
std::string& errorMessage, bool makeHidden, bool makeAutoTor )
@ -1991,6 +2034,7 @@ bool RsLoginHelper::createLocation(
rsNotify->setDisableAskPassword(false);
return ret;
}
#endif // !RS_VERSION_AT_LEAST(0,6,6)
bool RsLoginHelper::isLoggedIn()
{