Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Interfaces/AAA/Accounting_Defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "libTargomanCommon/Configuration/Validators.hpp"
#include <QString>
#include <QJsonArray>
#include "Interfaces/Server/clsSimpleCrypt.h"

namespace Targoman::API::AAA {

Expand Down Expand Up @@ -99,6 +100,8 @@ namespace Targoman::API::AAA {
inline QString makeConfig(const QString& _name) { return "/Module_Account/" + _name; }
extern Targoman::Common::Configuration::tmplConfigurable<QString> Secret;
extern QByteArray voucherSign(const QByteArray& _data);
extern Targoman::Common::Configuration::tmplConfigurable<quint64> SimpleCryptKey;
extern clsSimpleCrypt* simpleCryptInstance();

constexpr char VOUCHER_ITEM_NAME_INC_WALLET[] = "INC_WALLET";
constexpr char VOUCHER_ITEM_NAME_COD_CREDIT[] = "COD_CREDIT";
Expand Down Expand Up @@ -755,6 +758,10 @@ TAPI_DEFINE_STRUCT(stuPendingVoucher,
);
//typedef QList<stuPendingVoucher> PendingVouchers_t;

TAPI_DEFINE_STRUCT(stuVoucherItemPrivate,
SF_QListOfVarStruct (PendingVouchers, stuPendingVoucher)
);

TAPI_DEFINE_STRUCT(stuAssetItem,
SF_Struct (Product, tblAccountProductsBase::DTO, v.prdID),
SF_Struct (Saleable, tblAccountSaleablesBase::DTO, v.slbID),
Expand Down Expand Up @@ -784,7 +791,8 @@ TAPI_DEFINE_STRUCT(stuAssetItem,

SF_QJsonObject (AdditionalInfo), //per service

SF_QListOfVarStruct (PendingVouchers, stuPendingVoucher)
SF_Struct (Private, stuVoucherItemPrivate, [](auto v) { return true; } (v))
// SF_QListOfVarStruct (PendingVouchers, stuPendingVoucher)
);

typedef QMap<QString, stuAssetItem> ActiveCredits_t;
Expand Down Expand Up @@ -857,7 +865,9 @@ TAPI_DEFINE_STRUCT(stuVoucherItem,
SF_QString (Referrer),
SF_JSON_t (ReferrerParams),

SF_QListOfVarStruct (PendingVouchers, stuPendingVoucher),
// SF_QListOfVarStruct (PendingVouchers, stuPendingVoucher),
SF_QString (Private), //encrypted + base64
// SF_Struct (Private, stuVoucherItemPrivate, [](auto v) { return true; } (v)),
SF_QListOfVarStruct (SubItems, stuVoucherItem),

SF_QString (Sign)
Expand Down
58 changes: 53 additions & 5 deletions Interfaces/AAA/intfAccountingBasedModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "PrivHelpers.h"
#include "Interfaces/AAA/Authorization.h"
#include "App/Server/ServerConfigs.h"
#include "libTargomanCommon/Configuration/Validators.hpp"
using namespace Targoman::Common::Configuration;

//#include "Server/clsSimpleCrypt.h"
#include "Interfaces/Helpers/SecurityHelper.h"
Expand All @@ -41,9 +43,9 @@ namespace Targoman::API::AAA {
static QMap<QString, intfAccountingBasedModule*> ServiceRegistry;

Targoman::Common::Configuration::tmplConfigurable<QString> Secret(
makeConfig("Secret"),
"Secret to be used for signing voucher and prevoucher",
"fcy^E?a*4<;auY?>^6s@");
makeConfig("Secret"),
"Secret to be used for signing voucher and prevoucher",
"fcy^E?a*4<;auY?>^6s@");

QByteArray voucherSign(const QByteArray& _data) {
return QMessageAuthenticationCode::hash(_data, Secret.value().toUtf8(), QCryptographicHash::Sha256);
Expand All @@ -67,6 +69,27 @@ void checkPreVoucherSanity(stuPreVoucher _preVoucher) {
}
}

Targoman::Common::Configuration::tmplConfigurable<quint64> SimpleCryptKey(
makeConfig("SimpleCryptKey"),
"Secret to be used for encrypting private objects",
static_cast<quint64>(43121109170974192),
ReturnTrueCrossValidator(),
"",
"", //"SECRET",
"", //"jwt-private-key",
/*enuConfigSource::Arg | */enuConfigSource::File);

thread_local static clsSimpleCrypt* SimpleCryptInstance = nullptr;
clsSimpleCrypt* simpleCryptInstance()
{
if (Q_UNLIKELY(!SimpleCryptInstance)) {
SimpleCryptInstance = new clsSimpleCrypt(SimpleCryptKey.value());
SimpleCryptInstance->setIntegrityProtectionMode(clsSimpleCrypt::ProtectionHash);
}

return SimpleCryptInstance;
}

intfAccountingBasedModule* serviceAccounting(const QString& _serviceName) {
return ServiceRegistry.value(_serviceName);
}
Expand Down Expand Up @@ -464,7 +487,17 @@ Targoman::API::AAA::stuPreVoucher IMPL_REST_POST(intfAccountingBasedModule, addT
//-- --------------------------------
stuVoucherItem PreVoucherItem;

PreVoucherItem.PendingVouchers = AssetItem.PendingVouchers;


// QJsonArray JSAPendingVouchers;
// foreach (auto PendingVoucher, AssetItem.PendingVouchers)
// JSAPendingVouchers.append(PendingVoucher.toJson());
QJsonDocument JSDPendingVouchers = QJsonDocument();
// JSDPendingVouchers.setArray(JSAPendingVouchers);
// PreVoucherItem.PendingVouchers = simpleCryptInstance()->encryptToString(JSDPendingVouchers.toJson(QJsonDocument::Compact));
JSDPendingVouchers.setObject(AssetItem.Private.toJson());
PreVoucherItem.Private = simpleCryptInstance()->encryptToString(JSDPendingVouchers.toJson(QJsonDocument::Compact));

PreVoucherItem.Service = this->ServiceName;
//PreVoucherItem.OrderID
PreVoucherItem.UUID = SecurityHelper::UUIDtoMD5();
Expand Down Expand Up @@ -717,6 +750,13 @@ Targoman::API::AAA::stuPreVoucher intfAccountingBasedModule::internalUpdateBaske
AssetItem.Product.fromJson(QJsonObject::fromVariantMap(UserAssetInfo));
AssetItem.Saleable.fromJson(QJsonObject::fromVariantMap(UserAssetInfo));

//-- --------------------------------
// AssetItem.PendingVouchers.clear();
QString StrPrivate = simpleCryptInstance()->decryptToString(_voucherItem.Private);
// foreach (auto I, QJsonDocument().fromJson(StrPendingVouchers.toLatin1()).array())
// AssetItem.PendingVouchers.append(stuPendingVoucher().fromJson(I.toObject()));
AssetItem.Private.fromJson(QJsonDocument().fromJson(StrPrivate.toLatin1()).object());

//-- --------------------------------
AssetItem.DiscountCode = NULLABLE_GET_OR_DEFAULT(_newDiscountCode, _voucherItem.CouponDiscount.Code);
AssetItem.OrderAdditives = _voucherItem.Additives;
Expand Down Expand Up @@ -771,7 +811,15 @@ Targoman::API::AAA::stuPreVoucher intfAccountingBasedModule::internalUpdateBaske
} else { //update
FinalPrice += AssetItem.TotalPrice;

_voucherItem.PendingVouchers = AssetItem.PendingVouchers;
// QJsonArray JSAPendingVouchers;
// foreach (auto PendingVoucher, AssetItem.PendingVouchers)
// JSAPendingVouchers.append(PendingVoucher.toJson());
QJsonDocument JSDPendingVouchers = QJsonDocument();
// JSDPendingVouchers.setArray(JSAPendingVouchers);
// _voucherItem.PendingVouchers = simpleCryptInstance()->encryptToString(JSDPendingVouchers.toJson(QJsonDocument::Compact));
JSDPendingVouchers.setObject(AssetItem.Private.toJson());
_voucherItem.Private = simpleCryptInstance()->encryptToString(JSDPendingVouchers.toJson(QJsonDocument::Compact));

_voucherItem.Service = this->ServiceName;
// _voucherItem.UUID = _voucherItem.UUID;
_voucherItem.Desc = AssetItem.Saleable.slbName;
Expand Down
4 changes: 1 addition & 3 deletions Interfaces/Server/QJWT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ tmplConfigurable<quint32> QJWT::RememberLoginTTL(
enuConfigSource::Arg | enuConfigSource::File);

thread_local static clsSimpleCrypt* SimpleCryptInstance = nullptr;

static clsSimpleCrypt* simpleCryptInstance()
{
static clsSimpleCrypt* simpleCryptInstance() {
if (Q_UNLIKELY(!SimpleCryptInstance)) {
SimpleCryptInstance = new clsSimpleCrypt(QJWT::SimpleCryptKey.value());
SimpleCryptInstance->setIntegrityProtectionMode(clsSimpleCrypt::ProtectionHash);
Expand Down
17 changes: 10 additions & 7 deletions Modules/Advert/moduleSrc/Advert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,22 @@ void Advert::computeReferrer(
if (_oldVoucherItem != nullptr) {
if (_oldVoucherItem->Qty != _assetItem.Qty) {
//remove old
for (QList<stuPendingVoucher>::iterator it = _assetItem.PendingVouchers.begin();
it != _assetItem.PendingVouchers.end();
it++
) {
QList<stuPendingVoucher>::iterator it = _assetItem.Private.PendingVouchers.begin();
while (it != _assetItem.Private.PendingVouchers.end()) {
if ((it->Name == PENDING_VOUCHER_NAME_REFERRER_PRIZE)
&& (it->Info.contains("referrer")))
it = _assetItem.PendingVouchers.erase(it);
&& (it->Info.contains("referrer"))
) {
it = _assetItem.Private.PendingVouchers.erase(it);
continue;
}

++it;
}
}
}

if (_assetItem.Qty > 0) {
_assetItem.PendingVouchers.append({
_assetItem.Private.PendingVouchers.append({
/* Name */ PENDING_VOUCHER_NAME_REFERRER_PRIZE,
/* Type */ enuVoucherType::Prize, //Credit,
/* Amount */ 2000 * _assetItem.Qty,
Expand Down