QCA

eventhandlerdemo.cpp
1 /*
2  Copyright (C) 2007 Brad Hards <[email protected]>
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 */
36 class ClientPassphraseHandler : public QObject
37 {
38  Q_OBJECT
39 public:
40  ClientPassphraseHandler(QObject *parent = nullptr)
41  : QObject(parent)
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 
53 private 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.
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 
105 private:
106  QCA::EventHandler m_handler;
107 };
108 
109 void asker_procedure();
110 
111 class AskerThread : public QThread
112 {
113  Q_OBJECT
114 protected:
115  void run() override
116  {
117  asker_procedure();
118  }
119 };
120 
121 int 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;
133  QObject::connect(&askerThread, &AskerThread::finished, &exampleApp, &QCoreApplication::quit);
134  askerThread.start();
135 
136  exampleApp.exec();
137  return 0;
138 }
139 
140 void 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"
An asynchronous event.
Definition: qca_core.h:1390
Type type() const
the Type of this event
SecureArray password() const
The password / passphrase / PIN provided by the user in response to the asker request.
bool isNull() const
test if this event has been setup correctly
User password / passphrase / PIN handler.
Definition: qca_core.h:1660
Key store information, outside of a KeyStore object.
Definition: qca_keystore.h:623
Interface class for password / passphrase / PIN and token handlers.
Definition: qca_core.h:1578
Asking for a password, PIN or passphrase.
Definition: qca_core.h:1400
User should be prompted for a "PIN".
Definition: qca_core.h:1434
PasswordStyle passwordStyle() const
the style of password required.
void waitForResponse()
Block until the token request is completed.
QByteArray toByteArray() const
Copy the contents of the secure array out to a standard QByteArray.
Q_OBJECTQ_OBJECT
User should be prompted for a "Passphrase".
Definition: qca_core.h:1433
void ask(Event::PasswordStyle pstyle, const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr)
queue a password / passphrase request associated with a key store
QString fileName() const
Name or other identifier for the file or byte array associated with this event.
File or bytearray generated the event.
Definition: qca_core.h:1419
KeyStore generated the event.
Definition: qca_core.h:1418
void ask(const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr)
queue a token request associated with a key store
Single entry in a KeyStore.
Definition: qca_keystore.h:140
bool accepted() const
Test if the token request was accepted or not.
QCA_EXPORT void init()
Initialise QCA.
Asking for a token.
Definition: qca_core.h:1401
Secure array of bytes.
Definition: qca_tools.h:316
void eventReady(int id, const QCA::Event &context)
signal emitted when an Event requires attention.
KIOWIDGETS_EXPORT bool run(const QUrl &_url, bool _is_local)
User should be prompted for a "Password".
Definition: qca_core.h:1432
Source source() const
the Source of this event
char * data()
User token handler.
Definition: qca_core.h:1753
Convenience method for initialising and cleaning up QCA.
Definition: qca_core.h:659
Q_SLOTSQ_SLOTS
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
void waitForResponse()
Block until the password / passphrase request is completed.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Sep 25 2021 23:05:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.