isds_wrapper.cpp 17.5 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2017 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

#include <QDebug>

26
#include "src/dialogues/dialogues.h"
27 28 29 30
#include "src/net/isds_wrapper.h"
#include "src/models/accountmodel.h"
#include "src/settings.h"

31 32
IsdsWrapper::IsdsWrapper(QObject *parent)
    : QObject(parent)
33 34 35
{
}

36
IsdsWrapper::~IsdsWrapper(void)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
{
}

/* ========================================================================= */
/*
 * Slot: Download complete message.
 */
void IsdsWrapper::downloadMessage(const QString &userName, int mesasgeType,
    qint64 msgId)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	QString errTxt = tr("Wrong username or message ID");
	if (userName.isEmpty() || msgId <= 0) {
52
		Dialogues::errorMessage(Dialogues::CRITICAL,
53
		    tr("Error"), errTxt, tr("Internal error"));
54 55 56 57
		return;
	}

	emit statusBarTextChanged(tr("Downloading message %1").
58
	    arg(QString::number(msgId)), true, true);
59 60 61 62 63 64

	if (!isLoggedToIsds(userName, errTxt)) {
		showLoginProblemDialog(userName, errTxt);
		return;
	}

65 66
	if (mesasgeType == MessageDb::TYPE_RECEIVED) {
		if (!m_xmlLayer.isdsGetCompleteMsgWithoutCms(
67
		     m_isdsSession.isdsCtxMap[userName], msgId)) {
68
			Dialogues::errorMessage(Dialogues::CRITICAL,
69
			    tr("Delivery info: %1").arg(userName),
70
			    tr("Failed to download complete received message %1.").arg(msgId),
71
			    m_isdsSession.isdsCtxMap[userName].last_isds_msg);
72 73 74
			return;
		}
	} else {
75
		if (!m_xmlLayer.isdsGetCompleteSentMsgWithCms(
76
		     m_isdsSession.isdsCtxMap[userName], msgId)) {
77
			Dialogues::errorMessage(Dialogues::CRITICAL,
78 79
			    tr("Downloading message: %1").arg(userName),
			    tr("Failed to download complete sent message %1.").arg(msgId),
80
			    m_isdsSession.isdsCtxMap[userName].last_isds_msg);
81 82 83 84 85 86 87 88 89 90 91
			return;
		}
	}

	m_xmlLayer.isdsGetMsgAuthorInfo(m_isdsSession.isdsCtxMap[userName], msgId);
	m_xmlLayer.isdsMarkMsgAsDownloaded(m_isdsSession.isdsCtxMap[userName],
	    msgId);
	m_xmlLayer.isdsGetMsgDeliveryInfoWithoutCms(
	    m_isdsSession.isdsCtxMap[userName], msgId);

	emit statusBarTextChanged(tr("Message %1 has been downloaded").arg(msgId),
92
	    false, true);
93 94 95 96 97 98 99 100 101 102 103 104 105 106
}


/* ========================================================================= */
/*
 * Slot: Download delivery info.
 */
void IsdsWrapper::getDeliveryInfo(const QString &userName, qint64 msgId)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	QString errTxt =  tr("Wrong username or message ID");
	if (userName.isEmpty() || msgId <= 0) {
107
		Dialogues::errorMessage(Dialogues::CRITICAL,
108
		    tr("Error"), errTxt, tr("Internal error"));
109 110 111 112
		return;
	}

	emit statusBarTextChanged(tr("Downloading delivery info %1").
113
	    arg(QString::number(msgId)), true, true);
114 115 116 117 118 119 120 121

	if (!isLoggedToIsds(userName, errTxt)) {
		showLoginProblemDialog(userName, errTxt);
		return;
	}

	if (!m_xmlLayer.isdsGetMsgDeliveryInfoWithoutCms(
	    m_isdsSession.isdsCtxMap[userName], msgId)) {
122
		Dialogues::errorMessage(Dialogues::WARNING,
123
		    tr("Delivery info: %1").arg(userName),
124
		    tr("Failed to download delivery info for message %1.").arg(msgId),
125
		    m_isdsSession.isdsCtxMap[userName].last_isds_msg);
126 127
	} else {
		emit statusBarTextChanged(
128 129
		    m_isdsSession.isdsCtxMap[userName].last_isds_msg, false,
		    true);
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
	}
}


