Commit e7c55949 authored by Karel Slaný's avatar Karel Slaný
Browse files

Merge branch 'reenter-otp-code' into 'develop'

Reenter OTP code if login error (issue #46)

Allows reenter OTP code if any error was occurred during login process (issue #46).

See merge request !34
parents 2faee0fc 967c1326
......@@ -66,6 +66,7 @@ bool IsdsSession::createIsdsCtx(const QString &userName, QString &errText)
IsdsContext isdsCtx;
bool ok;
isdsCtx.username = userName;
isdsCtx.account_name = acntData.accountName();
isdsCtx.login_method = acntData.loginMethod();
isdsCtx.is_logged = false;
isdsCtx.last_isds_msg = "";
......@@ -92,15 +93,6 @@ bool IsdsSession::createIsdsCtx(const QString &userName, QString &errText)
isdsCtx.last_isds_msg = errText;
return false;
}
} else if (acntData.loginMethod() == USERNAME_PWD_HOTP) {
QString text = PasteInputDialogue::getText(0,
tr("Security code: %1").arg(userName),
tr("Enter security code for account\n'%1'").arg(acntData.accountName()),
QLineEdit::Normal, NULL, &ok, Qt::Dialog,
Qt::ImhDigitsOnly);
if (ok && !text.isEmpty()) {
isdsCtx.pass_phrase = text;
}
} else if (acntData.loginMethod() == USERNAME_PWD_TOTP) {
/* show SMS request dialog */
QMessageBox msgBox;
......
......@@ -39,6 +39,7 @@
struct IsdsContext {
QString login_method; /* login method */
QUrl url; /* URL of the ISDS web service */
QString account_name; /* account name, NOT NULL */
QString username; /* username, NOT NULL */
QString password; /* password, may be NULL */
QNetworkCookie cookie; /* cookie for HOTP or TOTP else empty */
......
......@@ -471,6 +471,23 @@ bool IsdsWrapper::isLoggedToIsds(const QString &userName, QString &errText)
}
/* ========================================================================= */
/*
* 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;
}
/* ========================================================================= */
/*
* Func: Logout and close all active OTP connections, clear cookies.
......@@ -480,12 +497,11 @@ void IsdsWrapper::closeAllOtpConnections(void)
{
qDebug("%s()", __func__);
QString errText;
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) {
if (isLoggedToIsds(usernames.at(i), errText)) {
if (isLoggedToIsdsOtp(usernames.at(i))) {
isdsLogoutOTP(usernames.at(i));
}
}
......@@ -552,7 +568,6 @@ void IsdsWrapper::showErrorMessageBox(const QString &title,
}
/* ========================================================================= */
/*
* Func: Show error dialog when login problem occurs.
......@@ -561,11 +576,13 @@ void IsdsWrapper::showLoginProblemDialog(const QString &userName,
const QString &errTxt)
/* ========================================================================= */
{
showErrorMessageBox(tr("Login problem: %1").arg(userName),
tr("Error while logging in with username '%1'.").arg(userName),
tr("System ISDS returns: ") + errTxt + "\n\n" +
tr("You may have entered a wrong user name or password. Try again."),
true);
if (!errTxt.isEmpty()) {
showErrorMessageBox(tr("Login problem: %1").arg(userName),
tr("Error while logging in with username '%1'.").arg(userName),
tr("System ISDS returns:") + " " + errTxt + "\n\n" +
tr("You may have entered a wrong user name or password. Try again."),
true);
}
}
......@@ -577,10 +594,12 @@ void IsdsWrapper::showFirstLoginProblemDialog(const QString &userName,
const QString &errTxt)
/* ========================================================================= */
{
showErrorMessageBox(tr("Problem adding account: %1").arg(userName),
tr("New account could not be created because an error occurred "
"while trying to log in with user name '%1'.").arg(userName),
tr("ISDS returns:") + " " + errTxt + "\n\n" +
tr("Check your login data and try again."),
true);
if (!errTxt.isEmpty()) {
showErrorMessageBox(tr("Problem adding account: %1").arg(userName),
tr("New account could not be created because an error occurred "
"while trying to log in with user name '%1'.").arg(userName),
tr("ISDS returns:") + " " + errTxt + "\n\n" +
tr("Check your login data and try again."),
true);
}
}
......@@ -84,6 +84,14 @@ public:
*/
bool isLoggedToIsds(const QString &userName, QString &errText);
/*!
* @brief Test if user is connected to databox via otp.
*
* @param[in] userName - account username string.
* @return true if user is connected.
*/
bool isLoggedToIsdsOtp(const QString &userName);
/*!
* @brief Logout OTP connection.
*
......
......@@ -139,8 +139,25 @@ bool NetLayer::createLoginRequest(IsdsContext &ctx,
/* Create authorization string for header of request */
if (ctx.login_method == USERNAME_PWD_HOTP) {
bool ok;
QString code;
do {
code = PasteInputDialogue::getText(0,
tr("Security code: %1").arg(ctx.username),
tr("Enter security code for account\n'%1'").arg(ctx.account_name),
QLineEdit::Normal, NULL, &ok, Qt::Dialog,
Qt::ImhDigitsOnly);
if (!ok) {
ctx.last_isds_msg = "";
return false;
}
} while (ok && code.isEmpty());
ctx.pass_phrase = code;
authorization.append(ctx.username + ":" +
ctx.password + ctx.pass_phrase) ;
ctx.password + ctx.pass_phrase) ;
} else {
authorization.append(ctx.username + ":" + ctx.password);
}
......@@ -240,7 +257,7 @@ bool NetLayer::sendRequest(IsdsContext &ctx, const QNetworkRequest &request,
break;
}
#if PRINT_REQUEST
#if PRINT_REQUEST_STDOUT
printRequest(request, xmlDataIn);
#endif
......@@ -300,7 +317,7 @@ bool NetLayer::getResponse(IsdsContext &ctx, QNetworkReply *reply,
const QString reason = reply->attribute(
QNetworkRequest::HttpReasonPhraseAttribute).toString();
#if PRINT_REQUEST
#if PRINT_RESPONSE_STDOUT
qDebug() << "";
qDebug() << "====================REPLY===========================";
qDebug() << "URL:" << url;
......@@ -341,7 +358,7 @@ bool NetLayer::getResponse(IsdsContext &ctx, QNetworkReply *reply,
ctx.cookie = cookieList.at(0);
}
#if PRINT_REQUEST
#if PRINT_RESPONSE_STDOUT
qDebug() << "--------------------Content------------------------";
qDebug() << xmlDataOut;
qDebug() << "===================================================";
......@@ -503,37 +520,65 @@ bool NetLayer::doTotpLoginRequest(IsdsContext &ctx, const QByteArray &xmlDataIn,
/* ========================================================================= */
{
QNetworkRequest request;
QByteArray authorization;
bool success = false;
/* Fill request header */
fillRequestHeader(request);
request.setUrl(ctx.url);
request.setRawHeader("Host", ctx.url.host().toUtf8());
/* Set authorization header */
/* Calculate content length */
request.setRawHeader("Content-Length",
QString::number(xmlDataIn.length()).toUtf8());
int iteration = 0;
bool ok;
const QString code = PasteInputDialogue::getText(0,
tr("SMS code: %1").arg(ctx.username),
tr("Enter SMS code for user '%1'").arg(ctx.username),
QLineEdit::Normal, NULL, &ok, Qt::Dialog, Qt::ImhDigitsOnly);
if (ok && !code.isEmpty()) {
QString code;
/* Repeat SMS login proccess until isds returns success */
do {
/* Set SMS code by user and set to authorization header */
QByteArray authorization;
iteration++;
do {
if (iteration > 1) {
// SMS input dialog after wrong code
code = PasteInputDialogue::getText(0,
tr("Wrong SMS code: %1").arg(ctx.username),
tr("Enter correct SMS code for account '%1' again.").arg(ctx.account_name),
QLineEdit::Normal, NULL, &ok, Qt::Dialog,
Qt::ImhDigitsOnly);
} else {
// first SMS input dialog
code = PasteInputDialogue::getText(0,
tr("SMS code: %1").arg(ctx.username),
tr("Enter SMS code for account '%1'").arg(ctx.account_name),
QLineEdit::Normal, NULL, &ok, Qt::Dialog,
Qt::ImhDigitsOnly);
}
// if cancel dialog, cancel login action as well
if (!ok) {
ctx.last_isds_msg = "";
return success;
}
} while (ok && code.isEmpty());
/* Set new SMS code to authorization string */
authorization.append(ctx.username + ":" +
ctx.password + code) ;
}
authorization = authorization.toBase64();
authorization.prepend("Basic ");
request.setRawHeader("Authorization", authorization);
/* Calculate content length */
request.setRawHeader("Content-Length",
QString::number(xmlDataIn.length()).toUtf8());
authorization = authorization.toBase64();
authorization.prepend("Basic ");
request.setRawHeader("Authorization", authorization);
/* Send request */
bool success = sendRequest(ctx, request, xmlDataIn, xmlDataOut);
/* Send request, check success if SMS code is valid */
success = sendRequest(ctx, request, xmlDataIn, xmlDataOut);
if (success) {
success = doHotpLoginRequest(ctx, xmlDataIn, xmlDataOut);
}
} while (!success);
/* Do last step of SMS login proccess */
success = doHotpLoginRequest(ctx, xmlDataIn, xmlDataOut);
return success;
}
......
......@@ -32,7 +32,9 @@
#include "src/net/isds_session.h"
/* 1 = POST/GET request will be printed to terminal/console. 0 = Disable */
#define PRINT_REQUEST_STDOUT 1
#define PRINT_REQUEST_STDOUT 0
/* 1 = responce will be printed to terminal/console. 0 = Disable */
#define PRINT_RESPONSE_STDOUT 0
/*
* Class NetLayer realizes POST/GET requests
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment