signon 8.54

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

ssotest2plugin.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of signon
3 *
4 * Copyright (C) 2009-2010 Nokia Corporation.
5 *
6 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * version 2.1 as published by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23 #include <QMutex>
24 #include <QMutexLocker>
25 #include <QImage>
26 #include <unistd.h>
27
28 #include "ssotest2plugin.h"
29 #include "ssotest2data.h"
30
31 #include "SignOn/signonplugincommon.h"
32 #include "SignOn/uisessiondata.h"
33 #include "SignOn/uisessiondata_priv.h"
34
35 using namespace SignOn;
36
37 namespace SsoTest2PluginNS {
38
39 static QMutex mutex;
40 static bool is_canceled = false;
41 static QEventLoop uiLoop;
42 static SignOn::UiSessionData uiData;
43
44 SsoTest2Plugin::SsoTest2Plugin(QObject *parent):
45 AuthPluginInterface(parent)
46 {
47 TRACE();
48
49 m_type = QLatin1String("ssotest2");
50 m_mechanisms = QStringList(QLatin1String("mech1"));
51 m_mechanisms += QLatin1String("mech2");
52 m_mechanisms += QLatin1String("mech3");
53
54 qRegisterMetaType<SignOn::SessionData>("SignOn::SessionData");
55 }
56
57 SsoTest2Plugin::~SsoTest2Plugin()
58 {
59
60 }
61
62 void SsoTest2Plugin::cancel()
63 {
64 TRACE();
65 QMutexLocker locker(&mutex);
66 is_canceled = true;
67 if (uiLoop.isRunning()) uiLoop.quit();
68 }
69
70 /*
71 * dummy plugin is used for testing purposes only
72 * */
73 void SsoTest2Plugin::process(const SignOn::SessionData &inData,
74 const QString &mechanism)
75 {
76 if (! mechanisms().contains(mechanism) ) {
77 emit error(Error::MechanismNotAvailable);
78 return;
79 }
80
81 QMetaObject::invokeMethod(this,
82 "execProcess",
83 Qt::QueuedConnection,
84 Q_ARG(SignOn::SessionData, inData),
85 Q_ARG(QString, mechanism));
86 }
87
88 static QByteArray loadImage(const QString &name)
89 {
90 //TODO: adopt to something changeable
91 QString resource = QLatin1String(":/");
92 QByteArray ba;
93
94 QImage realImage(resource + name);
95 QBuffer buffer(&ba);
96 buffer.open(QIODevice::ReadWrite);
97 ba.clear();
98 realImage.save(&buffer);
99 return ba;
100 }
101
102 static QVariantMap nameToParameters(const QString &dialogName)
103 {
104 QVariantMap data;
105
106 if (dialogName == QLatin1String("Browser")) {
107 static int ii = 0;
108 ii++;
109
110 if (ii % 2)
111 data["OpenUrl"] = "www.yahoo.com";
112 else
113 data["OpenUrl"] = "www.google.com";
114
115 data["watchdog"] = QString(SSOUI_KEY_SLOT_ACCEPT);
116
117 } else if (dialogName == QLatin1String("Login")) {
118 data["UserName"] = "testUsername";
119 data["Secret"] = "testSecret";
120 data["QueryMessageId"] = 0;
121 data["QueryUserName"] = true;
122 data["QueryPassword"] = true;
123 data["watchdog"] = QString(SSOUI_KEY_SLOT_ACCEPT);
124 } else if (dialogName == QLatin1String("Captcha")) {
125 data["QueryMessageId"] = 0;
126 data["CaptchaImage"] = loadImage("Captcha1.jpg");
127 data["watchdog"] = QString(SSOUI_KEY_SLOT_REJECT);
128 } else if (dialogName == QLatin1String("LoginAndCaptcha")) {
129 data["UserName"] = "testUsername";
130 data["Secret"] = "testSecret";
131 data["QueryMessageId"] = 0;
132 data["QueryUserName"] = true;
133 data["QueryPassword"] = true;
134 data["QueryMessageId"] = 0;
135
136 data["CaptchaImage"] = loadImage("Captcha1.jpg");
137 data["watchdog"] = QString(SSOUI_KEY_SLOT_REJECT);
138 }
139
140 return data;
141 }
142
143 void SsoTest2Plugin::execProcess(const SignOn::SessionData &inData,
144 const QString &mechanism)
145 {
146 Q_UNUSED(mechanism);
147 int err;
148 SsoTest2Data testData = inData.data<SsoTest2Data>();
149 QStringList chainOfResults;
150
151 for (int i = 0; i < testData.ChainOfStates().length(); i++)
152 if (!is_canceled) {
153 quint32 currState = testData.CurrentState();
154 QString message =
155 QString("message from plugin, state : %1").arg(currState);
156 TRACE() << message;
157 emit statusChanged(PLUGIN_STATE_WAITING, message);
158 usleep(0.1 * 1000000);
159
160 QString dialogName = testData.ChainOfStates().at(currState);
161
162 QVariantMap parameters = nameToParameters(dialogName);
163 SignOn::UiSessionData data(parameters);
164
165 emit userActionRequired(data);
166 uiLoop.exec();
167
168 int errorCode = data.QueryErrorCode();
169
170 if ( dialogName == QLatin1String("Browser") ) {
171 if ( errorCode == SignOn::QUERY_ERROR_NONE ||
172 errorCode == SignOn::QUERY_ERROR_BAD_URL )
173 chainOfResults.append(QLatin1String("OK"));
174 else
175 chainOfResults.append(QString("FAIL"));
176 } else if ( dialogName == QLatin1String("Login") ) {
177 if (errorCode == SignOn::QUERY_ERROR_NONE)
178 chainOfResults.append(QLatin1String("OK"));
179 else
180 chainOfResults.append(QString("FAIL"));
181 } else if ( dialogName == QLatin1String("Captcha") ||
182 dialogName == QLatin1String("LoginAndCaptcha") ) {
183 if (errorCode == SignOn::QUERY_ERROR_CANCELED)
184 chainOfResults.append(QLatin1String("OK"));
185 else
186 chainOfResults.append(QLatin1String("FAIL"));
187 }
188
189 testData.setCurrentState(currState+1);
190 }
191
192 if (is_canceled) {
193 TRACE() << "Operation is canceled";
194 QMutexLocker locker(&mutex);
195 is_canceled = false;
196 emit error(Error::SessionCanceled);
197 return;
198 }
199
200 if (!testData.ChainOfStates().length() ||
201 testData.CurrentState() >= (quint32)testData.ChainOfStates().length()) {
202 err = 0;
203 }
204
205 testData.setSecret("testSecret_after_test");
206
207 foreach(QString key, testData.propertyNames())
208 TRACE() << key << ": " << testData.getProperty(key);
209
210 TRACE() << "Emit the signal";
211 if (err)
212 emit error(err);
213 else
214 emit result(testData);
215 }
216
217 void SsoTest2Plugin::userActionFinished(const SignOn::UiSessionData &data)
218 {
219 TRACE();
220 uiData = data;
221 uiLoop.quit();
222 TRACE() << "Completed";
223 }
224
225 void SsoTest2Plugin::refresh(const SignOn::UiSessionData &data)
226 {
227 TRACE();
228 static int ii = 2;
229
230 uiData = data;
231 QString imageName = QString("Captcha%1.jpg").arg(ii);
232 TRACE() << imageName;
233 uiData.setCaptchaImage(loadImage(imageName));
234 ii++;
235 if (ii>4)
236 ii = 1;
237 emit refreshed(uiData);
238 TRACE() << "Completed";
239 }
240
241 SIGNON_DECL_AUTH_PLUGIN(SsoTest2Plugin)
242 } //namespace SsoTest2PluginNS
243
244
SsoTest2PluginNS::SsoTest2Plugin::~SsoTest2Plugin
virtual ~SsoTest2Plugin()
Definition: ssotest2plugin.cpp:57
SsoTest2PluginNS::SsoTest2Plugin::refresh
void refresh(const SignOn::UiSessionData &data)
Definition: ssotest2plugin.cpp:225
ssotest2plugin.h
SsoTest2PluginNS::SsoTest2Plugin
Definition: ssotest2plugin.h:33
SsoTest2PluginNS::SsoTest2Plugin::userActionFinished
void userActionFinished(const SignOn::UiSessionData &data)
Definition: ssotest2plugin.cpp:217
SsoTest2PluginNS::SsoTest2Plugin::mechanisms
QStringList mechanisms() const
Definition: ssotest2plugin.h:43
TRACE
#define TRACE()
Definition: debug.h:28
SsoTest2PluginNS::SsoTest2Plugin::process
void process(const SignOn::SessionData &inData, const QString &mechanism=0)
Definition: ssotest2plugin.cpp:73
SsoTest2PluginNS::SsoTest2Plugin::cancel
void cancel()
Definition: ssotest2plugin.cpp:62
ssotest2data.h