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
32 changes: 32 additions & 0 deletions App/Server/StaticModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,36 @@ QVariant IMPL_REST_GET_OR_POST(StaticModule, ping, (
});
}

QVariant IMPL_REST_GET_OR_POST(StaticModule, pinguser, (
APICALLBOOM_TYPE_JWT_USER_IMPL &APICALLBOOM_PARAM
)) {
gServerStats.Success.inc();

QString HostPort = APICALLBOOM_PARAM.hostAndPort();
QString ServerUrl = QString("https://%1%2")
.arg(HostPort)
.arg(ServerCommonConfigs::BasePathWithVersion);

return QJsonObject({
{ "timestamp", QDateTime::currentDateTime().toMSecsSinceEpoch() },
{ "server-host", ServerUrl },
});
}

QVariant IMPL_REST_GET_OR_POST(StaticModule, pingapi, (
APICALLBOOM_TYPE_JWT_API_IMPL &APICALLBOOM_PARAM
)) {
gServerStats.Success.inc();

QString HostPort = APICALLBOOM_PARAM.hostAndPort();
QString ServerUrl = QString("https://%1%2")
.arg(HostPort)
.arg(ServerCommonConfigs::BasePathWithVersion);

return QJsonObject({
{ "timestamp", QDateTime::currentDateTime().toMSecsSinceEpoch() },
{ "server-host", ServerUrl },
});
}

} //namespace Targoman::API::Server
22 changes: 22 additions & 0 deletions App/Server/StaticModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,28 @@ private slots:
EXRESTCONFIG_HIDDEN,
}
);

QVariant EXREST_GET_OR_POST(
pinguser,
(
APICALLBOOM_TYPE_JWT_USER_DECL &APICALLBOOM_PARAM
),
"",
{
EXRESTCONFIG_HIDDEN,
}
);

