From 4241bcd4e14095e4340a0300e205f6fdc503f1d8 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 14 May 2018 03:07:43 +0200 Subject: Remove user/group classes, user public key/group id directly to database instead --- src/Database.cpp | 91 +++++++++++++++++++++----------------------------------- 1 file changed, 34 insertions(+), 57 deletions(-) (limited to 'src/Database.cpp') diff --git a/src/Database.cpp b/src/Database.cpp index 88ac8e4..9985bb9 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -1,7 +1,5 @@ #include "../include/odhtdb/Database.hpp" #include "../include/odhtdb/Group.hpp" -#include "../include/odhtdb/LocalUser.hpp" -#include "../include/odhtdb/RemoteUser.hpp" #include "../include/odhtdb/Encryption.hpp" #include "../include/odhtdb/DhtKey.hpp" #include "../include/odhtdb/bin2hex.hpp" @@ -61,19 +59,25 @@ namespace odhtdb return DataView(result, allocationSize); } - DatabaseCreateResponse::DatabaseCreateResponse(LocalUser *_nodeAdminUser, shared_ptr _key, shared_ptr _hash) : - nodeAdminUser(_nodeAdminUser), + DatabaseCreateResponse::DatabaseCreateResponse(std::shared_ptr _nodeAdminKeyPair, std::shared_ptr _nodeAdminGroupId, shared_ptr _key, shared_ptr _hash) : + nodeAdminKeyPair(_nodeAdminKeyPair), + nodeAdminGroupId(_nodeAdminGroupId), key(_key), hash(_hash) { } - const LocalUser* DatabaseCreateResponse::getNodeAdminUser() const + const shared_ptr DatabaseCreateResponse::getNodeAdminKeyPair() const { - return nodeAdminUser; + return nodeAdminKeyPair; } - + + const shared_ptr DatabaseCreateResponse::getNodeAdminGroupId() const + { + return nodeAdminGroupId; + } + const shared_ptr DatabaseCreateResponse::getNodeEncryptionKey() const { return key; @@ -262,9 +266,6 @@ namespace odhtdb if(!ok) Log::warn("Failed to put request to get old data"); }); - - //node.listen(CREATE_DATA_HASH, bind(&Database::listenCreateData, this, _1)); - //node.listen(ADD_DATA_HASH, bind(&Database::listenAddData, this, _1)); } void Database::stopSeeding(const Hash &nodeHash) @@ -285,27 +286,22 @@ namespace odhtdb { databaseStorage.loadNode(nodeHash); } - - unique_ptr Database::create() - { - return create(Signature::KeyPair()); - } - unique_ptr Database::create(const Signature::KeyPair &creatorKeyPair) + unique_ptr Database::create() { + shared_ptr creatorKeyPair = make_shared(); + // TODO: Should this be declared static? is there any difference in behavior/performance? boost::uuids::random_generator uuidGen; auto adminGroupId = uuidGen(); assert(adminGroupId.size() == GROUP_ID_LENGTH); - auto adminGroup = new Group(adminGroupId.data, ADMIN_PERMISSION); - LocalUser *nodeAdminUser = LocalUser::create(creatorKeyPair, adminGroup); // Header sibs::SafeSerializer serializer; serializer.add(DATABASE_CREATE_PACKET_STRUCTURE_VERSION); // Packet structure version u64 timestampCombined = getSyncedTimestampUtc().getCombined(); serializer.add(timestampCombined); - serializer.add((u8*)nodeAdminUser->getPublicKey().getData(), PUBLIC_KEY_NUM_BYTES); + serializer.add((u8*)creatorKeyPair->getPublicKey().getData(), PUBLIC_KEY_NUM_BYTES); serializer.add(adminGroupId.data, adminGroupId.size()); try @@ -316,7 +312,7 @@ namespace odhtdb shared_ptr hashRequestKey = make_shared(serializer.getBuffer().data(), serializer.getBuffer().size()); databaseStorage.setNodeDecryptionKey(*hashRequestKey, DataView(encryptionKey->data, encryptionKey->size)); - databaseStorage.createStorage(*hashRequestKey, adminGroup, timestampCombined, (const u8*)serializer.getBuffer().data(), serializer.getBuffer().size()); + databaseStorage.createStorage(*hashRequestKey, creatorKeyPair->getPublicKey(), DataView(adminGroupId.data, adminGroupId.size()), timestampCombined, (const u8*)serializer.getBuffer().data(), serializer.getBuffer().size()); DhtKey dhtKey(*hashRequestKey); Value createDataValue(move(serializer.getBuffer())); @@ -327,7 +323,9 @@ namespace odhtdb Log::warn("Failed to put: %s, what to do?", "Database::create"); }); - return make_unique(nodeAdminUser, encryptionKey, hashRequestKey); + shared_ptr adminGroupIdResponse = make_shared(new u8[GROUP_ID_LENGTH], GROUP_ID_LENGTH); + memcpy(adminGroupIdResponse->data, adminGroupId.data, GROUP_ID_LENGTH); + return make_unique(creatorKeyPair, adminGroupIdResponse, encryptionKey, hashRequestKey); } catch (EncryptionException &e) { @@ -335,9 +333,9 @@ namespace odhtdb } } - void Database::addData(const DatabaseNode &nodeInfo, const LocalUser *userToPerformActionWith, DataView dataToAdd) + void Database::addData(const DatabaseNode &nodeInfo, const Signature::KeyPair &userToPerformActionWith, DataView dataToAdd) { - if(!userToPerformActionWith->isAllowedToPerformAction(PermissionType::ADD_DATA)) + if(!databaseStorage.isUserAllowedToAddDataInNode(*nodeInfo.getRequestHash(), userToPerformActionWith.getPublicKey())) { // TODO: User might have permission to perform operation, but we haven't got the packet that adds user to the group with the permission, // or we haven't received the packet that modifies group with the permission to perform the operation. @@ -345,7 +343,7 @@ namespace odhtdb // and remote peers would accept our request to perform operation if they haven't received the operation that removes the user from the group. // How to handle this? string errMsg = "User "; - errMsg += userToPerformActionWith->getPublicKey().toString(); + errMsg += userToPerformActionWith.getPublicKey().toString(); errMsg += " is not allowed to perform the operation: ADD_USER"; throw PermissionDeniedException(errMsg); } @@ -359,11 +357,11 @@ namespace odhtdb DataView encryptionKey(nodeInfo.getNodeEncryptionKey()->data, ENCRYPTION_KEY_BYTE_SIZE); Encryption encryptedBody(dataToAdd, DataView(), encryptionKey); DataView requestData = combine(serializer, encryptedBody); - string signedRequestData = userToPerformActionWith->getPrivateKey().sign(requestData); - DataView stagedAddObject = combine(userToPerformActionWith->getPublicKey(), signedRequestData); + string signedRequestData = userToPerformActionWith.getPrivateKey().sign(requestData); + DataView stagedAddObject = combine(userToPerformActionWith.getPublicKey(), signedRequestData); Hash requestDataHash(stagedAddObject.data, stagedAddObject.size); DataView encryptedDataView((char*)requestData.data + serializer.getBuffer().size(), requestData.size - serializer.getBuffer().size()); - databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_DATA, userToPerformActionWith->getPublicKey(), timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView); + databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_DATA, userToPerformActionWith.getPublicKey(), timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView); delete[] (char*)requestData.data; DhtKey dhtKey(requestDataHash); @@ -377,32 +375,14 @@ namespace odhtdb }); } - Group* getGroupWithRightsToAddUserToGroup(const vector &groups, Group *groupToAddUserTo) - { - for(auto group : groups) - { - const auto &groupPermission = group->getPermission(); - if(groupPermission.getFlag(PermissionType::ADD_USER_HIGHER_LEVEL) && groupPermission.getPermissionLevel() < groupToAddUserTo->getPermission().getPermissionLevel()) - { - return group; - } - else if(groupPermission.getFlag(PermissionType::ADD_USER_SAME_LEVEL) && groupPermission.getPermissionLevel() == groupToAddUserTo->getPermission().getPermissionLevel()) - { - return group; - } - } - return nullptr; - } - - void Database::addUser(const DatabaseNode &nodeInfo, const LocalUser *userToPerformActionWith, const Signature::PublicKey &userToAddPublicKey, Group *groupToAddUserTo) + void Database::addUser(const DatabaseNode &nodeInfo, const Signature::KeyPair &userToPerformActionWith, const Signature::PublicKey &userToAddPublicKey, const DataView &groupToAddUserTo) { - auto groupWithAddUserRights = getGroupWithRightsToAddUserToGroup(userToPerformActionWith->getGroups(), groupToAddUserTo); - if(!groupWithAddUserRights) + if(!databaseStorage.isUserAllowedToAddUserToGroupInNode(*nodeInfo.getRequestHash(), userToPerformActionWith.getPublicKey(), groupToAddUserTo)) { string errMsg = "The user "; - errMsg += userToPerformActionWith->getPublicKey().toString(); + errMsg += userToPerformActionWith.getPublicKey().toString(); errMsg += " does not belong to any group that is allowed to add an user to the group "; - errMsg += bin2hex((const char*)groupToAddUserTo->getId().data, groupToAddUserTo->getId().size).c_str(); + errMsg += bin2hex((const char*)groupToAddUserTo.data, groupToAddUserTo.size).c_str(); throw PermissionDeniedException(errMsg); } @@ -413,7 +393,7 @@ namespace odhtdb serializer.add(DatabaseOperation::ADD_USER); usize additionalDataOffset = serializer.getBuffer().size(); serializer.add((u8*)userToAddPublicKey.getData(), PUBLIC_KEY_NUM_BYTES); - serializer.add((uint8_t*)groupToAddUserTo->getId().data, groupToAddUserTo->getId().size); + serializer.add((uint8_t*)groupToAddUserTo.data, groupToAddUserTo.size); // TODO: Should this be declared static? is there any difference in behavior/performance? boost::uuids::random_generator uuidGen; @@ -422,11 +402,11 @@ namespace odhtdb serializer.add(padding.data, padding.size()); DataView requestData { serializer.getBuffer().data(), serializer.getBuffer().size() }; - string signedRequestData = userToPerformActionWith->getPrivateKey().sign(requestData); - DataView stagedAddObject = combine(userToPerformActionWith->getPublicKey(), signedRequestData); + string signedRequestData = userToPerformActionWith.getPrivateKey().sign(requestData); + DataView stagedAddObject = combine(userToPerformActionWith.getPublicKey(), signedRequestData); Hash requestDataHash(stagedAddObject.data, stagedAddObject.size); DataView additionalDataView((void*)(static_cast(requestData.data) + additionalDataOffset), requestData.size - additionalDataOffset); - databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_USER, userToPerformActionWith->getPublicKey(), timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, additionalDataView); + databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_USER, userToPerformActionWith.getPublicKey(), timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, additionalDataView); DhtKey dhtKey(requestDataHash); Value addDataValue((u8*)stagedAddObject.data, stagedAddObject.size); @@ -492,10 +472,7 @@ namespace odhtdb if(deserializer.getSize() < ENCRYPTION_NONCE_BYTE_SIZE) throw sibs::DeserializeException("Unsigned encrypted body is too small (unable to extract nonce)"); - auto adminGroup = new Group(adminGroupId, ADMIN_PERMISSION); - // TODO: Username is encrypted, we dont know it... unless we have encryption key, in which case we should modify the user name and set it - auto creatorUser = RemoteUser::create(userPublicKey, adminGroup); - databaseStorage.createStorage(hash, adminGroup, creationDate, value->data.data(), value->data.size()); + databaseStorage.createStorage(hash, userPublicKey, DataView(adminGroupId, GROUP_ID_LENGTH), creationDate, value->data.data(), value->data.size()); } void Database::deserializeAddRequest(const shared_ptr &value, const Hash &requestDataHash, const std::shared_ptr &nodeHash, const shared_ptr encryptionKey) -- cgit v1.2.3