File indexing completed on 2024-05-12 17:16:02

0001 /***************************************************************************
0002  *   Copyright (C) 2006-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 #include "pwstorage.h"
0021 #include "kdesvn-config.h"
0022 #include "settings/kdesvnsettings.h"
0023 
0024 #include <KWallet>
0025 
0026 #include <QApplication>
0027 #include <QWidget>
0028 #include <QMutex>
0029 #include <QMap>
0030 #include <QPair>
0031 
0032 class PwStorageData
0033 {
0034 public:
0035 
0036     PwStorageData()
0037     {
0038         m_Wallet = nullptr;
0039     }
0040 
0041     ~PwStorageData()
0042     {
0043         delete m_Wallet;
0044         m_Wallet = nullptr;
0045     }
0046 
0047     KWallet::Wallet *getWallet();
0048 
0049     typedef QPair<QString, QString> userpw_type;
0050     typedef QMap<QString, userpw_type> cache_type;
0051 
0052     cache_type *getLoginCache();
0053 
0054     QMutex *getCacheMutex();
0055 
0056 protected:
0057     KWallet::Wallet *m_Wallet;
0058 
0059 };
0060 
0061 QMutex *PwStorageData::getCacheMutex()
0062 {
0063     static QMutex _mutex;
0064     return &_mutex;
0065 }
0066 
0067 PwStorageData::cache_type *PwStorageData::getLoginCache()
0068 {
0069     static PwStorageData::cache_type _LoginCache;
0070     return &_LoginCache;
0071 }
0072 
0073 KWallet::Wallet *PwStorageData::getWallet()
0074 {
0075     if ((m_Wallet && m_Wallet->isOpen()) || !qApp) {
0076         return m_Wallet;
0077     }
0078     if (KWallet::Wallet::isEnabled()) {
0079         WId window = 0;
0080         if (QApplication::activeModalWidget()) {
0081             window = QApplication::activeModalWidget()->winId();
0082         } else if (QApplication::activeWindow()) {
0083             window = QApplication::activeWindow()->winId();
0084         }
0085         delete m_Wallet;
0086         m_Wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), window);
0087     }
0088     if (m_Wallet) {
0089         if (!m_Wallet->hasFolder(QStringLiteral(WALLETNAME))) {
0090             m_Wallet->createFolder(QStringLiteral(WALLETNAME));
0091         }
0092         m_Wallet->setFolder(QStringLiteral(WALLETNAME));
0093     }
0094     return m_Wallet;
0095 }
0096 
0097 PwStorage *PwStorage::self()
0098 {
0099     static PwStorage *_me = nullptr;
0100     if (!_me) {
0101         _me = new PwStorage();
0102     }
0103     return _me;
0104 }
0105 
0106 /*!
0107     \fn PwStorage::PwStorageData()
0108  */
0109 PwStorage::PwStorage()
0110     : mData(new PwStorageData)
0111 {}
0112 
0113 /*!
0114     \fn PwStorage::~PwStorageData()
0115  */
0116 PwStorage::~PwStorage()
0117 {
0118     delete mData;
0119 }
0120 
0121 /*!
0122     \fn PwStorage::connectWallet()
0123  */
0124 bool PwStorage::connectWallet()
0125 {
0126     return mData->getWallet() != nullptr;
0127 }
0128 
0129 /*!
0130     \fn PwStorage::getCertPw(const QString&realm,QString&pw)
0131  */
0132 bool PwStorage::getCertPw(const QString &realm, QString &pw)
0133 {
0134     if (!mData->getWallet()) {
0135         return false;
0136     }
0137     return (mData->getWallet()->readPassword(realm, pw) == 0);
0138 }
0139 
0140 /*!
0141     \fn PwStorage::getLogin(const QString&realm,QString&user,QString&pw)
0142  */
0143 bool PwStorage::getLogin(const QString &realm, QString &user, QString &pw)
0144 {
0145     if (!mData->getWallet()) {
0146         return false;
0147     }
0148     QMap<QString, QString> content;
0149     int j = mData->getWallet()->readMap(realm, content);
0150     if (j != 0 || !content.contains(QStringLiteral("user"))) {
0151         return true;
0152     }
0153     user = content[QStringLiteral("user")];
0154     pw = content[QStringLiteral("password")];
0155     return true;
0156 }
0157 
0158 bool PwStorage::getCachedLogin(const QString &realm, QString &user, QString &pw)
0159 {
0160     QMutexLocker lc(mData->getCacheMutex());
0161     PwStorageData::cache_type::ConstIterator it = mData->getLoginCache()->constFind(realm);
0162     if (it != mData->getLoginCache()->constEnd()) {
0163         user = (*it).first;
0164         pw = (*it).second;
0165     }
0166     return true;
0167 }
0168 
0169 /*!
0170     \fn PwStorage::setCertPw(const QString&realm, const QString&pw)
0171  */
0172 bool PwStorage::setCertPw(const QString &realm, const QString &pw)
0173 {
0174     if (!mData->getWallet()) {
0175         return false;
0176     }
0177     return (mData->getWallet()->writePassword(realm, pw) == 0);
0178 }
0179 
0180 /*!
0181     \fn PwStorage::setLogin(const QString&realm,const QString&user,const QString&pw)
0182  */
0183 bool PwStorage::setLogin(const QString &realm, const QString &user, const QString &pw)
0184 {
0185     if (!mData->getWallet()) {
0186         return false;
0187     }
0188     QMap<QString, QString> content;
0189     content[QStringLiteral("user")] = user;
0190     content[QStringLiteral("password")] = pw;
0191     return (mData->getWallet()->writeMap(realm, content) == 0);
0192 }
0193 
0194 bool PwStorage::setCachedLogin(const QString &realm, const QString &user, const QString &pw)
0195 {
0196     QMutexLocker lc(mData->getCacheMutex());
0197     PwStorageData::cache_type *_Cache = mData->getLoginCache();
0198     (*_Cache)[realm] = PwStorageData::userpw_type(user, pw);
0199     return true;
0200 }