9 #include <QNetworkAccessManager>
10 #include <QNetworkCookie>
15 #include <QStringMatcher>
51 : m_theClass(theClass)
52 , m_netmanager(
new QNetworkAccessManager())
56 , m_fileInfoUrl(fileInfoUrl)
57 , m_loginServer(
"login.cern.ch")
59 #if defined BUILDVP1LIGHT
65 if(checkAuthEnabled) {
66 QString
logname(
"vp1live-auth.log");
68 std::cerr <<
"VP1Authenticator ERROR: Unable to remove old logfile" << std::endl;
71 if(!
m_log->open(QIODevice::WriteOnly)) {
72 std::cerr <<
"VP1Authenticator ERROR: Unable to open new logfile for writing" << std::endl;
93 QStringMatcher startMatcher(attribute +
"=\"",Qt::CaseInsensitive);
94 QStringMatcher endMatcher(
"\"");
96 int startPos = startMatcher.indexIn(
tag);
100 int endPos = endMatcher.indexIn(
tag,startPos+startMatcher.pattern().size());
104 return tag.mid(startPos+startMatcher.pattern().size(),endPos-startPos-startMatcher.pattern().size());
112 connect(m_netreply, SIGNAL(
error(QNetworkReply::NetworkError)),
113 authenticator, SLOT(
error(QNetworkReply::NetworkError)));
116 authenticator, SLOT(
sslErrors(
const QList<QSslError>&)));
122 m_theClass->teditError->setVisible(
true);
123 m_theClass->setFixedSize(400,350);
124 m_theClass->teditError->append(
message);
125 m_theClass->inpPhr->clear();
126 m_theClass->inpPers->setEnabled(
true);
127 m_theClass->inpPhr->setEnabled(
true);
128 m_theClass->inpPhr->setFocus();
130 QByteArray logBa(QString(
message+
"\n").toStdString().c_str());
141 setFixedSize(400,200);
144 teditError->setVisible(
false);
145 teditError->setLineWrapMode(QTextEdit::NoWrap);
146 pbtnLogin->setDefault(
true);
150 connect(pbtnCancel,SIGNAL(clicked()),
this,SLOT(reject()));
153 qRegisterMetaType<QList<QSslError> >(
"QList<QSslError>");
167 QNetworkRequest netrequest(fileinfoUrl);
179 QList<QNetworkCookie> cookielist;
185 if(
val.type()==QVariant::Invalid) {
186 message += QString(
" No set cookies\n");
187 }
else if (!
val.canConvert<QList<QNetworkCookie> >()){
188 message += QString(
" Cannot convert to the list of cookies\n");
190 cookielist =
val.value<QList<QNetworkCookie> >();
191 for(
int ii=0; ii<cookielist.size(); ++ii) {
192 const QNetworkCookie& cookie = cookielist.at(ii);
194 message += (
" *** Path: " + cookie.path() +
"\n");
195 message += (
" *** Domain: " + cookie.domain() +
"\n");
196 message += (QString(
" *** Secure: ") + (cookie.isSecure() ?
"YES" :
"NO") +
"\n");
197 message += (QString(
" *** Session: ") + (cookie.isSessionCookie() ?
"YES" :
"NO") +
"\n");
198 message += (
" *** Name: " + QString(cookie.name().constData()) +
"\n");
199 message += (
" *** Value: " + QString(cookie.value().constData()) +
"\n");
204 if(
val.type()==QVariant::Invalid) {
205 message += QString(
" No ContentType\n");
206 }
else if (!
val.canConvert<QString>()) {
207 message += QString(
" Cannot convert Content Type to String\n");
209 QString conttype =
val.value<QString>();
210 message += QString(
" Content type: " + conttype +
"\n");
214 if(
val.type()==QVariant::Invalid) {
215 message += QString(
" No ContentLength\n");
216 }
else if (!
val.canConvert<
int>()) {
217 message += QString(
" Cannot convert Content Length to int\n");
219 int contlength =
val.value<
int>();
224 if(
val.type()==QVariant::Invalid) {
225 message += QString(
" No Location\n");
226 }
else if (!
val.canConvert<QUrl>()) {
227 message += QString(
" Cannot convert Content Length to QUrl\n");
229 QUrl
url =
val.value<QUrl>();
230 message += QString(
" Location URL " +
url.toString() +
"\n");
235 if(
val.type()==QVariant::Invalid) {
236 message += QString(
" No StatusCode Attribute\n");
237 }
else if (!
val.canConvert<
int>()) {
238 message += QString(
" Cannot convert StatusCode to int\n");
240 int sc =
val.value<
int>();
244 val =
m_d->
m_netreply->attribute(QNetworkRequest::RedirectionTargetAttribute);
245 if(
val.type()==QVariant::Invalid) {
246 message += QString(
" No Redirection Attribute\n");
247 }
else if (!
val.canConvert<QUrl>()) {
248 message += QString(
" Cannot convert Redirection to QUrl\n");
250 redirectionUrl =
val.value<QUrl>();
251 message += QString(
" Redirection : " + redirectionUrl.toString() +
"\n");
254 message += QString(
"\n HTML response >>>>>\n");
256 QByteArray logMessage(
message.toStdString().c_str());
260 logMessage = logMessage.append(ba);
261 logMessage = logMessage.append(
"\n<<<<< HTML response\n\n\n");
273 QString replyBody(ba.data());
274 if(replyBody.contains(
"authorization failed", Qt::CaseInsensitive)) {
275 QString errMessage(
"Authorization Failed");
290 QString errMessage(
"Network error occured during authentication phase\n");
291 errMessage += QString(
"Error code " +
QString::number((
int)
m_d->
m_netreply->error()) +
"\nExplanation on http://doc.trolltech.com/4.4/qnetworkreply.html#NetworkError-enum");
301 if(redirectionUrl.isEmpty() || redirectionUrl.host() !=
m_d->
m_loginServer) {
302 QString errMessage(
"Wrong URL: " +
m_d->
m_fileInfoUrl +
"\nPlease fix the URL and restart the job");
307 QNetworkRequest netrequest(redirectionUrl);
310 QByteArray baLog(
"Get request sent\n_______________________________________________________\n\n");
324 QString replyBody(ba.data());
326 QString newRequestBody(
"__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=");
328 QStringMatcher inputStartMatcher(
"<input ",Qt::CaseInsensitive);
329 QStringMatcher inputEndMatcher(
"/>");
331 int inputStart = inputStartMatcher.indexIn(replyBody,0);
333 while(inputStart!=-1) {
334 int inputEnd = inputEndMatcher.indexIn(replyBody,inputStart+inputStartMatcher.pattern().size());
339 QString
tag = replyBody.mid(inputStart,inputEnd-inputStart);
345 valueVal = inpPers->text();
347 valueVal = inpPhr->text();
350 QByteArray encodedNameVal = QUrl::toPercentEncoding(nameVal);
354 newRequestBody+=(
"&"+QString(encodedNameVal)+
"="+valueVal);
356 QByteArray encodedValueVal = QUrl::toPercentEncoding(valueVal);
357 if(newRequestBody.size()!=0)
359 newRequestBody+=(QString(encodedNameVal)+
"="+QString(encodedValueVal));
364 inputStart = inputStartMatcher.indexIn(replyBody,inputEnd+inputEndMatcher.pattern().size());
368 QByteArray newBody = newRequestBody.toUtf8();
370 QString logMessage =
"New Request Length: " +
QString::number(newBody.size()) +
"\n";
371 logMessage += (
"New Request Body:\n" + newRequestBody.replace(inpPhr->text(),
"xxx") +
"\n");
377 QStringMatcher actionStartMatcher(
"action=\"",Qt::CaseInsensitive);
378 QStringMatcher actionEndMatcher(
"\"");
379 int actionStart = actionStartMatcher.indexIn(replyBody,0);
380 if(actionStart!=-1) {
381 int actionEnd = actionEndMatcher.indexIn(replyBody,actionStart+actionStartMatcher.pattern().size());
383 actionUrlString += replyBody.mid(actionStart+actionStartMatcher.pattern().size(),actionEnd-actionStart-actionStartMatcher.pattern().size());
386 logMessage += (
"New URL: " + actionUrlString +
"\n");
387 QByteArray actionUrlBa(actionUrlString.toStdString().c_str());
388 QString actionUrlStringDecoded = QUrl::fromPercentEncoding(actionUrlBa);
389 logMessage += (
"Decoded URL: " + actionUrlStringDecoded.replace(
"&",
"&")
390 +
"\n\nPost request sent\n_______________________________________________________\n\n");
393 QNetworkRequest netrequest(QUrl(actionUrlStringDecoded.replace(
"&",
"&")));
394 netrequest.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
395 netrequest.setHeader(QNetworkRequest::ContentLengthHeader,newBody.size());
398 QByteArray baLog(logMessage.toStdString().c_str());
420 if(cookielist.size()==0) {
422 QString errMessage(
"Authentication failed, please try again\n");
428 QString replyBody(ba.data());
429 QByteArray excludeFromEncoding(
" ");
430 QString newRequestBody(
"");
432 QStringMatcher inputStartMatcher(
"<input ",Qt::CaseInsensitive);
433 QStringMatcher inputEndMatcher(
"/>");
435 int inputStart = inputStartMatcher.indexIn(replyBody,0);
437 while(inputStart!=-1) {
441 int inputEnd = inputStart;
443 int quotePos = inputStart;
444 QStringMatcher quoteMatcher(
"\"");
446 while(quoteCount%2!=0) {
447 inputEnd = inputEndMatcher.indexIn(replyBody,quotePos);
451 quotePos = inputStart;
454 quotePos = quoteMatcher.indexIn(replyBody,quotePos);
455 if(quotePos==-1||quotePos>inputEnd)
466 QString
tag = replyBody.mid(inputStart,inputEnd-inputStart);
472 valueVal = inpPers->text();
474 valueVal = inpPhr->text();
478 QByteArray encodedNameVal = QUrl::toPercentEncoding(nameVal);
479 QString valueVal1 = valueVal.replace(
"<",
"<");
480 QString valueVal2 = valueVal.replace(
""",
"\"");
481 QByteArray encodedValueVal = QUrl::toPercentEncoding(valueVal2,excludeFromEncoding);
482 if(newRequestBody.size()!=0)
484 newRequestBody+=(QString(encodedNameVal)+
"="+QString(encodedValueVal).replace(
" ",
"+"));
488 inputStart = inputStartMatcher.indexIn(replyBody,inputEnd+inputEndMatcher.pattern().size());
492 QByteArray newBody = newRequestBody.toUtf8();
494 QString logMessage =
"New Request Length: " +
QString::number(newBody.size()) +
"\n";
495 logMessage += (
"New Request Body:\n" + newRequestBody +
"\n");
498 QString actionUrlString(
"");
500 QStringMatcher actionStartMatcher(
"action=\"",Qt::CaseInsensitive);
501 QStringMatcher actionEndMatcher(
"\"");
502 int actionStart = actionStartMatcher.indexIn(replyBody,0);
503 if(actionStart!=-1) {
504 int actionEnd = actionEndMatcher.indexIn(replyBody,actionStart+actionStartMatcher.pattern().size());
506 actionUrlString = replyBody.mid(actionStart+actionStartMatcher.pattern().size(),actionEnd-actionStart-actionStartMatcher.pattern().size());
509 logMessage += (
"New URL: " + actionUrlString +
"\n");
512 QNetworkRequest netrequest;
513 netrequest.setUrl(actionUrlString);
514 netrequest.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
515 netrequest.setHeader(QNetworkRequest::ContentLengthHeader,newBody.size());
517 logMessage += (
"\n\nPost request sent\n_______________________________________________________\n\n");
519 QByteArray baLog(logMessage.toStdString().c_str());
531 QNetworkRequest netrequest(redirectionUrl);
534 QByteArray baLog(
"Get request sent\n_______________________________________________________\n\n");
547 QByteArray ba(
message.toStdString().c_str());
557 for(
int ii=0; ii<errlist.size(); ++ii)
558 message += (
" " +
QString::number((
int)errlist.at(ii).error()) +
", " + errlist.at(ii).errorString() +
"\n");
559 QByteArray ba(
message.toStdString().c_str());
570 if(teditError->isVisible()) {
571 teditError->setVisible(
false);
572 setFixedSize(400,200);
577 if(inpPers->text().isEmpty()) {
583 if(inpPhr->text().isEmpty()) {
588 inpPers->setEnabled(
false);
589 inpPhr->setEnabled(
false);