mirror of
https://github.com/nextcloud/desktop.git
synced 2024-11-24 05:55:59 +03:00
Cleaned up credentialstore class a bit.
Removed all "user mode" stuff from the class that was used to ask the user interactively for the password. We do not do that any more.
This commit is contained in:
parent
7ae95b14f4
commit
40ab325a37
3 changed files with 15 additions and 101 deletions
|
@ -136,16 +136,9 @@ void ConnectionValidator::slotNoStatusFound(QNetworkReply *reply)
|
||||||
void ConnectionValidator::slotFetchCredentials()
|
void ConnectionValidator::slotFetchCredentials()
|
||||||
{
|
{
|
||||||
if( _connection.isEmpty() ) {
|
if( _connection.isEmpty() ) {
|
||||||
if( CredentialStore::instance()->canTryAgain() ) {
|
|
||||||
connect( CredentialStore::instance(), SIGNAL(fetchCredentialsFinished(bool)),
|
connect( CredentialStore::instance(), SIGNAL(fetchCredentialsFinished(bool)),
|
||||||
this, SLOT(slotCredentialsFetched(bool)) );
|
this, SLOT(slotCredentialsFetched(bool)) );
|
||||||
CredentialStore::instance()->fetchCredentials();
|
CredentialStore::instance()->fetchCredentials();
|
||||||
}
|
|
||||||
|
|
||||||
if( CredentialStore::instance()->state() == CredentialStore::TooManyAttempts ) {
|
|
||||||
_errors << tr("Too many attempts to get a valid password.");
|
|
||||||
emit connectionResult( CredentialsTooManyAttempts );
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Pull credentials from Mirall config.
|
// Pull credentials from Mirall config.
|
||||||
slotCredentialsFetched( true );
|
slotCredentialsFetched( true );
|
||||||
|
@ -160,14 +153,8 @@ void ConnectionValidator::slotCredentialsFetched( bool ok )
|
||||||
if( ! ok ) {
|
if( ! ok ) {
|
||||||
Status stat;
|
Status stat;
|
||||||
_errors << tr("Error: Could not retrieve the password!");
|
_errors << tr("Error: Could not retrieve the password!");
|
||||||
|
|
||||||
if( CredentialStore::instance()->state() == CredentialStore::UserCanceled ) {
|
|
||||||
_errors << tr("Password dialog was canceled!");
|
|
||||||
stat = CredentialsUserCanceled;
|
|
||||||
} else {
|
|
||||||
_errors << CredentialStore::instance()->errorMessage();
|
_errors << CredentialStore::instance()->errorMessage();
|
||||||
stat = CredentialError;
|
stat = CredentialError;
|
||||||
}
|
|
||||||
|
|
||||||
qDebug() << "Could not fetch credentials" << _errors;
|
qDebug() << "Could not fetch credentials" << _errors;
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,6 @@ QString CredentialStore::_passwd = QString::null;
|
||||||
QString CredentialStore::_user = QString::null;
|
QString CredentialStore::_user = QString::null;
|
||||||
QString CredentialStore::_url = QString::null;
|
QString CredentialStore::_url = QString::null;
|
||||||
QString CredentialStore::_errorMsg = QString::null;
|
QString CredentialStore::_errorMsg = QString::null;
|
||||||
int CredentialStore::_tries = 0;
|
|
||||||
#ifdef WITH_QTKEYCHAIN
|
#ifdef WITH_QTKEYCHAIN
|
||||||
CredentialStore::CredentialType CredentialStore::_type = KeyChain;
|
CredentialStore::CredentialType CredentialStore::_type = KeyChain;
|
||||||
#else
|
#else
|
||||||
|
@ -67,39 +66,14 @@ CredentialStore::CredState CredentialStore::state()
|
||||||
return _state;
|
return _state;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CredentialStore::canTryAgain()
|
|
||||||
{
|
|
||||||
if( _tries > MAX_LOGIN_ATTEMPTS ) {
|
|
||||||
qDebug() << "canTryAgain: Max attempts reached.";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Since QtKeyChain is required now, it makes sense to only query once. */
|
|
||||||
if( _state == NotFetched || _state == AsyncWriting ) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void CredentialStore::fetchCredentials()
|
void CredentialStore::fetchCredentials()
|
||||||
{
|
{
|
||||||
MirallConfigFile cfgFile;
|
MirallConfigFile cfgFile;
|
||||||
if( ++_tries > MAX_LOGIN_ATTEMPTS ) {
|
|
||||||
qDebug() << "Too many attempts to enter password!";
|
|
||||||
_state = TooManyAttempts;
|
|
||||||
emit( fetchCredentialsFinished(false) );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
QString pwd;
|
QString pwd;
|
||||||
_user = cfgFile.ownCloudUser();
|
_user = cfgFile.ownCloudUser();
|
||||||
_url = cfgFile.ownCloudUrl();
|
_url = cfgFile.ownCloudUrl();
|
||||||
if( !cfgFile.passwordStorageAllowed() ) {
|
|
||||||
_type = CredentialStore::User;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString key = keyChainKey(_url);
|
QString key = keyChainKey(_url);
|
||||||
|
|
||||||
|
@ -111,26 +85,16 @@ void CredentialStore::fetchCredentials()
|
||||||
}
|
}
|
||||||
|
|
||||||
switch( _type ) {
|
switch( _type ) {
|
||||||
case CredentialStore::User: {
|
|
||||||
/* Ask the user for the password */
|
|
||||||
/* Fixme: Move user interaction out here. */
|
|
||||||
_state = AsyncFetching;
|
|
||||||
_inputDialog = new QInputDialog;
|
|
||||||
_inputDialog->setWindowTitle(QApplication::translate("MirallConfigFile","Password Required") );
|
|
||||||
_inputDialog->setLabelText( QApplication::translate("MirallConfigFile","Please enter your %1 password:")
|
|
||||||
.arg(Theme::instance()->appNameGUI()));
|
|
||||||
_inputDialog->setInputMode( QInputDialog::TextInput );
|
|
||||||
_inputDialog->setTextEchoMode( QLineEdit::Password );
|
|
||||||
|
|
||||||
connect(_inputDialog, SIGNAL(finished(int)), SLOT(slotUserDialogDone(int)));
|
|
||||||
_inputDialog->open();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case CredentialStore::Settings: {
|
case CredentialStore::Settings: {
|
||||||
/* Read from config file. */
|
/* Read from config file. */
|
||||||
_state = Fetching;
|
_state = Fetching;
|
||||||
|
if( cfgFile.ownCloudPasswordExists() ) {
|
||||||
pwd = cfgFile.ownCloudPasswd();
|
pwd = cfgFile.ownCloudPasswd();
|
||||||
ok = true;
|
ok = true;
|
||||||
|
} else {
|
||||||
|
ok = false;
|
||||||
|
_state = EntryNotFound;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case CredentialStore::KeyChain: {
|
case CredentialStore::KeyChain: {
|
||||||
|
@ -177,25 +141,11 @@ void CredentialStore::fetchCredentials()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CredentialStore::slotUserDialogDone( int result )
|
|
||||||
{
|
|
||||||
if( result == QDialog::Accepted ) {
|
|
||||||
_passwd = _inputDialog->textValue();
|
|
||||||
_state = Ok;
|
|
||||||
} else {
|
|
||||||
_state = UserCanceled;
|
|
||||||
_passwd = QString::null;
|
|
||||||
}
|
|
||||||
_inputDialog->deleteLater();
|
|
||||||
emit(fetchCredentialsFinished(_state == Ok));
|
|
||||||
}
|
|
||||||
|
|
||||||
void CredentialStore::reset()
|
void CredentialStore::reset()
|
||||||
{
|
{
|
||||||
_state = NotFetched;
|
_state = NotFetched;
|
||||||
_user = QString::null;
|
_user = QString::null;
|
||||||
_passwd = QString::null;
|
_passwd = QString::null;
|
||||||
_tries = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString CredentialStore::keyChainKey( const QString& url ) const
|
QString CredentialStore::keyChainKey( const QString& url ) const
|
||||||
|
@ -242,9 +192,6 @@ void CredentialStore::slotKeyChainReadFinished(QKeychain::Job* job)
|
||||||
case QKeychain::CouldNotDeleteEntry:
|
case QKeychain::CouldNotDeleteEntry:
|
||||||
_state = Error;
|
_state = Error;
|
||||||
break;
|
break;
|
||||||
case QKeychain::AccessDeniedByUser:
|
|
||||||
_state = AccessDeniedByUser;
|
|
||||||
break;
|
|
||||||
case QKeychain::AccessDenied:
|
case QKeychain::AccessDenied:
|
||||||
_state = AccessDenied;
|
_state = AccessDenied;
|
||||||
break;
|
break;
|
||||||
|
@ -303,8 +250,6 @@ void CredentialStore::setCredentials( const QString& url, const QString& user,
|
||||||
#else
|
#else
|
||||||
_type = Settings;
|
_type = Settings;
|
||||||
#endif
|
#endif
|
||||||
} else {
|
|
||||||
_type = User;
|
|
||||||
}
|
}
|
||||||
_url = url;
|
_url = url;
|
||||||
_state = Ok;
|
_state = Ok;
|
||||||
|
@ -323,9 +268,6 @@ void CredentialStore::saveCredentials( )
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
switch( _type ) {
|
switch( _type ) {
|
||||||
case CredentialStore::User:
|
|
||||||
deleteKeyChainCredential( key );
|
|
||||||
break;
|
|
||||||
case CredentialStore::KeyChain:
|
case CredentialStore::KeyChain:
|
||||||
#ifdef WITH_QTKEYCHAIN
|
#ifdef WITH_QTKEYCHAIN
|
||||||
// Set password in KeyChain
|
// Set password in KeyChain
|
||||||
|
@ -372,7 +314,6 @@ void CredentialStore::slotKeyChainWriteFinished( QKeychain::Job *job )
|
||||||
MirallConfigFile cfgFile;
|
MirallConfigFile cfgFile;
|
||||||
cfgFile.clearPasswordFromConfig();
|
cfgFile.clearPasswordFromConfig();
|
||||||
_state = NotFetched;
|
_state = NotFetched;
|
||||||
_tries = 0;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qDebug() << "Error: KeyChain Write Password Job failed!";
|
qDebug() << "Error: KeyChain Write Password Job failed!";
|
||||||
|
|
|
@ -34,10 +34,8 @@ namespace Mirall {
|
||||||
* The fetchCredentials() call changes the internal state of the credential store
|
* The fetchCredentials() call changes the internal state of the credential store
|
||||||
* to one of
|
* to one of
|
||||||
* Ok: There are credentials. Note that it's unknown if they are correct!!
|
* Ok: There are credentials. Note that it's unknown if they are correct!!
|
||||||
* UserCanceled: The fetching involved user interaction and the user canceled
|
|
||||||
* the operation. No valid credentials are there.
|
|
||||||
* TooManyAttempts: The user tried to often to enter a password.
|
|
||||||
* Fetching: The fetching is not yet finished.
|
* Fetching: The fetching is not yet finished.
|
||||||
|
* EntryNotFound: No password entry found in the storage.
|
||||||
* Error: A general error happened.
|
* Error: A general error happened.
|
||||||
* After fetching has finished, signal fetchCredentialsFinished(bool) is emitted.
|
* After fetching has finished, signal fetchCredentialsFinished(bool) is emitted.
|
||||||
* The result can be retrieved with state() and password() and user() methods.
|
* The result can be retrieved with state() and password() and user() methods.
|
||||||
|
@ -49,20 +47,16 @@ class CredentialStore : public QObject
|
||||||
public:
|
public:
|
||||||
enum CredState { NotFetched = 0,
|
enum CredState { NotFetched = 0,
|
||||||
Ok,
|
Ok,
|
||||||
UserCanceled,
|
|
||||||
Fetching,
|
Fetching,
|
||||||
AsyncFetching,
|
AsyncFetching,
|
||||||
EntryNotFound,
|
EntryNotFound,
|
||||||
AccessDeniedByUser,
|
|
||||||
AccessDenied,
|
AccessDenied,
|
||||||
NoKeychainBackend,
|
NoKeychainBackend,
|
||||||
Error,
|
Error,
|
||||||
AsyncWriting,
|
AsyncWriting };
|
||||||
TooManyAttempts };
|
|
||||||
|
|
||||||
enum CredentialType {
|
enum CredentialType {
|
||||||
User = 0,
|
Settings = 0,
|
||||||
Settings,
|
|
||||||
KeyChain
|
KeyChain
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -105,11 +99,6 @@ public:
|
||||||
|
|
||||||
QString errorMessage();
|
QString errorMessage();
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief canTryAgain - check if another try to get credentials makes sense.
|
|
||||||
*/
|
|
||||||
bool canTryAgain();
|
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
signals:
|
signals:
|
||||||
/**
|
/**
|
||||||
|
@ -125,7 +114,6 @@ signals:
|
||||||
protected slots:
|
protected slots:
|
||||||
void slotKeyChainReadFinished( QKeychain::Job* );
|
void slotKeyChainReadFinished( QKeychain::Job* );
|
||||||
void slotKeyChainWriteFinished( QKeychain::Job* );
|
void slotKeyChainWriteFinished( QKeychain::Job* );
|
||||||
void slotUserDialogDone(int);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit CredentialStore(QObject *parent = 0);
|
explicit CredentialStore(QObject *parent = 0);
|
||||||
|
@ -138,9 +126,7 @@ private:
|
||||||
static QString _user;
|
static QString _user;
|
||||||
static QString _url;
|
static QString _url;
|
||||||
static QString _errorMsg;
|
static QString _errorMsg;
|
||||||
static int _tries;
|
|
||||||
static CredentialType _type;
|
static CredentialType _type;
|
||||||
QInputDialog *_inputDialog;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue