mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 14:00:39 +02:00
146 lines
4.5 KiB
C++
146 lines
4.5 KiB
C++
#include "requestmanager.h"
|
|
|
|
RequestManager::RequestManager()
|
|
{
|
|
manager = new QNetworkAccessManager();
|
|
token = "";
|
|
loginStatus = false; //should be false, true for testing request header
|
|
|
|
|
|
|
|
QFile configFile(QDir::current().filePath("config.json"));
|
|
if (!configFile.open(QIODevice::ReadOnly)) {
|
|
qCritical() << "Couldn't open config file config.json" << QDir::current().filePath("config.json");
|
|
QCoreApplication::exit(-1);
|
|
return;
|
|
}
|
|
this->config = QJsonDocument::fromJson(configFile.readAll());
|
|
QObject::connect(manager, &QNetworkAccessManager::sslErrors, this, &RequestManager::onSSLError);
|
|
}
|
|
|
|
void RequestManager::login(QString host, QString username, QString password)
|
|
{
|
|
QNetworkRequest request;
|
|
QString hash = encryptedPassword(password);
|
|
authHeader = getLoginHeader(username, hash);
|
|
QJsonObject reqBody = {
|
|
{"password", hash},
|
|
{"username", username}
|
|
};
|
|
this->host = host;
|
|
|
|
request.setUrl(host + "/AaaManager/authSession");
|
|
request.setRawHeader("Authorization", authHeader.toLocal8Bit());
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=UTF-8");
|
|
QJsonDocument jsonDocument;
|
|
|
|
jsonDocument.setObject(reqBody);
|
|
QNetworkReply* reply = manager->post(request, jsonDocument.toJson());
|
|
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
|
if (reply->error())
|
|
{
|
|
qDebug() << reply->errorString();
|
|
loginStatus = false;
|
|
return;
|
|
}
|
|
jsonResponse = reply->readAll();
|
|
prepareAuthHeader();
|
|
loginStatus = true;
|
|
emit successLogin();
|
|
reply->deleteLater();
|
|
});
|
|
}
|
|
|
|
void RequestManager::getRequest(QString endpoint)
|
|
{
|
|
if (!loginStatus)
|
|
{
|
|
login(config.object()["host"].toString(), config.object()["username"].toString(), config.object()["password"].toString());
|
|
// QEventLoop loop;
|
|
// QObject::connect(this, &RequestManager::successLogin, &loop, &QEventLoop::quit);
|
|
// loop.exec();
|
|
}
|
|
|
|
//
|
|
//host = config.object()["host"].toString();
|
|
//
|
|
|
|
QNetworkRequest request;
|
|
request.setUrl(host + endpoint);
|
|
qDebug() << authHeader;
|
|
request.setRawHeader("Authorization", authHeader.toLocal8Bit());
|
|
request.setRawHeader("User-Agent", "curl/7.65.3");
|
|
request.setRawHeader("Connection", "Keep-Alive");
|
|
request.setRawHeader("Accept", "*/*");
|
|
request.setRawHeader("Accept-Encoding", "*");
|
|
request.setRawHeader("Accept-Language", "*");
|
|
|
|
QNetworkReply* reply = manager->post(request, QByteArray());
|
|
|
|
//
|
|
//QEventLoop loop;
|
|
//QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
// loop.exec();
|
|
//
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
|
if (reply->error())
|
|
{
|
|
qDebug() << reply->errorString();
|
|
qDebug() << reply->readAll();
|
|
loginStatus = false;
|
|
getRequest(endpoint);
|
|
return;
|
|
}
|
|
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
jsonResponse = reply->readAll();
|
|
emit responseReady();
|
|
});
|
|
}
|
|
|
|
QString RequestManager::getFromJson(QString json, const QString &member)
|
|
{
|
|
QJsonParseError err;
|
|
QJsonDocument document = QJsonDocument::fromJson(json.toUtf8(),&err);
|
|
if (err.error != 0)
|
|
{
|
|
qDebug() << err.errorString();
|
|
}
|
|
return document.object().value(member).toString();
|
|
}
|
|
|
|
QString RequestManager::encryptedPassword(QString password)
|
|
{
|
|
return QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Sha512).toHex().toUpper();
|
|
}
|
|
|
|
QString RequestManager::getLoginHeader(QString username, QString hash)
|
|
{
|
|
QString authValue = username + ":" + hash;
|
|
return "Basic " + authValue.toLocal8Bit().toBase64();
|
|
}
|
|
|
|
void RequestManager::prepareAuthHeader()
|
|
{
|
|
token = getFromJson(jsonResponse, "id");
|
|
QString authValue = token + ":";
|
|
authHeader = "Basic" + authValue.toLocal8Bit().toBase64();
|
|
}
|
|
|
|
void RequestManager::onSSLError(QNetworkReply *reply, const QList<QSslError> &errors)
|
|
{
|
|
qDebug() << "Network SSL errors";
|
|
for (const auto &error : errors)
|
|
{
|
|
if (error.error() == QSslError::SelfSignedCertificate)
|
|
reply->ignoreSslErrors(errors);
|
|
qDebug() << error.errorString();
|
|
}
|
|
|
|
}
|
|
|
|
void RequestManager::getSubjects()
|
|
{
|
|
getRequest("/SubjectManager/getSubjects");
|
|
}
|