signon  8.58
credentialsdb_p.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*
00003  * This file is part of signon
00004  *
00005  * Copyright (C) 2009-2010 Nokia Corporation.
00006  * Copyright (C) 2012 Canonical Ltd.
00007  *
00008  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public License
00012  * version 2.1 as published by the Free Software Foundation.
00013  *
00014  * This library is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00022  * 02110-1301 USA
00023  */
00024 
00025 #ifndef CREDENTIALS_DB_P_H
00026 #define CREDENTIALS_DB_P_H
00027 
00028 #include <QObject>
00029 #include <QtSql>
00030 
00031 #include "SignOn/abstract-secrets-storage.h"
00032 #include "signonidentityinfo.h"
00033 
00034 #define SSO_METADATADB_VERSION 2
00035 #define SSO_SECRETSDB_VERSION 1
00036 
00037 class TestDatabase;
00038 
00039 namespace SignonDaemonNS {
00040 
00045 class SecretsCache
00046 {
00047     friend class ::TestDatabase;
00048 public:
00049     class AuthCache
00050     {
00051         friend class SecretsCache;
00052 
00053     private:
00054         QString m_username;
00055         QString m_password;
00056         bool m_storePassword;
00057         QHash<quint32,QVariantMap> m_blobData;
00058     };
00059 
00060     SecretsCache() {};
00061     ~SecretsCache() {};
00062 
00063     bool lookupCredentials(quint32 id,
00064                            QString &username,
00065                            QString &password) const;
00066     QVariantMap lookupData(quint32 id, quint32 method) const;
00067 
00068     void updateCredentials(quint32 id,
00069                            const QString &username,
00070                            const QString &password,
00071                            bool storePassword);
00072     void updateData(quint32 id, quint32 method, const QVariantMap &data);
00073 
00074     void storeToDB(SignOn::AbstractSecretsStorage *secretsStorage) const;
00075     void clear();
00076 
00077 private:
00078     QHash<quint32, AuthCache> m_cache;
00079 };
00080 
00086 class SqlDatabase
00087 {
00088     friend class ::TestDatabase;
00089 public:
00094     SqlDatabase(const QString &hostname, const QString &connectionName,
00095                 int version);
00096 
00100     virtual ~SqlDatabase();
00101 
00105     bool init();
00106 
00107     virtual bool createTables() = 0;
00108     virtual bool clear() = 0;
00109     virtual bool updateDB(int version);
00110 
00115     bool connect();
00119     void disconnect();
00120 
00121     bool startTransaction();
00122     bool commit();
00123     void rollback();
00124 
00128     bool connected() { return m_database.isOpen(); }
00129 
00134     void setDatabaseName(const QString &databaseName) {
00135         m_database.setDatabaseName(databaseName);
00136     }
00137 
00142     void setUsername(const QString &username) {
00143         m_database.setUserName(username);
00144     }
00145 
00150     void setPassword(const QString &password) {
00151         m_database.setPassword(password);
00152     }
00153 
00157     QString databaseName() const { return m_database.databaseName(); }
00158 
00162     QString username() const { return m_database.userName(); }
00163 
00167     QString password() const { return m_database.password(); }
00168 
00169     QSqlQuery newQuery() const { return QSqlQuery(m_database); }
00170 
00179     QSqlQuery exec(const QString &query);
00180 
00189     QSqlQuery exec(QSqlQuery &query);
00190 
00200     bool transactionalExec(const QStringList &queryList);
00201 
00205     bool hasTables() const {
00206         return m_database.tables().count() > 0 ? true : false;
00207     }
00208 
00212     static QStringList supportedDrivers() { return QSqlDatabase::drivers(); }
00213 
00218     SignOn::CredentialsDBError lastError() const;
00219     bool errorOccurred() const { return lastError().isValid(); };
00220     void clearError() { m_lastError.clear(); }
00221 
00227     static QString errorInfo(const QSqlError &error);
00228 
00229     QString connectionName() const { return m_database.connectionName(); }
00230 
00231 protected:
00232     QStringList queryList(const QString &query_str);
00233     QStringList queryList(QSqlQuery &query);
00234     void setLastError(const QSqlError &sqlError);
00235 
00236 private:
00237     SignOn::CredentialsDBError m_lastError;
00238 
00239 protected:
00240     int m_version;
00241     QSqlDatabase m_database;
00242 
00243     friend class CredentialsDB;
00244 };
00245 
00246 class MetaDataDB: public SqlDatabase
00247 {
00248     friend class ::TestDatabase;
00249 public:
00250     MetaDataDB(const QString &name):
00251         SqlDatabase(name, QLatin1String("SSO-metadata"),
00252                     SSO_METADATADB_VERSION) {}
00253 
00254     bool createTables();
00255     bool updateDB(int version);
00256 
00257     QStringList methods(const quint32 id,
00258                         const QString &securityToken = QString());
00259     quint32 insertMethod(const QString &method, bool *ok = 0);
00260     quint32 methodId(const QString &method);
00261     SignonIdentityInfo identity(const quint32 id);
00262     QList<SignonIdentityInfo> identities(const QMap<QString, QString> &filter);
00263 
00264     quint32 updateIdentity(const SignonIdentityInfo &info);
00265     bool removeIdentity(const quint32 id);
00266 
00267     bool clear();
00268 
00269     QStringList accessControlList(const quint32 identityId);
00270     QStringList ownerList(const quint32 identityId);
00271 
00272     bool addReference(const quint32 id,
00273                       const QString &token,
00274                       const QString &reference);
00275     bool removeReference(const quint32 id,
00276                          const QString &token,
00277                          const QString &reference = QString());
00278     QStringList references(const quint32 id, const QString &token = QString());
00279 
00280 private:
00281     bool insertMethods(QMap<QString, QStringList> methods);
00282     quint32 updateCredentials(const SignonIdentityInfo &info);
00283     bool updateRealms(quint32 id, const QStringList &realms, bool isNew);
00284     QStringList tableUpdates2();
00285 };
00286 
00287 } // namespace SignonDaemonNS
00288 
00289 #endif // CREDENTIALSDB_P_H