mirror of
https://github.com/IT4Change/gradido.git
synced 2026-01-20 20:01:31 +00:00
Add auto-sign Transaction functionality
This commit is contained in:
parent
06374023b5
commit
55922753a7
@ -1,158 +1,169 @@
|
||||
#include "JsonTransaction.h"
|
||||
#include "Poco/URI.h"
|
||||
#include "Poco/Dynamic/Struct.h"
|
||||
|
||||
#include "../SingletonManager/SessionManager.h"
|
||||
|
||||
Poco::JSON::Object* JsonTransaction::handle(Poco::Dynamic::Var params)
|
||||
{
|
||||
Poco::JSON::Object* result = new Poco::JSON::Object;
|
||||
int session_id = 0;
|
||||
|
||||
// if is json object
|
||||
if (params.type() == typeid(Poco::JSON::Object::Ptr)) {
|
||||
Poco::JSON::Object::Ptr paramJsonObject = params.extract<Poco::JSON::Object::Ptr>();
|
||||
|
||||
try {
|
||||
/// Throws a RangeException if the value does not fit
|
||||
/// into the result variable.
|
||||
/// Throws a NotImplementedException if conversion is
|
||||
/// not available for the given type.
|
||||
/// Throws InvalidAccessException if Var is empty.
|
||||
paramJsonObject->get("session_id").convert(session_id);
|
||||
auto sm = SessionManager::getInstance();
|
||||
if (session_id != 0) {
|
||||
auto session = sm->getSession(session_id);
|
||||
if (!session) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "session not found");
|
||||
return result;
|
||||
}
|
||||
|
||||
int balance = 0;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string transactionBase64String;
|
||||
Poco::Dynamic::Var transaction_base64 = paramJsonObject->get("transaction_base64");
|
||||
int alreadyEnlisted = 0;
|
||||
if (transaction_base64.isString()) {
|
||||
paramJsonObject->get("transaction_base64").convert(transactionBase64String);
|
||||
|
||||
if (!session->startProcessingTransaction(transactionBase64String)) {
|
||||
auto lastError = session->getLastError();
|
||||
if (lastError) delete lastError;
|
||||
result->set("state", "error");
|
||||
result->set("msg", "already enlisted");
|
||||
return result;
|
||||
}
|
||||
|
||||
} else {
|
||||
Poco::DynamicStruct ds = *paramJsonObject;
|
||||
for (int i = 0; i < ds["transaction_base64"].size(); i++) {
|
||||
ds["transaction_base64"][i].convert(transactionBase64String);
|
||||
if (!session->startProcessingTransaction(transactionBase64String)) {
|
||||
auto lastError = session->getLastError();
|
||||
if (lastError) delete lastError;
|
||||
alreadyEnlisted++;
|
||||
}
|
||||
}
|
||||
|
||||
if (alreadyEnlisted > 0) {
|
||||
result->set("state", "warning");
|
||||
result->set("msg", std::to_string(alreadyEnlisted) + " already enlisted");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
result->set("state", "success");
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Poco::Exception& ex) {
|
||||
printf("[JsonTransaction::handle] try to use params as jsonObject: %s\n", ex.displayText().data());
|
||||
result->set("state", "error");
|
||||
result->set("msg", "json exception");
|
||||
result->set("details", ex.displayText());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else if (params.isVector()) {
|
||||
const Poco::URI::QueryParameters queryParams = params.extract<Poco::URI::QueryParameters>();
|
||||
auto transactionIT = queryParams.begin();
|
||||
for (auto it = queryParams.begin(); it != queryParams.end(); it++) {
|
||||
if (it->first == "session_id") {
|
||||
session_id = stoi(it->second);
|
||||
//break;
|
||||
}
|
||||
else if (it->first == "transaction_base64") {
|
||||
transactionIT = it;
|
||||
}
|
||||
}
|
||||
if (session_id) {
|
||||
auto sm = SessionManager::getInstance();
|
||||
auto session = sm->getSession(session_id);
|
||||
if (!session) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "session not found");
|
||||
return result;
|
||||
}
|
||||
if (!session->startProcessingTransaction(transactionIT->second)) {
|
||||
auto lastError = session->getLastError();
|
||||
if (lastError) delete lastError;
|
||||
result->set("state", "error");
|
||||
result->set("msg", "already enlisted");
|
||||
return result;
|
||||
}
|
||||
result->set("state", "success");
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "session id not set");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else if (params.isStruct()) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "struct not implemented yet");
|
||||
}
|
||||
else if (params.isArray()) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "array not implemented yet");
|
||||
}
|
||||
else if (params.isList()) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "list not implemented yet");
|
||||
}
|
||||
else if (params.isString()) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "string not implemented yet");
|
||||
}
|
||||
else if (params.isDeque()) {
|
||||
result->set("state", "error");
|
||||
result->set("meg", "deque not implemented yet");
|
||||
}
|
||||
else {
|
||||
|
||||
result->set("state", "error");
|
||||
result->set("msg", "format not implemented");
|
||||
result->set("details", std::string(params.type().name()));
|
||||
}
|
||||
|
||||
return result;
|
||||
#include "JsonTransaction.h"
|
||||
#include "Poco/URI.h"
|
||||
#include "Poco/Dynamic/Struct.h"
|
||||
|
||||
#include "../SingletonManager/SessionManager.h"
|
||||
#include "../ServerConfig.h"
|
||||
|
||||
Poco::JSON::Object* JsonTransaction::handle(Poco::Dynamic::Var params)
|
||||
{
|
||||
Poco::JSON::Object* result = new Poco::JSON::Object;
|
||||
int session_id = 0;
|
||||
|
||||
// if is json object
|
||||
if (params.type() == typeid(Poco::JSON::Object::Ptr)) {
|
||||
Poco::JSON::Object::Ptr paramJsonObject = params.extract<Poco::JSON::Object::Ptr>();
|
||||
|
||||
try {
|
||||
/// Throws a RangeException if the value does not fit
|
||||
/// into the result variable.
|
||||
/// Throws a NotImplementedException if conversion is
|
||||
/// not available for the given type.
|
||||
/// Throws InvalidAccessException if Var is empty.
|
||||
paramJsonObject->get("session_id").convert(session_id);
|
||||
auto sm = SessionManager::getInstance();
|
||||
if (session_id != 0) {
|
||||
auto session = sm->getSession(session_id);
|
||||
if (!session) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "session not found");
|
||||
return result;
|
||||
}
|
||||
|
||||
int balance = 0;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string transactionBase64String;
|
||||
Poco::Dynamic::Var transaction_base64 = paramJsonObject->get("transaction_base64");
|
||||
|
||||
if (transaction_base64.isString()) {
|
||||
paramJsonObject->get("transaction_base64").convert(transactionBase64String);
|
||||
|
||||
if (!session->startProcessingTransaction(transactionBase64String)) {
|
||||
auto lastError = session->getLastError();
|
||||
if (lastError) delete lastError;
|
||||
result->set("state", "error");
|
||||
result->set("msg", "already enlisted");
|
||||
return result;
|
||||
}
|
||||
|
||||
} else {
|
||||
Poco::DynamicStruct ds = *paramJsonObject;
|
||||
int alreadyEnlisted = 0;
|
||||
|
||||
for (int i = 0; i < ds["transaction_base64"].size(); i++) {
|
||||
ds["transaction_base64"][i].convert(transactionBase64String);
|
||||
if (!session->startProcessingTransaction(transactionBase64String)) {
|
||||
auto lastError = session->getLastError();
|
||||
if (lastError) delete lastError;
|
||||
alreadyEnlisted++;
|
||||
}
|
||||
}
|
||||
|
||||
if (alreadyEnlisted > 0) {
|
||||
result->set("state", "warning");
|
||||
result->set("msg", std::to_string(alreadyEnlisted) + " already enlisted");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
result->set("state", "success");
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Poco::Exception& ex) {
|
||||
printf("[JsonTransaction::handle] try to use params as jsonObject: %s\n", ex.displayText().data());
|
||||
result->set("state", "error");
|
||||
result->set("msg", "json exception");
|
||||
result->set("details", ex.displayText());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else if (params.isVector()) {
|
||||
const Poco::URI::QueryParameters queryParams = params.extract<Poco::URI::QueryParameters>();
|
||||
auto transactionIT = queryParams.begin();
|
||||
for (auto it = queryParams.begin(); it != queryParams.end(); it++) {
|
||||
if (it->first == "session_id") {
|
||||
session_id = stoi(it->second);
|
||||
//break;
|
||||
}
|
||||
else if (it->first == "transaction_base64") {
|
||||
transactionIT = it;
|
||||
}
|
||||
}
|
||||
if (session_id) {
|
||||
auto sm = SessionManager::getInstance();
|
||||
auto session = sm->getSession(session_id);
|
||||
if (!session) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "session not found");
|
||||
return result;
|
||||
}
|
||||
if (!session->startProcessingTransaction(transactionIT->second)) {
|
||||
auto lastError = session->getLastError();
|
||||
if (lastError) delete lastError;
|
||||
result->set("state", "error");
|
||||
result->set("msg", "already enlisted");
|
||||
return result;
|
||||
}
|
||||
result->set("state", "success");
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "session id not set");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else if (params.isStruct()) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "struct not implemented yet");
|
||||
}
|
||||
else if (params.isArray()) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "array not implemented yet");
|
||||
}
|
||||
else if (params.isList()) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "list not implemented yet");
|
||||
}
|
||||
else if (params.isString()) {
|
||||
result->set("state", "error");
|
||||
result->set("msg", "string not implemented yet");
|
||||
}
|
||||
else if (params.isDeque()) {
|
||||
result->set("state", "error");
|
||||
result->set("meg", "deque not implemented yet");
|
||||
}
|
||||
else {
|
||||
|
||||
result->set("state", "error");
|
||||
result->set("msg", "format not implemented");
|
||||
result->set("details", std::string(params.type().name()));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool JsonTransaction::startProcessingTransaction(Session* session, const std::string& transactionBase64)
|
||||
{
|
||||
if ((ServerConfig::g_AllowUnsecureFlags & ServerConfig::UNSECURE_AUTO_SIGN_TRANSACTIONS) == ServerConfig::UNSECURE_AUTO_SIGN_TRANSACTIONS) {
|
||||
|
||||
}
|
||||
return session->startProcessingTransaction(transactionBase64);
|
||||
}
|
||||
@ -1,16 +1,19 @@
|
||||
#ifndef __JSON_INTERFACE_JSON_TRANSACTION_
|
||||
#define __JSON_INTERFACE_JSON_TRANSACTION_
|
||||
|
||||
#include "JsonRequestHandler.h"
|
||||
|
||||
class JsonTransaction : public JsonRequestHandler
|
||||
{
|
||||
public:
|
||||
Poco::JSON::Object* handle(Poco::Dynamic::Var params);
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
};
|
||||
|
||||
#ifndef __JSON_INTERFACE_JSON_TRANSACTION_
|
||||
#define __JSON_INTERFACE_JSON_TRANSACTION_
|
||||
|
||||
#include "JsonRequestHandler.h"
|
||||
|
||||
class Session;
|
||||
|
||||
class JsonTransaction : public JsonRequestHandler
|
||||
{
|
||||
public:
|
||||
Poco::JSON::Object* handle(Poco::Dynamic::Var params);
|
||||
|
||||
protected:
|
||||
bool startProcessingTransaction(Session* session, const std::string& transactionBase64);
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // __JSON_INTERFACE_JSON_TRANSACTION_
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,281 +1,282 @@
|
||||
/*!
|
||||
*
|
||||
* \author: einhornimmond
|
||||
*
|
||||
* \date: 02.03.19
|
||||
*
|
||||
* \brief: store session data
|
||||
*/
|
||||
|
||||
#ifndef DR_LUA_WEB_MODULE_SESSION_SESSION_H
|
||||
#define DR_LUA_WEB_MODULE_SESSION_SESSION_H
|
||||
|
||||
#include "../lib/ErrorList.h"
|
||||
#include "User.h"
|
||||
#include "../controller/User.h"
|
||||
|
||||
#include "../lib/MultithreadContainer.h"
|
||||
#include "../tasks/ProcessingTransaction.h"
|
||||
|
||||
#include "../SingletonManager/LanguageManager.h"
|
||||
|
||||
#include "../controller/EmailVerificationCode.h"
|
||||
|
||||
#include "Poco/Thread.h"
|
||||
#include "Poco/Types.h"
|
||||
#include "Poco/DateTime.h"
|
||||
#include "Poco/Net/IPAddress.h"
|
||||
#include "Poco/Net/HTTPCookie.h"
|
||||
|
||||
#include <mutex>
|
||||
|
||||
|
||||
class WriteEmailVerification;
|
||||
|
||||
enum SessionStates {
|
||||
SESSION_STATE_EMPTY,
|
||||
SESSION_STATE_CRYPTO_KEY_GENERATED,
|
||||
SESSION_STATE_USER_WRITTEN,
|
||||
SESSION_STATE_EMAIL_VERIFICATION_WRITTEN,
|
||||
SESSION_STATE_EMAIL_VERIFICATION_SEND,
|
||||
SESSION_STATE_EMAIL_VERIFICATION_CODE_CHECKED,
|
||||
SESSION_STATE_PASSPHRASE_GENERATED,
|
||||
SESSION_STATE_PASSPHRASE_SHOWN,
|
||||
SESSION_STATE_PASSPHRASE_WRITTEN,
|
||||
SESSION_STATE_KEY_PAIR_GENERATED,
|
||||
SESSION_STATE_KEY_PAIR_WRITTEN,
|
||||
SESSION_STATE_RESET_PASSWORD_REQUEST,
|
||||
SESSION_STATE_RESET_PASSWORD_SUCCEED,
|
||||
SESSION_STATE_COUNT
|
||||
};
|
||||
|
||||
class SessionManager;
|
||||
class UpdateUserPasswordPage;
|
||||
class PassphrasePage;
|
||||
class RepairDefectPassphrase;
|
||||
|
||||
class Session : public ErrorList, public UniLib::lib::MultithreadContainer
|
||||
{
|
||||
friend WriteEmailVerification;
|
||||
friend SessionManager;
|
||||
friend UpdateUserPasswordPage;
|
||||
friend PassphrasePage;
|
||||
friend RepairDefectPassphrase;
|
||||
public:
|
||||
Session(int handle);
|
||||
~Session();
|
||||
|
||||
// get new model objects
|
||||
Poco::AutoPtr<controller::EmailVerificationCode> getEmailVerificationCodeObject();
|
||||
|
||||
// set new model objects
|
||||
inline void setUser(Poco::AutoPtr<controller::User> user) { mNewUser = user; }
|
||||
inline Poco::AutoPtr<controller::User> getNewUser() { return mNewUser; }
|
||||
|
||||
// ---------------- User functions ----------------------------
|
||||
// TODO: register state: written into db, mails sended, update state only if new state is higher as old state
|
||||
// create User send e-mail activation link
|
||||
bool createUser(const std::string& first_name, const std::string& last_name, const std::string& email, const std::string& password);
|
||||
|
||||
//! \brief new register function, without showing user pubkeys, using controller/user
|
||||
bool createUserDirect(const std::string& first_name, const std::string& last_name, const std::string& email, const std::string& password);
|
||||
|
||||
|
||||
// adminRegister without passwort
|
||||
bool adminCreateUser(const std::string& first_name, const std::string& last_name, const std::string& email);
|
||||
|
||||
// TODO: check if email exist and if not, fake waiting on password hashing with profiled times of real password hashing
|
||||
UserStates loadUser(const std::string& email, const std::string& password);
|
||||
bool ifUserExist(const std::string& email);
|
||||
|
||||
inline void setUser(Poco::AutoPtr<User> user) { mSessionUser = user; }
|
||||
|
||||
|
||||
bool deleteUser();
|
||||
|
||||
Poco::AutoPtr<User> getUser() {
|
||||
return mSessionUser;
|
||||
}
|
||||
|
||||
// ------------------------- Email Verification Code functions -------------------------------
|
||||
|
||||
bool loadFromEmailVerificationCode(Poco::UInt64 emailVerificationCode);
|
||||
|
||||
//! \return 1 = konto already exist
|
||||
//! -1 = invalid code
|
||||
//! -2 = critical error
|
||||
//! 0 = ok
|
||||
int updateEmailVerification(Poco::UInt64 emailVerificationCode);
|
||||
|
||||
// called from page with same name
|
||||
//! \return 1 = reset password email already send
|
||||
//! \return 2 = reset password email already shortly before
|
||||
//! \return 0 = ok
|
||||
int sendResetPasswordEmail(Poco::AutoPtr<controller::User> user, bool passphraseMemorized);
|
||||
//
|
||||
//! \return 0 = not the same
|
||||
//! \return 1 = same
|
||||
//! \return -1 = error
|
||||
//! \return -2 = critical error
|
||||
int comparePassphraseWithSavedKeys(const std::string& inputPassphrase, Mnemonic* wordSource);
|
||||
|
||||
Poco::Net::HTTPCookie getLoginCookie();
|
||||
|
||||
|
||||
inline int getHandle() { return mHandleId; }
|
||||
|
||||
// ------------------------ Passphrase functions ----------------------------
|
||||
|
||||
inline void setPassphrase(Poco::AutoPtr<Passphrase> passphrase) { mNewPassphrase = passphrase; }
|
||||
inline Poco::AutoPtr<Passphrase> getPassphrase() { return mNewPassphrase; }
|
||||
|
||||
inline void setPassphrase(const std::string& passphrase) { mPassphrase = passphrase; }
|
||||
|
||||
inline const std::string& getOldPassphrase() { return mPassphrase; }
|
||||
bool generatePassphrase();
|
||||
bool generateKeys(bool savePrivkey, bool savePassphrase);
|
||||
|
||||
inline void setClientIp(Poco::Net::IPAddress ip) { mClientLoginIP = ip; }
|
||||
inline Poco::Net::IPAddress getClientIp() { return mClientLoginIP; }
|
||||
|
||||
inline bool isIPValid(Poco::Net::IPAddress ip) { return mClientLoginIP == ip; }
|
||||
bool isPwdValid(const std::string& pwd);
|
||||
void reset();
|
||||
|
||||
void updateState(SessionStates newState);
|
||||
const char* getSessionStateString();
|
||||
inline SessionStates getSessionState() { SessionStates s; lock("Session::getSessionState"); s = mState; unlock(); return s; }
|
||||
|
||||
inline Poco::UInt64 getEmailVerificationCode() {
|
||||
std::shared_lock<std::shared_mutex> _lock(mSharedMutex);
|
||||
if (mEmailVerificationCodeObject.isNull()) return 0; return mEmailVerificationCodeObject->getModel()->getCode();
|
||||
}
|
||||
inline void setEmailVerificationCodeObject(Poco::AutoPtr<controller::EmailVerificationCode> emailVerficationObject) {
|
||||
std::unique_lock<std::shared_mutex> _lock(mSharedMutex);
|
||||
mEmailVerificationCodeObject = emailVerficationObject;
|
||||
}
|
||||
inline model::table::EmailOptInType getEmailVerificationType() {
|
||||
std::shared_lock<std::shared_mutex> _lock(mSharedMutex);
|
||||
if (mEmailVerificationCodeObject.isNull()) {
|
||||
return model::table::EMAIL_OPT_IN_EMPTY;
|
||||
}
|
||||
return mEmailVerificationCodeObject->getModel()->getType();
|
||||
}
|
||||
|
||||
//! \return -1 if session is locked
|
||||
//! \return 1 if session is active
|
||||
//! \return 0
|
||||
int isActive();
|
||||
//! \return false if session is locked
|
||||
bool setActive(bool active);
|
||||
|
||||
bool isDeadLocked();
|
||||
|
||||
inline Poco::DateTime getLastActivity() { return mLastActivity; }
|
||||
|
||||
// ------------------------ transactions functions ----------------------------
|
||||
|
||||
//! \return true if succeed
|
||||
bool startProcessingTransaction(const std::string& proto_message_base64);
|
||||
//! \param working if set will filled with transaction running
|
||||
Poco::AutoPtr<ProcessingTransaction> getNextReadyTransaction(size_t* working = nullptr);
|
||||
bool finalizeTransaction(bool sign, bool reject);
|
||||
size_t getProcessingTransactionCount();
|
||||
|
||||
inline LanguageCatalog* getLanguageCatalog() { return mLanguageCatalog.isNull() ? nullptr : mLanguageCatalog; }
|
||||
void setLanguage(Languages lang);
|
||||
inline void setLanguageCatalog(Poco::AutoPtr<LanguageCatalog> languageCatalog) { mLanguageCatalog = languageCatalog; }
|
||||
Languages getLanguage();
|
||||
inline const char* gettext(const char* text) { if (mLanguageCatalog.isNull()) return text; return mLanguageCatalog->gettext(text); }
|
||||
|
||||
// last referer
|
||||
inline void setLastReferer(const std::string& lastReferer) { mLastExternReferer = lastReferer; }
|
||||
inline const std::string& getLastReferer() const { return mLastExternReferer; }
|
||||
|
||||
protected:
|
||||
void updateTimeout();
|
||||
inline void setHandle(int newHandle) { mHandleId = newHandle; }
|
||||
|
||||
void detectSessionState();
|
||||
static const char* translateSessionStateToString(SessionStates state);
|
||||
|
||||
inline const std::string& getPassphrase() const { return mPassphrase; }
|
||||
|
||||
|
||||
private:
|
||||
int mHandleId;
|
||||
Poco::AutoPtr<User> mSessionUser;
|
||||
Poco::AutoPtr<controller::User> mNewUser;
|
||||
std::string mPassphrase;
|
||||
Poco::AutoPtr<Passphrase> mNewPassphrase;
|
||||
Poco::DateTime mLastActivity;
|
||||
Poco::Net::IPAddress mClientLoginIP;
|
||||
std::string mLastExternReferer;
|
||||
Poco::AutoPtr<controller::EmailVerificationCode> mEmailVerificationCodeObject;
|
||||
std::shared_mutex mSharedMutex;
|
||||
|
||||
|
||||
SessionStates mState;
|
||||
|
||||
bool mActive;
|
||||
std::list<Poco::AutoPtr<ProcessingTransaction>> mProcessingTransactions;
|
||||
Poco::AutoPtr<ProcessingTransaction> mCurrentActiveProcessingTransaction;
|
||||
|
||||
Poco::AutoPtr<LanguageCatalog> mLanguageCatalog;
|
||||
};
|
||||
|
||||
|
||||
class WriteEmailVerification : public UniLib::controller::CPUTask
|
||||
{
|
||||
public:
|
||||
WriteEmailVerification(Poco::AutoPtr<User> user, Poco::AutoPtr<controller::EmailVerificationCode> emailVerificationCode, UniLib::controller::CPUSheduler* cpuScheduler, size_t taskDependenceCount = 0)
|
||||
: UniLib::controller::CPUTask(cpuScheduler, taskDependenceCount), mUser(user), mEmailVerificationCode(emailVerificationCode) {
|
||||
#ifdef _UNI_LIB_DEBUG
|
||||
setName(user->getEmail());
|
||||
#endif
|
||||
}
|
||||
|
||||
virtual const char* getResourceType() const { return "WriteEmailVerification"; };
|
||||
virtual int run();
|
||||
|
||||
private:
|
||||
Poco::AutoPtr<User> mUser;
|
||||
Poco::AutoPtr<controller::EmailVerificationCode> mEmailVerificationCode;
|
||||
|
||||
};
|
||||
|
||||
class WritePassphraseIntoDB : public UniLib::controller::CPUTask
|
||||
{
|
||||
public:
|
||||
WritePassphraseIntoDB(int userId, const std::string& passphrase)
|
||||
: mUserId(userId), mPassphrase(passphrase) {
|
||||
#ifdef _UNI_LIB_DEBUG
|
||||
setName(std::to_string(userId).data());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
virtual int run();
|
||||
virtual const char* getResourceType() const { return "WritePassphraseIntoDB"; };
|
||||
|
||||
protected:
|
||||
int mUserId;
|
||||
std::string mPassphrase;
|
||||
};
|
||||
|
||||
class SessionStateUpdateCommand : public UniLib::controller::Command
|
||||
{
|
||||
public:
|
||||
SessionStateUpdateCommand(SessionStates state, Session* session)
|
||||
: mState(state), mSession(session) {}
|
||||
virtual int taskFinished(UniLib::controller::Task* task) {
|
||||
mSession->updateState(mState);
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
SessionStates mState;
|
||||
Session* mSession;
|
||||
};
|
||||
|
||||
#endif // DR_LUA_WEB_MODULE_SESSION_SESSION_H
|
||||
/*!
|
||||
*
|
||||
* \author: einhornimmond
|
||||
*
|
||||
* \date: 02.03.19
|
||||
*
|
||||
* \brief: store session data
|
||||
*/
|
||||
|
||||
#ifndef DR_LUA_WEB_MODULE_SESSION_SESSION_H
|
||||
#define DR_LUA_WEB_MODULE_SESSION_SESSION_H
|
||||
|
||||
#include "../lib/ErrorList.h"
|
||||
#include "User.h"
|
||||
#include "../controller/User.h"
|
||||
|
||||
#include "../lib/MultithreadContainer.h"
|
||||
#include "../tasks/ProcessingTransaction.h"
|
||||
|
||||
#include "../SingletonManager/LanguageManager.h"
|
||||
|
||||
#include "../controller/EmailVerificationCode.h"
|
||||
|
||||
#include "Poco/Thread.h"
|
||||
#include "Poco/Types.h"
|
||||
#include "Poco/DateTime.h"
|
||||
#include "Poco/Net/IPAddress.h"
|
||||
#include "Poco/Net/HTTPCookie.h"
|
||||
|
||||
#include <mutex>
|
||||
|
||||
|
||||
class WriteEmailVerification;
|
||||
|
||||
enum SessionStates {
|
||||
SESSION_STATE_EMPTY,
|
||||
SESSION_STATE_CRYPTO_KEY_GENERATED,
|
||||
SESSION_STATE_USER_WRITTEN,
|
||||
SESSION_STATE_EMAIL_VERIFICATION_WRITTEN,
|
||||
SESSION_STATE_EMAIL_VERIFICATION_SEND,
|
||||
SESSION_STATE_EMAIL_VERIFICATION_CODE_CHECKED,
|
||||
SESSION_STATE_PASSPHRASE_GENERATED,
|
||||
SESSION_STATE_PASSPHRASE_SHOWN,
|
||||
SESSION_STATE_PASSPHRASE_WRITTEN,
|
||||
SESSION_STATE_KEY_PAIR_GENERATED,
|
||||
SESSION_STATE_KEY_PAIR_WRITTEN,
|
||||
SESSION_STATE_RESET_PASSWORD_REQUEST,
|
||||
SESSION_STATE_RESET_PASSWORD_SUCCEED,
|
||||
SESSION_STATE_COUNT
|
||||
};
|
||||
|
||||
class SessionManager;
|
||||
|
||||
class UpdateUserPasswordPage;
|
||||
class PassphrasePage;
|
||||
class RepairDefectPassphrase;
|
||||
|
||||
class Session : public ErrorList, public UniLib::lib::MultithreadContainer
|
||||
{
|
||||
friend WriteEmailVerification;
|
||||
friend SessionManager;
|
||||
friend UpdateUserPasswordPage;
|
||||
friend PassphrasePage;
|
||||
friend RepairDefectPassphrase;
|
||||
public:
|
||||
Session(int handle);
|
||||
~Session();
|
||||
|
||||
// get new model objects
|
||||
Poco::AutoPtr<controller::EmailVerificationCode> getEmailVerificationCodeObject();
|
||||
|
||||
// set new model objects
|
||||
inline void setUser(Poco::AutoPtr<controller::User> user) { mNewUser = user; }
|
||||
inline Poco::AutoPtr<controller::User> getNewUser() { return mNewUser; }
|
||||
|
||||
// ---------------- User functions ----------------------------
|
||||
// TODO: register state: written into db, mails sended, update state only if new state is higher as old state
|
||||
// create User send e-mail activation link
|
||||
bool createUser(const std::string& first_name, const std::string& last_name, const std::string& email, const std::string& password);
|
||||
|
||||
//! \brief new register function, without showing user pubkeys, using controller/user
|
||||
bool createUserDirect(const std::string& first_name, const std::string& last_name, const std::string& email, const std::string& password);
|
||||
|
||||
|
||||
// adminRegister without passwort
|
||||
bool adminCreateUser(const std::string& first_name, const std::string& last_name, const std::string& email);
|
||||
|
||||
// TODO: check if email exist and if not, fake waiting on password hashing with profiled times of real password hashing
|
||||
UserStates loadUser(const std::string& email, const std::string& password);
|
||||
bool ifUserExist(const std::string& email);
|
||||
|
||||
inline void setUser(Poco::AutoPtr<User> user) { mSessionUser = user; }
|
||||
|
||||
|
||||
bool deleteUser();
|
||||
|
||||
Poco::AutoPtr<User> getUser() {
|
||||
return mSessionUser;
|
||||
}
|
||||
|
||||
// ------------------------- Email Verification Code functions -------------------------------
|
||||
|
||||
bool loadFromEmailVerificationCode(Poco::UInt64 emailVerificationCode);
|
||||
|
||||
//! \return 1 = konto already exist
|
||||
//! -1 = invalid code
|
||||
//! -2 = critical error
|
||||
//! 0 = ok
|
||||
int updateEmailVerification(Poco::UInt64 emailVerificationCode);
|
||||
|
||||
// called from page with same name
|
||||
//! \return 1 = reset password email already send
|
||||
//! \return 2 = reset password email already shortly before
|
||||
//! \return 0 = ok
|
||||
int sendResetPasswordEmail(Poco::AutoPtr<controller::User> user, bool passphraseMemorized);
|
||||
//
|
||||
//! \return 0 = not the same
|
||||
//! \return 1 = same
|
||||
//! \return -1 = error
|
||||
//! \return -2 = critical error
|
||||
int comparePassphraseWithSavedKeys(const std::string& inputPassphrase, Mnemonic* wordSource);
|
||||
|
||||
Poco::Net::HTTPCookie getLoginCookie();
|
||||
|
||||
|
||||
inline int getHandle() { return mHandleId; }
|
||||
|
||||
// ------------------------ Passphrase functions ----------------------------
|
||||
|
||||
inline void setPassphrase(Poco::AutoPtr<Passphrase> passphrase) { mNewPassphrase = passphrase; }
|
||||
inline Poco::AutoPtr<Passphrase> getPassphrase() { return mNewPassphrase; }
|
||||
|
||||
inline void setPassphrase(const std::string& passphrase) { mPassphrase = passphrase; }
|
||||
|
||||
inline const std::string& getOldPassphrase() { return mPassphrase; }
|
||||
bool generatePassphrase();
|
||||
bool generateKeys(bool savePrivkey, bool savePassphrase);
|
||||
|
||||
inline void setClientIp(Poco::Net::IPAddress ip) { mClientLoginIP = ip; }
|
||||
inline Poco::Net::IPAddress getClientIp() { return mClientLoginIP; }
|
||||
|
||||
inline bool isIPValid(Poco::Net::IPAddress ip) { return mClientLoginIP == ip; }
|
||||
bool isPwdValid(const std::string& pwd);
|
||||
void reset();
|
||||
|
||||
void updateState(SessionStates newState);
|
||||
const char* getSessionStateString();
|
||||
inline SessionStates getSessionState() { SessionStates s; lock("Session::getSessionState"); s = mState; unlock(); return s; }
|
||||
|
||||
inline Poco::UInt64 getEmailVerificationCode() {
|
||||
std::shared_lock<std::shared_mutex> _lock(mSharedMutex);
|
||||
if (mEmailVerificationCodeObject.isNull()) return 0; return mEmailVerificationCodeObject->getModel()->getCode();
|
||||
}
|
||||
inline void setEmailVerificationCodeObject(Poco::AutoPtr<controller::EmailVerificationCode> emailVerficationObject) {
|
||||
std::unique_lock<std::shared_mutex> _lock(mSharedMutex);
|
||||
mEmailVerificationCodeObject = emailVerficationObject;
|
||||
}
|
||||
inline model::table::EmailOptInType getEmailVerificationType() {
|
||||
std::shared_lock<std::shared_mutex> _lock(mSharedMutex);
|
||||
if (mEmailVerificationCodeObject.isNull()) {
|
||||
return model::table::EMAIL_OPT_IN_EMPTY;
|
||||
}
|
||||
return mEmailVerificationCodeObject->getModel()->getType();
|
||||
}
|
||||
|
||||
//! \return -1 if session is locked
|
||||
//! \return 1 if session is active
|
||||
//! \return 0
|
||||
int isActive();
|
||||
//! \return false if session is locked
|
||||
bool setActive(bool active);
|
||||
|
||||
bool isDeadLocked();
|
||||
|
||||
inline Poco::DateTime getLastActivity() { return mLastActivity; }
|
||||
|
||||
// ------------------------ transactions functions ----------------------------
|
||||
|
||||
//! \return true if succeed
|
||||
bool startProcessingTransaction(const std::string& proto_message_base64);
|
||||
//! \param working if set will filled with transaction running
|
||||
Poco::AutoPtr<ProcessingTransaction> getNextReadyTransaction(size_t* working = nullptr);
|
||||
bool finalizeTransaction(bool sign, bool reject);
|
||||
size_t getProcessingTransactionCount();
|
||||
|
||||
inline LanguageCatalog* getLanguageCatalog() { return mLanguageCatalog.isNull() ? nullptr : mLanguageCatalog; }
|
||||
void setLanguage(Languages lang);
|
||||
inline void setLanguageCatalog(Poco::AutoPtr<LanguageCatalog> languageCatalog) { mLanguageCatalog = languageCatalog; }
|
||||
Languages getLanguage();
|
||||
inline const char* gettext(const char* text) { if (mLanguageCatalog.isNull()) return text; return mLanguageCatalog->gettext(text); }
|
||||
|
||||
// last referer
|
||||
inline void setLastReferer(const std::string& lastReferer) { mLastExternReferer = lastReferer; }
|
||||
inline const std::string& getLastReferer() const { return mLastExternReferer; }
|
||||
|
||||
protected:
|
||||
void updateTimeout();
|
||||
inline void setHandle(int newHandle) { mHandleId = newHandle; }
|
||||
|
||||
void detectSessionState();
|
||||
static const char* translateSessionStateToString(SessionStates state);
|
||||
|
||||
inline const std::string& getPassphrase() const { return mPassphrase; }
|
||||
|
||||
|
||||
private:
|
||||
int mHandleId;
|
||||
Poco::AutoPtr<User> mSessionUser;
|
||||
Poco::AutoPtr<controller::User> mNewUser;
|
||||
std::string mPassphrase;
|
||||
Poco::AutoPtr<Passphrase> mNewPassphrase;
|
||||
Poco::DateTime mLastActivity;
|
||||
Poco::Net::IPAddress mClientLoginIP;
|
||||
std::string mLastExternReferer;
|
||||
Poco::AutoPtr<controller::EmailVerificationCode> mEmailVerificationCodeObject;
|
||||
std::shared_mutex mSharedMutex;
|
||||
|
||||
|
||||
SessionStates mState;
|
||||
|
||||
bool mActive;
|
||||
std::list<Poco::AutoPtr<ProcessingTransaction>> mProcessingTransactions;
|
||||
Poco::AutoPtr<ProcessingTransaction> mCurrentActiveProcessingTransaction;
|
||||
|
||||
Poco::AutoPtr<LanguageCatalog> mLanguageCatalog;
|
||||
};
|
||||
|
||||
|
||||
class WriteEmailVerification : public UniLib::controller::CPUTask
|
||||
{
|
||||
public:
|
||||
WriteEmailVerification(Poco::AutoPtr<User> user, Poco::AutoPtr<controller::EmailVerificationCode> emailVerificationCode, UniLib::controller::CPUSheduler* cpuScheduler, size_t taskDependenceCount = 0)
|
||||
: UniLib::controller::CPUTask(cpuScheduler, taskDependenceCount), mUser(user), mEmailVerificationCode(emailVerificationCode) {
|
||||
#ifdef _UNI_LIB_DEBUG
|
||||
setName(user->getEmail());
|
||||
#endif
|
||||
}
|
||||
|
||||
virtual const char* getResourceType() const { return "WriteEmailVerification"; };
|
||||
virtual int run();
|
||||
|
||||
private:
|
||||
Poco::AutoPtr<User> mUser;
|
||||
Poco::AutoPtr<controller::EmailVerificationCode> mEmailVerificationCode;
|
||||
|
||||
};
|
||||
|
||||
class WritePassphraseIntoDB : public UniLib::controller::CPUTask
|
||||
{
|
||||
public:
|
||||
WritePassphraseIntoDB(int userId, const std::string& passphrase)
|
||||
: mUserId(userId), mPassphrase(passphrase) {
|
||||
#ifdef _UNI_LIB_DEBUG
|
||||
setName(std::to_string(userId).data());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
virtual int run();
|
||||
virtual const char* getResourceType() const { return "WritePassphraseIntoDB"; };
|
||||
|
||||
protected:
|
||||
int mUserId;
|
||||
std::string mPassphrase;
|
||||
};
|
||||
|
||||
class SessionStateUpdateCommand : public UniLib::controller::Command
|
||||
{
|
||||
public:
|
||||
SessionStateUpdateCommand(SessionStates state, Session* session)
|
||||
: mState(state), mSession(session) {}
|
||||
virtual int taskFinished(UniLib::controller::Task* task) {
|
||||
mSession->updateState(mState);
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
SessionStates mState;
|
||||
Session* mSession;
|
||||
};
|
||||
|
||||
#endif // DR_LUA_WEB_MODULE_SESSION_SESSION_H
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user