signon  8.58
ssotestplugin.cpp
Go to the documentation of this file.
00001 /*
00002  * This file is part of signon
00003  *
00004  * Copyright (C) 2009-2010 Nokia Corporation.
00005  *
00006  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * version 2.1 as published by the Free Software Foundation.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00020  * 02110-1301 USA
00021  */
00022 
00023 #include <QMutex>
00024 #include <QMutexLocker>
00025 #include <unistd.h>
00026 
00027 #include "ssotestplugin.h"
00028 
00029 #include "SignOn/signonplugincommon.h"
00030 
00031 using namespace SignOn;
00032 
00033 namespace SsoTestPluginNS {
00034 
00035 static QMutex mutex;
00036 static bool is_canceled = false;
00037 
00038 SsoTestPlugin::SsoTestPlugin(QObject *parent):
00039     AuthPluginInterface(parent)
00040 {
00041     TRACE();
00042 
00043     m_type = QLatin1String("ssotest");
00044     m_mechanisms = QStringList(QLatin1String("mech1"));
00045     m_mechanisms += QLatin1String("mech2");
00046     m_mechanisms += QLatin1String("mech3");
00047     m_mechanisms += QLatin1String("BLOB");
00048 
00049     qRegisterMetaType<SignOn::SessionData>("SignOn::SessionData");
00050 }
00051 
00052 SsoTestPlugin::~SsoTestPlugin()
00053 {
00054 }
00055 
00056 void SsoTestPlugin::cancel()
00057 {
00058     TRACE();
00059     QMutexLocker locker(&mutex);
00060     is_canceled = true;
00061 }
00062 
00063 /*
00064  * dummy plugin is used for testing purposes only
00065  */
00066 void SsoTestPlugin::process(const SignOn::SessionData &inData,
00067                             const QString &mechanism)
00068 {
00069     if (!mechanisms().contains(mechanism)) {
00070         QString message = QLatin1String("The given mechanism is unavailable");
00071         TRACE() << message;
00072         emit error(Error(Error::MechanismNotAvailable, message));
00073         return;
00074     }
00075 
00076     QMetaObject::invokeMethod(this,
00077                               "execProcess",
00078                               Qt::QueuedConnection,
00079                               Q_ARG(SignOn::SessionData, inData),
00080                               Q_ARG(QString, mechanism));
00081 }
00082 
00083 void SsoTestPlugin::execProcess(const SignOn::SessionData &inData,
00084                                 const QString &mechanism)
00085 {
00086     SignOn::SessionData outData(inData);
00087     outData.setRealm("testRealm_after_test");
00088 
00089     for (int i = 0; i < 10; i++)
00090         if (!is_canceled) {
00091             TRACE() << "Signal is sent";
00092             emit statusChanged(PLUGIN_STATE_WAITING,
00093                                QLatin1String("hello from the test plugin"));
00094             usleep(0.1 * 1000000);
00095         }
00096 
00097     if (is_canceled) {
00098         TRACE() << "Operation is canceled";
00099         QMutexLocker locker(&mutex);
00100         is_canceled = false;
00101         emit error(Error(Error::SessionCanceled,
00102                          QLatin1String("The operation is canceled")));
00103         return;
00104     }
00105 
00106     if (mechanism == QLatin1String("BLOB")) {
00107         emit result(outData);
00108         return;
00109     }
00110 
00111     foreach(QString key, outData.propertyNames())
00112         TRACE() << key << ": " << outData.getProperty(key);
00113 
00114     if (mechanism == QLatin1String("mech1")) {
00115         emit result(outData);
00116         return;
00117     }
00118 
00119     if (mechanism == QLatin1String("mech2")) {
00120         SignOn::UiSessionData data;
00121         data.setQueryPassword(true);
00122         emit userActionRequired(data);
00123         return;
00124     }
00125 
00126     emit result(outData);
00127 }
00128 
00129 void SsoTestPlugin::userActionFinished(const SignOn::UiSessionData &data)
00130 {
00131     TRACE();
00132 
00133     if (data.QueryErrorCode() == QUERY_ERROR_NONE) {
00134         SignOn::SessionData response;
00135         response.setUserName(data.UserName());
00136         response.setSecret(data.Secret());
00137         emit result(response);
00138         return;
00139     }
00140 
00141     if (data.QueryErrorCode() == QUERY_ERROR_FORBIDDEN)
00142         emit error(Error(Error::NotAuthorized,
00143                    QLatin1String("userActionFinished forbidden ")));
00144     else
00145         emit error(Error(Error::UserInteraction,
00146                    QLatin1String("userActionFinished error: ")
00147                    + QString::number(data.QueryErrorCode())));
00148 
00149     return;
00150 }
00151 
00152 SIGNON_DECL_AUTH_PLUGIN(SsoTestPlugin)
00153 
00154 } //namespace SsoTestPluginNS