This repository was archived by the owner on Sep 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Port to QtKeychain #66
Open
nicolasfella
wants to merge
1
commit into
KDAB:master
Choose a base branch
from
nicolasfella:work/qtkeychain
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,79 +22,65 @@ | |
| #include "sugarcrmresource_debug.h" | ||
| #include <settings.h> | ||
|
|
||
| #if USE_KWALLET | ||
| #include <KWallet/KWallet> | ||
| using KWallet::Wallet; | ||
| #include <qt5keychain/keychain.h> | ||
|
|
||
| static const char s_walletFolderName[] = "SugarCRM"; | ||
| #endif | ||
| static const char s_keychainFolderName[] = "SugarCRM"; | ||
|
|
||
| // Implementation inspired from kdepim-runtime/resources/imap/settings.cpp branch KDE/4.14 | ||
|
|
||
| PasswordHandler::PasswordHandler(const QString &resourceId, QObject *parent) : | ||
| QObject(parent), | ||
| mResourceId(resourceId) | ||
| { | ||
| #if USE_KWALLET | ||
| // We have no GUI to use on startup | ||
| // We could at least use the config dialog though. | ||
| m_winId = 0; | ||
| mWalletOpened = false; | ||
|
|
||
| Wallet *wallet = Wallet::openWallet( Wallet::NetworkWallet(), m_winId, Wallet::Asynchronous ); | ||
| if (wallet) { | ||
| connect(wallet, SIGNAL(walletOpened(bool)), | ||
| this, SLOT(onWalletOpened(bool))); | ||
| } else { | ||
| qCWarning(FATCRM_SUGARCRMRESOURCE_LOG) << "openWallet(Asynchronous) failed!"; | ||
| } | ||
| #endif | ||
| auto *job = new QKeychain::ReadPasswordJob(s_keychainFolderName); | ||
|
|
||
| job->setKey(resourceId); | ||
|
|
||
| connect(job, &QKeychain::Job::finished, this, [this, job] { | ||
| if (job->error()) { | ||
| qWarning() << "Error reading password for" << mResourceId << job->errorString(); | ||
|
|
||
| if (job->error() == QKeychain::AccessDeniedByUser) { | ||
| mDeniedByUser = true; | ||
| } | ||
| } else { | ||
| mPassword = job->textData(); | ||
| mKeychainOpened = true; | ||
| Q_EMIT passwordAvailable(); | ||
| } | ||
| }); | ||
|
|
||
| job->start(); | ||
| } | ||
|
|
||
| bool PasswordHandler::isPasswordAvailable() | ||
| { | ||
| #if USE_KWALLET | ||
| if (!mWalletOpened) | ||
| return false; | ||
| QScopedPointer<Wallet> wallet(Wallet::openWallet(Wallet::NetworkWallet(), m_winId)); | ||
| return wallet && wallet->isOpen(); | ||
| #else | ||
| return true; | ||
| #endif | ||
| return mKeychainOpened; | ||
| } | ||
|
|
||
| QString PasswordHandler::password(bool *userRejected) | ||
| { | ||
| if (userRejected != nullptr) { | ||
| *userRejected = false; | ||
| } | ||
| *userRejected = mDeniedByUser; | ||
|
|
||
| if (!mPassword.isEmpty()) | ||
| return mPassword; | ||
| #if USE_KWALLET | ||
| QScopedPointer<Wallet> wallet(Wallet::openWallet(Wallet::NetworkWallet(), m_winId)); | ||
| if (wallet && wallet->isOpen()) { | ||
| if (wallet->hasFolder(QString(s_walletFolderName))) { | ||
| wallet->setFolder(QString(s_walletFolderName)); | ||
| wallet->readPassword(mResourceId, mPassword); | ||
| } else { | ||
| wallet->createFolder(QString(s_walletFolderName)); | ||
| if (mDeniedByUser) { | ||
| return QString(); | ||
| } | ||
| // Initial migration: password not in wallet yet | ||
| if (mPassword.isEmpty()) { | ||
| mPassword = Settings::password(); | ||
| if (!mPassword.isEmpty()) { | ||
| savePassword(); | ||
| } | ||
| } | ||
|
|
||
| if (!mKeychainOpened) { | ||
| return QString(); | ||
| } | ||
|
|
||
| // Initial migration: password not in keychain yet | ||
| if (mPassword.isEmpty()) { | ||
| mPassword = Settings::password(); | ||
| if (!mPassword.isEmpty()) { | ||
| savePassword(); | ||
| } | ||
| } else if (userRejected != nullptr) { | ||
| *userRejected = true; | ||
| } | ||
| #else | ||
| mPassword = Settings::password(); | ||
| #endif | ||
| return mPassword; | ||
|
|
||
| return mPassword; | ||
| } | ||
|
|
||
| void PasswordHandler::setPassword(const QString &password) | ||
|
|
@@ -103,47 +89,25 @@ void PasswordHandler::setPassword(const QString &password) | |
| return; | ||
|
|
||
| mPassword = password; | ||
|
|
||
| #if USE_KWALLET | ||
| savePassword(); | ||
| #else | ||
| Settings::setPassword(mPassword); | ||
| Settings::self()->writeConfig(); | ||
| #endif | ||
| } | ||
|
|
||
| void PasswordHandler::onWalletOpened(bool success) | ||
| { | ||
| #if USE_KWALLET | ||
| Wallet *wallet = qobject_cast<Wallet*>( sender() ); | ||
| mWalletOpened = success; | ||
| if (wallet && success) { | ||
| // read and store the password | ||
| password(); | ||
| emit passwordAvailable(); | ||
| } | ||
| if (wallet) { | ||
| wallet->deleteLater(); | ||
| } | ||
| #else | ||
| Q_UNUSED(success); | ||
| #endif | ||
| } | ||
|
|
||
| #if USE_KWALLET | ||
| bool PasswordHandler::savePassword() | ||
| { | ||
| QScopedPointer<Wallet> wallet(Wallet::openWallet(Wallet::NetworkWallet(), m_winId)); | ||
| if (wallet && wallet->isOpen()) { | ||
| if (!wallet->hasFolder(QString(s_walletFolderName))) | ||
| wallet->createFolder(QString(s_walletFolderName)); | ||
| wallet->setFolder(QString(s_walletFolderName)); | ||
| wallet->writePassword(mResourceId, mPassword); | ||
| wallet->sync(); | ||
| Settings::setPassword(QString()); // ensure no plain-text password from before in the config file | ||
| Settings::self()->save(); | ||
| return true; | ||
| } | ||
| return false; | ||
| auto *job = new QKeychain::WritePasswordJob(s_keychainFolderName); | ||
| job->setKey(mResourceId); | ||
| job->setTextData(mPassword); | ||
|
|
||
| connect(job, &QKeychain::Job::finished, this, [this, job] { | ||
| if (job->error()) { | ||
| qWarning() << "password save error" << job->errorString(); | ||
| } | ||
| }); | ||
|
|
||
| job->start(); | ||
|
|
||
| Settings::setPassword(QString()); // ensure no plain-text password from before in the config file | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be kept in the case the write job fails? I'd say security over keeping the password at all costs though. |
||
| Settings::self()->save(); | ||
|
|
||
| return true; | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should there be a mDeniedByUser = false; here?