2019-07-12 16:54:26 +02:00
|
|
|
#include <hyperion/AuthManager.h>
|
|
|
|
|
2019-07-14 22:43:22 +02:00
|
|
|
// db
|
2019-07-12 16:54:26 +02:00
|
|
|
#include <db/AuthTable.h>
|
2019-07-14 22:43:22 +02:00
|
|
|
#include <db/MetaTable.h>
|
2019-07-12 16:54:26 +02:00
|
|
|
|
|
|
|
// qt
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QTimer>
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
AuthManager *AuthManager::manager = nullptr;
|
2019-07-12 16:54:26 +02:00
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
AuthManager::AuthManager(QObject *parent)
|
2019-07-12 16:54:26 +02:00
|
|
|
: QObject(parent)
|
2020-03-26 17:59:41 +01:00
|
|
|
, _authTable(new AuthTable("", this))
|
2019-07-14 22:43:22 +02:00
|
|
|
, _metaTable(new MetaTable(this))
|
2019-07-12 16:54:26 +02:00
|
|
|
, _pendingRequests()
|
|
|
|
, _authRequired(true)
|
|
|
|
, _timer(new QTimer(this))
|
2019-09-17 21:33:46 +02:00
|
|
|
, _authBlockTimer(new QTimer(this))
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
|
|
|
AuthManager::manager = this;
|
|
|
|
|
2019-07-14 22:43:22 +02:00
|
|
|
// get uuid
|
|
|
|
_uuid = _metaTable->getUUID();
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
// Register meta
|
|
|
|
qRegisterMetaType<QVector<AuthManager::AuthDefinition>>("QVector<AuthManager::AuthDefinition>");
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
// setup timer
|
|
|
|
_timer->setInterval(1000);
|
|
|
|
connect(_timer, &QTimer::timeout, this, &AuthManager::checkTimeout);
|
|
|
|
|
2019-09-17 21:33:46 +02:00
|
|
|
// setup authBlockTimer
|
|
|
|
_authBlockTimer->setInterval(60000);
|
|
|
|
connect(_authBlockTimer, &QTimer::timeout, this, &AuthManager::checkAuthBlockTimeout);
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
// init with default user and password
|
2020-03-26 17:59:41 +01:00
|
|
|
if (!_authTable->userExist("Hyperion"))
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
_authTable->createUser("Hyperion", "hyperion");
|
2019-07-12 16:54:26 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 21:33:46 +02:00
|
|
|
// update Hyperion user token on startup
|
|
|
|
_authTable->setUserToken("Hyperion");
|
2019-07-12 16:54:26 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
AuthManager::AuthDefinition AuthManager::createToken(const QString &comment)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
|
|
|
const QString token = QUuid::createUuid().toString().mid(1, 36);
|
|
|
|
const QString id = QUuid::createUuid().toString().mid(1, 36).left(5);
|
|
|
|
|
|
|
|
_authTable->createToken(token, comment, id);
|
|
|
|
|
|
|
|
AuthDefinition def;
|
|
|
|
def.comment = comment;
|
|
|
|
def.token = token;
|
|
|
|
def.id = id;
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
emit tokenChange(getTokenList());
|
2019-07-12 16:54:26 +02:00
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
QVector<AuthManager::AuthDefinition> AuthManager::getTokenList()
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
|
|
|
QVector<QVariantMap> vector = _authTable->getTokenList();
|
|
|
|
QVector<AuthManager::AuthDefinition> finalVec;
|
2020-03-26 17:59:41 +01:00
|
|
|
for (const auto &entry : vector)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
|
|
|
AuthDefinition def;
|
|
|
|
def.comment = entry["comment"].toString();
|
|
|
|
def.id = entry["id"].toString();
|
|
|
|
def.lastUse = entry["last_use"].toString();
|
|
|
|
|
|
|
|
// don't add empty ids
|
2020-03-26 17:59:41 +01:00
|
|
|
if (!entry["id"].toString().isEmpty())
|
2019-07-12 16:54:26 +02:00
|
|
|
finalVec.append(def);
|
|
|
|
}
|
|
|
|
return finalVec;
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
const QString AuthManager::getUserToken(const QString &usr)
|
2019-09-17 21:33:46 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
QString tok = _authTable->getUserToken(usr);
|
2019-09-17 21:33:46 +02:00
|
|
|
return QString(_authTable->getUserToken(usr));
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
void AuthManager::setAuthBlock(const bool &user)
|
2019-09-17 21:33:46 +02:00
|
|
|
{
|
|
|
|
// current timestamp +10 minutes
|
2020-03-26 17:59:41 +01:00
|
|
|
if (user)
|
|
|
|
_userAuthAttempts.append(QDateTime::currentMSecsSinceEpoch() + 600000);
|
2019-09-17 21:33:46 +02:00
|
|
|
else
|
2020-03-26 17:59:41 +01:00
|
|
|
_tokenAuthAttempts.append(QDateTime::currentMSecsSinceEpoch() + 600000);
|
2019-09-17 21:33:46 +02:00
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
_authBlockTimer->start();
|
2019-09-17 21:33:46 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
bool AuthManager::isUserAuthorized(const QString &user, const QString &pw)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
if (isUserAuthBlocked())
|
2019-09-17 21:33:46 +02:00
|
|
|
return false;
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
if (!_authTable->isUserAuthorized(user, pw))
|
|
|
|
{
|
2019-09-17 21:33:46 +02:00
|
|
|
setAuthBlock(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-07-12 16:54:26 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
bool AuthManager::isTokenAuthorized(const QString &token)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
if (isTokenAuthBlocked())
|
2019-09-17 21:33:46 +02:00
|
|
|
return false;
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
if (!_authTable->tokenExist(token))
|
|
|
|
{
|
2019-09-17 21:33:46 +02:00
|
|
|
setAuthBlock();
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-26 17:59:41 +01:00
|
|
|
// timestamp update
|
|
|
|
tokenChange(getTokenList());
|
2019-09-17 21:33:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
bool AuthManager::isUserTokenAuthorized(const QString &usr, const QString &token)
|
2019-09-17 21:33:46 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
if (isUserAuthBlocked())
|
2019-09-17 21:33:46 +02:00
|
|
|
return false;
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
if (!_authTable->isUserTokenAuthorized(usr, token))
|
|
|
|
{
|
2019-09-17 21:33:46 +02:00
|
|
|
setAuthBlock(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
bool AuthManager::updateUserPassword(const QString &user, const QString &pw, const QString &newPw)
|
2019-09-17 21:33:46 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
if (isUserAuthorized(user, pw))
|
2019-09-17 21:33:46 +02:00
|
|
|
return _authTable->updateUserPassword(user, newPw);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AuthManager::resetHyperionUser()
|
|
|
|
{
|
|
|
|
return _authTable->resetHyperionUser();
|
2019-07-12 16:54:26 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
void AuthManager::setNewTokenRequest(QObject *caller, const QString &comment, const QString &id)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
if (!_pendingRequests.contains(id))
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
AuthDefinition newDef{id, comment, caller, uint64_t(QDateTime::currentMSecsSinceEpoch() + 180000)};
|
2019-07-12 16:54:26 +02:00
|
|
|
_pendingRequests[id] = newDef;
|
|
|
|
_timer->start();
|
|
|
|
emit newPendingTokenRequest(id, comment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
void AuthManager::cancelNewTokenRequest(QObject *caller, const QString &comment, const QString &id)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
if (_pendingRequests.contains(id))
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
AuthDefinition def = _pendingRequests.value(id);
|
|
|
|
if (def.caller == caller)
|
|
|
|
_pendingRequests.remove(id);
|
|
|
|
emit newPendingTokenRequest(id, "");
|
2019-07-12 16:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
void AuthManager::handlePendingTokenRequest(const QString &id, const bool &accept)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
if (_pendingRequests.contains(id))
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
|
|
|
AuthDefinition def = _pendingRequests.take(id);
|
2020-03-26 17:59:41 +01:00
|
|
|
|
|
|
|
if (accept)
|
|
|
|
{
|
|
|
|
const QString token = QUuid::createUuid().toString().remove("{").remove("}");
|
|
|
|
_authTable->createToken(token, def.comment, id);
|
|
|
|
emit tokenResponse(true, def.caller, token, def.comment, id);
|
|
|
|
emit tokenChange(getTokenList());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit tokenResponse(false, def.caller, QString(), def.comment, id);
|
|
|
|
}
|
2019-07-12 16:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
QVector<AuthManager::AuthDefinition> AuthManager::getPendingRequests()
|
|
|
|
{
|
|
|
|
QVector<AuthManager::AuthDefinition> finalVec;
|
|
|
|
for (const auto &entry : _pendingRequests)
|
|
|
|
{
|
|
|
|
AuthDefinition def;
|
|
|
|
def.comment = entry.comment;
|
|
|
|
def.id = entry.id;
|
|
|
|
def.timeoutTime = entry.timeoutTime - QDateTime::currentMSecsSinceEpoch();
|
|
|
|
finalVec.append(def);
|
|
|
|
}
|
|
|
|
return finalVec;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AuthManager::renameToken(const QString &id, const QString &comment)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
if (_authTable->renameToken(id, comment))
|
|
|
|
{
|
|
|
|
emit tokenChange(getTokenList());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-07-12 16:54:26 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
bool AuthManager::deleteToken(const QString &id)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
if (_authTable->deleteToken(id))
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
emit tokenChange(getTokenList());
|
2019-07-12 16:54:26 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
void AuthManager::handleSettingsUpdate(const settings::type &type, const QJsonDocument &config)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
if (type == settings::NETWORK)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
const QJsonObject &obj = config.object();
|
2019-07-12 16:54:26 +02:00
|
|
|
_authRequired = obj["apiAuth"].toBool(true);
|
|
|
|
_localAuthRequired = obj["localApiAuth"].toBool(false);
|
2020-03-26 17:59:41 +01:00
|
|
|
_localAdminAuthRequired = obj["localAdminAuth"].toBool(true);
|
2019-07-12 16:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::checkTimeout()
|
|
|
|
{
|
|
|
|
const uint64_t now = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
|
|
|
|
QMapIterator<QString, AuthDefinition> i(_pendingRequests);
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
2020-03-26 17:59:41 +01:00
|
|
|
i.next();
|
2019-07-12 16:54:26 +02:00
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
const AuthDefinition &def = i.value();
|
|
|
|
if (def.timeoutTime <= now)
|
2019-07-12 16:54:26 +02:00
|
|
|
{
|
|
|
|
emit tokenResponse(false, def.caller, QString(), def.comment, def.id);
|
|
|
|
_pendingRequests.remove(i.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// abort if empty
|
2020-03-26 17:59:41 +01:00
|
|
|
if (_pendingRequests.isEmpty())
|
2019-07-12 16:54:26 +02:00
|
|
|
_timer->stop();
|
|
|
|
}
|
2019-09-17 21:33:46 +02:00
|
|
|
|
2020-03-26 17:59:41 +01:00
|
|
|
void AuthManager::checkAuthBlockTimeout()
|
|
|
|
{
|
2019-09-17 21:33:46 +02:00
|
|
|
// handle user auth block
|
2020-03-26 17:59:41 +01:00
|
|
|
for (auto it = _userAuthAttempts.begin(); it != _userAuthAttempts.end(); it++)
|
|
|
|
{
|
2019-09-17 21:33:46 +02:00
|
|
|
// after 10 minutes, we remove the entry
|
2020-03-26 17:59:41 +01:00
|
|
|
if (*it < (uint64_t)QDateTime::currentMSecsSinceEpoch())
|
|
|
|
{
|
2019-09-17 21:33:46 +02:00
|
|
|
_userAuthAttempts.erase(it--);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle token auth block
|
2020-03-26 17:59:41 +01:00
|
|
|
for (auto it = _tokenAuthAttempts.begin(); it != _tokenAuthAttempts.end(); it++)
|
|
|
|
{
|
2019-09-17 21:33:46 +02:00
|
|
|
// after 10 minutes, we remove the entry
|
2020-03-26 17:59:41 +01:00
|
|
|
if (*it < (uint64_t)QDateTime::currentMSecsSinceEpoch())
|
|
|
|
{
|
2019-09-17 21:33:46 +02:00
|
|
|
_tokenAuthAttempts.erase(it--);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the lists are empty we stop
|
2020-03-26 17:59:41 +01:00
|
|
|
if (_userAuthAttempts.empty() && _tokenAuthAttempts.empty())
|
2019-09-17 21:33:46 +02:00
|
|
|
_authBlockTimer->stop();
|
|
|
|
}
|