forked from RobertKrajewski/Sync-my-L2P
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logintester.cpp
176 lines (151 loc) · 6.51 KB
/
logintester.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/****************************************************************************
** This file is part of Sync-my-L2P.
**
** Sync-my-L2P is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Sync-my-L2P 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with Sync-my-L2P. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "logintester.h"
#include "ui_logintester.h"
#include <QSslConfiguration>
#include <QSsl>
#include <QFile>
LoginTester::LoginTester(QString username,
QString password,
int maxcount,
QWidget *parent) :
QDialog(parent, Qt::FramelessWindowHint),
ui(new Ui::LoginTester)
{
ui->setupUi(this);
ui->button->hide();
// Initialisieren der Variablen
tryCounter = 0;
this->maxTries = maxcount;
this->username = username;
this->password = password;
// Initialisieren des NetworkManagers und der Slots
manager = new QNetworkAccessManager(qApp);
QFile cert(":/certs/l2p");
cert.open(QFile::ReadOnly);
QList<QSslCertificate> newCertificates = QSslCertificate::fromData(cert.readAll(),QSsl::Der);
cert.close();
QSslCertificate newCertificate = newCertificates.first();
QFile cert2(":/certs/utn");
cert2.open(QFile::ReadOnly);
QList<QSslCertificate> newCertificates2 = QSslCertificate::fromData(cert2.readAll(),QSsl::Der);
cert2.close();
QSslCertificate newCertificate2 = newCertificates2.first();
QFile cert3(":/certs/ssl");
cert3.open(QFile::ReadOnly);
QList<QSslCertificate> newCertificates3 = QSslCertificate::fromData(cert3.readAll(),QSsl::Der);
cert3.close();
QSslCertificate newCertificate3 = newCertificates3.first();
QSslConfiguration newSslConfiguration = QSslConfiguration::defaultConfiguration();
newCertificates = newSslConfiguration.caCertificates();
newCertificates.append(newCertificate);
newCertificates.append(newCertificate2);
newCertificates.append(newCertificate3);
newSslConfiguration.setCaCertificates(newCertificates);
QSslConfiguration::setDefaultConfiguration(newSslConfiguration);
QSslConfiguration newSslConfiguration2 = QSslConfiguration::defaultConfiguration();
newCertificates2 = newSslConfiguration2.caCertificates();
foreach (QSslCertificate c, newCertificates2)
{
QStringList list = QStringList(c.subjectInfo(QSslCertificate::CommonName));
for(QStringList::iterator i = list.begin(); i != list.end(); i++){
qDebug(i->toLatin1());
}
}
QObject::connect(manager, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), this, SLOT(authenticationSlot(QNetworkReply*, QAuthenticator*)));
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
// Starte den Login nach 100ms
// Bei 0ms wird der Shot unter Mac nicht ausgeführt!?
QTimer::singleShot(100, this, SLOT(startSlot()));
}
LoginTester::~LoginTester()
{
delete ui;
}
void LoginTester::authenticationSlot(QNetworkReply* , QAuthenticator* authenticator)
{
qDebug("authenticationSlot call");
// Logindaten nur ausfüllen, falls nicht mehr als die maximale Anzahl an Versuchen durchgeführt wurden
if (tryCounter < maxTries)
{
authenticator->setUser(username);
authenticator->setPassword(password);
}
// Erhöhen des Zählers nach jedem Versuch
tryCounter++;
}
void LoginTester::startSlot()
{
// Aufruf der L2P-Startseite zum Test der Logindaten
QNetworkReply* reply = manager->get(QNetworkRequest(QUrl("https://www2.elearning.rwth-aachen.de/foyer/summary/default.aspx")));
QObject::connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrorsSlot(QList<QSslError>)));
qDebug("Connection started");
}
void LoginTester::checkCertValidity(const QSslCertificate& cert){
if(cert.isValid())
qDebug() << "VALID certificate for" << cert.subjectInfo(QSslCertificate::CommonName);
else if (cert.isNull())
qDebug("NULL certificate");
else if(QDateTime::currentDateTime() > cert.expiryDate())
{
qDebug() << "EXPIRED certificate for" << cert.subjectInfo(QSslCertificate::CommonName);
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
else if(cert.isBlacklisted())
{
qDebug() << "BLACKLISTED certificate for" << cert.subjectInfo(QSslCertificate::CommonName);
}
#endif
}
void LoginTester::sslErrorsSlot(QList<QSslError> sslErrors)
{
qDebug("sslErrorsSlot call");
foreach(QSslError error, sslErrors)
{
qDebug((QString("Error: ") + QString::number(error.error())+ QString(" ")+ error.errorString()).toLatin1());
QSslCertificate certificate = error.certificate();
checkCertValidity(certificate);
QMessageBox messageBox;
messageBox.setText("Ssl Error");
messageBox.setInformativeText(QString("Error: ") + QString::number(error.error())+ QString(" ")+ error.errorString() +"\r\n Zertifikat:"+certificate.subjectInfo(QSslCertificate::CommonName).at(0));
messageBox.exec();
}
}
void LoginTester::finishedSlot(QNetworkReply* reply)
{
// qDebug(QString(reply->readAll()).toLatin1());
QSslConfiguration sslConfiguration = reply->sslConfiguration();
qDebug(QString("Protocol: " + QString::number(sslConfiguration.protocol())).toLatin1());
qDebug(QString("Verfiy Depth: " + QString::number(sslConfiguration.peerVerifyDepth())).toLatin1());
QList<QSslCertificate> sslCertificateList = sslConfiguration.peerCertificateChain();
foreach(QSslCertificate localCertificate, sslCertificateList){
checkCertValidity(localCertificate);
}
// Fehlerbehandlung
if (reply->error())
{
QMessageBox messageBox;
messageBox.setText("Login fehlgeschlagen");
messageBox.setInformativeText(QString(reply->errorString()));
messageBox.setDetailedText(QString(reply->readAll()));
messageBox.setStandardButtons(QMessageBox::Ok);
messageBox.exec();
reject();
}
else
accept();
}