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.";
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)
287 TRACE() <<
"Initializing open DB; closing first...";
291 QString name = configuration.value(QLatin1String(
"name")).toString();
294 if (!m_secretsDB->
init()) {
307 if (m_secretsDB != 0) {
310 QSqlDatabase::removeDatabase(connectionName);
320 return m_secretsDB->
clear();
324 const QString &username,
325 const QString &password)
352 return m_secretsDB->
loadData(
id, method);
356 const QVariantMap &data)
360 return m_secretsDB->
storeData(
id, method, data);