signon  8.41
credentialsdb.h
Go to the documentation of this file.
1 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of signon
4  *
5  * Copyright (C) 2009-2010 Nokia Corporation.
6  *
7  * Contact: Aurel Popirtac <ext-aurel.popirtac@nokia.com>
8  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * version 2.1 as published by the Free Software Foundation.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  */
24 
31 #ifndef CREDENTIALS_DB_H
32 #define CREDENTIALS_DB_H
33 
34 #include <QObject>
35 #include <QtSql>
36 
37 #include "SignOn/abstract-secrets-storage.h"
38 #include "signonidentityinfo.h"
39 
40 #define SSO_MAX_TOKEN_STORAGE (4*1024) // 4 kB for token store/identity/method
41 #define SSO_METADATADB_VERSION 2
42 #define SSO_SECRETSDB_VERSION 1
43 
44 class TestDatabase;
45 
46 namespace SignonDaemonNS {
47 
53  Validated = 0x0001,
54  RememberPassword = 0x0002,
55  UserNameIsSecret = 0x0004,
56 };
57 
64 {
65  friend class ::TestDatabase;
66 public:
71  SqlDatabase(const QString &hostname, const QString &connectionName,
72  int version);
73 
77  virtual ~SqlDatabase();
78 
82  bool init();
83 
84  virtual bool createTables() = 0;
85  virtual bool clear() = 0;
86  virtual bool updateDB(int version);
87 
92  bool connect();
96  void disconnect();
97 
98  bool startTransaction();
99  bool commit();
100  void rollback();
101 
105  bool connected() { return m_database.isOpen(); }
106 
111  void setDatabaseName(const QString &databaseName) {
112  m_database.setDatabaseName(databaseName);
113  }
114 
119  void setUsername(const QString &username) {
120  m_database.setUserName(username);
121  }
122 
127  void setPassword(const QString &password) {
128  m_database.setPassword(password);
129  }
130 
134  QString databaseName() const { return m_database.databaseName(); }
135 
139  QString username() const { return m_database.userName(); }
140 
144  QString password() const { return m_database.password(); }
145 
146  QSqlQuery newQuery() const { return QSqlQuery(m_database); }
147 
156  QSqlQuery exec(const QString &query);
157 
166  QSqlQuery exec(QSqlQuery &query);
167 
177  bool transactionalExec(const QStringList &queryList);
178 
182  bool hasTables() const {
183  return m_database.tables().count() > 0 ? true : false;
184  }
185 
189  static QStringList supportedDrivers() { return QSqlDatabase::drivers(); }
190 
195  SignOn::CredentialsDBError lastError() const;
196  bool errorOccurred() const { return lastError().isValid(); };
197  void clearError() { m_lastError.clear(); }
198 
204  static QString errorInfo(const QSqlError &error);
205 
206  QString connectionName() const { return m_database.connectionName(); }
207 
208 protected:
209  QStringList queryList(const QString &query_str);
210  QStringList queryList(QSqlQuery &query);
211  void setLastError(const QSqlError &sqlError);
212 
213 private:
214  SignOn::CredentialsDBError m_lastError;
215 
216 protected:
218  QSqlDatabase m_database;
219 
220  friend class CredentialsDB;
221 };
222 
223 class MetaDataDB: public SqlDatabase
224 {
225  friend class ::TestDatabase;
226 public:
227  MetaDataDB(const QString &name):
228  SqlDatabase(name, QLatin1String("SSO-metadata"),
230 
231  bool createTables();
232  bool updateDB(int version);
233 
234  QStringList methods(const quint32 id,
235  const QString &securityToken = QString());
236  quint32 insertMethod(const QString &method, bool *ok = 0);
237  quint32 methodId(const QString &method);
238  SignonIdentityInfo identity(const quint32 id);
239  QList<SignonIdentityInfo> identities(const QMap<QString, QString> &filter);
240 
241  quint32 updateIdentity(const SignonIdentityInfo &info);
242  bool removeIdentity(const quint32 id);
243 
244  bool clear();
245 
246  QStringList accessControlList(const quint32 identityId);
247  QStringList ownerList(const quint32 identityId);
248 
249  bool addReference(const quint32 id,
250  const QString &token,
251  const QString &reference);
252  bool removeReference(const quint32 id,
253  const QString &token,
254  const QString &reference = QString());
255  QStringList references(const quint32 id, const QString &token = QString());
256 
257 private:
258  bool insertMethods(QMap<QString, QStringList> methods);
259  quint32 updateCredentials(const SignonIdentityInfo &info);
260  bool updateRealms(quint32 id, const QStringList &realms, bool isNew);
261  QStringList tableUpdates2();
262 };
263 
270 class CredentialsDB: public QObject
271 {
272  Q_OBJECT
273  Q_DISABLE_COPY(CredentialsDB)
274 
275  friend class ::TestDatabase;
276 
277  class ErrorMonitor
278  {
279  public:
280  /* The constructor clears the errors in CredentialsDB, MetaDataDB and
281  * SecretsDB. */
283  /* The destructor collects the errors and sets
284  * CredentialsDB::_lastError to the appropriate value. */
285  ~ErrorMonitor();
286  private:
287  CredentialsDB *_db;
288  };
289  friend class ErrorMonitor;
290 
291 public:
292  CredentialsDB(const QString &metaDataDbName,
293  SignOn::AbstractSecretsStorage *secretsStorage);
294  ~CredentialsDB();
295 
296  bool init();
302  bool openSecretsDB(const QString &secretsDbName);
303  bool isSecretsDBOpen();
304  void closeSecretsDB();
305 
306  SignOn::CredentialsDBError lastError() const;
307  bool errorOccurred() const { return lastError().isValid(); };
308 
309  QStringList methods(const quint32 id,
310  const QString &securityToken = QString());
311  bool checkPassword(const quint32 id,
312  const QString &username, const QString &password);
313  SignonIdentityInfo credentials(const quint32 id, bool queryPassword = true);
314  QList<SignonIdentityInfo> credentials(const QMap<QString, QString> &filter);
315 
316  quint32 insertCredentials(const SignonIdentityInfo &info,
317  bool storeSecret = true);
318  quint32 updateCredentials(const SignonIdentityInfo &info,
319  bool storeSecret = true);
320  bool removeCredentials(const quint32 id);
321 
322  bool clear();
323 
324  QStringList accessControlList(const quint32 identityId);
325  QStringList ownerList(const quint32 identityId);
326  QString credentialsOwnerSecurityToken(const quint32 identityId);
327 
328  QVariantMap loadData(const quint32 id, const QString &method);
329  bool storeData(const quint32 id,
330  const QString &method,
331  const QVariantMap &data);
332  bool removeData(const quint32 id, const QString &method = QString());
333 
334  bool addReference(const quint32 id,
335  const QString &token,
336  const QString &reference);
337  bool removeReference(const quint32 id,
338  const QString &token,
339  const QString &reference = QString());
340  QStringList references(const quint32 id,
341  const QString &token = QString());
342 
343 private:
344  SignOn::AbstractSecretsStorage *secretsStorage;
345  MetaDataDB *metaDataDB;
346  SignOn::CredentialsDBError _lastError;
347  SignOn::CredentialsDBError noSecretsDB;
348 };
349 
350 } // namespace SignonDaemonNS
351 
352 #endif // CREDENTIALSDB_H