File indexing completed on 2024-05-05 05:44:50

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 <QMap>
0028 #include <QMutex>
0029 #include <QPair>
0030 #include <QWidget>
0031 
0032 class PwStorageData
0033 {
0034 public:
0035     PwStorageData()
0036     {
0037         m_Wallet = nullptr;
0038     }
0039 
0040     ~PwStorageData()
0041     {
0042         delete m_Wallet;
0043         m_Wallet = nullptr;
0044     }
0045 
0046     KWallet::Wallet *getWallet();
0047 
0048     typedef QPair<QString, QString> userpw_type;
0049     typedef QMap<QString, userpw_type> cache_type;
0050 
0051     cache_type *getLoginCache();
0052 
0053     QMutex *getCacheMutex();
0054 
0055 protected:
0056     KWallet::Wallet *m_Wallet;
0057 };
0058 
0059 QMutex *PwStorageData::getCacheMutex()
0060 {
0061     static QMutex _mutex;
0062     return &_mutex;
0063 }
0064 
0065 PwStorageData::cache_type *PwStorageData::getLoginCache()
0066 {
0067     static PwStorageData::cache_type _LoginCache;
0068     return &_LoginCache;
0069 }
0070 
0071 KWallet::Wallet *PwStorageData::getWallet()
0072 {
0073     if ((m_Wallet && m_Wallet->isOpen()) || !qApp) {
0074         return m_Wallet;
0075     }
0076     if (KWallet::Wallet::isEnabled()) {
0077         WId window = 0;
0078         if (QApplication::activeModalWidget()) {
0079             window = QApplication::activeModalWidget()->winId();
0080         } else if (QApplication::activeWindow()) {
0081             window = QApplication::activeWindow()->winId();
0082         }
0083         delete m_Wallet;
0084         m_Wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), window);
0085     }
0086     if (m_Wallet) {
0087         if (!m_Wallet->hasFolder(QStringLiteral(WALLETNAME))) {
0088             m_Wallet->createFolder(QStringLiteral(WALLETNAME));
0089         }
0090         m_Wallet->setFolder(QStringLiteral(WALLETNAME));
0091     }
0092     return m_Wallet;
0093 }
0094 
0095 PwStorage *PwStorage::self()
0096 {
0097     static PwStorage *_me = nullptr;
0098     if (!_me) {
0099         _me = new PwStorage();
0100     }
0101     return _me;
0102 }
0103 
0104 /*!
0105     \fn PwStorage::PwStorageData()
0106  */
0107 PwStorage::PwStorage()
0108     : mData(new PwStorageData)
0109 {
0110 }
0111 
0112 /*!
0113     \fn PwStorage::~PwStorageData()
0114  */
0115 PwStorage::~PwStorage()
0116 {
0117     delete mData;
0118 }
0119 
0120 /*!
0121     \fn PwStorage::connectWallet()
0122  */
0123 bool PwStorage::connectWallet()
0124 {
0125     return mData->getWallet() != nullptr;
0126 }
0127 
0128 /*!
0129     \fn PwStorage::getCertPw(const QString&realm,QString&pw)
0130  */
0131 bool PwStorage::getCertPw(const QString &realm, QString &pw)
0132 {
0133     if (!mData->getWallet()) {
0134         return false;
0135     }
0136     return (mData->getWallet()->readPassword(realm, pw) == 0);
0137 }
0138 
0139 /*!
0140     \fn PwStorage::getLogin(const QString&realm,QString&user,QString&pw)
0141  */
0142 bool PwStorage::getLogin(const QString &realm, QString &user, QString &pw)
0143 {
0144     if (!mData->getWallet()) {
0145         return false;
0146     }
0147     QMap<QString, QString> content;
0148     int j = mData->getWallet()->readMap(realm, content);
0149     if (j != 0 || !content.contains(QStringLiteral("user"))) {
0150         return true;
0151     }
0152     user = content[QStringLiteral("user")];
0153     pw = content[QStringLiteral("password")];
0154     return true;
0155 }
0156 
0157 bool PwStorage::getCachedLogin(const QString &realm, QString &user, QString &pw)
0158 {
0159     QMutexLocker lc(mData->getCacheMutex());
0160     PwStorageData::cache_type::ConstIterator it = mData->getLoginCache()->constFind(realm);
0161     if (it != mData->getLoginCache()->constEnd()) {
0162         user = (*it).first;
0163         pw = (*it).second;
0164     }
0165     return true;
0166 }
0167 
0168 /*!
0169     \fn PwStorage::setCertPw(const QString&realm, const QString&pw)
0170  */
0171 bool PwStorage::setCertPw(const QString &realm, const QString &pw)
0172 {
0173     if (!mData->getWallet()) {
0174         return false;
0175     }
0176     return (mData->getWallet()->writePassword(realm, pw) == 0);
0177 }
0178 
0179 /*!
0180     \fn PwStorage::setLogin(const QString&realm,const QString&user,const QString&pw)
0181  */
0182 bool PwStorage::setLogin(const QString &realm, const QString &user, const QString &pw)
0183 {
0184     if (!mData->getWallet()) {
0185         return false;
0186     }
0187     QMap<QString, QString> content;
0188     content[QStringLiteral("user")] = user;
0189     content[QStringLiteral("password")] = pw;
0190     return (mData->getWallet()->writeMap(realm, content) == 0);
0191 }
0192 
0193 bool PwStorage::setCachedLogin(const QString &realm, const QString &user, const QString &pw)
0194 {
0195     QMutexLocker lc(mData->getCacheMutex());
0196     PwStorageData::cache_type *_Cache = mData->getLoginCache();
0197     (*_Cache)[realm] = PwStorageData::userpw_type(user, pw);
0198     return true;
0199 }