Warning, file /network/signon-kwallet-extension/src/secretsstorage.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*************************************************************************************
0002  *  Copyright (C) 2013 by Alejandro Fiestas Olivares <afiestas@kde.org>              *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #include "secretsstorage.h"
0020 
0021 #include <QDataStream>
0022 #include <QDebug>
0023 
0024 #include <SignOn/AbstractSecretsStorage>
0025 
0026 #include <KWallet>
0027 
0028 using namespace SignOn;
0029 using namespace KWallet;
0030 
0031 SecretsStorage::SecretsStorage(QObject *parent)
0032  : AbstractSecretsStorage(parent)
0033  , m_keyringName()
0034  , m_wallet(nullptr)
0035 {
0036 }
0037 
0038 SecretsStorage::~SecretsStorage()
0039 {
0040 }
0041 
0042 bool SecretsStorage::initialize(const QVariantMap &configuration)
0043 {
0044     m_wallet = Wallet::openWallet(Wallet::NetworkWallet(), 0, Wallet::Synchronous);
0045 
0046     if (!m_wallet || !m_wallet->isOpen() || !m_wallet->isEnabled()) {
0047         qWarning("Wallet can't be accessed");
0048         return false;
0049     }
0050 
0051     if (!m_wallet->hasFolder(QStringLiteral("accounts"))) {
0052         m_wallet->createFolder(QStringLiteral("accounts"));
0053     }
0054 
0055     m_wallet->setFolder(QStringLiteral("accounts"));
0056     setIsOpen(true);
0057     return true;
0058 }
0059 
0060 bool SecretsStorage::close()
0061 {
0062     if (m_wallet->closeWallet(Wallet::NetworkWallet(), false) == 0) {
0063         setIsOpen(false);
0064         return true;
0065     }
0066 
0067     return false;
0068 }
0069 
0070 
0071 bool SecretsStorage::clear()
0072 {
0073     m_wallet->removeFolder(QStringLiteral("accounts"));
0074     return true;
0075 }
0076 
0077 bool SecretsStorage::updateCredentials(const quint32 id,
0078                                        const QString &username,
0079                                        const QString &password)
0080 {
0081     QString sId = QString::number(id);
0082     if (username.isEmpty() && password.isEmpty()) {
0083         return false;//Nothing to do
0084     }
0085 
0086     QMap<QString, QString> map;
0087     if (m_wallet->readMap(sId, map) != 0) {
0088         return false;
0089     }
0090 
0091     if (!username.isEmpty()) {
0092         map[QStringLiteral("username")] = username;
0093     }
0094 
0095     if (!password.isEmpty()) {
0096         map[QStringLiteral("password")] = password;
0097     }
0098 
0099     return m_wallet->writeMap(sId, map) == 0;
0100 }
0101 
0102 bool SecretsStorage::removeCredentials(const quint32 id)
0103 {
0104     return m_wallet->removeEntry(QString::number(id)) == 0;
0105 }
0106 
0107 bool SecretsStorage::loadCredentials(const quint32 id,
0108                                      QString &username,
0109                                      QString &password)
0110 {
0111     QString sId = QString::number(id);
0112     if (!m_wallet->hasEntry(sId)) {
0113         return false;
0114     }
0115 
0116     QMap<QString, QString> map;
0117     if (m_wallet->readMap(sId, map) != 0) {
0118         return false;
0119     }
0120 
0121     username = map[QStringLiteral("username")];
0122     password = map[QStringLiteral("password")];
0123 
0124     return true;
0125 }
0126 
0127 QVariantMap SecretsStorage::loadData(quint32 id, quint32 method)
0128 {
0129     QString sId = QString::number(id);
0130     sId.append(QLatin1Char('/') + QString::number(method));
0131 
0132     QVariantMap data;
0133     if (!m_wallet->hasEntry(sId)) {
0134         return data;
0135     }
0136 
0137     QByteArray binaryData;
0138     if (m_wallet->readEntry(sId, binaryData) != 0) {
0139         return data;
0140     }
0141 
0142     QDataStream stream(binaryData);
0143     stream >> data;
0144     return data;
0145 }
0146 
0147 bool SecretsStorage::storeData(quint32 id, quint32 method,
0148                                const QVariantMap &data)
0149 {
0150     QString sId = QString::number(id);
0151     sId.append(QLatin1Char('/') + QString::number(method));
0152 
0153     QByteArray serializedData;
0154     QDataStream stream(&serializedData, QIODevice::WriteOnly);
0155     stream << data;
0156 
0157 
0158     return m_wallet->writeEntry(sId, serializedData) == 0;
0159 }
0160 
0161 bool SecretsStorage::removeData(quint32 id, quint32 method)
0162 {
0163     QString sId = QString::number(id);
0164     sId.append(QLatin1Char('/') + QString::number(method));
0165 
0166     return m_wallet->removeEntry(sId) == 0;
0167 }