/* ========================================================================= */
/*
 * Slot: Synchronize all accounts.
 */
void IsdsWrapper::syncAllAccounts(void)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	QList<QString> userNameList = AccountListModel::globAccounts.keys();
	foreach (const QString userName, userNameList) {
		syncOneAccount(userName);
	}
}


/* ========================================================================= */
/*
 * Slot: Synchronize one account.
 */
void IsdsWrapper::syncOneAccount(const QString &userName)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

159
	if (syncSingleAccountSent(userName)) {
160
		/* Download received only when sent successfully downloaded. */
161 162 163 164
		syncSingleAccountReceived(userName);
	}
}

165 166 167 168 169

/* ========================================================================= */
/*
 * Slot: Synchronize recevied messages.
 */
170
bool IsdsWrapper::syncSingleAccountReceived(const QString &userName)
171
/* ========================================================================= */
172
{
173
	if (userName.isEmpty()) {
174
		Dialogues::errorMessage(Dialogues::CRITICAL,
175
		    tr("Error"), tr("Empty user name"), tr("Internal error"));
176
		return false;
177 178
	}

179
	emit statusBarTextChanged(tr("%1: downloading received").arg(userName),
180
	    true, true);
181

182 183 184 185
	QString errText;
	if (!isLoggedToIsds(userName, errText)) {
		showLoginProblemDialog(userName, errText);
		return false;
186 187 188 189
	}

	QList<qint64> receivedMsgIds;

190
	/* I don't like the following function. */
191 192 193
	if (!m_xmlLayer.isdsGetReceivedMessageList(
	    m_isdsSession.isdsCtxMap[userName],
	    globSet.downloadOnlyNewMsgs, receivedMsgIds)) {
194
		Dialogues::errorMessage(Dialogues::CRITICAL,
195 196 197
		    tr("%1: received messages").arg(userName),
		    tr("Failed to download list of received messages for user name '%1'.").arg(userName),
		    m_isdsSession.isdsCtxMap[userName].last_isds_msg);
198 199
	} else {
		emit statusBarTextChanged(
200 201
		    m_isdsSession.isdsCtxMap[userName].last_isds_msg,
		    false, true);
202 203
	}

204
	globSet.setLastUpdateToNow();
205 206 207 208
	saveAllSettings();

	if (globSet.downloadCompleteMsgs) {
		foreach (qint64 msgId, receivedMsgIds) {
209 210
			downloadMessage(userName, MessageDb::TYPE_RECEIVED,
			    msgId);
211
		}
212 213 214 215 216
	}

	return true;
}

217 218 219 220 221

/* ========================================================================= */
/*
 * Slot: Synchronize sent messages.
 */
222
bool IsdsWrapper::syncSingleAccountSent(const QString &userName)
223
/* ========================================================================= */
224 225
{
	if (userName.isEmpty()) {
226
		Dialogues::errorMessage(Dialogues::CRITICAL,
227
		    tr("Error"), tr("Empty user name"), tr("Internal error"));
228 229 230 231
		return false;
	}

	emit statusBarTextChanged(tr("%1: downloading sent").arg(userName),
232
	    true, true);
233 234 235 236 237 238 239 240 241 242 243 244

	QString errText;
	if (!isLoggedToIsds(userName, errText)) {
		showLoginProblemDialog(userName, errText);
		return false;
	}

	QList<qint64> sentMsgIds;

	if (!m_xmlLayer.isdsGetSentMessageList(
	    m_isdsSession.isdsCtxMap[userName],
	    globSet.downloadOnlyNewMsgs, sentMsgIds)) {
245
		Dialogues::errorMessage(Dialogues::CRITICAL,
246 247 248
		    tr("%1: sent messages").arg(userName),
		    tr("Failed to download list of sent messages for user name %1.").arg(userName),
		    m_isdsSession.isdsCtxMap[userName].last_isds_msg);
249 250
	} else {
		emit statusBarTextChanged(
251 252
		    m_isdsSession.isdsCtxMap[userName].last_isds_msg,
		    false, true);
253 254 255
	}

	if (globSet.downloadCompleteMsgs) {
256
		foreach (qint64 msgId, sentMsgIds) {
257
			downloadMessage(userName, MessageDb::TYPE_SENT, msgId);
258 259 260
		}
	}

261 262
	return true;
}
263 264 265 266 267

