diff --git a/CMakeLists.txt b/CMakeLists.txt index d5df00b87..4fbf6c8bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,6 +120,14 @@ if(WIN32) else (WIN32) + find_library(MYSQL_LIBRARIES libmariadb.so PATHS ${MARIADB_CONNECTOR_PATH} REQUIRED) + find_library(CONAN_OPENSSL_SSL ssl PATHS ${CONAN_LIB_DIRS_OPENSSL} REQUIRED NO_DEFAULT_PATH ) + find_library(CONAN_OPENSSL_CRYPTO crypto PATHS ${CONAN_LIB_DIRS_OPENSSL} REQUIRED NO_DEFAULT_PATH ) + set(GRPC_PROTOBUF_DEBUG_PATH "${GRPC_PROTOBUF_PATH}") + + list(REMOVE_ITEM CONAN_LIBS "ssl") + list(REMOVE_ITEM CONAN_LIBS "crypto") + list(REMOVE_ITEM CONAN_LIBS "dl") find_library(MYSQL_LIBRARIES libmariadb.so PATHS ${MARIADB_CONNECTOR_PATH} REQUIRED) find_library(CONAN_OPENSSL_SSL ssl PATHS ${CONAN_LIB_DIRS_OPENSSL} REQUIRED NO_DEFAULT_PATH ) @@ -132,7 +140,6 @@ else (WIN32) endif(WIN32) - # load same ssl version like used from poco #find_package(OpenSSL PATHS . NO_DEFAULT_PATH) @@ -156,7 +163,6 @@ if(WIN32) else(WIN32) - set(INSTALL_BINDIR "bin") set(INSTALL_PLUGINDIR "bin") @@ -167,11 +173,9 @@ else(WIN32) endif(WIN32) - set(GRPC_LIBS libprotobuf grpc++_reflection grpc++) target_link_libraries(Gradido_LoginServer ${CONAN_LIBS}) - if(WIN32) TARGET_LINK_LIBRARIES(Gradido_LoginServer optimized ${MYSQL_LIBRARIES} Shlwapi) TARGET_LINK_LIBRARIES(Gradido_LoginServer debug ${COMPILED_MARIADB_CLIENT_DEBUG} Shlwapi) @@ -211,4 +215,4 @@ else(WIN32) target_link_libraries(Gradido_LoginServer_Test ${MYSQL_LIBRARIES} ${CONAN_OPENSSL_CUSTOM_LIBS} ${GRPC_LIBS} ${CMAKE_DL_LIBS} ${PROTOBUF_LIBS}) endif(WIN32) -add_test(NAME main COMMAND Gradido_LoginServer_Test) \ No newline at end of file +add_test(NAME main COMMAND Gradido_LoginServer_Test) diff --git a/parse_proto.sh b/parse_proto.sh index 1580a8616..1ec7c106e 100644 --- a/parse_proto.sh +++ b/parse_proto.sh @@ -8,8 +8,9 @@ fi ./protoc --cpp_out=./src/cpp/proto --proto_path=./src/proto ./src/proto/gradido/*.proto -#if [ ! -d "./src/cpp/proto/hedera" ] ; then -# mkdir ./src/cpp/proto/hedera -#fi +if [ ! -d "./src/cpp/proto/hedera" ] ; then + mkdir ./src/cpp/proto/hedera +fi + ./protoc --plugin=protoc-gen-grpc=./grpc_cpp_plugin.exe --cpp_out=./src/cpp/proto/hedera --grpc_out=./src/cpp/proto/hedera --proto_path=./src/proto/hedera/hedera-protobuf/src/main/proto ./src/proto/hedera/hedera-protobuf/src/main/proto/*.proto diff --git a/src/cpp/HTTPInterface/LoginPage.cpp b/src/cpp/HTTPInterface/LoginPage.cpp index b264eda16..5bf7fae11 100644 --- a/src/cpp/HTTPInterface/LoginPage.cpp +++ b/src/cpp/HTTPInterface/LoginPage.cpp @@ -319,12 +319,15 @@ void LoginPage::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net:: responseStream << "
\n"; responseStream << "
\n"; responseStream << "\t

