signon  8.58
credentialsdb_p.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  * Copyright (C) 2012 Canonical Ltd.
7  *
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 
25 #ifndef CREDENTIALS_DB_P_H
26 #define CREDENTIALS_DB_P_H
27 
28 #include <QObject>
29 #include <QtSql>
30 
31 #include "SignOn/abstract-secrets-storage.h"
32 #include "signonidentityinfo.h"
33 
34 #define SSO_METADATADB_VERSION 2
35 #define SSO_SECRETSDB_VERSION 1
36 
37 class TestDatabase;
38 
39 namespace SignonDaemonNS {
40 
46 {
47  friend class ::TestDatabase;
48 public:
49  class AuthCache
50  {
51  friend class SecretsCache;
52 
53  private:
54  QString m_username;
55  QString m_password;
56  bool m_storePassword;
57  QHash<quint32,QVariantMap> m_blobData;
58  };
59 
62 
63  bool lookupCredentials(quint32 id,
64  QString &username,
65  QString &password) const;
66  QVariantMap lookupData(quint32 id, quint32 method) const;
67 
68  void updateCredentials(quint32 id,
69  const QString &username,
70  const QString &password,
71  bool storePassword);
72  void updateData(quint32 id, quint32 method, const QVariantMap &data);
73 
74  void storeToDB(SignOn::AbstractSecretsStorage *secretsStorage) const;
75  void clear();
76 
77 private:
78  QHash<quint32, AuthCache> m_cache;
79 };
80 
87 {
88  friend class ::TestDatabase;
89 public:
94  SqlDatabase(const QString &hostname, const QString &connectionName,
95  int version);
96 
100  virtual ~SqlDatabase();
101 
105  bool init();
106 
107  virtual bool createTables() = 0;
108  virtual bool clear() = 0;
109  virtual bool updateDB(int version);
110 
115  bool connect();
119  void disconnect();
120 
121  bool startTransaction();
122  bool commit();
123  void rollback();
124 
128  bool connected() { return m_database.isOpen(); }
129 
134  void setDatabaseName(const QString &databaseName) {
135  m_database.setDatabaseName(databaseName);
136  }
137 
142  void setUsername(const QString &username) {
143  m_database.setUserName(username);
144  }
145 
150  void setPassword(const QString &password) {
151  m_database.setPassword(password);
152  }
153 
157  QString databaseName() const { return m_database.databaseName(); }
158 
162  QString username() const { return m_database.userName(); }
163 
167  QString password() const { return m_database.password(); }
168 
169  QSqlQuery newQuery() const { return QSqlQuery(m_database); }
170 
179  QSqlQuery exec(const QString &query);
180 
189  QSqlQuery exec(QSqlQuery &query);
190 
200  bool transactionalExec(const QStringList &queryList);
201 
205  bool hasTables() const {
206  return m_database.tables().count() > 0 ? true : false;
207  }
208 
212  static QStringList supportedDrivers() { return QSqlDatabase::drivers(); }
213 
218  SignOn::CredentialsDBError lastError() const;
219  bool errorOccurred() const { return lastError().isValid(); };
220  void clearError() { m_lastError.clear(); }
221 
227  static QString errorInfo(const QSqlError &error);
228 
229  QString connectionName() const { return m_database.connectionName(); }
230 
231 protected:
232  QStringList queryList(const QString &query_str);
233  QStringList queryList(QSqlQuery &query);
234  void setLastError(const QSqlError &sqlError);
235 
236 private:
237  SignOn::CredentialsDBError m_lastError;
238 
239 protected:
241  QSqlDatabase m_database;
242 
243  friend class CredentialsDB;
244 };
245 
246 class MetaDataDB: public SqlDatabase
247 {
248  friend class ::TestDatabase;
249 public:
250  MetaDataDB(const QString &name):
251  SqlDatabase(name, QLatin1String("SSO-metadata"),
253 
254  bool createTables();
255  bool updateDB(int version);
256 
257  QStringList methods(const quint32 id,
258  const QString &securityToken = QString());
259  quint32 insertMethod(const QString &method, bool *ok = 0);
260  quint32 methodId(const QString &method);
261  SignonIdentityInfo identity(const quint32 id);
262  QList<SignonIdentityInfo> identities(const QMap<QString, QString> &filter);
263 
264  quint32 updateIdentity(const SignonIdentityInfo &info);
265  bool removeIdentity(const quint32 id);
266 
267  bool clear();
268 
269  QStringList accessControlList(const quint32 identityId);
270  QStringList ownerList(const quint32 identityId);
271 
272  bool addReference(const quint32 id,
273  const QString &token,
274  const QString &reference);
275  bool removeReference(const quint32 id,
276  const QString &token,
277  const QString &reference = QString());
278  QStringList references(const quint32 id, const QString &token = QString());
279 
280 private:
281  bool insertMethods(QMap<QString, QStringList> methods);
282  quint32 updateCredentials(const SignonIdentityInfo &info);
283  bool updateRealms(quint32 id, const QStringList &realms, bool isNew);
284  QStringList tableUpdates2();
285 };
286 
287 } // namespace SignonDaemonNS
288 
289 #endif // CREDENTIALSDB_P_H
static QStringList supportedDrivers()
bool lookupCredentials(quint32 id, QString &username, QString &password) const
#define SSO_METADATADB_VERSION
Caches credentials or BLOB authentication data.
void storeToDB(SignOn::AbstractSecretsStorage *secretsStorage) const
void setDatabaseName(const QString &databaseName)
Sets the database name.
void setPassword(const QString &password)
Sets the password for the database connection.
QVariantMap lookupData(quint32 id, quint32 method) const
Daemon side representation of identity information.
Manages the credentials I/O.
Definition: credentialsdb.h:66
QSqlQuery newQuery() const
void updateCredentials(quint32 id, const QString &username, const QString &password, bool storePassword)
MetaDataDB(const QString &name)
QString connectionName() const
void setUsername(const QString &username)
Sets the username for the database connection.
Will be used manage the SQL database interaction.
void updateData(quint32 id, quint32 method, const QVariantMap &data)