/* ========================================================================= */
/*
 * Slot: Get account info from ISDS.
 */
268
bool IsdsWrapper::getAccountInfo(const QString &userName, bool firstLogin)
269 270 271 272 273 274 275
/* ========================================================================= */
{
	qDebug("%s()", __func__);
	bool success = true;

	QString errTxt =  tr("Wrong username");
	if (userName.isEmpty()) {
276
		Dialogues::errorMessage(Dialogues::CRITICAL,
277
		    tr("Error"), errTxt, tr("Internal error"));
278
		return false;
279 280 281
	}

	emit statusBarTextChanged(tr("Getting account info %1").
282
	    arg(userName), true, true);
283 284

	if (!isLoggedToIsds(userName, errTxt)) {
285 286 287 288 289 290
		if (firstLogin) {
			showFirstLoginProblemDialog(userName, errTxt);
		} else {
			showLoginProblemDialog(userName, errTxt);
		}
		return false;
291 292 293 294 295 296
	}

	success = success &&
	    m_xmlLayer.isdsGetOwnerInfoFromLogin(m_isdsSession.isdsCtxMap[userName]);

	emit statusBarTextChanged(tr("Getting user info %1").
297
	    arg(userName), true, true);
298 299 300 301 302

	success = success &&
	    m_xmlLayer.isdsGetUserInfoFromLogin(m_isdsSession.isdsCtxMap[userName]);

	emit statusBarTextChanged(tr("Getting password info %1").
303
	    arg(userName), true, true);
304 305 306 307 308

	success = success &&
	    m_xmlLayer.isdsGetPasswordExpiration(m_isdsSession.isdsCtxMap[userName]);

	if (!success) {
309
		Dialogues::errorMessage(Dialogues::CRITICAL,
310
		    tr("Account info: %1").arg(userName),
311
		    tr("Failed to get account info for username %1.").arg(userName),
312
		    m_isdsSession.isdsCtxMap[userName].last_isds_msg);
313 314
	} else {
		emit statusBarTextChanged(
315 316
		    m_isdsSession.isdsCtxMap[userName].last_isds_msg,
		    false, true);
317
	}
318 319

	return true;
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
}


/* ========================================================================= */
/*
 * Slot: Get account info from ISDS.
 */
void IsdsWrapper::changePassword(const QString &userName,
    const QString &accountName)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	QString errTxt = tr("Wrong username");

	if (userName.isEmpty()) {
336
		Dialogues::errorMessage(Dialogues::CRITICAL,
337
		    tr("Error"), errTxt, tr("Internal error"));
338 339 340 341 342 343 344 345 346
		return;
	}

	if (!isLoggedToIsds(userName, errTxt)) {
		showLoginProblemDialog(userName, errTxt);
		return;
	}

	bool ok;
347
	QString newPwd = Dialogues::getText(Q_NULLPTR,
348
	    tr("New password: %1").arg(userName),
349 350
	    tr("Enter new password for account\n'%1'").arg(accountName),
	    QmlDlgHelper::EM_NORMAL, QString(), tr("Enter password"), &ok);
