27 #define RETURN_IF_NOT_OPEN(retval) \ 29 TRACE() << "Secrets DB is not available"; \ 30 SignOn::CredentialsDBError error(QLatin1String("Not open"), \ 31 SignOn::CredentialsDBError::NotOpen); \ 32 setLastError(error); return retval; \ 35 #define S(s) QLatin1String(s) 41 QStringList createTableQuery = QStringList()
42 << QString::fromLatin1(
43 "CREATE TABLE CREDENTIALS" 44 "(id INTEGER NOT NULL UNIQUE," 48 << QString::fromLatin1(
50 "(identity_id INTEGER," 54 "PRIMARY KEY (identity_id, method_id, key))")
56 << QString::fromLatin1(
58 "CREATE TRIGGER tg_delete_credentials " 59 "BEFORE DELETE ON CREDENTIALS " 61 " DELETE FROM STORE WHERE STORE.identity_id = OLD.id; " 65 foreach (QString createTable, createTableQuery) {
66 QSqlQuery query =
exec(createTable);
68 TRACE() <<
"Error occurred while creating the database.";
81 QStringList clearCommands = QStringList()
82 << QLatin1String(
"DELETE FROM CREDENTIALS")
83 << QLatin1String(
"DELETE FROM STORE");
93 TRACE() <<
"Could not start transaction. Error inserting credentials.";
98 TRACE() <<
"INSERT:" << id;
99 query.prepare(
S(
"INSERT OR REPLACE INTO CREDENTIALS " 100 "(id, username, password) " 101 "VALUES(:id, :username, :password)"));
103 query.bindValue(
S(
":id"),
id);
104 query.bindValue(
S(
":username"), username);
105 query.bindValue(
S(
":password"), password);
111 TRACE() <<
"Error occurred while storing crendentials";
121 QStringList queries = QStringList()
122 << QString::fromLatin1(
123 "DELETE FROM CREDENTIALS WHERE id = %1").arg(
id)
124 << QString::fromLatin1(
125 "DELETE FROM STORE WHERE identity_id = %1").arg(
id);
137 QString::fromLatin1(
"SELECT username, password FROM credentials " 138 "WHERE id = %1").arg(
id);
139 QSqlQuery query =
exec(queryStr);
140 if (!query.first()) {
141 TRACE() <<
"No result or invalid credentials query.";
145 username = query.value(0).toString();
146 password = query.value(1).toString();
155 q.prepare(
S(
"SELECT key, value " 156 "FROM STORE WHERE identity_id = :id AND method_id = :method"));
157 q.bindValue(
S(
":id"),
id);
158 q.bindValue(
S(
":method"), method);
161 return QVariantMap();
166 array = q.value(1).toByteArray();
167 QDataStream stream(array);
170 result.insert(q.value(0).toString(), data);
180 TRACE() <<
"Could not start transaction. Error inserting data.";
186 q.prepare(
S(
"DELETE FROM STORE WHERE identity_id = :id " 187 "AND method_id = :method"));
188 q.bindValue(
S(
":id"),
id);
189 q.bindValue(
S(
":method"), method);
193 TRACE() <<
"Data removal failed.";
198 qint32 dataCounter = 0;
199 if (!(data.keys().empty())) {
200 QMapIterator<QString, QVariant> it(data);
201 while (it.hasNext()) {
205 QDataStream stream(&array, QIODevice::WriteOnly);
206 stream << it.value();
208 dataCounter += it.key().size() +array.size();
210 BLAME() <<
"storing data max size exceeded";
216 if (!it.value().isValid() || it.value().isNull()) {
221 "INSERT OR REPLACE INTO STORE " 222 "(identity_id, method_id, key, value) " 223 "VALUES(:id, :method, :key, :value)"));
224 query.bindValue(
S(
":value"), array);
225 query.bindValue(
S(
":id"),
id);
226 query.bindValue(
S(
":method"), method);
227 query.bindValue(
S(
":key"), it.key());
237 TRACE() <<
"Data insertion ok.";
241 TRACE() <<
"Data insertion failed.";
250 TRACE() <<
"Could not start transaction. Error removing data.";
256 q.prepare(
S(
"DELETE FROM STORE WHERE identity_id = :id"));
258 q.prepare(
S(
"DELETE FROM STORE WHERE identity_id = :id " 259 "AND method_id = :method"));
260 q.bindValue(
S(
":method"), method);
262 q.bindValue(
S(
":id"),
id);
265 TRACE() <<
"Data removal ok.";
269 TRACE() <<
"Data removal failed.";
275 AbstractSecretsStorage(parent),
288 TRACE() <<
"Initializing open DB; closing first...";
293 name.append(configuration.value(QLatin1String(
"name")).toString());
296 if (!m_secretsDB->
init()) {
303 m_secretsDBConnectionName.clear();
311 if (m_secretsDB != 0) {
313 QSqlDatabase::removeDatabase(m_secretsDBConnectionName);
316 return AbstractSecretsStorage::close();
323 return m_secretsDB->
clear();
327 const QString &username,
328 const QString &password)
355 return m_secretsDB->
loadData(
id, method);
359 const QVariantMap &data)
363 return m_secretsDB->
storeData(
id, method, data);
QSqlQuery exec(const QString &query)
Executes a specific database query.
bool updateCredentials(const quint32 id, const QString &username, const QString &password)
bool removeData(quint32 id, quint32 method)
bool updateCredentials(const quint32 id, const QString &username, const QString &password)
DefaultSecretsStorage(QObject *parent=0)
bool removeData(quint32 id, quint32 method)
bool removeCredentials(const quint32 id)
QVariantMap loadData(quint32 id, quint32 method)
bool storeData(quint32 id, quint32 method, const QVariantMap &data)
bool storeData(quint32 id, quint32 method, const QVariantMap &data)
#define SSO_MAX_TOKEN_STORAGE
SignOn::CredentialsDBError lastError() const
bool initialize(const QVariantMap &configuration)
bool removeCredentials(const quint32 id)
bool init()
Connects to the DB and if necessary creates the tables.
bool errorOccurred() const
QVariantMap loadData(quint32 id, quint32 method)
bool loadCredentials(const quint32 id, QString &username, QString &password)
bool transactionalExec(const QStringList &queryList)
Executes a specific database set of queryes (INSERTs, UPDATEs, DELETEs) in a transaction context (No ...
QSqlQuery newQuery() const
bool loadCredentials(const quint32 id, QString &username, QString &password)
QString connectionName() const
#define RETURN_IF_NOT_OPEN(retval)