Expose more login related JSON API

/rsLoginHelper/isLoggedIn to check if already logged in
/rsAccounts/getCurrentAccountId to get the id of current selected
  account, beware that an account may be selected without actually logging in
This commit is contained in:
Gioacchino Mazzurco 2018-08-30 19:06:20 +02:00
parent a9b1a15b43
commit a8ddec03fc
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
7 changed files with 105 additions and 55 deletions

View File

@ -53,8 +53,13 @@ public:
static RsControl *instance();
static void earlyInitNotificationSystem() { instance(); }
/* Real Startup Fn */
virtual int StartupRetroShare() = 0;
/* Real Startup Fn */
virtual int StartupRetroShare() = 0;
/** Check if core is fully ready, true only after
* StartupRetroShare() finish and before rsGlobalShutDown() begin
*/
virtual bool isReady() = 0;
/****************************************/
/* Config */

View File

@ -51,6 +51,15 @@ struct RsLoginHelper;
*/
extern RsLoginHelper* rsLoginHelper;
class RsAccounts;
/**
* Pointer to global instance of RsAccounts needed to expose JSON API
* @jsonapi{development}
*/
extern RsAccounts* rsAccounts;
/*!
* Initialisation Class (not publicly disclosed to RsIFace)
*/
@ -132,31 +141,36 @@ private:
/* Seperate static Class for dealing with Accounts */
class RsAccountsDetail ;
class RsAccountsDetail;
class RsAccounts
{
public:
// Should be called once before everything else.
/// Should be called once before everything else.
static bool init(const std::string &opt_base_dir, int& error_code);
/**
* @brief ConfigDirectory (normally ~/.retroshare) you can call this method
* even before initialisation (you can't with some other methods)
*
* On linux: ~/.retroshare/
*
* @see RsAccountsDetail::PathBaseDirectory()
*/
* @brief ConfigDirectory (usually ~/.retroshare) you can call this method
* even before initialisation (you can't with some other methods)
* @see RsAccountsDetail::PathBaseDirectory()
*/
static std::string ConfigDirectory();
/**
* @brief DataDirectory
* you can call this method even before initialisation (you can't with some other methods)
* @param check if set to true and directory does not exist, return empty string
* @return path where global platform independent files are stored, like bdboot.txt or webinterface files
*/
* @brief Get current account id. Beware that an account may be selected
* without actually logging in.
* @jsonapi{development}
* @param[out] id storage for current account id
* @return false if account hasn't been selected yet, true otherwise
*/
bool getCurrentAccountId(RsPeerId &id);
/**
* @brief DataDirectory
* you can call this method even before initialisation (you can't with some other methods)
* @param check if set to true and directory does not exist, return empty string
* @return path where global platform independent files are stored, like bdboot.txt or webinterface files
*/
static std::string systemDataDirectory(bool check = true);
static std::string PGPDirectory();
@ -207,7 +221,7 @@ public:
static void unlockPreferredAccount() ;
private:
static RsAccountsDetail *rsAccounts ;
static RsAccountsDetail* rsAccountsDetails;
};
@ -263,6 +277,14 @@ struct RsLoginHelper
const std::string& password, bool makeHidden,
bool makeAutoTor, std::string& errorMessage );
/**
* @brief Check if RetroShare is already logged in, this usually return true
* after a successfull attemptLogin() and before closeSession()
* @jsonapi{development}
* @return true if already logged in, false otherwise
*/
bool isLoggedIn();
/**
* @brief Close RetroShare session
* @jsonapi{development}

View File

@ -83,6 +83,7 @@ void RsServer::startServiceThread(RsTickingThread *t, const std::string &threadN
void RsServer::rsGlobalShutDown()
{
coreReady = false;
// TODO: cache should also clean up old files
ConfigFinalSave(); // save configuration before exit

View File

@ -79,8 +79,8 @@ const double RsServer::maxTimeDelta = 0.2;
const double RsServer::kickLimit = 0.15;
RsServer::RsServer()
: coreMutex("RsServer")
RsServer::RsServer() :
coreMutex("RsServer"), coreReady(false)
{
// This is needed asap.
//

View File

@ -82,6 +82,9 @@ class RsServer: public RsControl, public RsTickingThread
/****************************************/
/* p3face.cc: main loop / util fns / locking. */
/// @see RsControl::isReady()
virtual bool isReady() { return coreReady; }
RsServer() ;
virtual ~RsServer();
@ -122,11 +125,11 @@ class RsServer: public RsControl, public RsTickingThread
/************* Rs shut down function: in upnp 'port lease time' bug *****************/
/**
* This function is responsible for ensuring Retroshare exits in a legal state:
* i.e. releases all held resources and saves current configuration
*/
virtual void rsGlobalShutDown( );
/**
* This function is responsible for ensuring Retroshare exits in a legal state:
* i.e. releases all held resources and saves current configuration
*/
virtual void rsGlobalShutDown();
/****************************************/
@ -198,6 +201,11 @@ class RsServer: public RsControl, public RsTickingThread
static const double minTimeDelta; // 25;
static const double maxTimeDelta;
static const double kickLimit;
/** Keep track of the core being fully ready, true only after
* StartupRetroShare() finish and before rsGlobalShutDown() begin
*/
bool coreReady;
};
/* Helper function to convert windows paths

View File

@ -49,7 +49,7 @@
#include <openssl/ssl.h>
// Global singleton declaration of data.
RsAccountsDetail *RsAccounts::rsAccounts;
RsAccountsDetail* RsAccounts::rsAccountsDetails = nullptr;
/* Uses private class - so must be hidden */
static bool checkAccount(const std::string &accountdir, AccountDetails &account,std::map<std::string,std::vector<std::string> >& unsupported_keys);
@ -1267,17 +1267,18 @@ bool RsInit::LoadPassword(const std::string& id, const std::string& inPwd)
bool RsAccounts::init(const std::string& opt_base_dir,int& error_code)
{
rsAccounts = new RsAccountsDetail ;
rsAccountsDetails = new RsAccountsDetail;
rsAccounts = new RsAccounts;
// first check config directories, and set bootstrap values.
if(!rsAccounts->setupBaseDirectory(opt_base_dir))
if(!rsAccountsDetails->setupBaseDirectory(opt_base_dir))
{
error_code = RS_INIT_BASE_DIR_ERROR ;
return false ;
}
// Setup PGP stuff.
std::string pgp_dir = rsAccounts->PathPGPDirectory();
std::string pgp_dir = rsAccountsDetails->PathPGPDirectory();
if(!RsDirUtil::checkCreateDirectory(pgp_dir))
throw std::runtime_error("Cannot create pgp directory " + pgp_dir) ;
@ -1288,7 +1289,7 @@ bool RsAccounts::init(const std::string& opt_base_dir,int& error_code)
pgp_dir + "/lock");
// load Accounts.
if (!rsAccounts->loadAccounts())
if (!rsAccountsDetails->loadAccounts())
{
error_code = RS_INIT_NO_KEYRING ;
return false ;
@ -1299,75 +1300,75 @@ bool RsAccounts::init(const std::string& opt_base_dir,int& error_code)
// Directories.
std::string RsAccounts::ConfigDirectory() { return RsAccountsDetail::PathBaseDirectory(); }
std::string RsAccounts::systemDataDirectory(bool check) { return RsAccountsDetail::PathDataDirectory(check); }
std::string RsAccounts::PGPDirectory() { return rsAccounts->PathPGPDirectory(); }
std::string RsAccounts::AccountDirectory() { return rsAccounts->getCurrentAccountPathAccountDirectory(); }
std::string RsAccounts::AccountKeysDirectory() { return rsAccounts->getCurrentAccountPathAccountKeysDirectory(); }
std::string RsAccounts::AccountPathCertFile() { return rsAccounts->getCurrentAccountPathCertFile(); }
std::string RsAccounts::AccountPathKeyFile() { return rsAccounts->getCurrentAccountPathKeyFile(); }
std::string RsAccounts::AccountLocationName() { return rsAccounts->getCurrentAccountLocationName(); }
std::string RsAccounts::PGPDirectory() { return rsAccountsDetails->PathPGPDirectory(); }
std::string RsAccounts::AccountDirectory() { return rsAccountsDetails->getCurrentAccountPathAccountDirectory(); }
std::string RsAccounts::AccountKeysDirectory() { return rsAccountsDetails->getCurrentAccountPathAccountKeysDirectory(); }
std::string RsAccounts::AccountPathCertFile() { return rsAccountsDetails->getCurrentAccountPathCertFile(); }
std::string RsAccounts::AccountPathKeyFile() { return rsAccountsDetails->getCurrentAccountPathKeyFile(); }
std::string RsAccounts::AccountLocationName() { return rsAccountsDetails->getCurrentAccountLocationName(); }
bool RsAccounts::lockPreferredAccount() { return rsAccounts->lockPreferredAccount();} // are these methods any useful??
void RsAccounts::unlockPreferredAccount() { rsAccounts->unlockPreferredAccount(); }
bool RsAccounts::lockPreferredAccount() { return rsAccountsDetails->lockPreferredAccount();} // are these methods any useful??
void RsAccounts::unlockPreferredAccount() { rsAccountsDetails->unlockPreferredAccount(); }
bool RsAccounts::checkCreateAccountDirectory() { return rsAccounts->checkAccountDirectory(); }
bool RsAccounts::checkCreateAccountDirectory() { return rsAccountsDetails->checkAccountDirectory(); }
// PGP Accounts.
int RsAccounts::GetPGPLogins(std::list<RsPgpId> &pgpIds)
{
return rsAccounts->GetPGPLogins(pgpIds);
return rsAccountsDetails->GetPGPLogins(pgpIds);
}
int RsAccounts::GetPGPLoginDetails(const RsPgpId& id, std::string &name, std::string &email)
{
return rsAccounts->GetPGPLoginDetails(id, name, email);
return rsAccountsDetails->GetPGPLoginDetails(id, name, email);
}
bool RsAccounts::GeneratePGPCertificate(const std::string &name, const std::string& email, const std::string& passwd, RsPgpId &pgpId, const int keynumbits, std::string &errString)
{
return rsAccounts->GeneratePGPCertificate(name, email, passwd, pgpId, keynumbits, errString);
return rsAccountsDetails->GeneratePGPCertificate(name, email, passwd, pgpId, keynumbits, errString);
}
// PGP Support Functions.
bool RsAccounts::ExportIdentity(const std::string& fname,const RsPgpId& pgp_id)
{
return rsAccounts->exportIdentity(fname,pgp_id);
return rsAccountsDetails->exportIdentity(fname,pgp_id);
}
bool RsAccounts::ImportIdentity(const std::string& fname,RsPgpId& imported_pgp_id,std::string& import_error)
{
return rsAccounts->importIdentity(fname,imported_pgp_id,import_error);
return rsAccountsDetails->importIdentity(fname,imported_pgp_id,import_error);
}
bool RsAccounts::ImportIdentityFromString(const std::string& data,RsPgpId& imported_pgp_id,std::string& import_error)
{
return rsAccounts->importIdentityFromString(data,imported_pgp_id,import_error);
return rsAccountsDetails->importIdentityFromString(data,imported_pgp_id,import_error);
}
void RsAccounts::GetUnsupportedKeys(std::map<std::string,std::vector<std::string> > &unsupported_keys)
{
return rsAccounts->getUnsupportedKeys(unsupported_keys);
return rsAccountsDetails->getUnsupportedKeys(unsupported_keys);
}
bool RsAccounts::CopyGnuPGKeyrings()
{
return rsAccounts->copyGnuPGKeyrings();
return rsAccountsDetails->copyGnuPGKeyrings();
}
void RsAccounts::storeSelectedAccount() { rsAccounts->storePreferredAccount() ;}
void RsAccounts::storeSelectedAccount() { rsAccountsDetails->storePreferredAccount() ;}
// Rs Accounts
bool RsAccounts::SelectAccount(const RsPeerId &id)
{
return rsAccounts->selectId(id);
return rsAccountsDetails->selectId(id);
}
bool RsAccounts::GetPreferredAccountId(RsPeerId &id)
{
return rsAccounts->getCurrentAccountId(id);
return rsAccountsDetails->getCurrentAccountId(id);
}
bool RsAccounts::getCurrentAccountOptions(bool& is_hidden,bool& is_tor_auto,bool& is_first_time)
{
return rsAccounts->getCurrentAccountOptions(is_hidden,is_tor_auto,is_first_time);
return rsAccountsDetails->getCurrentAccountOptions(is_hidden,is_tor_auto,is_first_time);
}
bool RsAccounts::isHiddenNode()
{
@ -1400,14 +1401,14 @@ bool RsAccounts::isTorAuto()
bool RsAccounts::GetAccountIds(std::list<RsPeerId> &ids)
{
return rsAccounts->getAccountIds(ids);
return rsAccountsDetails->getAccountIds(ids);
}
bool RsAccounts::GetAccountDetails(const RsPeerId &id,
RsPgpId &pgpId, std::string &pgpName,
std::string &pgpEmail, std::string &location)
{
return rsAccounts->getCurrentAccountDetails(id, pgpId, pgpName, pgpEmail, location);
return rsAccountsDetails->getCurrentAccountDetails(id, pgpId, pgpName, pgpEmail, location);
}
bool RsAccounts::createNewAccount(
@ -1415,7 +1416,7 @@ bool RsAccounts::createNewAccount(
const std::string& country, bool ishiddenloc, bool isautotor,
const std::string& passwd, RsPeerId &sslId, std::string &errString )
{
return rsAccounts->GenerateSSLCertificate(pgp_id, org, loc, country, ishiddenloc, isautotor, passwd, sslId, errString);
return rsAccountsDetails->GenerateSSLCertificate(pgp_id, org, loc, country, ishiddenloc, isautotor, passwd, sslId, errString);
}
/*********************************************************************************

View File

@ -101,7 +101,9 @@ RsDht *rsDht = NULL ;
//std::map<std::string,std::vector<std::string> > RsInit::unsupported_keys ;
RsLoginHelper* rsLoginHelper;
RsLoginHelper* rsLoginHelper = nullptr;
RsAccounts* rsAccounts = nullptr;
class RsInitConfig
{
@ -1931,6 +1933,7 @@ int RsServer::StartupRetroShare()
std::cerr << "== RsInit:: Retroshare core started ==" << std::endl;
std::cerr << "========================================================================" << std::endl;
coreReady = true;
return 1;
}
@ -2006,6 +2009,11 @@ bool RsLoginHelper::createLocation(
return ret;
}
bool RsLoginHelper::isLoggedIn()
{
return RsControl::instance()->isReady();
}
void RsLoginHelper::closeSession()
{
RsControl::instance()->rsGlobalShutDown();
@ -2020,3 +2028,8 @@ void RsLoginHelper::Location::serial_process(
RS_SERIAL_PROCESS(mLocationName);
RS_SERIAL_PROCESS(mPpgName);
}
bool RsAccounts::getCurrentAccountId(RsPeerId& id)
{
return rsAccountsDetails->getCurrentAccountId(id);
}