Expose RsIdentity JSON API

This commit is contained in:
Gioacchino Mazzurco 2019-02-13 17:08:38 -03:00
parent ac1d24dba4
commit 6633e8bb28
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
3 changed files with 411 additions and 72 deletions

View file

@ -21,6 +21,7 @@
* *
*******************************************************************************/
#include <unistd.h>
#include <algorithm>
#include "services/p3idservice.h"
#include "pgp/pgpauxutils.h"
@ -207,6 +208,29 @@ void p3IdService::setNes(RsNetworkExchangeService *nes)
mNes = nes;
}
bool p3IdService::getIdentitiesInfo(
const std::set<RsGxsId>& ids, std::vector<RsGxsIdGroup>& idsInfo )
{
uint32_t token;
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
std::list<RsGxsGroupId> idsList(ids.begin(), ids.end());
if( !requestGroupInfo(token, opts, idsList)
|| waitToken(token) != RsTokenService::COMPLETE ) return false;
return getGroupData(token, idsInfo);
}
bool p3IdService::getIdentitiesSummaries(std::list<RsGroupMetaData>& ids)
{
uint32_t token;
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_META;
if( !requestGroupInfo(token, opts)
|| waitToken(token) != RsTokenService::COMPLETE ) return false;
return getGroupSummary(token, ids);
}
uint32_t p3IdService::idAuthenPolicy()
{
uint32_t policy = 0;
@ -727,6 +751,46 @@ bool p3IdService::isOwnId(const RsGxsId& id)
return std::find(mOwnIds.begin(),mOwnIds.end(),id) != mOwnIds.end() ;
}
bool p3IdService::getOwnSignedIds(std::vector<RsGxsId> ids)
{
ids.clear();
std::chrono::seconds maxWait(5);
auto timeout = std::chrono::steady_clock::now() + maxWait;
while( !ownIdsAreLoaded() && std::chrono::steady_clock::now() < timeout )
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if(ownIdsAreLoaded())
{
RS_STACK_MUTEX(mIdMtx);
ids.reserve(mOwnSignedIds.size());
ids.insert(ids.end(), mOwnSignedIds.begin(), mOwnSignedIds.end());
return true;
}
return false;
}
bool p3IdService::getOwnPseudonimousIds(std::vector<RsGxsId> ids)
{
ids.clear();
std::vector<RsGxsId> signedV;
// this implicitely ensure ids are already loaded ;)
if(!getOwnSignedIds(signedV)) return false;
std::set<RsGxsId> signedS(signedV.begin(), signedV.end());
{
RS_STACK_MUTEX(mIdMtx);
std::copy_if(mOwnIds.begin(), mOwnIds.end(), ids.end(),
[&](const RsGxsId& id) {return !signedS.count(id);});
}
return true;
}
bool p3IdService::getOwnIds(std::list<RsGxsId> &ownIds,bool signed_only)
{
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
@ -742,6 +806,11 @@ bool p3IdService::getOwnIds(std::list<RsGxsId> &ownIds,bool signed_only)
return true ;
}
bool p3IdService::identityToBase64( const RsGxsId& id,
std::string& base64String )
{ return serialiseIdentityToMemory(id, base64String); }
bool p3IdService::serialiseIdentityToMemory( const RsGxsId& id,
std::string& radix_string )
{
@ -803,6 +872,10 @@ void p3IdService::handle_get_serialized_grp(uint32_t token)
mSerialisedIdentities[RsGxsId(id)] = s ;
}
bool p3IdService::identityFromBase64(
const std::string& base64String, RsGxsId& id )
{ return deserialiseIdentityFromMemory(base64String, &id); }
bool p3IdService::deserialiseIdentityFromMemory(const std::string& radix_string,
RsGxsId* id /* = nullptr */)
{
@ -826,6 +899,47 @@ bool p3IdService::deserialiseIdentityFromMemory(const std::string& radix_string,
return true;
}
bool p3IdService::createIdentity(
RsGxsId& id,
const std::string& name, const RsGxsImage& avatar,
bool pseudonimous, const std::string& pgpPassword)
{
if(!pgpPassword.empty())
std::cerr<< __PRETTY_FUNCTION__ << " Warning! PGP Password handling "
<< "not implemented yet!" << std::endl;
RsIdentityParameters params;
params.isPgpLinked = !pseudonimous;
params.nickname = name;
params.mImage = avatar;
uint32_t token;
if(!createIdentity(token, params))
{
std::cerr << __PRETTY_FUNCTION__ << " Error! Failed creating group."
<< std::endl;
return false;
}
if(waitToken(token) != RsTokenService::COMPLETE)
{
std::cerr << __PRETTY_FUNCTION__ << " Error! GXS operation failed."
<< std::endl;
return false;
}
RsGroupMetaData meta;
if(!RsGenExchange::getPublishedGroupMeta(token, meta))
{
std::cerr << __PRETTY_FUNCTION__ << "Error! Failure getting updated "
<< " group data." << std::endl;
return false;
}
id = RsGxsId(meta.mGroupId);
return true;
}
bool p3IdService::createIdentity(uint32_t& token, RsIdentityParameters &params)
{
@ -863,6 +977,26 @@ bool p3IdService::createIdentity(uint32_t& token, RsIdentityParameters &params)
return true;
}
bool p3IdService::updateIdentity(RsGxsIdGroup& identityData)
{
uint32_t token;
if(!updateGroup(token, identityData))
{
std::cerr << __PRETTY_FUNCTION__ << "Error! Failed updating group."
<< std::endl;
return false;
}
if(waitToken(token) != RsTokenService::COMPLETE)
{
std::cerr << __PRETTY_FUNCTION__ << "Error! GXS operation failed."
<< std::endl;
return false;
}
return true;
}
bool p3IdService::updateIdentity(uint32_t& token, RsGxsIdGroup &group)
{
#ifdef DEBUG_IDS
@ -876,6 +1010,27 @@ bool p3IdService::updateIdentity(uint32_t& token, RsGxsIdGroup &group)
return false;
}
bool p3IdService::deleteIdentity(RsGxsId& id)
{
uint32_t token;
RsGxsGroupId grouId = RsGxsGroupId(id);
if(!deleteGroup(token, grouId))
{
std::cerr << __PRETTY_FUNCTION__ << "Error! Failed deleting group."
<< std::endl;
return false;
}
if(waitToken(token) != RsTokenService::COMPLETE)
{
std::cerr << __PRETTY_FUNCTION__ << "Error! GXS operation failed."
<< std::endl;
return false;
}
return true;
}
bool p3IdService::deleteIdentity(uint32_t& token, RsGxsIdGroup &group)
{
#ifdef DEBUG_IDS
@ -883,7 +1038,7 @@ bool p3IdService::deleteIdentity(uint32_t& token, RsGxsIdGroup &group)
std::cerr << std::endl;
#endif
deleteGroup(token, group);
deleteGroup(token, group.mMeta.mGroupId);
return false;
}
@ -1796,16 +1951,16 @@ bool p3IdService::updateGroup(uint32_t& token, RsGxsIdGroup &group)
return true;
}
bool p3IdService::deleteGroup(uint32_t& token, RsGxsIdGroup &group)
bool p3IdService::deleteGroup(uint32_t& token, RsGxsGroupId& groupId)
{
RsGxsId id(group.mMeta.mGroupId);
RsGxsId id(groupId);
#ifdef DEBUG_IDS
std::cerr << "p3IdService::deleteGroup() Deleting RsGxsId: " << id;
std::cerr << std::endl;
#endif
RsGenExchange::deleteGroup(token,group.mMeta.mGroupId);
RsGenExchange::deleteGroup(token, groupId);
// if its in the cache - clear it.
{

View file

@ -215,9 +215,8 @@ struct SerialisedIdentityStruct
rstime_t mLastUsageTS;
};
// Not sure exactly what should be inherited here?
// Chris - please correct as necessary.
// We cache all identities, and provide alternative (instantaneous)
// functions to extract info, rather than the horrible Token system.
class p3IdService: public RsGxsIdExchange, public RsIdentity, public GxsTokenQueue, public RsTickEvent, public p3Config
{
public:
@ -239,6 +238,13 @@ public:
/* Data Specific Interface */
/// @see RsIdentity
bool getIdentitiesInfo(const std::set<RsGxsId>& ids,
std::vector<RsGxsIdGroup>& idsInfo ) override;
/// @see RsIdentity
bool getIdentitiesSummaries(std::list<RsGroupMetaData>& ids) override;
// These are exposed via RsIdentity.
virtual bool getGroupData(const uint32_t &token, std::vector<RsGxsIdGroup> &groups);
virtual bool getGroupSerializedData(const uint32_t &token, std::map<RsGxsId,std::string>& serialized_groups);
@ -248,7 +254,7 @@ public:
// These are local - and not exposed via RsIdentity.
virtual bool createGroup(uint32_t& token, RsGxsIdGroup &group);
virtual bool updateGroup(uint32_t& token, RsGxsIdGroup &group);
virtual bool deleteGroup(uint32_t& token, RsGxsIdGroup &group);
virtual bool deleteGroup(uint32_t& token, RsGxsGroupId& group);
//virtual bool createMsg(uint32_t& token, RsGxsIdOpinion &opinion);
/**************** RsIdentity External Interface.
@ -263,12 +269,28 @@ public:
//virtual bool getNickname(const RsGxsId &id, std::string &nickname);
virtual bool getIdDetails(const RsGxsId &id, RsIdentityDetails &details);
//
RS_DEPRECATED_FOR(RsReputations)
virtual bool submitOpinion(uint32_t& token, const RsGxsId &id,
bool absOpinion, int score);
/// @see RsIdentity
virtual bool createIdentity(
RsGxsId& id,
const std::string& name, const RsGxsImage& avatar = RsGxsImage(),
bool pseudonimous = true, const std::string& pgpPassword = "" ) override;
virtual bool createIdentity(uint32_t& token, RsIdentityParameters &params);
/// @see RsIdentity
bool updateIdentity(RsGxsIdGroup& identityData) override;
RS_DEPRECATED
virtual bool updateIdentity(uint32_t& token, RsGxsIdGroup &group);
/// @see RsIdentity
bool deleteIdentity(RsGxsId& id) override;
RS_DEPRECATED
virtual bool deleteIdentity(uint32_t& token, RsGxsIdGroup &group);
virtual void setDeleteBannedNodesThreshold(uint32_t days) ;
@ -289,6 +311,12 @@ public:
/**************** RsGixs Implementation ***************/
/// @see RsIdentity
bool getOwnSignedIds(std::vector<RsGxsId> ids) override;
/// @see RsIdentity
bool getOwnPseudonimousIds(std::vector<RsGxsId> ids) override;
virtual bool getOwnIds(std::list<RsGxsId> &ownIds, bool signed_only = false);
//virtual bool getPublicKey(const RsGxsId &id, RsTlvSecurityKey &key) ;
@ -350,6 +378,15 @@ public:
const RsIdentityUsage &use_info );
virtual bool requestPrivateKey(const RsGxsId &id);
/// @see RsIdentity
bool identityToBase64( const RsGxsId& id,
std::string& base64String ) override;
/// @see RsIdentity
bool identityFromBase64( const std::string& base64String,
RsGxsId& id ) override;
virtual bool serialiseIdentityToMemory(const RsGxsId& id,
std::string& radix_string);
virtual bool deserialiseIdentityFromMemory(const std::string& radix_string,
@ -599,7 +636,9 @@ private:
rstime_t mLastKeyCleaningTime ;
rstime_t mLastConfigUpdate ;
bool mOwnIdsLoaded ;
bool mOwnIdsLoaded;
bool ownIdsAreLoaded() { RS_STACK_MUTEX(mIdMtx); return mOwnIdsLoaded; }
bool mAutoAddFriendsIdentitiesAsContacts;
uint32_t mMaxKeepKeysBanned ;
};