mirror of
https://github.com/nextcloud/desktop.git
synced 2024-11-23 21:46:03 +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()
|
||||
{
|
||||
if( _connection.isEmpty() ) {
|
||||
if( CredentialStore::instance()->canTryAgain() ) {
|
||||
connect( CredentialStore::instance(), SIGNAL(fetchCredentialsFinished(bool)),
|
||||
this, SLOT(slotCredentialsFetched(bool)) );
|
||||
CredentialStore::instance()->fetchCredentials();
|
||||
}
|
||||
|
||||
if( CredentialStore::instance()->state() == CredentialStore::TooManyAttempts ) {
|
||||
_errors << tr("Too many attempts to get a valid password.");
|
||||
emit connectionResult( CredentialsTooManyAttempts );
|
||||
}
|
||||
connect( CredentialStore::instance(), SIGNAL(fetchCredentialsFinished(bool)),
|
||||
this, SLOT(slotCredentialsFetched(bool)) );
|
||||
CredentialStore::instance()->fetchCredentials();
|
||||
} else {
|
||||
// Pull credentials from Mirall config.
|
||||
slotCredentialsFetched( true );
|
||||
|
@ -160,14 +153,8 @@ void ConnectionValidator::slotCredentialsFetched( bool ok )
|
|||
if( ! ok ) {
|
||||
Status stat;
|
||||
_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();
|
||||
stat = CredentialError;
|
||||
}
|
||||
_errors << CredentialStore::instance()->errorMessage();
|
||||
stat = CredentialError;
|
||||
|
||||
qDebug() << "Could not fetch credentials" << _errors;
|
||||
|
||||
|
|
|
@ -35,7 +35,6 @@ QString CredentialStore::_passwd = QString::null;
|
|||
QString CredentialStore::_user = QString::null;
|
||||
QString CredentialStore::_url = QString::null;
|
||||
QString CredentialStore::_errorMsg = QString::null;
|
||||
int CredentialStore::_tries = 0;
|
||||
#ifdef WITH_QTKEYCHAIN
|
||||
CredentialStore::CredentialType CredentialStore::_type = KeyChain;
|
||||
#else
|
||||
|
@ -67,39 +66,14 @@ CredentialStore::CredState CredentialStore::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()
|
||||
{
|
||||
MirallConfigFile cfgFile;
|
||||
if( ++_tries > MAX_LOGIN_ATTEMPTS ) {
|
||||
qDebug() << "Too many attempts to enter password!";
|
||||
_state = TooManyAttempts;
|
||||
emit( fetchCredentialsFinished(false) );
|
||||
return;
|
||||
}
|
||||
|
||||
bool ok = false;
|
||||
QString pwd;
|
||||
_user = cfgFile.ownCloudUser();
|
||||
_url = cfgFile.ownCloudUrl();
|
||||
if( !cfgFile.passwordStorageAllowed() ) {
|
||||
_type = CredentialStore::User;
|
||||
}
|
||||
|
||||
QString key = keyChainKey(_url);
|
||||
|
||||
|
@ -111,26 +85,16 @@ void CredentialStore::fetchCredentials()
|
|||
}
|
||||
|
||||
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: {
|
||||
/* Read from config file. */
|
||||
_state = Fetching;
|
||||
pwd = cfgFile.ownCloudPasswd();
|
||||
ok = true;
|
||||
if( cfgFile.ownCloudPasswordExists() ) {
|
||||
pwd = cfgFile.ownCloudPasswd();
|
||||
ok = true;
|
||||
} else {
|
||||
ok = false;
|
||||
_state = EntryNotFound;
|
||||
}
|
||||
break;
|
||||
}
|
||||
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()
|
||||
{
|
||||
_state = NotFetched;
|
||||
_user = QString::null;
|
||||
_passwd = QString::null;
|
||||
_tries = 0;
|
||||
}
|
||||
|
||||
QString CredentialStore::keyChainKey( const QString& url ) const
|
||||
|
@ -242,9 +192,6 @@ void CredentialStore::slotKeyChainReadFinished(QKeychain::Job* job)
|
|||
case QKeychain::CouldNotDeleteEntry:
|
||||
_state = Error;
|
||||
break;
|
||||
case QKeychain::AccessDeniedByUser:
|
||||
_state = AccessDeniedByUser;
|
||||
break;
|
||||
case QKeychain::AccessDenied:
|
||||
_state = AccessDenied;
|
||||
break;
|
||||
|
@ -303,8 +250,6 @@ void CredentialStore::setCredentials( const QString& url, const QString& user,
|
|||
#else
|
||||
_type = Settings;
|
||||
#endif
|
||||
} else {
|
||||
_type = User;
|
||||
}
|
||||
_url = url;
|
||||
_state = Ok;
|
||||
|
@ -323,9 +268,6 @@ void CredentialStore::saveCredentials( )
|
|||
#endif
|
||||
|
||||
switch( _type ) {
|
||||
case CredentialStore::User:
|
||||
deleteKeyChainCredential( key );
|
||||
break;
|
||||
case CredentialStore::KeyChain:
|
||||
#ifdef WITH_QTKEYCHAIN
|
||||
// Set password in KeyChain
|
||||
|
@ -372,7 +314,6 @@ void CredentialStore::slotKeyChainWriteFinished( QKeychain::Job *job )
|
|||
MirallConfigFile cfgFile;
|
||||
cfgFile.clearPasswordFromConfig();
|
||||
_state = NotFetched;
|
||||
_tries = 0;
|
||||
}
|
||||
} else {
|
||||
qDebug() << "Error: KeyChain Write Password Job failed!";
|
||||
|
|
|
@ -34,10 +34,8 @@ namespace Mirall {
|
|||
* The fetchCredentials() call changes the internal state of the credential store
|
||||
* to one of
|
||||
* 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.
|
||||
* EntryNotFound: No password entry found in the storage.
|
||||
* Error: A general error happened.
|
||||
* After fetching has finished, signal fetchCredentialsFinished(bool) is emitted.
|
||||
* The result can be retrieved with state() and password() and user() methods.
|
||||
|
@ -49,20 +47,16 @@ class CredentialStore : public QObject
|
|||
public:
|
||||
enum CredState { NotFetched = 0,
|
||||
Ok,
|
||||
UserCanceled,
|
||||
Fetching,
|
||||
AsyncFetching,
|
||||
EntryNotFound,
|
||||
AccessDeniedByUser,
|
||||
AccessDenied,
|
||||
NoKeychainBackend,
|
||||
Error,
|
||||
AsyncWriting,
|
||||
TooManyAttempts };
|
||||
AsyncWriting };
|
||||
|
||||
enum CredentialType {
|
||||
User = 0,
|
||||
Settings,
|
||||
Settings = 0,
|
||||
KeyChain
|
||||
};
|
||||
|
||||
|
@ -105,11 +99,6 @@ public:
|
|||
|
||||
QString errorMessage();
|
||||
|
||||
/**
|
||||
* @brief canTryAgain - check if another try to get credentials makes sense.
|
||||
*/
|
||||
bool canTryAgain();
|
||||
|
||||
void reset();
|
||||
signals:
|
||||
/**
|
||||
|
@ -125,7 +114,6 @@ signals:
|
|||
protected slots:
|
||||
void slotKeyChainReadFinished( QKeychain::Job* );
|
||||
void slotKeyChainWriteFinished( QKeychain::Job* );
|
||||
void slotUserDialogDone(int);
|
||||
|
||||
private:
|
||||
explicit CredentialStore(QObject *parent = 0);
|
||||
|
@ -138,9 +126,7 @@ private:
|
|||
static QString _user;
|
||||
static QString _url;
|
||||
static QString _errorMsg;
|
||||
static int _tries;
|
||||
static CredentialType _type;
|
||||
QInputDialog *_inputDialog;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue