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)
37 using namespace SignonDaemonNS;
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");
89 const QString &username,
90 const QString &password)
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.";
185 qint32 dataCounter = 0;
186 if (!(data.keys().empty())) {
187 QMapIterator<QString, QVariant> it(data);
188 while (it.hasNext()) {
192 QDataStream stream(&array, QIODevice::WriteOnly);
193 stream << it.value();
195 dataCounter += it.key().size() +array.size();
197 BLAME() <<
"storing data max size exceeded";
203 if (it.value().isValid() && !it.value().isNull()) {
206 "INSERT OR REPLACE INTO STORE "
207 "(identity_id, method_id, key, value) "
208 "VALUES(:id, :method, :key, :value)"));
209 query.bindValue(
S(
":value"), array);
213 "DELETE FROM STORE WHERE identity_id = :id "
214 "AND method_id = :method "
218 query.bindValue(
S(
":id"),
id);
219 query.bindValue(
S(
":method"), method);
220 query.bindValue(
S(
":key"), it.key());
230 TRACE() <<
"Data insertion ok.";
234 TRACE() <<
"Data insertion failed.";
243 TRACE() <<
"Could not start transaction. Error removing data.";
249 q.prepare(
S(
"DELETE FROM STORE WHERE identity_id = :id"));
251 q.prepare(
S(
"DELETE FROM STORE WHERE identity_id = :id "
252 "AND method_id = :method"));
253 q.bindValue(
S(
":method"), method);
255 q.bindValue(
S(
":id"),
id);
258 TRACE() <<
"Data removal ok.";
262 TRACE() <<
"Data removal failed.";
268 AbstractSecretsStorage(parent)
280 TRACE() <<
"Initializing open DB; closing first...";
284 QString name = configuration.value(QLatin1String(
"name")).toString();
287 if (!m_secretsDB->
init()) {
300 if (m_secretsDB != 0) {
303 QSqlDatabase::removeDatabase(connectionName);
313 return m_secretsDB->
clear();
317 const QString &username,
318 const QString &password)
345 return m_secretsDB->
loadData(
id, method);
349 const QVariantMap &data)
353 return m_secretsDB->
storeData(
id, method, data);