signon  8.58
ssotestplugin.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 <unistd.h>
26 
27 #include "ssotestplugin.h"
28 
29 #include "SignOn/signonplugincommon.h"
30 
31 using namespace SignOn;
32 
33 namespace SsoTestPluginNS {
34 
35 static QMutex mutex;
36 static bool is_canceled = false;
37 
38 SsoTestPlugin::SsoTestPlugin(QObject *parent):
39  AuthPluginInterface(parent)
40 {
41  TRACE();
42 
43  m_type = QLatin1String("ssotest");
44  m_mechanisms = QStringList(QLatin1String("mech1"));
45  m_mechanisms += QLatin1String("mech2");
46  m_mechanisms += QLatin1String("mech3");
47  m_mechanisms += QLatin1String("BLOB");
48 
49  qRegisterMetaType<SignOn::SessionData>("SignOn::SessionData");
50 }
51 
53 {
54 }
55 
57 {
58  TRACE();
59  QMutexLocker locker(&mutex);
60  is_canceled = true;
61 }
62 
63 /*
64  * dummy plugin is used for testing purposes only
65  */
66 void SsoTestPlugin::process(const SignOn::SessionData &inData,
67  const QString &mechanism)
68 {
69  if (!mechanisms().contains(mechanism)) {
70  QString message = QLatin1String("The given mechanism is unavailable");
71  TRACE() << message;
72  emit error(Error(Error::MechanismNotAvailable, message));
73  return;
74  }
75 
76  QMetaObject::invokeMethod(this,
77  "execProcess",
78  Qt::QueuedConnection,
79  Q_ARG(SignOn::SessionData, inData),
80  Q_ARG(QString, mechanism));
81 }
82 
83 void SsoTestPlugin::execProcess(const SignOn::SessionData &inData,
84  const QString &mechanism)
85 {
86  SignOn::SessionData outData(inData);
87  outData.setRealm("testRealm_after_test");
88 
89  for (int i = 0; i < 10; i++)
90  if (!is_canceled) {
91  TRACE() << "Signal is sent";
92  emit statusChanged(PLUGIN_STATE_WAITING,
93  QLatin1String("hello from the test plugin"));
94  usleep(0.1 * 1000000);
95  }
96 
97  if (is_canceled) {
98  TRACE() << "Operation is canceled";
99  QMutexLocker locker(&mutex);
100  is_canceled = false;
101  emit error(Error(Error::SessionCanceled,
102  QLatin1String("The operation is canceled")));
103  return;
104  }
105 
106  if (mechanism == QLatin1String("BLOB")) {
107  emit result(outData);
108  return;
109  }
110 
111  foreach(QString key, outData.propertyNames())
112  TRACE() << key << ": " << outData.getProperty(key);
113 
114  if (mechanism == QLatin1String("mech1")) {
115  emit result(outData);
116  return;
117  }
118 
119  if (mechanism == QLatin1String("mech2")) {
120  SignOn::UiSessionData data;
121  data.setQueryPassword(true);
122  emit userActionRequired(data);
123  return;
124  }
125 
126  emit result(outData);
127 }
128 
129 void SsoTestPlugin::userActionFinished(const SignOn::UiSessionData &data)
130 {
131  TRACE();
132 
133  if (data.QueryErrorCode() == QUERY_ERROR_NONE) {
134  SignOn::SessionData response;
135  response.setUserName(data.UserName());
136  response.setSecret(data.Secret());
137  emit result(response);
138  return;
139  }
140 
141  if (data.QueryErrorCode() == QUERY_ERROR_FORBIDDEN)
142  emit error(Error(Error::NotAuthorized,
143  QLatin1String("userActionFinished forbidden ")));
144  else
145  emit error(Error(Error::UserInteraction,
146  QLatin1String("userActionFinished error: ")
147  + QString::number(data.QueryErrorCode())));
148 
149  return;
150 }
151 
152 SIGNON_DECL_AUTH_PLUGIN(SsoTestPlugin)
153 
154 } //namespace SsoTestPluginNS
void userActionFinished(const SignOn::UiSessionData &data)
QStringList mechanisms() const
Definition: ssotestplugin.h:44
#define TRACE()
Definition: debug.h:28
void process(const SignOn::SessionData &inData, const QString &mechanism=0)