QCA
eventhandlerdemo.cpp
The code below shows to implement a client side handler for password / passphrase / PIN and token requests from QCA and any associated providers.
The code below shows to implement a client side handler for password / passphrase / PIN and token requests from QCA and any associated providers.
/*
Copyright (C) 2007 Brad Hards <bradh@frogmouth.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// QtCrypto has the declarations for all of QCA
#include <QtCrypto>
#include <QCoreApplication>
#include <iostream>
#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif
/**
We need a class on the client side to handle password requests.
*/
{
public:
{
// When the PasswordAsker or TokenAsker needs to interact
// with the user, it raises a signal. We connect that to a
// local slot to get the required information.
// Now that we are set up, we can start the EventHandler. Nothing
// will happen if you don't call this method.
m_handler.start();
}
// This slot gets called when the provider needs a token inserted,
// or to get a passphrase / password / PIN.
{
// We can sanity check the event
if (event.isNull()) {
return;
}
// Events can be associated with a a keystore or a file/bytearray
// You can tell which by looking at the Source
std::cout << "Event is associated with a key store operation" << std::endl;
std::cout << "Event is associated with a file or some other data" << std::endl;
// if the event comes from a file type operation, you can get the
// name / label using fileName()
std::cout << " Filename: " << qPrintable(event.fileName()) << std::endl;
} else {
std::cout << "Unexpected Source for Event" << std::endl;
}
// There are different kinds of events.
// You would typically ask the user to insert the token here
std::cout << "Request for token" << std::endl;
// we just fake it for this demo.
// you could use m_handler.reject( id ) to refuse the token request
std::cout << "Request for password, passphrase or PIN" << std::endl;
// and within the Password type, we have a few different styles.
std::cout << " [Password request]" << std::endl;
std::cout << " [Passphrase request]" << std::endl;
std::cout << " [PIN request]" << std::endl;
} else {
std::cout << " [unexpect request style]" << std::endl;
}
// You would typically request the password/PIN/passphrase.
// again, we just fake it.
} else {
std::cout << "Unexpected event type" << std::endl;
}
}
private:
QCA::EventHandler m_handler;
};
void asker_procedure();
{
protected:
void run() override
{
asker_procedure();
}
};
int main(int argc, char **argv)
{
// the Initializer object sets things up, and
// also does cleanup when it goes out of scope
QCoreApplication exampleApp(argc, argv);
ClientPassphraseHandler cph;
// handler and asker cannot occur in the same thread
AskerThread askerThread;
askerThread.start();
exampleApp.exec();
return 0;
}
void asker_procedure()
{
QCA::PasswordAsker pwAsker;
pwAsker.waitForResponse();
std::cout << std::endl << "Now do token:" << std::endl;
QCA::TokenAsker tokenAsker;
tokenAsker.ask(
QCA::KeyStoreInfo(QCA::KeyStore::SmartCard, QStringLiteral("Token Id"), QStringLiteral("Token Name")),
nullptr);
tokenAsker.waitForResponse();
std::cout << "Token was accepted" << std::endl;
} else {
std::cout << "Token was not accepted" << std::endl;
}
}
#include "eventhandlerdemo.moc"
Interface class for password / passphrase / PIN and token handlers.
Definition qca_core.h:1579
void submitPassword(int id, const SecureArray &password)
function to call to return the user provided password, passphrase or PIN.
void eventReady(int id, const QCA::Event &context)
signal emitted when an Event requires attention.
void tokenOkay(int id)
function to call to indicate that the token has been inserted by the user.
void start()
mandatory function to call after connecting the signal to a slot in your application specific passwor...
void waitForResponse()
Block until the password / passphrase request is completed.
void ask(Event::PasswordStyle pstyle, const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr)
queue a password / passphrase request associated with a key store
SecureArray password() const
The password / passphrase / PIN provided by the user in response to the asker request.
QByteArray toByteArray() const
Copy the contents of the secure array out to a standard QByteArray.
void ask(const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr)
queue a token request associated with a key store
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
char * data()
void quit()
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
QObject * parent() const const
void finished()
void start(Priority priority)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:46:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:46:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.