"; -#line 183 "D:\\code\\gradido\\gradido_login_server_grpc\\src\\cpsp\\login.cpsp" +#line 176 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\login.cpsp" responseStream << ( langCatalog->gettext("You haven't any account yet? Please follow the link to create one.") ); responseStream << "

\n"; - responseStream << "\t \n"; + responseStream << "\t \n"; responseStream << "\t\t\t"; -#line 185 "D:\\code\\gradido\\gradido_login_server_grpc\\src\\cpsp\\login.cpsp" +#line 178 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\login.cpsp" responseStream << ( langCatalog->gettext("Create New Account") ); responseStream << "\n"; responseStream << "\t\t \n"; diff --git a/src/cpp/JSONInterface/JsonCreateUser.cpp b/src/cpp/JSONInterface/JsonCreateUser.cpp index c7d93568e..d21874aaa 100644 --- a/src/cpp/JSONInterface/JsonCreateUser.cpp +++ b/src/cpp/JSONInterface/JsonCreateUser.cpp @@ -35,7 +35,6 @@ Poco::JSON::Object* JsonCreateUser::handle(Poco::Dynamic::Var params) paramJsonObject->get("last_name").convert(last_name); paramJsonObject->get("emailType").convert(emailType); paramJsonObject->get("group_id").convert(group_id); - if ((ServerConfig::g_AllowUnsecureFlags & ServerConfig::UNSECURE_PASSWORD_REQUESTS)) { paramJsonObject->get("password").convert(password); } diff --git a/src/cpp/JSONInterface/JsonTransaction.cpp b/src/cpp/JSONInterface/JsonTransaction.cpp index 99fa5a6f6..1e7f7844e 100644 --- a/src/cpp/JSONInterface/JsonTransaction.cpp +++ b/src/cpp/JSONInterface/JsonTransaction.cpp @@ -35,10 +35,6 @@ Poco::JSON::Object* JsonTransaction::handle(Poco::Dynamic::Var params) if (!paramJsonObject->isNull("balance")) { paramJsonObject->get("balance").convert(balance); if (balance) { - auto u = session->getUser(); - if (u) { - u->setBalance(balance); - } auto nu = session->getNewUser(); if (!nu.isNull()) { nu->setBalance(balance); diff --git a/src/cpp/SingletonManager/ConnectionManager.cpp b/src/cpp/SingletonManager/ConnectionManager.cpp index 93bf1eb6a..eb4e43192 100644 --- a/src/cpp/SingletonManager/ConnectionManager.cpp +++ b/src/cpp/SingletonManager/ConnectionManager.cpp @@ -74,7 +74,6 @@ Poco::Data::Session ConnectionManager::getConnection(ConnectionType type) } auto session = mSessionPools.getPool(mSessionPoolNames[type]).get(); - //return mSessionPoolNames[type]; /*if (!session.isConnected()) { printf("reconnect called\n"); diff --git a/src/cpp/controller/User.cpp b/src/cpp/controller/User.cpp index caa3f851a..0a445ec25 100644 --- a/src/cpp/controller/User.cpp +++ b/src/cpp/controller/User.cpp @@ -445,7 +445,6 @@ namespace controller { return 0; } - int User::addMissingEmailHashes() { auto cm = ConnectionManager::getInstance(); @@ -499,6 +498,4 @@ namespace controller { } return updated_count; } - } - diff --git a/src/cpp/lib/Error.h b/src/cpp/lib/Error.h index bf4805da9..34199034f 100644 --- a/src/cpp/lib/Error.h +++ b/src/cpp/lib/Error.h @@ -29,8 +29,7 @@ public: virtual bool isError() { return true; } protected: - std::string mFunctionName; - std::string mMessage; + }; class ParamError : public Error diff --git a/src/cpp/lib/JsonRequest.cpp b/src/cpp/lib/JsonRequest.cpp index c5dff9281..825d30f66 100644 --- a/src/cpp/lib/JsonRequest.cpp +++ b/src/cpp/lib/JsonRequest.cpp @@ -190,5 +190,95 @@ JsonRequestReturn JsonRequest::requestGRPCRelay(const Poco::Net::NameValueCollec + return JSON_REQUEST_RETURN_OK; +} +#include "Poco/JSON/Stringifier.h" +JsonRequestReturn JsonRequest::requestGRPCRelay(const Poco::Net::NameValueCollection& payload) +{ + static const char* functionName = "JsonRequest::requestGRPCRelay"; + Poco::JSON::Object requestJson; + + for (auto it = payload.begin(); it != payload.end(); it++) { + requestJson.set(it->first, it->second); + } + + // send post request via https + // 443 = HTTPS Default + // TODO: adding port into ServerConfig + try { + Profiler phpRequestTime; + Poco::Net::HTTPClientSession httpClientSession(mServerHost, mServerPort); + Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/hedera_rpc_relay/gRPCProxy.php"); + + request.setChunkedTransferEncoding(false); + std::ostream& requestStream = httpClientSession.sendRequest(request); + requestJson.stringify(requestStream); + + std::stringstream ss; + requestJson.stringify(ss); + auto f = fopen("grpc.txt", "wt"); + std::string grpc = ss.str(); + fwrite(grpc.data(), grpc.size(), 1, f); + fclose(f); + + Poco::Net::HTTPResponse response; + std::istream& request_stream = httpClientSession.receiveResponse(response); + + // debugging answer + + std::stringstream responseStringStream; + for (std::string line; std::getline(request_stream, line); ) { + responseStringStream << line << std::endl; + } + Poco::Logger& speedLog = Poco::Logger::get("SpeedLog"); + speedLog.information("[gRPC relay] php server time: %s", phpRequestTime.string()); + + // extract parameter from request + Poco::JSON::Parser jsonParser; + Poco::Dynamic::Var parsedJson; + try { + parsedJson = jsonParser.parse(responseStringStream.str()); + } + catch (Poco::Exception& ex) { + addError(new ParamError(functionName, "error parsing request answer grpc relay", ex.displayText().data())); + + std::string fileName = "response_grpc_"; + fileName += ".html"; + + FILE* f = fopen(fileName.data(), "wt"); + std::string responseString = responseStringStream.str(); + fwrite(responseString.data(), 1, responseString.size(), f); + fclose(f); + // */ + sendErrorsAsEmail(responseStringStream.str()); + return JSON_REQUEST_RETURN_PARSE_ERROR; + } + + Poco::JSON::Object object = *parsedJson.extract(); + auto state = object.get("state"); + std::string stateString = state.convert(); + if (stateString == "error") { + addError(new Error(functionName, "php server return error")); + if (!object.isNull("msg")) { + addError(new ParamError(functionName, "msg:", object.get("msg").convert().data())); + } + if (!object.isNull("details")) { + addError(new ParamError(functionName, "details:", object.get("details").convert().data())); + } + sendErrorsAsEmail(); + return JSON_REQUEST_RETURN_ERROR; + } + ss.clear(); + Poco::JSON::Stringifier::stringify(object, ss); + printf("json request result: %s\n", ss.str().data()); + } + catch (Poco::Exception& e) { + addError(new ParamError(functionName, "connect error to php server", e.displayText().data())); + sendErrorsAsEmail(); + return JSON_REQUEST_CONNECT_ERROR; + } + + + return JSON_REQUEST_RETURN_OK; } \ No newline at end of file diff --git a/src/cpp/model/Session.cpp b/src/cpp/model/Session.cpp index fdf6afe8c..f86177795 100644 --- a/src/cpp/model/Session.cpp +++ b/src/cpp/model/Session.cpp @@ -34,59 +34,12 @@ using namespace Poco::Data::Keywords; -int WriteEmailVerification::run() -{ - auto em = ErrorManager::getInstance(); - - mEmailVerificationCode->getModel()->setUserId(mUser->getDBId()); - auto emailVerificationModel = mEmailVerificationCode->getModel(); - emailVerificationModel->setUserId(mUser->getDBId()); - if (!emailVerificationModel->insertIntoDB(true) || emailVerificationModel->errorCount() > 0) { - emailVerificationModel->sendErrorsAsEmail(); - return -1; - } - - return 0; -} - -// --------------------------------------------------------------------------------------------------------------- - -int WritePassphraseIntoDB::run() -{ - Profiler timeUsed; - - // TODO: encrypt passphrase, need server admin crypto box pubkey - //int crypto_box_seal(unsigned char *c, const unsigned char *m, - //unsigned long long mlen, const unsigned char *pk); - size_t mlen = mPassphrase.size(); - size_t crypto_size = crypto_box_SEALBYTES + mlen; - - auto em = ErrorManager::getInstance(); - - auto dbSession = ConnectionManager::getInstance()->getConnection(CONNECTION_MYSQL_LOGIN_SERVER); - Poco::Data::Statement insert(dbSession); - insert << "INSERT INTO user_backups (user_id, passphrase) VALUES(?,?)", - use(mUserId), use(mPassphrase); - try { - if (insert.execute() != 1) { - em->addError(new ParamError("WritePassphraseIntoDB::run", "inserting passphrase for user failed", std::to_string(mUserId))); - em->sendErrorsAsEmail(); - } - } - catch (Poco::Exception& ex) { - em->addError(new ParamError("WritePassphraseIntoDB::run", "insert passphrase mysql error", ex.displayText().data())); - em->sendErrorsAsEmail(); - } - - //printf("[WritePassphraseIntoDB] timeUsed: %s\n", timeUsed.string().data()); - return 0; -} // -------------------------------------------------------------------------------------------------------------- Session::Session(int handle) - : mHandleId(handle), mSessionUser(nullptr), mState(SESSION_STATE_EMPTY), mActive(false) + : mHandleId(handle), mState(SESSION_STATE_EMPTY), mActive(false) { } @@ -109,7 +62,6 @@ void Session::reset() //printf("[Session::reset]\n"); lock("Session::reset"); std::unique_lock _lock(mSharedMutex); - mSessionUser.assign(nullptr); mNewUser.assign(nullptr); mEmailVerificationCodeObject.assign(nullptr); @@ -471,16 +423,8 @@ int Session::updateEmailVerification(Poco::UInt64 emailVerificationCode) } auto email_verification_code_model = mEmailVerificationCodeObject->getModel(); assert(email_verification_code_model); - if(email_verification_code_model->getCode() == emailVerificationCode) { - if (mSessionUser && mSessionUser->getDBId() == 0) { - //addError(new Error("E-Mail Verification", "Benutzer wurde nicht richtig gespeichert, bitte wende dich an den Server-Admin")); - em->addError(new Error(funcName, "user exist with 0 as id")); - em->sendErrorsAsEmail(); - - //return false; - return -2; - } - + if(email_verification_code_model->getCode() == emailVerificationCode) + { // load correct user from db if (mNewUser.isNull() || !mNewUser->getModel() || mNewUser->getModel()->getID() != email_verification_code_model->getUserId()) { mNewUser = controller::User::create(); @@ -502,7 +446,6 @@ int Session::updateEmailVerification(Poco::UInt64 emailVerificationCode) first_email_activation = true; } if (first_email_activation && user_model->isEmailChecked()) { - mSessionUser = new User(mNewUser); addError(new Error(gettext("E-Mail Verification"), gettext("Du hast dein Konto bereits aktiviert!")), false); return 1; @@ -541,28 +484,6 @@ int Session::updateEmailVerification(Poco::UInt64 emailVerificationCode) return -2; - /*if (updated_rows == 1) { - Poco::Data::Statement delete_row(dbConnection); - delete_row << "DELETE FROM email_opt_in where verification_code = ?", use(emailVerificationCode); - if (delete_row.execute() != 1) { - em->addError(new Error(funcName, "delete from email_opt_in entry didn't work as expected, please check db")); - em->sendErrorsAsEmail(); - } - if (mSessionUser) { - mSessionUser->setEmailChecked(); - mSessionUser->setLanguage(getLanguage()); - } - updateState(SESSION_STATE_EMAIL_VERIFICATION_CODE_CHECKED); - //printf("[%s] time: %s\n", funcName, usedTime.string().data()); - unlock(); - return true; - } - else { - em->addError(new ParamError(funcName, "update user work not like expected, updated row count", updated_rows)); - em->sendErrorsAsEmail(); - }*/ - - } else { addError(new Error(gettext("E-Mail Verification"), gettext("Falscher Code für aktiven Login"))); @@ -579,7 +500,6 @@ int Session::updateEmailVerification(Poco::UInt64 emailVerificationCode) int Session::sendResetPasswordEmail(Poco::AutoPtr user, bool passphraseMemorized) { mNewUser = user; - mSessionUser = new User(user); auto em = EmailManager::getInstance(); std::unique_lock _lock(mSharedMutex); @@ -623,7 +543,7 @@ int Session::sendResetPasswordEmail(Poco::AutoPtr user, bool p int Session::comparePassphraseWithSavedKeys(const std::string& inputPassphrase, const Mnemonic* wordSource) { - KeyPair keys; + static const char* functionName = "Session::comparePassphraseWithSavedKeys"; if (!wordSource) { addError(new Error(functionName, "wordSource is empty")); @@ -831,9 +751,7 @@ UserState Session::loadUser(const std::string& email, const std::string& passwor if (user_model && user_model->isDisabled()) { return USER_DISABLED; } - if (mNewUser->getUserState() >= USER_LOADED_FROM_DB) { - int loginResult = mNewUser->login(password); int exitCount = 0; if (loginResult == -3) @@ -908,6 +826,7 @@ UserState Session::loadUser(const std::string& email, const std::string& passwor } detectSessionState(); + unlock(); if (0 == mNewUser->getModel()->getGroupId()) { return USER_NO_GROUP; } @@ -985,7 +904,6 @@ SESSION_STATE_COUNT */ void Session::detectSessionState() { - if (mNewUser.isNull() || !mNewUser->getModel() || mNewUser->getPassword().isNull()) { return; } diff --git a/src/cpp/model/TransactionCreation.cpp b/src/cpp/model/TransactionCreation.cpp index bf2825a74..2cc139f59 100644 --- a/src/cpp/model/TransactionCreation.cpp +++ b/src/cpp/model/TransactionCreation.cpp @@ -17,11 +17,11 @@ TransactionCreation::~TransactionCreation() int TransactionCreation::prepare() { const static char functionName[] = { "TransactionCreation::prepare" }; - if (!mProtoCreation.has_receiveramount()) { + if (!mProtoCreation.has_receiver()) { addError(new Error(functionName, "hasn't receiver amount")); return -1; } - auto receiverAmount = mProtoCreation.receiveramount(); + auto receiver_amount = mProtoCreation.receiver(); if (receiverAmount.amount() <= 0) { addError(new Error(functionName, "amount must be > 0")); @@ -43,8 +43,7 @@ int TransactionCreation::prepare() getErrors(mReceiverUser->getModel()); if (mReceiverUser->getUserState() == USER_EMPTY) { sodium_bin2hex(mReceiverPublicHex, 65, (const unsigned char*)receiverPublic.data(), receiverPublic.size()); - delete mReceiverUser; - mReceiverUser = nullptr; + mReceiverUser.assign(nullptr); } else { memcpy(mReceiverPublicHex, mReceiverUser->getModel()->getPublicKeyHex().data(), 64); diff --git a/src/cpp/model/TransactionTransfer.cpp b/src/cpp/model/TransactionTransfer.cpp index d46d1a03a..014b4b80d 100644 --- a/src/cpp/model/TransactionTransfer.cpp +++ b/src/cpp/model/TransactionTransfer.cpp @@ -62,22 +62,11 @@ int TransactionTransfer::prepare() { lock(); const static char functionName[] = { "TransactionTransfer::prepare" }; - if (mProtoTransfer.senderamounts_size() == 0) { - addError(new Error(functionName, "hasn't sender amount(s)")); - unlock(); - return -1; - } - if (mProtoTransfer.receiveramounts_size() == 0) { - addError(new Error(functionName, "hasn't receiver amount(s)")); - unlock(); - return -2; - } - mKontoTable.reserve(mProtoTransfer.senderamounts_size() + mProtoTransfer.receiveramounts_size()); + + mKontoTable.reserve(2); //auto receiverAmount = mProtoTransfer.receiveramount(); //auto senderAmount - int senderSum = 0; - int receiverSum = 0; char pubkeyHexTemp[65]; @@ -125,25 +114,15 @@ int TransactionTransfer::prepare() mKontoTable.push_back(KontoTableEntry(pubkeyHexTemp, senderAmount.amount(), true)); } else { - mKontoTable.push_back(KontoTableEntry(user->getModel(), senderAmount.amount(), true)); + mKontoTable.push_back(KontoTableEntry(sender_user->getModel(), -amount, true)); } - } - for (int i = 0; i < mProtoTransfer.receiveramounts_size(); i++) { - auto receiverAmount = mProtoTransfer.receiveramounts(i); - auto pubkey = receiverAmount.ed25519_receiver_pubkey(); - receiverSum += receiverAmount.amount(); - if (receiverAmount.ed25519_receiver_pubkey().size() != 32) { - addError(new ParamError(functionName, "invalid public key for receiver ", i)); - unlock(); - return -4; - } - auto user = controller::User::create(); - if (!user->load((const unsigned char*)pubkey.data())) { - sodium_bin2hex(pubkeyHexTemp, 65, (const unsigned char*)pubkey.data(), pubkey.size()); - mKontoTable.push_back(KontoTableEntry(pubkeyHexTemp, receiverAmount.amount(), false)); + + if (!receiver_user->load((const unsigned char*)receiver_pubkey.data())) { + sodium_bin2hex(pubkeyHexTemp, 65, (const unsigned char*)receiver_pubkey.data(), receiver_pubkey.size()); + mKontoTable.push_back(KontoTableEntry(pubkeyHexTemp, amount, true)); } else { - mKontoTable.push_back(KontoTableEntry(user->getModel(), receiverAmount.amount(), false)); + mKontoTable.push_back(KontoTableEntry(sender_user->getModel(), amount, true)); } } if (senderSum != receiverSum) { @@ -199,5 +178,3 @@ const std::string& TransactionTransfer::getAmountCell(int index) return mKontoTable[index].amountCell; } - ->>>>>>> 1e4ae4a (update proto files matching with pauls proto version, update code which use them) diff --git a/src/cpp/model/table/User.h b/src/cpp/model/table/User.h index 8733457f5..d4fb2ea87 100644 --- a/src/cpp/model/table/User.h +++ b/src/cpp/model/table/User.h @@ -56,7 +56,6 @@ namespace model { size_t updateFieldsFromCommunityServer(); // default getter unlocked - inline const std::string getEmail() const { SHARED_LOCK; return mEmail; } inline const std::string getFirstName() const { SHARED_LOCK; return mFirstName; } inline const std::string getLastName() const { SHARED_LOCK; return mLastName; } diff --git a/src/cpsp/decodeTransaction.cpsp b/src/cpsp/decodeTransaction.cpsp index 1416e8471..b57d92161 100644 --- a/src/cpsp/decodeTransaction.cpsp +++ b/src/cpsp/decodeTransaction.cpsp @@ -101,7 +101,6 @@ <% } else if(transactionBody.has_creation()) { auto creation = transactionBody.creation(); - TransactionCreation creationObject("", creation); auto receiver = creation.receiver(); char hex[65]; memset(hex, 0, 65); diff --git a/src/cpsp/login.cpsp b/src/cpsp/login.cpsp index b2b091fef..bfa7011d2 100644 --- a/src/cpsp/login.cpsp +++ b/src/cpsp/login.cpsp @@ -26,8 +26,8 @@ auto langCatalog = lm->getFreeCatalog(lang); std::string presetEmail(""); - if(mSession && mSession->getUser()) { - presetEmail = mSession->getUser()->getEmail(); + if(mSession && mSession->getNewUser()) { + presetEmail = mSession->getNewUser()->getModel()->getEmail(); } if(!form.empty()) {