351 352 353 354
	if (!ok) {
		return;
	}
	if (!isCorrectPassword(newPwd)) {
355
		Dialogues::errorMessage(Dialogues::CRITICAL,
356
		    tr("Change password: %1").arg(userName),
357
		    tr("Wrong password format for username '%1'.").arg(userName),
358 359
		    tr("The new password must contain at least 8 characters including "
		        "at least 1 number and at least 1 upper-case letter."));
360 361 362 363
		return;
	}

	if (m_isdsSession.isdsCtxMap[userName].login_method == USERNAME_PWD_TOTP) {
364
		/* Show SMS request dialogue. */
365 366
		int msgResponse = Dialogues::message(Dialogues::QUESTION,
		    tr("SMS code: %1").arg(userName),
367 368
		    tr("Account '%1' requires authentication with SMS code.").arg(accountName),
		    tr("Do you want to send SMS code now?"),
369 370
		    Dialogues::NO | Dialogues::YES, Dialogues::YES);
		if (msgResponse == Dialogues::NO) {
371 372 373 374
			return;
		}

		if (!m_xmlLayer.isdsSendSMS(m_isdsSession.isdsCtxMap[userName])) {
375
			Dialogues::errorMessage(Dialogues::CRITICAL,
376
			    tr("Change password: %1").arg(userName),
377
			    tr("Failed to send SMS code for username '%1'.").arg(userName),
378
			    m_isdsSession.isdsCtxMap[userName].last_isds_msg);
379 380 381
			return;
		}

382
		QString text = Dialogues::getText(Q_NULLPTR,
383
		    tr("SMS code: %1").arg(userName),
384 385 386
		    tr("SMS code for '%1' required").arg(accountName),
		    QmlDlgHelper::EM_NORMAL, QString(), tr("Enter SMS code"),
		    &ok, Qt::ImhDigitsOnly);
387 388 389 390 391 392 393
		if (ok && !text.isEmpty()) {
			m_isdsSession.isdsCtxMap[userName].pass_phrase = text;
		} else {
			return;
		}

	} else if (m_isdsSession.isdsCtxMap[userName].login_method == USERNAME_PWD_HOTP) {
394
		QString text = Dialogues::getText(Q_NULLPTR,
395
		    tr("Security code: %1").arg(userName),
396 397 398
		    tr("Security code for '%1' required").arg(accountName),
		    QmlDlgHelper::EM_NORMAL, QString(),
		    tr("Enter security code"), &ok, Qt::ImhDigitsOnly);
399 400 401 402 403 404 405 406
		if (ok && !text.isEmpty()) {
			m_isdsSession.isdsCtxMap[userName].pass_phrase = text;
		} else {
			return;
		}
	}

	emit statusBarTextChanged(tr("Password changing: %1").arg(userName),
407
	    true, true);
408 409 410

	if (!m_xmlLayer.isdsChangePassword(m_isdsSession.isdsCtxMap[userName],
	    newPwd)) {
411
		Dialogues::errorMessage(Dialogues::CRITICAL,
412
		    tr("Change password: %1").arg(userName),
413
		    tr("Failed to change password for username '%1'.").arg(userName),
414
		    m_isdsSession.isdsCtxMap[userName].last_isds_msg);
415 416
	} else {
		emit statusBarTextChanged(
417 418
		    m_isdsSession.isdsCtxMap[userName].last_isds_msg,
		    false, true);
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

		m_isdsSession.removeIsdsCtx(userName);

		AcntData &acntData(AccountListModel::globAccounts[userName]);
		acntData.setPassword(newPwd);
		saveAllSettings();
	}
}


/* ========================================================================= */
/*
 * Func: Check if new password is valid for ISDS.
 */
bool IsdsWrapper::isCorrectPassword(const QString &password)
/* ========================================================================= */
{
	if (password.isEmpty()) {
		return false;
	}
	if (password.length() < 8 || password.length() > 32) {
		return false;
	}
	if (!password.contains(QRegExp(".*[a-z]+.*"))) {
		return false;
	}
	if (!password.contains(QRegExp(".*[A-Z]+.*"))) {
		return false;
	}
	if (!password.contains(QRegExp(".*[0-9]+.*"))) {
		return false;
	}

	return true;
}


/* ========================================================================= */
/*
 * Func: Test if user is connected to databox.
 */
