signon  8.58
exampleplugin.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 "exampleplugin.h"
00024 #include "exampledata.h"
00025 #include "SignOn/signonplugincommon.h"
00026 
00027 using namespace SignOn;
00028 
00029 namespace ExamplePluginNS {
00030 
00031 ExamplePlugin::ExamplePlugin(QObject *parent):
00032     AuthPluginInterface(parent)
00033 {
00034     TRACE();
00035     m_showTos = false;
00036 }
00037 
00038 ExamplePlugin::~ExamplePlugin()
00039 {
00040     TRACE();
00041 }
00042 
00043 QString ExamplePlugin::type() const
00044 {
00045     return QLatin1String("example");
00046 }
00047 
00048 QStringList ExamplePlugin::mechanisms() const
00049 {
00050     QStringList res = QStringList(QLatin1String("default"));
00051     res << QLatin1String("example");
00052 
00053     return res;
00054 }
00055 
00056 void ExamplePlugin::cancel()
00057 {
00058 }
00059 /*
00060  * example plugin is used for testing purposes only
00061  * */
00062 void ExamplePlugin::process(const SignOn::SessionData &inData,
00063                             const QString &mechanism )
00064 {
00065     ExampleData response;
00066     ExampleData input = inData.data<ExampleData>();
00067 
00068     if (!mechanisms().contains(mechanism) ) {
00069         TRACE() << "invalid mechanism: " << mechanism;
00070         // next is commented to allow invalid machanism to be used
00071         /*
00072         emit error(PLUGIN_ERROR_MECHANISM_NOT_SUPPORTED);
00073         return;
00074         */
00075     }
00076     TRACE() << "User: " << inData.UserName() ;
00077     TRACE() << "Example" << input.Example();
00078 
00079     if (input.Params() == QLatin1String("Example")) {
00080         qDebug() << inData.UserName();
00081         response.setExample(QLatin1String("authenticated"));
00082         emit result(response);
00083         return;
00084     }
00085 
00086     if (input.Params() == QLatin1String("error")) {
00087         emit error(Error::NotAuthorized);
00088         return;
00089     }
00090 
00091     if (input.Params() == QLatin1String("toserror")) {
00092         emit error(Error::TOSNotAccepted);
00093         return;
00094     }
00095 
00096     if (input.Params() == QLatin1String("store")) {
00097         ExampleData storeData;
00098         storeData.setExample(QLatin1String("store:") + input.Example());
00099         emit store(storeData);
00100     }
00101 
00102     if (input.Params() == QLatin1String("url")) {
00103         SignOn::UiSessionData data;
00104         data.setOpenUrl(input.Example());
00105         data.setNetworkProxy(inData.NetworkProxy());
00106         emit userActionRequired(data);
00107 
00108         return;
00109     }
00110 
00111     if (input.Params() == QLatin1String("ui")) {
00112         SignOn::UiSessionData data;
00113         data.setQueryPassword(true);
00114         data.setQueryUserName(true);
00115         emit userActionRequired(data);
00116 
00117         return;
00118     }
00119 
00120     if (input.Params() == QLatin1String("captcha")) {
00121         SignOn::UiSessionData data;
00122         data.setCaptchaUrl(input.Example());
00123         data.setNetworkProxy(inData.NetworkProxy());
00124         emit userActionRequired(data);
00125 
00126         return;
00127     }
00128 
00129     if (!input.Tos().isEmpty()) {
00130         SignOn::UiSessionData data;
00131         //% "Click here to see TOS update"
00132         /*
00133         QString tos("Terms of service has changed. Click <a href=\"%1\">"
00134                     "here " "! </a> to see changes.");
00135         */
00136         QString tos = input.Tos();
00137         data.setQueryMessage(tos.arg(input.Example()));
00138         data.setOpenUrl(input.Example());
00139         m_showTos = true;
00140         emit userActionRequired(data);
00141 
00142         return;
00143     }
00144 
00145     response.setExample(QLatin1String("authenticated"));
00146     TRACE() << "Emitting results";
00147 
00148     emit store(response);
00149     emit result(response);
00150     return;
00151 }
00152 
00153 
00154 void ExamplePlugin::userActionFinished(const SignOn::UiSessionData &data)
00155 {
00156     Q_UNUSED(data);
00157     ExampleData response;
00158     TRACE();
00159     if (m_showTos) {
00160         m_showTos = false;
00161         if (data.QueryErrorCode() != QUERY_ERROR_NONE) {
00162             emit error(Error::TOSNotAccepted);
00163             return;
00164         }
00165     }
00166 
00167     response.setExample(QLatin1String("signon-ui shown"));
00168     emit result(response);
00169 
00170 }
00171 
00172 SIGNON_DECL_AUTH_PLUGIN(ExamplePlugin)
00173 
00174 } //namespace ExamplePluginNS