signon  8.40
signonclient.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 <QtGui>
00024 #include <QDebug>
00025 #include <QMessageBox>
00026 
00027 #include <SignOn/SessionData>
00028 #include "signonclient.h"
00029 #include "exampledata.h"
00030 
00031 using namespace SignOn;
00032 
00033 namespace SignOn {
00034 
00035 SignonClient::SignonClient(QWidget *parent):
00036     QWidget(parent)
00037 {
00038     ui.setupUi(this);
00039 
00040     m_service = new SignOn::AuthService();
00041     m_identity = NULL;
00042     m_session = NULL;
00043     connect(m_service, SIGNAL(methodsAvailable(const QStringList &)),
00044             this, SLOT(methodsAvailable(const QStringList &)));
00045     connect(m_service,
00046             SIGNAL(mechanismsAvailable(const QString &, const QStringList &)),
00047             this,
00048             SLOT( mechanismsAvailable(const QString &, const QStringList&)));
00049     connect(m_service, SIGNAL(identities(const QList<SignOn::IdentityInfo> &)),
00050             this, SLOT(identities(const QList<SignOn::IdentityInfo> &)));
00051 
00052     qRegisterMetaType<SignOn::SessionData>("SignOn::SessionData");
00053 }
00054 
00055 SignonClient::~SignonClient()
00056 {
00057     delete m_service;
00058     if (m_identity) delete m_identity;
00059 }
00060 
00061 void SignonClient::methodsAvailable(const QStringList &mechs)
00062 {
00063     qDebug("methodsAvailable");
00064     for (int i = 0; i < mechs.size(); ++i) {
00065         qDebug() << mechs.at(i).toLocal8Bit().constData() << endl;
00066         m_service->queryMechanisms(mechs.at(i));
00067     }
00068 }
00069 
00070 void SignonClient::mechanismsAvailable(const QString &method,
00071                                        const QStringList &mechs)
00072 {
00073     qDebug("mechanismsAvailable");
00074     qDebug() << method;
00075     for (int i = 0; i < mechs.size(); ++i) {
00076         qDebug() << mechs.at(i).toLocal8Bit().constData() << endl;
00077     }
00078 }
00079 
00080 void SignonClient::identities(const QList<SignOn::IdentityInfo> &identityList)
00081 {
00082     qDebug("identities");
00083      for (int i = 0; i < identityList.size(); ++i) {
00084          qDebug() << identityList.at(i).caption().toLocal8Bit().constData() <<
00085              endl;
00086     }
00087  }
00088 
00089 void SignonClient::response(const SessionData &sessionData)
00090 {
00091     qDebug("response");
00092     ExampleData response = sessionData.data<ExampleData>();
00093     qDebug() << response.Example();
00094 }
00095 
00096 void SignonClient::error(const SignOn::Error &error)
00097 {
00098     qDebug("identity Err: %d", error.type());
00099     qDebug() << error.message();
00100 }
00101 
00102 void SignonClient::sessionError(const SignOn::Error &error)
00103 {
00104     qDebug("session Err: %d", error.type());
00105     qDebug() << error.message();
00106 }
00107 
00108 void SignonClient::userVerified(const bool valid)
00109 {
00110     qDebug() << "user verified:" << valid;
00111 }
00112 
00113 void SignonClient::credentialsStored(const quint32 id)
00114 {
00115     qDebug() << "stored id: " << id;
00116     QString message;
00117     message.setNum(id);
00118 }
00119 
00120 void SignonClient::on_store_clicked()
00121 {
00122     qDebug("on_store_clicked");
00123     if (m_identity) delete m_identity;
00124 
00125     QMap<MethodName,MechanismsList> methods;
00126 
00127     QStringList mechs = QStringList() << QString::fromLatin1("ClientLogin")
00128                         << QString::fromLatin1("Example") ;
00129     methods.insert(QLatin1String("google"), mechs);
00130 
00131     //example method to be able to use example plugin
00132     methods.insert(QLatin1String("example"), QStringList());
00133 
00134     int randomNumber = qrand() % 100;
00135     m_info = new IdentityInfo(QLatin1String("test_caption")
00136                               + QString().number(randomNumber),
00137                               QLatin1String("test_username")
00138                               + QString().number(randomNumber), methods);
00139     m_info->setSecret(QLatin1String("test_secret"));
00140 
00141     QStringList realms = QStringList() << QString::fromLatin1("google.com")
00142                          << QString::fromLatin1("example.com")
00143                          << QString::fromLatin1("example2.com");
00144     m_info->setRealms(realms);
00145 
00146     QStringList acl = QStringList() << QString::fromLatin1("AID::12345678")
00147                       << QString::fromLatin1("AID::87654321")
00148                       << QString::fromLatin1("signon::example");
00149     m_info->setAccessControlList(acl);
00150 
00151     int randomType = qrand() % 4;
00152     switch (randomType) {
00153     case 0:
00154         m_info->setType(IdentityInfo::Other);
00155         break;
00156     case 1:
00157         m_info->setType(IdentityInfo::Application);
00158         break;
00159     case 2:
00160         m_info->setType(IdentityInfo::Web);
00161         break;
00162     case 3:
00163         m_info->setType(IdentityInfo::Network);
00164         break;
00165     }
00166 
00167     m_identity = Identity::newIdentity(*m_info);
00168 
00169     connect(m_identity, SIGNAL(credentialsStored(const quint32)),
00170             this, SLOT(credentialsStored(const quint32)));
00171 
00172     connect(m_identity, SIGNAL(userVerified(const bool)),
00173             this, SLOT(userVerified(const bool)));
00174 
00175     connect(m_identity, SIGNAL(error(const SignOn::Error &)),
00176             this, SLOT(error(const SignOn::Error &)));
00177 
00178     m_identity->storeCredentials();
00179 }
00180 
00181 void SignonClient::on_query_clicked()
00182 {
00183     qDebug("on_query_clicked");
00184     m_service->queryMethods();
00185 }
00186 
00187 void SignonClient::on_challenge_clicked()
00188 {
00189     qDebug("on_challenge_clicked");
00190     if (!m_identity) {
00191         error(Error(SignOn::Identity::CanceledError,
00192                     QLatin1String("Identity not created")));
00193         return;
00194     }
00195     ExampleData data;
00196 
00197     data.setSecret("secret");
00198     data.setExample("http://www.flickr.com/");
00199 
00200     data.setTos(QLatin1String("<b>Terms of Service</b><br>"
00201                               "blah blaah blah hah haa"
00202                               "blah blaah blah hah haa"
00203                               "blah blaah blah hah haa"
00204                               "<br>Click <a href=\"%1\">"
00205                               "here" "! </a> to see changes."
00206                               ));
00207 
00208     //do not show tos dialog by default
00209     data.setTos(QString());
00210 
00211     if (!m_session) {
00212         m_session = m_identity->createSession(QLatin1String("example"));
00213 
00214         connect(m_session, SIGNAL(response(const SignOn::SessionData &)),
00215             this, SLOT(response(const SignOn::SessionData &)));
00216 
00217         connect(m_session, SIGNAL(error(const SignOn::Error &)),
00218             this, SLOT(sessionError(const SignOn::Error &)));
00219     }
00220 
00221     m_session->process(data, QLatin1String("example"));
00222 
00223 }
00224 
00225 void SignonClient::on_google_clicked()
00226 {
00227     qDebug("on_google_clicked");
00228     if (!m_identity) {
00229         error(Error(SignOn::Identity::CanceledError,
00230                     QLatin1String("Identity not created")));
00231         return;
00232     }
00233     SignOn::SessionData data;
00234 
00235     data.setSecret("test");
00236     data.setUserName("user@google.com");
00237 
00238     if (!m_session) {
00239         m_session = m_identity->createSession(QLatin1String("google"));
00240 
00241         connect(m_session, SIGNAL(response(const SignOn::SessionData &)),
00242             this, SLOT(response(const SignOn::SessionData &)));
00243 
00244         connect(m_session, SIGNAL(error(const SignOn::Error &)),
00245             this, SLOT(sessionError(const SignOn::Error &)));
00246     }
00247 
00248     m_session->process(data , QLatin1String("ClientLogin"));
00249 }
00250 
00251 void SignonClient::on_verify_clicked()
00252 {
00253     qDebug("on_verify_clicked");
00254     if (!m_identity) {
00255         error(Error(SignOn::Identity::CanceledError,
00256                     QLatin1String("Identity not created")));
00257         return;
00258     }
00259     //verifyUser takes QVariantMap containing setup parameters
00260     //see uisessiondata.h for details
00261     QVariantMap params;
00262     QString link("<a href=\"http://www.google.com\"> "
00263                     "Click for Google"
00264                  "! </a>"
00265                     " or maybe you can try "
00266                  "<a href=\"http::/error\"> "
00267                     "Invalid link"
00268                  "! </a> "
00269                  );
00270     params.insert(QLatin1String("ForgotPassword"), link);
00271 
00272     //params.insert(QLatin1String("ForgotPasswordUrl"), QLatin1String("http:://www.google.com"));
00273 
00274     WId id = this->window()->winId();
00275     params.insert(QLatin1String("WindowId"), (quint32)id);
00276     m_identity->verifyUser(params);
00277 }
00278 }