bool IsdsWrapper::isLoggedToIsds(const QString &userName, QString &errText)
/* ========================================================================= */
{
	bool ret = false;

	if (m_isdsSession.isdsCtxMap.contains(userName)) {
		if (m_isdsSession.isdsCtxMap[userName].is_logged) {
			ret = true;
		} else {
			ret = m_xmlLayer.isdsConnectToDatabox(m_isdsSession.isdsCtxMap[userName]);
			errText = m_isdsSession.isdsCtxMap[userName].last_isds_msg;
		}
	} else {
		if (m_isdsSession.createIsdsCtx(userName, errText)) {
			ret = m_xmlLayer.isdsConnectToDatabox(m_isdsSession.isdsCtxMap[userName]);
			errText = m_isdsSession.isdsCtxMap[userName].last_isds_msg;
		}
	}

	return ret;
}


483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
/* ========================================================================= */
/*
 * Func: Test if user is connected to databox with otp.
 */
bool IsdsWrapper::isLoggedToIsdsOtp(const QString &userName)
/* ========================================================================= */
{
	bool ret = false;

	if (m_isdsSession.isdsCtxMap.contains(userName)) {
		return m_isdsSession.isdsCtxMap[userName].is_logged;
	}

	return ret;
}


500 501 502 503 504 505 506 507 508 509 510 511 512
/* ========================================================================= */
/*
 * Func: Logout and close all active OTP connections, clear cookies.
 */
void IsdsWrapper::closeAllOtpConnections(void)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	QList<QString> usernames = m_isdsSession.isdsCtxMap.keys();
	for (int i = 0; i < usernames.count(); ++i) {
		if (m_isdsSession.isdsCtxMap[usernames.at(i)].login_method == USERNAME_PWD_HOTP ||
		    m_isdsSession.isdsCtxMap[usernames.at(i)].login_method == USERNAME_PWD_TOTP) {
513
			if (isLoggedToIsdsOtp(usernames.at(i))) {
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
				isdsLogoutOTP(usernames.at(i));
			}
		}
	}
}


/* ========================================================================= */
/*
 * Func: Logout and close OTP connection, remove isds context.
 */
bool IsdsWrapper::isdsLogoutOTP(const QString &userName)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	QString errText;
	if (!isLoggedToIsds(userName, errText)) {
		return false;
	}

	if (m_xmlLayer.isdsLogoutOtp(m_isdsSession.isdsCtxMap[userName])) {
		return m_isdsSession.removeIsdsCtx(userName);
	}

	return true;
}


/* ========================================================================= */
/*
 * Slot: Remove isds context.
 */
void IsdsWrapper::removeIsdsCtx(const QString &userName)
/* ========================================================================= */
{
	qDebug("%s()", __func__);

	m_isdsSession.removeIsdsCtx(userName);
}


/* ========================================================================= */
/*
 * Func: Show error dialog when login problem occurs.
 */
void IsdsWrapper::showLoginProblemDialog(const QString &userName,
    const QString &errTxt)
/* ========================================================================= */
{
564 565
	emit statusBarTextChanged("", false, false);

566
	if (!errTxt.isEmpty()) {
567
		Dialogues::errorMessage(Dialogues::CRITICAL,
568
		    tr("Login problem: %1").arg(userName),
569 570
		    tr("Error while logging in with username '%1'.").arg(userName),
		    tr("System ISDS returns:") + " " + errTxt + "\n\n" +
571
		    tr("You may have entered a wrong user name or password. Try again."));
572
	}
573
}
574 575 576 577 578 579 580 581 582 583


/* ========================================================================= */
/*
 * Func: Show error dialog when login problem occurs during new account creation.
 */
void IsdsWrapper::showFirstLoginProblemDialog(const QString &userName,
    const QString &errTxt)
/* ========================================================================= */
{
584 585
	emit statusBarTextChanged("", false, false);

586
	if (!errTxt.isEmpty()) {
587
		Dialogues::errorMessage(Dialogues::CRITICAL,
588
		    tr("Problem adding account: %1").arg(userName),
589
		    tr("New account could not be created because an error occurred "
590
		        "while trying to log in with user name '%1'.").arg(userName),
591
		    tr("ISDS returns:") + " " + errTxt + "\n\n" +
592
		    tr("Check your login data and try again."));
593
	}
594
}