AusweisApp2/src/cli/UIPlugInCli.cpp

235 lines
5.0 KiB
C++
Raw Normal View History

2017-07-03 09:30:10 +02:00
/*!
2018-03-28 15:10:51 +02:00
* \copyright Copyright (c) 2015-2018 Governikus GmbH & Co. KG, Germany
2017-07-03 09:30:10 +02:00
*/
#include "UIPlugInCli.h"
2017-07-03 09:33:28 +02:00
#include "states/StateEstablishPacePin.h"
2017-07-03 09:30:10 +02:00
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
2017-12-20 14:54:05 +01:00
#include "Env.h"
2017-07-03 09:33:28 +02:00
#include "HttpServer.h"
2017-07-03 09:30:10 +02:00
#endif
#include <QLoggingCategory>
#include <QRegularExpression>
Q_DECLARE_LOGGING_CATEGORY(stdinput)
Q_DECLARE_LOGGING_CATEGORY(cli)
using namespace governikus;
UIPlugInCli::UIPlugInCli()
: mReader()
, mAvailableCommands()
{
2017-07-03 09:33:28 +02:00
addCommand(QStringLiteral("cancel"), &UIPlugInCli::handleCancelWorkflow);
2017-07-03 09:30:10 +02:00
addCommand(QStringLiteral("changepin"), &UIPlugInCli::handleChangePin);
addCommand(QStringLiteral("enter-pin"), &UIPlugInCli::handleEnterPin);
addCommand(QStringLiteral("help"), &UIPlugInCli::handleHelp);
addCommand(QStringLiteral("ping"), &UIPlugInCli::handlePing);
addCommand(QStringLiteral("port"), &UIPlugInCli::handlePort);
addCommand(QStringLiteral("quit"), &UIPlugInCli::handleQuit);
connect(&mReader, &ConsoleReader::fireText, this, &UIPlugInCli::doInput);
mReader.init();
}
UIPlugInCli::~UIPlugInCli()
{
}
void UIPlugInCli::onApplicationStarted()
{
qCInfo(cli) << "ready";
}
void UIPlugInCli::doShutdown()
{
mReader.shutdown();
}
void UIPlugInCli::onWorkflowStarted(QSharedPointer<WorkflowContext> pContext)
{
mContext = pContext;
if (!mContext.isNull())
{
2017-12-20 14:54:05 +01:00
connect(mContext.data(), &WorkflowContext::fireStateChanged,
this, &UIPlugInCli::onStateChanged);
mContext->setReaderPlugInTypes({ReaderManagerPlugInType::PCSC});
2017-07-03 09:30:10 +02:00
}
}
void UIPlugInCli::onWorkflowFinished(QSharedPointer<WorkflowContext> pContext)
{
Q_UNUSED(pContext);
if (!mContext.isNull())
{
mContext->disconnect(this);
mContext.reset();
}
}
2017-12-20 14:54:05 +01:00
void UIPlugInCli::onStateChanged(const QString& pState)
2017-07-03 09:30:10 +02:00
{
Q_UNUSED(pState);
bool userInteractionRequired = false;
2017-07-03 09:33:28 +02:00
if (AbstractState::isState<StateEstablishPacePin>(pState))
2017-07-03 09:30:10 +02:00
{
userInteractionRequired = true;
qCInfo(cli) << "enter-pin";
}
mContext->setStateApproved(!userInteractionRequired);
}
void UIPlugInCli::doInput(const QString& pData)
{
qCInfo(stdinput) << pData;
2017-12-20 14:54:05 +01:00
const QStringList chunks = pData.split(QLatin1Char(' '));
2017-07-03 09:30:10 +02:00
auto func = mAvailableCommands.value(chunks.at(0).toLower());
if (func)
{
mCurrentCommandArgs = chunks.mid(1);
func();
}
else
{
qCWarning(cli) << "Unknown command:" << pData;
}
}
void UIPlugInCli::handleChangePin()
{
2017-07-03 09:33:28 +02:00
mOldPin.clear();
mNewPin.clear();
2017-07-03 09:30:10 +02:00
qCDebug(cli) << "Change PIN requested";
2017-07-03 09:33:28 +02:00
qCInfo(cli) << "Please enter old PIN";
disconnect(&mReader, &ConsoleReader::fireText, this, &UIPlugInCli::doInput);
connect(&mReader, &ConsoleReader::fireText, this, &UIPlugInCli::handleOldPinEntered);
}
2017-07-03 09:30:10 +02:00
2017-07-03 09:33:28 +02:00
void UIPlugInCli::handleOldPinEntered(const QString& pLine)
{
2017-07-03 09:30:10 +02:00
const QRegularExpression regexOldPin(QStringLiteral("^[0-9]{5,6}$"));
2017-07-03 09:33:28 +02:00
if (regexOldPin.match(pLine).hasMatch() && mReader.isInputOpen())
{
mOldPin = pLine;
qCInfo(cli) << "Please enter new PIN";
disconnect(&mReader, &ConsoleReader::fireText, this, &UIPlugInCli::handleOldPinEntered);
connect(&mReader, &ConsoleReader::fireText, this, &UIPlugInCli::handleNewPinEntered);
}
else
2017-07-03 09:30:10 +02:00
{
2017-07-03 09:33:28 +02:00
qCInfo(cli) << "Please enter old PIN";
2017-07-03 09:30:10 +02:00
}
2017-07-03 09:33:28 +02:00
}
2017-07-03 09:30:10 +02:00
2017-07-03 09:33:28 +02:00
void UIPlugInCli::handleNewPinEntered(const QString& pLine)
{
2017-07-03 09:30:10 +02:00
const QRegularExpression regexNewPin(QStringLiteral("^[0-9]{6}$"));
2017-07-03 09:33:28 +02:00
if (regexNewPin.match(pLine).hasMatch() && mReader.isInputOpen())
{
mNewPin = pLine;
qCInfo(cli) << "Please enter new PIN again";
disconnect(&mReader, &ConsoleReader::fireText, this, &UIPlugInCli::handleNewPinEntered);
connect(&mReader, &ConsoleReader::fireText, this, &UIPlugInCli::handleNewPinEnteredAgain);
}
else
2017-07-03 09:30:10 +02:00
{
2017-07-03 09:33:28 +02:00
qCInfo(cli) << "Please enter new PIN";
2017-07-03 09:30:10 +02:00
}
2017-07-03 09:33:28 +02:00
}
2017-07-03 09:30:10 +02:00
2017-07-03 09:33:28 +02:00
void UIPlugInCli::handleNewPinEnteredAgain(const QString& pLine)
{
if (mNewPin != pLine)
{
qCInfo(cli) << "PINs were not equal";
qCInfo(cli) << "Please enter new PIN";
disconnect(&mReader, &ConsoleReader::fireText, this, &UIPlugInCli::handleNewPinEnteredAgain);
connect(&mReader, &ConsoleReader::fireText, this, &UIPlugInCli::handleNewPinEntered);
}
else if (mReader.isInputOpen())
2017-07-03 09:30:10 +02:00
{
qCDebug(cli) << "Start";
Q_EMIT fireChangePinRequest();
2017-07-03 09:33:28 +02:00
connect(&mReader, &ConsoleReader::fireText, this, &UIPlugInCli::doInput);
}
}
void UIPlugInCli::handleCancelWorkflow()
{
if (mContext.isNull())
{
qCInfo(cli) << "error";
}
else
{
Q_EMIT mContext->fireCancelWorkflow();
qCInfo(cli) << "ok";
2017-07-03 09:30:10 +02:00
}
}
void UIPlugInCli::handleEnterPin()
{
if (mCurrentCommandArgs.size() != 1 || mContext.isNull())
{
qCInfo(cli) << "error";
}
else
{
mContext->setPin(mCurrentCommandArgs.at(0));
mContext->setStateApproved(true);
qCInfo(cli) << "ok";
}
}
void UIPlugInCli::handleHelp()
{
2017-07-03 09:33:28 +02:00
qCInfo(cli) << "Available commands:" << mAvailableCommands.keys().join(QStringLiteral(" | "));
2017-07-03 09:30:10 +02:00
}
void UIPlugInCli::handlePing()
{
qCInfo(cli) << "Pong!";
}
void UIPlugInCli::handlePort()
{
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
2017-12-20 14:54:05 +01:00
qCInfo(cli) << "Port:" << Env::getShared<HttpServer>()->getServerPort();
2017-07-03 09:30:10 +02:00
#else
qCInfo(cli) << "Port is undefined";
#endif
}
void UIPlugInCli::handleQuit()
{
qCInfo(cli) << "Shutdown application...";
Q_EMIT fireQuitApplicationRequest();
}