signon  8.42
signonclient.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 <QtGui>
24 #include <QDebug>
25 #include <QMessageBox>
26 
27 #include <SignOn/SessionData>
28 #include "signonclient.h"
29 #include "exampledata.h"
30 
31 using namespace SignOn;
32 
33 namespace SignOn {
34 
35 SignonClient::SignonClient(QWidget *parent):
36  QWidget(parent)
37 {
38  ui.setupUi(this);
39 
40  m_service = new SignOn::AuthService();
41  m_identity = NULL;
42  m_session = NULL;
43  connect(m_service, SIGNAL(methodsAvailable(const QStringList &)),
44  this, SLOT(methodsAvailable(const QStringList &)));
45  connect(m_service,
46  SIGNAL(mechanismsAvailable(const QString &, const QStringList &)),
47  this,
48  SLOT( mechanismsAvailable(const QString &, const QStringList&)));
49  connect(m_service, SIGNAL(identities(const QList<SignOn::IdentityInfo> &)),
50  this, SLOT(identities(const QList<SignOn::IdentityInfo> &)));
51 
52  qRegisterMetaType<SignOn::SessionData>("SignOn::SessionData");
53 }
54 
56 {
57  delete m_service;
58  if (m_identity) delete m_identity;
59 }
60 
61 void SignonClient::methodsAvailable(const QStringList &mechs)
62 {
63  qDebug("methodsAvailable");
64  for (int i = 0; i < mechs.size(); ++i) {
65  qDebug() << mechs.at(i).toLocal8Bit().constData() << endl;
66  m_service->queryMechanisms(mechs.at(i));
67  }
68 }
69 
70 void SignonClient::mechanismsAvailable(const QString &method,
71  const QStringList &mechs)
72 {
73  qDebug("mechanismsAvailable");
74  qDebug() << method;
75  for (int i = 0; i < mechs.size(); ++i) {
76  qDebug() << mechs.at(i).toLocal8Bit().constData() << endl;
77  }
78 }
79 
80 void SignonClient::identities(const QList<SignOn::IdentityInfo> &identityList)
81 {
82  qDebug("identities");
83  for (int i = 0; i < identityList.size(); ++i) {
84  qDebug() << identityList.at(i).caption().toLocal8Bit().constData() <<
85  endl;
86  }
87  }
88 
89 void SignonClient::response(const SessionData &sessionData)
90 {
91  qDebug("response");
92  ExampleData response = sessionData.data<ExampleData>();
93  qDebug() << response.Example();
94 }
95 
96 void SignonClient::error(const SignOn::Error &error)
97 {
98  qDebug("identity Err: %d", error.type());
99  qDebug() << error.message();
100 }
101 
102 void SignonClient::sessionError(const SignOn::Error &error)
103 {
104  qDebug("session Err: %d", error.type());
105  qDebug() << error.message();
106 }
107 
108 void SignonClient::userVerified(const bool valid)
109 {
110  qDebug() << "user verified:" << valid;
111 }
112 
113 void SignonClient::credentialsStored(const quint32 id)
114 {
115  qDebug() << "stored id: " << id;
116  QString message;
117  message.setNum(id);
118 }
119 
120 void SignonClient::on_store_clicked()
121 {
122  qDebug("on_store_clicked");
123  if (m_identity) delete m_identity;
124 
125  QMap<MethodName,MechanismsList> methods;
126 
127  QStringList mechs = QStringList() << QString::fromLatin1("ClientLogin")
128  << QString::fromLatin1("Example") ;
129  methods.insert(QLatin1String("google"), mechs);
130 
131  //example method to be able to use example plugin
132  methods.insert(QLatin1String("example"), QStringList());
133 
134  int randomNumber = qrand() % 100;
135  m_info = new IdentityInfo(QLatin1String("test_caption")
136  + QString().number(randomNumber),
137  QLatin1String("test_username")
138  + QString().number(randomNumber), methods);
139  m_info->setSecret(QLatin1String("test_secret"));
140 
141  QStringList realms = QStringList() << QString::fromLatin1("google.com")
142  << QString::fromLatin1("example.com")
143  << QString::fromLatin1("example2.com");
144  m_info->setRealms(realms);
145 
146  QStringList acl = QStringList() << QString::fromLatin1("AID::12345678")
147  << QString::fromLatin1("AID::87654321")
148  << QString::fromLatin1("signon::example");
149  m_info->setAccessControlList(acl);
150 
151  int randomType = qrand() % 4;
152  switch (randomType) {
153  case 0:
154  m_info->setType(IdentityInfo::Other);
155  break;
156  case 1:
157  m_info->setType(IdentityInfo::Application);
158  break;
159  case 2:
160  m_info->setType(IdentityInfo::Web);
161  break;
162  case 3:
163  m_info->setType(IdentityInfo::Network);
164  break;
165  }
166 
167  m_identity = Identity::newIdentity(*m_info);
168 
169  connect(m_identity, SIGNAL(credentialsStored(const quint32)),
170  this, SLOT(credentialsStored(const quint32)));
171 
172  connect(m_identity, SIGNAL(userVerified(const bool)),
173  this, SLOT(userVerified(const bool)));
174 
175  connect(m_identity, SIGNAL(error(const SignOn::Error &)),
176  this, SLOT(error(const SignOn::Error &)));
177 
178  m_identity->storeCredentials();
179 }
180 
181 void SignonClient::on_query_clicked()
182 {
183  qDebug("on_query_clicked");
184  m_service->queryMethods();
185 }
186 
187 void SignonClient::on_challenge_clicked()
188 {
189  qDebug("on_challenge_clicked");
190  if (!m_identity) {
191  error(Error(SignOn::Identity::CanceledError,
192  QLatin1String("Identity not created")));
193  return;
194  }
195  ExampleData data;
196 
197  data.setSecret("secret");
198  data.setExample("http://www.flickr.com/");
199 
200  data.setTos(QLatin1String("<b>Terms of Service</b><br>"
201  "blah blaah blah hah haa"
202  "blah blaah blah hah haa"
203  "blah blaah blah hah haa"
204  "<br>Click <a href=\"%1\">"
205  "here" "! </a> to see changes."
206  ));
207 
208  //do not show tos dialog by default
209  data.setTos(QString());
210 
211  if (!m_session) {
212  m_session = m_identity->createSession(QLatin1String("example"));
213 
214  connect(m_session, SIGNAL(response(const SignOn::SessionData &)),
215  this, SLOT(response(const SignOn::SessionData &)));
216 
217  connect(m_session, SIGNAL(error(const SignOn::Error &)),
218  this, SLOT(sessionError(const SignOn::Error &)));
219  }
220 
221  m_session->process(data, QLatin1String("example"));
222 
223 }
224 
225 void SignonClient::on_google_clicked()
226 {
227  qDebug("on_google_clicked");
228  if (!m_identity) {
229  error(Error(SignOn::Identity::CanceledError,
230  QLatin1String("Identity not created")));
231  return;
232  }
233  SignOn::SessionData data;
234 
235  data.setSecret("test");
236  data.setUserName("user@google.com");
237 
238  if (!m_session) {
239  m_session = m_identity->createSession(QLatin1String("google"));
240 
241  connect(m_session, SIGNAL(response(const SignOn::SessionData &)),
242  this, SLOT(response(const SignOn::SessionData &)));
243 
244  connect(m_session, SIGNAL(error(const SignOn::Error &)),
245  this, SLOT(sessionError(const SignOn::Error &)));
246  }
247 
248  m_session->process(data , QLatin1String("ClientLogin"));
249 }
250 
251 void SignonClient::on_verify_clicked()
252 {
253  qDebug("on_verify_clicked");
254  if (!m_identity) {
255  error(Error(SignOn::Identity::CanceledError,
256  QLatin1String("Identity not created")));
257  return;
258  }
259  //verifyUser takes QVariantMap containing setup parameters
260  //see uisessiondata.h for details
261  QVariantMap params;
262  QString link("<a href=\"http://www.google.com\"> "
263  "Click for Google"
264  "! </a>"
265  " or maybe you can try "
266  "<a href=\"http::/error\"> "
267  "Invalid link"
268  "! </a> "
269  );
270  params.insert(QLatin1String("ForgotPassword"), link);
271 
272  //params.insert(QLatin1String("ForgotPasswordUrl"), QLatin1String("http:://www.google.com"));
273 
274  WId id = this->window()->winId();
275  params.insert(QLatin1String("WindowId"), (quint32)id);
276  m_identity->verifyUser(params);
277 }
278 }