QCA

eventhandlerdemo.cpp
1/*
2 Copyright (C) 2007 Brad Hards <bradh@frogmouth.net>
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20*/
21
22// QtCrypto has the declarations for all of QCA
23#include <QtCrypto>
24
25#include <QCoreApplication>
26
27#include <iostream>
28
29#ifdef QT_STATICPLUGIN
30#include "import_plugins.h"
31#endif
32
33/**
34 We need a class on the client side to handle password requests.
35*/
36class ClientPassphraseHandler : public QObject
37{
39public:
40 ClientPassphraseHandler(QObject *parent = nullptr)
42 {
43 // When the PasswordAsker or TokenAsker needs to interact
44 // with the user, it raises a signal. We connect that to a
45 // local slot to get the required information.
46 connect(&m_handler, &QCA::EventHandler::eventReady, this, &ClientPassphraseHandler::my_eventReady);
47
48 // Now that we are set up, we can start the EventHandler. Nothing
49 // will happen if you don't call this method.
50 m_handler.start();
51 }
52
53private Q_SLOTS:
54 // This slot gets called when the provider needs a token inserted,
55 // or to get a passphrase / password / PIN.
56 void my_eventReady(int id, const QCA::Event &event)
57 {
58 // We can sanity check the event
59 if (event.isNull()) {
60 return;
61 }
62
63 // Events can be associated with a a keystore or a file/bytearray
64 // You can tell which by looking at the Source
65 if (event.source() == QCA::Event::KeyStore) {
66 std::cout << "Event is associated with a key store operation" << std::endl;
67 } else if (event.source() == QCA::Event::Data) {
68 std::cout << "Event is associated with a file or some other data" << std::endl;
69 // if the event comes from a file type operation, you can get the
70 // name / label using fileName()
71 std::cout << " Filename: " << qPrintable(event.fileName()) << std::endl;
72 } else {
73 std::cout << "Unexpected Source for Event" << std::endl;
74 }
75
76 // There are different kinds of events.
77 if (event.type() == QCA::Event::Token) {
78 // You would typically ask the user to insert the token here
79 std::cout << "Request for token" << std::endl;
80 // we just fake it for this demo.
81 m_handler.tokenOkay(id);
82 // you could use m_handler.reject( id ) to refuse the token request
83
84 } else if (event.type() == QCA::Event::Password) {
85 std::cout << "Request for password, passphrase or PIN" << std::endl;
86 // and within the Password type, we have a few different styles.
87 if (event.passwordStyle() == QCA::Event::StylePassword) {
88 std::cout << " [Password request]" << std::endl;
89 } else if (event.passwordStyle() == QCA::Event::StylePassphrase) {
90 std::cout << " [Passphrase request]" << std::endl;
91 } else if (event.passwordStyle() == QCA::Event::StylePIN) {
92 std::cout << " [PIN request]" << std::endl;
93 } else {
94 std::cout << " [unexpect request style]" << std::endl;
95 }
96 // You would typically request the password/PIN/passphrase.
97 // again, we just fake it.
98 m_handler.submitPassword(id, QCA::SecureArray("hello"));
99
100 } else {
101 std::cout << "Unexpected event type" << std::endl;
102 }
103 }
104
105private:
106 QCA::EventHandler m_handler;
107};
108
109void asker_procedure();
110
111class AskerThread : public QThread
112{
114protected:
115 void run() override
116 {
117 asker_procedure();
118 }
119};
120
121int main(int argc, char **argv)
122{
123 // the Initializer object sets things up, and
124 // also does cleanup when it goes out of scope
126
127 QCoreApplication exampleApp(argc, argv);
128
129 ClientPassphraseHandler cph;
130
131 // handler and asker cannot occur in the same thread
132 AskerThread askerThread;
134 askerThread.start();
135
136 exampleApp.exec();
137 return 0;
138}
139
140void asker_procedure()
141{
142 QCA::PasswordAsker pwAsker;
143
144 pwAsker.ask(QCA::Event::StylePassword, QStringLiteral("foo.tmp"), nullptr);
145
146 pwAsker.waitForResponse();
147
148 std::cout << "Password was: " << pwAsker.password().toByteArray().data() << std::endl;
149
150 std::cout << std::endl << "Now do token:" << std::endl;
151
152 QCA::TokenAsker tokenAsker;
153
154 tokenAsker.ask(
155 QCA::KeyStoreInfo(QCA::KeyStore::SmartCard, QStringLiteral("Token Id"), QStringLiteral("Token Name")),
157 nullptr);
158
159 tokenAsker.waitForResponse();
160
161 if (tokenAsker.accepted()) {
162 std::cout << "Token was accepted" << std::endl;
163 } else {
164 std::cout << "Token was not accepted" << std::endl;
165 }
166}
167
168#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...
An asynchronous event.
Definition qca_core.h:1391
@ Data
File or bytearray generated the event.
Definition qca_core.h:1419
@ KeyStore
KeyStore generated the event.
Definition qca_core.h:1418
@ Password
Asking for a password, PIN or passphrase.
Definition qca_core.h:1400
@ Token
Asking for a token.
Definition qca_core.h:1401
@ StylePIN
User should be prompted for a "PIN".
Definition qca_core.h:1434
@ StylePassphrase
User should be prompted for a "Passphrase".
Definition qca_core.h:1433
@ StylePassword
User should be prompted for a "Password".
Definition qca_core.h:1432
Convenience method for initialising and cleaning up QCA.
Definition qca_core.h:660
Single entry in a KeyStore.
Key store information, outside of a KeyStore object.
@ SmartCard
for smartcards
User password / passphrase / PIN handler.
Definition qca_core.h:1661
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.
Secure array of bytes.
Definition qca_tools.h:317
QByteArray toByteArray() const
Copy the contents of the secure array out to a standard QByteArray.
User token handler.
Definition qca_core.h:1754
void waitForResponse()
Block until the token request is completed.
bool accepted() const
Test if the token request was accepted or not.
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()
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 Tue Mar 26 2024 11:18:26 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.