QVariant EXREST_GET_OR_POST(
pingapi,
(
APICALLBOOM_TYPE_JWT_API_DECL &APICALLBOOM_PARAM
),
"",
{
EXRESTCONFIG_HIDDEN,
}
);
};

} //namespace Targoman::API::Server
Expand Down
18 changes: 13 additions & 5 deletions Interfaces/Common/GenericTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,23 @@ struct stuStatistics {
*/
struct stuTable {
QVariantList Rows;
qint64 TotalRows;
quint64 TotalRows;
quint64 PageCount;
bool HasMore;
bool HasCount;

// stuTable(qint64 _totalRows = -1, const QVariantList& _rows = QVariantList()) :
// TotalRows(_totalRows),
// Rows(_rows)
// { ; }
stuTable(const QVariantList& _rows = QVariantList(),
quint64 _totalRows = 0,
quint64 _pageCount = 0,
bool _hasMore = false,
bool _hasCount = false
) :
Rows(_rows),
TotalRows(_totalRows),
PageCount(_pageCount),
HasMore(_hasMore),
HasCount(_hasCount)
{ ; }

QVariant toVariant(bool _compactList = false) const;
};
Expand Down
36 changes: 19 additions & 17 deletions Interfaces/DBM/QueryBuilders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4091,8 +4091,6 @@ quint64 ORMDeleteQuery::execute(quint64 _currentUserID, QVariantMap _args, bool
///@TODO: start transaction

QT_TRY {
QString statusFieldName = this->Data->table().getStatusColumnName();

//1: invalidate OLD removed row
QString invalidateQueryString = getInvalidatedAtQueryString(this->Data->table(), false, false);

Expand All @@ -4104,22 +4102,26 @@ quint64 ORMDeleteQuery::execute(quint64 _currentUserID, QVariantMap _args, bool
}

//2: soft delete this
QT_TRY {
quint64 rowsAffected = ORMUpdateQuery(this->Data->APICALLBOOM_PARAM, this->Data->table())
.set(statusFieldName, "Removed")
.where(this->WhereTraitData->WhereClauses)
.setPksByPath(this->WhereTraitData->PksByPath)
.addFilters(this->WhereTraitData->Filters)
.execute(_currentUserID);
if (rowsAffected > 0) {
///@TODO: commit
return rowsAffected;
QString statusFieldName = this->Data->table().getStatusColumnName();
if (statusFieldName.isEmpty() == false) {
QT_TRY {
quint64 rowsAffected = ORMUpdateQuery(this->Data->APICALLBOOM_PARAM, this->Data->table())
.set(statusFieldName, "Removed")
.where(this->WhereTraitData->WhereClauses)
.setPksByPath(this->WhereTraitData->PksByPath)
.addFilters(this->WhereTraitData->Filters)
.execute(_currentUserID);

if (rowsAffected > 0) {
///@TODO: commit
return rowsAffected;
}
}
QT_CATCH(...) {
//update failed. use hard delete instead
if (_realDelete == false)
QT_RETHROW;
}
}
QT_CATCH(...) {
//update failed. use hard delete instead
if (_realDelete == false)
QT_RETHROW;
}

//3: real delete this
Expand Down
5 changes: 4 additions & 1 deletion Interfaces/Helpers/RESTClientHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,12 @@ QVariant RESTClientHelper::callAPI(
QtCUrl::Options Opt;
Opt[CURLOPT_URL] = makeURL();
Opt[CURLOPT_FAILONERROR] = false;
#ifdef QT_DEBUG
Opt[CURLOPT_TIMEOUT] = 120;
#else
Opt[CURLOPT_TIMEOUT] = 5;
#endif
Opt[CURLOPT_CONNECTTIMEOUT] = 5;

Opt[CURLOPT_SSL_VERIFYPEER] = 0;
Opt[CURLOPT_SSL_VERIFYHOST] = 0;

Expand Down
144 changes: 144 additions & 0 deletions Interfaces/Helpers/TokenHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/******************************************************************************
# TargomanAPI: REST API for Targoman
#
# Copyright 2014-2020 by Targoman Intelligent Processing <http://tip.co.ir>
#
# TargomanAPI is free software: you can redistribute it and/or modify
# it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TargomanAPI is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU AFFERO GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
# along with Targoman. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
/**
* @author S. Mehran M. Ziabary <ziabary@targoman.com>
* @author Kambiz Zandi <kambizzandi@gmail.com>
*/

#include "TokenHelper.h"
#include "RESTClientHelper.h"

namespace Targoman::API::Helpers {

quint64 TokenHelper::LastFetchedTokenBinID = 0;
qint64 TokenHelper::LastFetchedSecsSinceEpoch = 0;
QMutex TokenHelper::Lock;
//key: TokenMD5
QMap<QString, stuTokenBanInfo> TokenHelper::TokenBanList;

bool TokenHelper::UpdateTokenBinList()
{
//every 1 minutes
#ifndef QT_DEBUG
if (QDateTime::currentSecsSinceEpoch() - TokenHelper::LastFetchedSecsSinceEpoch < 60)
return true;
#endif

QMutexLocker Locker(&TokenHelper::Lock);

//remove expired from cache
if (TokenHelper::TokenBanList.isEmpty() == false) {
for (QMap<QString, stuTokenBanInfo>::iterator it = TokenHelper::TokenBanList.begin();
it != TokenHelper::TokenBanList.end();
it++
) {
if (it->DueDate.isValid() && (it->DueDate <= QDateTime::currentDateTime()))
it = TokenHelper::TokenBanList.erase(it);
}
}

//delete expired from database and fetch newly items
QVariant Result = RESTClientHelper::callAPI(
RESTClientHelper::POST,
"Account/TokenBin/removeExpiredAndFetchNew",
{},
{
{ "lastFetchedID", TokenHelper::LastFetchedTokenBinID },
}
);

TokenHelper::LastFetchedSecsSinceEpoch = QDateTime::currentSecsSinceEpoch();

if (Result.isValid() == false)
return false;

QVariantList Rows = Result.toList();

foreach (QVariant Row, Rows) {
QVariantMap Map = Row.toMap();

stuTokenBanInfo TokenBanInfo;
TokenBanInfo.ID = Map["tkbID"].toULongLong();
TokenBanInfo.MD5 = Map["tkbTokenMD5"].toString();
TokenBanInfo.DueDate = Map["tkbDueDateTime"].toDateTime();

QString Type = Map["tkbType"].toString();
if (Type == "Block")
TokenBanInfo.BanType = enuTokenBanType::Block;
else if (Type == "Pause")
TokenBanInfo.BanType = enuTokenBanType::Pause;

TokenHelper::TokenBanList.insert(TokenBanInfo.MD5, TokenBanInfo);

if (TokenHelper::LastFetchedTokenBinID < TokenBanInfo.ID)
TokenHelper::LastFetchedTokenBinID = TokenBanInfo.ID;
}

return true;
}

enuTokenBanType TokenHelper::GetTokenBanType(const QString &_token)
{
try {
TokenHelper::UpdateTokenBinList();

QString TokenMD5 = QCryptographicHash::hash(_token.toLatin1(), QCryptographicHash::Md5).toHex().constData();

if (TokenHelper::TokenBanList.contains(TokenMD5) == false)
return enuTokenBanType::None;

return TokenHelper::TokenBanList[TokenMD5].BanType;

} catch (std::exception &_exp) {
TargomanDebug(5) << _exp.what();

return enuTokenBanType::None;
}
}

void TokenHelper::tokenRevoked(
const QString &_token,
QDateTime ExpireDateTime,
const QString &NewToken
) {
// QMutexLocker Locker(&TokenHelper::Lock);

// QString TokenMD5 = QCryptographicHash::hash(_token.toLatin1(), QCryptographicHash::Md5).toHex().constData();

}

void TokenHelper::tokenPaused(const QString &_token)
{
// QMutexLocker Locker(&TokenHelper::Lock);

// QString TokenMD5 = QCryptographicHash::hash(_token.toLatin1(), QCryptographicHash::Md5).toHex().constData();

}

void TokenHelper::tokenResumed(const QString &_token)
{
QMutexLocker Locker(&TokenHelper::Lock);

QString TokenMD5 = QCryptographicHash::hash(_token.toLatin1(), QCryptographicHash::Md5).toHex().constData();

if (TokenHelper::TokenBanList.contains(TokenMD5))
TokenHelper::TokenBanList.remove(TokenMD5);
}

} //namespace Targoman::API::Helpers
71 changes: 71 additions & 0 deletions Interfaces/Helpers/TokenHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/******************************************************************************
# TargomanAPI: REST API for Targoman
#
# Copyright 2014-2020 by Targoman Intelligent Processing <http://tip.co.ir>
#
# TargomanAPI is free software: you can redistribute it and/or modify
# it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TargomanAPI is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU AFFERO GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
# along with Targoman. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
/**
* @author S. Mehran M. Ziabary <ziabary@targoman.com>
* @author Kambiz Zandi <kambizzandi@gmail.com>
*/

#ifndef TARGOMAN_API_TOKENHELPER_H
#define TARGOMAN_API_TOKENHELPER_H

#include <QString>
#include <QDateTime>
#include <QMutex>
#include "libTargomanCommon/Macros.h"

namespace Targoman::API::Helpers {

enum class enuTokenBanType {
None,
Block,
Pause,
};

struct stuTokenBanInfo {
quint64 ID;
QString MD5;
QDateTime DueDate;
enuTokenBanType BanType;
};

class TokenHelper
{
public:
static bool UpdateTokenBinList();
static enuTokenBanType GetTokenBanType(const QString &_token);

static void tokenRevoked(
const QString &_token,
QDateTime ExpireDateTime,
const QString &NewToken
);
static void tokenPaused(const QString &_token);
static void tokenResumed(const QString &_token);

private:
static quint64 LastFetchedTokenBinID;
static qint64 LastFetchedSecsSinceEpoch;
static QMutex Lock;
//key: TokenMD5
static QMap<QString, stuTokenBanInfo> TokenBanList;
};

} //namespace Targoman::API::Helpers

#endif // TARGOMAN_API_TOKENHELPER_H
2 changes: 2 additions & 0 deletions Interfaces/Interfaces.pro
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ PRIVATE_HEADERS += \
HEADERS += \
API/intfModuleHelper.h \
Helpers/IteratorHelper.hpp \
Helpers/TokenHelper.h \
ORM/intfFAQ.h \
ORM/intfMigrations.h \
Server/APICallBoom.h \
Expand Down Expand Up @@ -79,6 +80,7 @@ HEADERS += \

SOURCES += \
API/intfModuleHelper.cpp \
Helpers/TokenHelper.cpp \
ORM/intfFAQ.cpp \
ORM/intfMigrations.cpp \
Server/APICallBoom.cpp \
Expand Down
Loading