|
signon
8.40
|
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 * 00007 * Contact: Aurel Popirtac <ext-aurel.popirtac@nokia.com> 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 00031 #ifndef CREDENTIALS_DB_H 00032 #define CREDENTIALS_DB_H 00033 00034 #include <QObject> 00035 #include <QtSql> 00036 00037 #include "SignOn/abstract-secrets-storage.h" 00038 #include "signonidentityinfo.h" 00039 00040 #define SSO_MAX_TOKEN_STORAGE (4*1024) // 4 kB for token store/identity/method 00041 #define SSO_METADATADB_VERSION 2 00042 #define SSO_SECRETSDB_VERSION 1 00043 00044 class TestDatabase; 00045 00046 namespace SignonDaemonNS { 00047 00052 enum IdentityFlags { 00053 Validated = 0x0001, 00054 RememberPassword = 0x0002, 00055 UserNameIsSecret = 0x0004, 00056 }; 00057 00063 class SqlDatabase 00064 { 00065 friend class ::TestDatabase; 00066 public: 00071 SqlDatabase(const QString &hostname, const QString &connectionName, 00072 int version); 00073 00077 virtual ~SqlDatabase(); 00078 00082 bool init(); 00083 00084 virtual bool createTables() = 0; 00085 virtual bool clear() = 0; 00086 virtual bool updateDB(int version); 00087 00092 bool connect(); 00096 void disconnect(); 00097 00098 bool startTransaction(); 00099 bool commit(); 00100 void rollback(); 00101 00105 bool connected() { return m_database.isOpen(); } 00106 00111 void setDatabaseName(const QString &databaseName) { 00112 m_database.setDatabaseName(databaseName); 00113 } 00114 00119 void setUsername(const QString &username) { 00120 m_database.setUserName(username); 00121 } 00122 00127 void setPassword(const QString &password) { 00128 m_database.setPassword(password); 00129 } 00130 00134 QString databaseName() const { return m_database.databaseName(); } 00135 00139 QString username() const { return m_database.userName(); } 00140 00144 QString password() const { return m_database.password(); } 00145 00146 QSqlQuery newQuery() const { return QSqlQuery(m_database); } 00147 00156 QSqlQuery exec(const QString &query); 00157 00166 QSqlQuery exec(QSqlQuery &query); 00167 00177 bool transactionalExec(const QStringList &queryList); 00178 00182 bool hasTables() const { 00183 return m_database.tables().count() > 0 ? true : false; 00184 } 00185 00189 static QStringList supportedDrivers() { return QSqlDatabase::drivers(); } 00190 00195 SignOn::CredentialsDBError lastError() const; 00196 bool errorOccurred() const { return lastError().isValid(); }; 00197 void clearError() { m_lastError.clear(); } 00198 00204 static QString errorInfo(const QSqlError &error); 00205 00206 QString connectionName() const { return m_database.connectionName(); } 00207 00208 protected: 00209 QStringList queryList(const QString &query_str); 00210 QStringList queryList(QSqlQuery &query); 00211 void setLastError(const QSqlError &sqlError); 00212 00213 private: 00214 SignOn::CredentialsDBError m_lastError; 00215 00216 protected: 00217 int m_version; 00218 QSqlDatabase m_database; 00219 00220 friend class CredentialsDB; 00221 }; 00222 00223 class MetaDataDB: public SqlDatabase 00224 { 00225 friend class ::TestDatabase; 00226 public: 00227 MetaDataDB(const QString &name): 00228 SqlDatabase(name, QLatin1String("SSO-metadata"), 00229 SSO_METADATADB_VERSION) {} 00230 00231 bool createTables(); 00232 bool updateDB(int version); 00233 00234 QStringList methods(const quint32 id, 00235 const QString &securityToken = QString()); 00236 quint32 insertMethod(const QString &method, bool *ok = 0); 00237 quint32 methodId(const QString &method); 00238 SignonIdentityInfo identity(const quint32 id); 00239 QList<SignonIdentityInfo> identities(const QMap<QString, QString> &filter); 00240 00241 quint32 updateIdentity(const SignonIdentityInfo &info); 00242 bool removeIdentity(const quint32 id); 00243 00244 bool clear(); 00245 00246 QStringList accessControlList(const quint32 identityId); 00247 QStringList ownerList(const quint32 identityId); 00248 00249 bool addReference(const quint32 id, 00250 const QString &token, 00251 const QString &reference); 00252 bool removeReference(const quint32 id, 00253 const QString &token, 00254 const QString &reference = QString()); 00255 QStringList references(const quint32 id, const QString &token = QString()); 00256 00257 private: 00258 bool insertMethods(QMap<QString, QStringList> methods); 00259 quint32 updateCredentials(const SignonIdentityInfo &info); 00260 bool updateRealms(quint32 id, const QStringList &realms, bool isNew); 00261 QStringList tableUpdates2(); 00262 }; 00263 00270 class CredentialsDB: public QObject 00271 { 00272 Q_OBJECT 00273 Q_DISABLE_COPY(CredentialsDB) 00274 00275 friend class ::TestDatabase; 00276 00277 class ErrorMonitor 00278 { 00279 public: 00280 /* The constructor clears the errors in CredentialsDB, MetaDataDB and 00281 * SecretsDB. */ 00282 ErrorMonitor(CredentialsDB *db); 00283 /* The destructor collects the errors and sets 00284 * CredentialsDB::_lastError to the appropriate value. */ 00285 ~ErrorMonitor(); 00286 private: 00287 CredentialsDB *_db; 00288 }; 00289 friend class ErrorMonitor; 00290 00291 public: 00292 CredentialsDB(const QString &metaDataDbName, 00293 SignOn::AbstractSecretsStorage *secretsStorage); 00294 ~CredentialsDB(); 00295 00296 bool init(); 00302 bool openSecretsDB(const QString &secretsDbName); 00303 bool isSecretsDBOpen(); 00304 void closeSecretsDB(); 00305 00306 SignOn::CredentialsDBError lastError() const; 00307 bool errorOccurred() const { return lastError().isValid(); }; 00308 00309 QStringList methods(const quint32 id, 00310 const QString &securityToken = QString()); 00311 bool checkPassword(const quint32 id, 00312 const QString &username, const QString &password); 00313 SignonIdentityInfo credentials(const quint32 id, bool queryPassword = true); 00314 QList<SignonIdentityInfo> credentials(const QMap<QString, QString> &filter); 00315 00316 quint32 insertCredentials(const SignonIdentityInfo &info, 00317 bool storeSecret = true); 00318 quint32 updateCredentials(const SignonIdentityInfo &info, 00319 bool storeSecret = true); 00320 bool removeCredentials(const quint32 id); 00321 00322 bool clear(); 00323 00324 QStringList accessControlList(const quint32 identityId); 00325 QStringList ownerList(const quint32 identityId); 00326 QString credentialsOwnerSecurityToken(const quint32 identityId); 00327 00328 QVariantMap loadData(const quint32 id, const QString &method); 00329 bool storeData(const quint32 id, 00330 const QString &method, 00331 const QVariantMap &data); 00332 bool removeData(const quint32 id, const QString &method = QString()); 00333 00334 bool addReference(const quint32 id, 00335 const QString &token, 00336 const QString &reference); 00337 bool removeReference(const quint32 id, 00338 const QString &token, 00339 const QString &reference = QString()); 00340 QStringList references(const quint32 id, 00341 const QString &token = QString()); 00342 00343 private: 00344 SignOn::AbstractSecretsStorage *secretsStorage; 00345 MetaDataDB *metaDataDB; 00346 SignOn::CredentialsDBError _lastError; 00347 SignOn::CredentialsDBError noSecretsDB; 00348 }; 00349 00350 } // namespace SignonDaemonNS 00351 00352 #endif // CREDENTIALSDB_H