Browse Source

Use QDeadlineTimer for tracking WebUI banned duration

It simplifies our code and the new timer is monotonic.
adaptive-webui-19844
Chocobo1 5 years ago
parent
commit
4f7b799732
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 14
      src/webui/api/authcontroller.cpp
  2. 3
      src/webui/api/authcontroller.h

14
src/webui/api/authcontroller.cpp

@ -28,7 +28,6 @@ @@ -28,7 +28,6 @@
#include "authcontroller.h"
#include <QDateTime>
#include <QString>
#include "base/logger.h"
@ -89,12 +88,13 @@ void AuthController::logoutAction() const @@ -89,12 +88,13 @@ void AuthController::logoutAction() const
bool AuthController::isBanned() const
{
const qint64 now = QDateTime::currentMSecsSinceEpoch() / 1000;
const FailedLogin failedLogin = m_clientFailedLogins.value(sessionManager()->clientId());
const auto failedLoginIter = m_clientFailedLogins.find(sessionManager()->clientId());
if (failedLoginIter == m_clientFailedLogins.end())
return false;
bool isBanned = (failedLogin.bannedAt > 0);
if (isBanned && ((now - failedLogin.bannedAt) > BAN_TIME)) {
m_clientFailedLogins.remove(sessionManager()->clientId());
bool isBanned = (failedLoginIter->banTimer.remainingTime() >= 0);
if (isBanned && failedLoginIter->banTimer.hasExpired()) {
m_clientFailedLogins.erase(failedLoginIter);
isBanned = false;
}
@ -116,6 +116,6 @@ void AuthController::increaseFailedAttempts() @@ -116,6 +116,6 @@ void AuthController::increaseFailedAttempts()
if (failedLogin.failedAttemptsCount >= Preferences::instance()->getWebUIMaxAuthFailCount()) {
// Max number of failed attempts reached
// Start ban period
failedLogin.bannedAt = QDateTime::currentMSecsSinceEpoch() / 1000;
failedLogin.banTimer.setRemainingTime(BAN_TIME);
}
}

3
src/webui/api/authcontroller.h

@ -28,6 +28,7 @@ @@ -28,6 +28,7 @@
#pragma once
#include <QDeadlineTimer>
#include <QHash>
#include "apicontroller.h"
@ -54,7 +55,7 @@ private: @@ -54,7 +55,7 @@ private:
struct FailedLogin
{
int failedAttemptsCount = 0;
qint64 bannedAt = 0;
QDeadlineTimer banTimer {-1};
};
mutable QHash<QString, FailedLogin> m_clientFailedLogins;
};

Loading…
Cancel
Save