mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
add controller classes for new db tables crypto_keys and hedera_ids, update model tables for them
This commit is contained in:
parent
71837dbe61
commit
d8766f8d2d
@ -146,3 +146,26 @@ bool KeyPairHedera::verify(const unsigned char* message, size_t messageSize, Mem
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MemoryBin* KeyPairHedera::getCryptedPrivKey(const Poco::AutoPtr<SecretKeyCryptography> password) const
|
||||||
|
{
|
||||||
|
if (password.isNull()) return nullptr;
|
||||||
|
if (!mPrivateKey) return nullptr;
|
||||||
|
|
||||||
|
MemoryBin* encryptedKey = nullptr;
|
||||||
|
if (SecretKeyCryptography::AUTH_ENCRYPT_OK == password->encrypt(mPrivateKey, &encryptedKey)) {
|
||||||
|
return encryptedKey;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
MemoryBin* KeyPairHedera::getPublicKeyCopy() const
|
||||||
|
{
|
||||||
|
auto mm = MemoryManager::getInstance();
|
||||||
|
auto public_key = mm->getFreeMemory(ed25519_pubkey_SIZE);
|
||||||
|
memcpy(*public_key, mPublicKey, ed25519_pubkey_SIZE);
|
||||||
|
return public_key;
|
||||||
|
}
|
||||||
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "sodium.h"
|
#include "sodium.h"
|
||||||
|
#include "SecretKeyCryptography.h"
|
||||||
#include "iroha-ed25519/include/ed25519/ed25519.h"
|
#include "iroha-ed25519/include/ed25519/ed25519.h"
|
||||||
|
|
||||||
class KeyPairHedera : public IKeyPair
|
class KeyPairHedera : public IKeyPair
|
||||||
@ -34,6 +35,7 @@ public:
|
|||||||
bool verify(const unsigned char* message, size_t messageSize, MemoryBin* signature) const;
|
bool verify(const unsigned char* message, size_t messageSize, MemoryBin* signature) const;
|
||||||
|
|
||||||
inline const unsigned char* getPublicKey() const { return mPublicKey; }
|
inline const unsigned char* getPublicKey() const { return mPublicKey; }
|
||||||
|
MemoryBin* getPublicKeyCopy() const;
|
||||||
|
|
||||||
inline bool isTheSame(const KeyPairHedera& b) const {
|
inline bool isTheSame(const KeyPairHedera& b) const {
|
||||||
return 0 == sodium_memcmp(mPublicKey, b.mPublicKey, ed25519_pubkey_SIZE);
|
return 0 == sodium_memcmp(mPublicKey, b.mPublicKey, ed25519_pubkey_SIZE);
|
||||||
@ -60,7 +62,8 @@ public:
|
|||||||
|
|
||||||
inline bool hasPrivateKey() const { return mPrivateKey != nullptr; }
|
inline bool hasPrivateKey() const { return mPrivateKey != nullptr; }
|
||||||
|
|
||||||
|
//! \brief only way to get a private key.. encrypted
|
||||||
|
MemoryBin* getCryptedPrivKey(const Poco::AutoPtr<SecretKeyCryptography> password) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
|||||||
@ -44,7 +44,7 @@ bool SessionManager::init()
|
|||||||
switch (i) {
|
switch (i) {
|
||||||
//case VALIDATE_NAME: mValidations[i] = new Poco::RegularExpression("/^[a-zA-Z_ -]{3,}$/"); break;
|
//case VALIDATE_NAME: mValidations[i] = new Poco::RegularExpression("/^[a-zA-Z_ -]{3,}$/"); break;
|
||||||
case VALIDATE_NAME: mValidations[i] = new Poco::RegularExpression("^[^<>&;]{3,}$"); break;
|
case VALIDATE_NAME: mValidations[i] = new Poco::RegularExpression("^[^<>&;]{3,}$"); break;
|
||||||
case VALIDATE_EMAIL: mValidations[i] = new Poco::RegularExpression("^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$"); break;
|
case VALIDATE_EMAIL: mValidations[i] = new Poco::RegularExpression("^[a-zA-Z0-9.!#$%&<EFBFBD>*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$"); break;
|
||||||
case VALIDATE_PASSWORD: mValidations[i] = new Poco::RegularExpression("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!%*?&+-_])[A-Za-z0-9@$!%*?&+-_]{8,}$"); break;
|
case VALIDATE_PASSWORD: mValidations[i] = new Poco::RegularExpression("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!%*?&+-_])[A-Za-z0-9@$!%*?&+-_]{8,}$"); break;
|
||||||
case VALIDATE_PASSPHRASE: mValidations[i] = new Poco::RegularExpression("^(?:[a-z]* ){23}[a-z]*\s*$"); break;
|
case VALIDATE_PASSPHRASE: mValidations[i] = new Poco::RegularExpression("^(?:[a-z]* ){23}[a-z]*\s*$"); break;
|
||||||
case VALIDATE_HAS_NUMBER: mValidations[i] = new Poco::RegularExpression(".*[0-9].*"); break;
|
case VALIDATE_HAS_NUMBER: mValidations[i] = new Poco::RegularExpression(".*[0-9].*"); break;
|
||||||
@ -354,6 +354,7 @@ Session* SessionManager::getSession(int handle)
|
|||||||
//mWorkingMutex.lock();
|
//mWorkingMutex.lock();
|
||||||
auto it = mRequestSessionMap.find(handle);
|
auto it = mRequestSessionMap.find(handle);
|
||||||
if (it != mRequestSessionMap.end()) {
|
if (it != mRequestSessionMap.end()) {
|
||||||
|
printf("[SessionManager::getSession] found existing session, try if active...\n");
|
||||||
result = it->second;
|
result = it->second;
|
||||||
int iResult = result->isActive();
|
int iResult = result->isActive();
|
||||||
if (iResult == -1) {
|
if (iResult == -1) {
|
||||||
@ -489,9 +490,9 @@ Session* SessionManager::findByEmail(const std::string& email)
|
|||||||
mDeadLockedSessionCount++;
|
mDeadLockedSessionCount++;
|
||||||
}
|
}
|
||||||
auto user = it->second->getNewUser();
|
auto user = it->second->getNewUser();
|
||||||
if (email == user->getModel()->getEmail()) {
|
if (email == user->getModel()->getEmail()) {
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mWorkingMutex.unlock();
|
mWorkingMutex.unlock();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -499,12 +500,11 @@ if (email == user->getModel()->getEmail()) {
|
|||||||
|
|
||||||
void SessionManager::checkTimeoutSession()
|
void SessionManager::checkTimeoutSession()
|
||||||
{
|
{
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//Poco::Mutex::ScopedLock _lock(mWorkingMutex, 500);
|
//Poco::Mutex::ScopedLock _lock(mWorkingMutex, 500);
|
||||||
mWorkingMutex.tryLock(500);
|
mWorkingMutex.tryLock(500);
|
||||||
}
|
}
|
||||||
catch (Poco::TimeoutException& ex) {
|
catch (Poco::TimeoutException &ex) {
|
||||||
printf("[SessionManager::checkTimeoutSession] exception timeout mutex: %s\n", ex.displayText().data());
|
printf("[SessionManager::checkTimeoutSession] exception timeout mutex: %s\n", ex.displayText().data());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -515,7 +515,6 @@ void SessionManager::checkTimeoutSession()
|
|||||||
//auto timeout = Poco::Timespan(1, 0);
|
//auto timeout = Poco::Timespan(1, 0);
|
||||||
std::stack<int> toRemove;
|
std::stack<int> toRemove;
|
||||||
for (auto it = mRequestSessionMap.begin(); it != mRequestSessionMap.end(); it++) {
|
for (auto it = mRequestSessionMap.begin(); it != mRequestSessionMap.end(); it++) {
|
||||||
|
|
||||||
if (it->second->tryLock()) {
|
if (it->second->tryLock()) {
|
||||||
// skip already disabled sessions
|
// skip already disabled sessions
|
||||||
if (!it->second->isActive()) {
|
if (!it->second->isActive()) {
|
||||||
|
|||||||
44
src/cpp/controller/CryptoKey.cpp
Normal file
44
src/cpp/controller/CryptoKey.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
|
||||||
|
#include "CryptoKey.h"
|
||||||
|
|
||||||
|
namespace controller {
|
||||||
|
|
||||||
|
CryptoKey::CryptoKey(model::table::CryptoKey* dbModel)
|
||||||
|
{
|
||||||
|
mDBModel = dbModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
CryptoKey::~CryptoKey()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Poco::AutoPtr<CryptoKey> CryptoKey::create(const KeyPairHedera* hederaKeyPair, Poco::AutoPtr<controller::User> user)
|
||||||
|
{
|
||||||
|
auto mm = MemoryManager::getInstance();
|
||||||
|
|
||||||
|
auto encrypted_priv_key = hederaKeyPair->getCryptedPrivKey(user->getPassword());
|
||||||
|
auto public_key = hederaKeyPair->getPublicKeyCopy();
|
||||||
|
|
||||||
|
auto db = new model::table::CryptoKey(encrypted_priv_key, public_key, model::table::KEY_TYPE_ED25519_HEDERA);
|
||||||
|
|
||||||
|
mm->releaseMemory(encrypted_priv_key);
|
||||||
|
mm->releaseMemory(public_key);
|
||||||
|
|
||||||
|
auto cryptoKey = new CryptoKey(db);
|
||||||
|
return Poco::AutoPtr<CryptoKey>(cryptoKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
Poco::AutoPtr<CryptoKey> CryptoKey::load(int id)
|
||||||
|
{
|
||||||
|
auto db = new model::table::CryptoKey();
|
||||||
|
if (1 == db->loadFromDB("id", id)) {
|
||||||
|
auto cryptoKey = new CryptoKey(db);
|
||||||
|
return Poco::AutoPtr<CryptoKey>(cryptoKey);
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
35
src/cpp/controller/CryptoKey.h
Normal file
35
src/cpp/controller/CryptoKey.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#ifndef GRADIDO_LOGIN_SERVER_CONTROLLER_CRYPTO_KEY_INCLUDE
|
||||||
|
#define GRADIDO_LOGIN_SERVER_CONTROLLER_CRYPTO_KEY_INCLUDE
|
||||||
|
|
||||||
|
#include "../model/table/CryptoKey.h"
|
||||||
|
#include "../Crypto/KeyPairHedera.h"
|
||||||
|
|
||||||
|
#include "Poco/SharedPtr.h"
|
||||||
|
|
||||||
|
#include "TableControllerBase.h"
|
||||||
|
#include "User.h"
|
||||||
|
|
||||||
|
namespace controller {
|
||||||
|
class CryptoKey : public TableControllerBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
~CryptoKey();
|
||||||
|
|
||||||
|
static Poco::AutoPtr<CryptoKey> create(const KeyPairHedera* hederaKeyPair, Poco::AutoPtr<controller::User> user);
|
||||||
|
|
||||||
|
//! if returned ptr is NULL, dataset not found
|
||||||
|
static Poco::AutoPtr<CryptoKey> load(int id);
|
||||||
|
|
||||||
|
inline bool deleteFromDB() { return mDBModel->deleteFromDB(); }
|
||||||
|
|
||||||
|
inline Poco::AutoPtr<model::table::CryptoKey> getModel() { return _getModel<model::table::CryptoKey>(); }
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
CryptoKey(model::table::CryptoKey* dbModel);
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //GRADIDO_LOGIN_SERVER_CONTROLLER_CRYPTO_KEY_INCLUDE
|
||||||
32
src/cpp/controller/HederaId.cpp
Normal file
32
src/cpp/controller/HederaId.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include "HederaId.h"
|
||||||
|
|
||||||
|
namespace controller {
|
||||||
|
|
||||||
|
HederaId::HederaId(model::table::HederaId* dbModel)
|
||||||
|
{
|
||||||
|
mDBModel = dbModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
HederaId::~HederaId()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Poco::AutoPtr<HederaId> HederaId::create(Poco::UInt64 shardNum, Poco::UInt64 realmNum, Poco::UInt64 num)
|
||||||
|
{
|
||||||
|
auto db = new model::table::HederaId(shardNum, realmNum, num);
|
||||||
|
|
||||||
|
auto hedera_id = new HederaId(db);
|
||||||
|
return Poco::AutoPtr<HederaId>(hedera_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
Poco::AutoPtr<HederaId> HederaId::load(int id)
|
||||||
|
{
|
||||||
|
auto db = new model::table::HederaId();
|
||||||
|
if (1 == db->loadFromDB("id", id)) {
|
||||||
|
auto cryptoKey = new HederaId(db);
|
||||||
|
return Poco::AutoPtr<HederaId>(cryptoKey);
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/cpp/controller/HederaId.h
Normal file
32
src/cpp/controller/HederaId.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef GRADIDO_LOGIN_SERVER_CONTROLLER_HEDERA_ID_INCLUDE
|
||||||
|
#define GRADIDO_LOGIN_SERVER_CONTROLLER_HEDERA_ID_INCLUDE
|
||||||
|
|
||||||
|
#include "../model/table/HederaId.h"
|
||||||
|
|
||||||
|
#include "Poco/SharedPtr.h"
|
||||||
|
|
||||||
|
#include "TableControllerBase.h"
|
||||||
|
|
||||||
|
namespace controller {
|
||||||
|
class HederaId : public TableControllerBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
~HederaId();
|
||||||
|
|
||||||
|
static Poco::AutoPtr<HederaId> create(Poco::UInt64 shardNum, Poco::UInt64 realmNum, Poco::UInt64 num);
|
||||||
|
|
||||||
|
static Poco::AutoPtr<HederaId> load(int id);
|
||||||
|
|
||||||
|
inline bool deleteFromDB() { return mDBModel->deleteFromDB(); }
|
||||||
|
|
||||||
|
inline Poco::AutoPtr<model::table::HederaId> getModel() { return _getModel<model::table::HederaId>(); }
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
HederaId(model::table::HederaId* dbModel);
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //GRADIDO_LOGIN_SERVER_CONTROLLER_HEDERA_ID_INCLUDE
|
||||||
@ -9,6 +9,22 @@ namespace model {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CryptoKey::CryptoKey(MemoryBin* privateKey, MemoryBin* publicKey, KeyType keyType)
|
||||||
|
: mKeyType(keyType)
|
||||||
|
{
|
||||||
|
if (!privateKey) {
|
||||||
|
mPrivateKey = Poco::Nullable<Poco::Data::BLOB>();
|
||||||
|
} else {
|
||||||
|
mPrivateKey = Poco::Nullable<Poco::Data::BLOB>(Poco::Data::BLOB(*privateKey, privateKey->size()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!publicKey) {
|
||||||
|
mPublicKey = Poco::Nullable<Poco::Data::BLOB>();
|
||||||
|
} else {
|
||||||
|
mPublicKey = Poco::Nullable<Poco::Data::BLOB>(Poco::Data::BLOB(*publicKey, publicKey->size()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CryptoKey::~CryptoKey()
|
CryptoKey::~CryptoKey()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@ -8,8 +8,8 @@ namespace model {
|
|||||||
namespace table {
|
namespace table {
|
||||||
|
|
||||||
enum KeyType {
|
enum KeyType {
|
||||||
KEY_TYPE_SODIUM_ED25519 = 0,
|
KEY_TYPE_ED25519_SODIUM = 0,
|
||||||
KEY_TYPE_ED25519_REF10 = 1,
|
KEY_TYPE_ED25519_HEDERA = 1,
|
||||||
KEY_TYPE_COUNT
|
KEY_TYPE_COUNT
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -17,6 +17,7 @@ namespace model {
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CryptoKey();
|
CryptoKey();
|
||||||
|
CryptoKey(MemoryBin* privateKey, MemoryBin* publicKey, KeyType keyType);
|
||||||
~CryptoKey();
|
~CryptoKey();
|
||||||
|
|
||||||
// generic db operations
|
// generic db operations
|
||||||
|
|||||||
@ -5,6 +5,13 @@ using namespace Poco::Data::Keywords;
|
|||||||
namespace model {
|
namespace model {
|
||||||
namespace table {
|
namespace table {
|
||||||
HederaId::HederaId()
|
HederaId::HederaId()
|
||||||
|
: mShardNum(0), mRealmNum(0), mNum(0)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
HederaId::HederaId(Poco::UInt64 shardNum, Poco::UInt64 realmNum, Poco::UInt64 num)
|
||||||
|
: mShardNum(shardNum), mRealmNum(realmNum), mNum(num)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,7 @@ namespace model {
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HederaId();
|
HederaId();
|
||||||
|
HederaId(Poco::UInt64 shardNum, Poco::UInt64 realmNum, Poco::UInt64 num);
|
||||||
~HederaId();
|
~HederaId();
|
||||||
|
|
||||||
// generic db operations
|
// generic db operations
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user