Merge pull request #2011 from G10h4ck/createLocationV2

Improve API to create locations
This commit is contained in:
G10h4ck 2020-06-10 10:49:32 +02:00 committed by GitHub
commit b49dfaead0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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()
{