mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-07-31 19:30:21 +02:00
CredentialsManager can now read/write under different service names.
This commit is contained in:
@@ -143,7 +143,7 @@ Account::syncConfig()
|
||||
s->sync();
|
||||
|
||||
CredentialsManager* c = AccountManager::instance()->credentialsManager();
|
||||
c->setCredentials( m_accountId, m_credentials );
|
||||
c->setCredentials( "Tomahawk", m_accountId, m_credentials );
|
||||
}
|
||||
|
||||
|
||||
@@ -161,7 +161,7 @@ Account::loadFromConfig( const QString& accountId )
|
||||
s->endGroup();
|
||||
|
||||
CredentialsManager* c = AccountManager::instance()->credentialsManager();
|
||||
m_credentials = c->credentials( m_accountId );
|
||||
m_credentials = c->credentials( "Tomahawk", m_accountId );
|
||||
}
|
||||
|
||||
|
||||
@@ -179,7 +179,7 @@ Account::removeFromConfig()
|
||||
s->remove( "accounts/" + m_accountId );
|
||||
|
||||
CredentialsManager* c = AccountManager::instance()->credentialsManager();
|
||||
c->setCredentials( m_accountId, QVariantHash() );
|
||||
c->setCredentials( "Tomahawk", m_accountId, QVariantHash() );
|
||||
}
|
||||
|
||||
|
||||
|
@@ -291,7 +291,13 @@ AccountManager::loadFromConfig()
|
||||
m_creds = new CredentialsManager( this );
|
||||
NewClosure( m_creds, SIGNAL( ready() ),
|
||||
this, SLOT( finishLoadingFromConfig( QStringList ) ), accountIds );
|
||||
m_creds->loadCredentials( accountIds );
|
||||
|
||||
CredentialsManager::Service tomahawkSvc;
|
||||
tomahawkSvc.name = "Tomahawk"; //the string where we store in QtKeychain
|
||||
tomahawkSvc.keys = accountIds;
|
||||
QList< CredentialsManager::Service > svcs;
|
||||
svcs << tomahawkSvc;
|
||||
m_creds->loadCredentials( svcs );
|
||||
}
|
||||
|
||||
|
||||
|
@@ -24,7 +24,6 @@
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
#define TOMAHAWK_KEYCHAINSVC QLatin1String("Tomahawk")
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
@@ -32,6 +31,32 @@ namespace Tomahawk
|
||||
namespace Accounts
|
||||
{
|
||||
|
||||
CredentialsStorageKey::CredentialsStorageKey( const QString& service, const QString& key )
|
||||
: m_service( service )
|
||||
, m_key( key )
|
||||
{}
|
||||
|
||||
bool
|
||||
CredentialsStorageKey::operator ==( const CredentialsStorageKey& other ) const
|
||||
{
|
||||
return ( m_key == other.m_key ) && ( m_service == other.m_service );
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
CredentialsStorageKey::operator !=( const CredentialsStorageKey& other ) const
|
||||
{
|
||||
return !operator ==( other );
|
||||
}
|
||||
|
||||
uint
|
||||
qHash( const Tomahawk::Accounts::CredentialsStorageKey& key )
|
||||
{
|
||||
return qHash( key.service() + key.key() );
|
||||
}
|
||||
|
||||
|
||||
// CredentialsManager
|
||||
|
||||
CredentialsManager::CredentialsManager( QObject* parent )
|
||||
: QObject( parent )
|
||||
@@ -41,65 +66,74 @@ CredentialsManager::CredentialsManager( QObject* parent )
|
||||
|
||||
|
||||
void
|
||||
CredentialsManager::loadCredentials( QStringList keys )
|
||||
CredentialsManager::loadCredentials( QList< Service > keysByService )
|
||||
{
|
||||
tDebug() << Q_FUNC_INFO << "keys:" << keys;
|
||||
foreach ( QString key, keys )
|
||||
foreach ( const Service svc, keysByService )
|
||||
{
|
||||
QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( TOMAHAWK_KEYCHAINSVC, this );
|
||||
j->setKey( key );
|
||||
j->setAutoDelete( false );
|
||||
tDebug() << Q_FUNC_INFO << "keys for service" << svc.name << ":" << svc.keys;
|
||||
foreach ( QString key, svc.keys )
|
||||
{
|
||||
QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( svc.name, this );
|
||||
j->setKey( key );
|
||||
j->setAutoDelete( false );
|
||||
#if defined( Q_OS_UNIX ) && !defined( Q_OS_MAC )
|
||||
j->setInsecureFallback( true );
|
||||
j->setInsecureFallback( true );
|
||||
#endif
|
||||
connect( j, SIGNAL( finished( QKeychain::Job* ) ),
|
||||
SLOT( keychainJobFinished( QKeychain::Job* ) ) );
|
||||
m_readJobs << j;
|
||||
j->start();
|
||||
tDebug() << "Launching QtKeychain readJob for" << key;
|
||||
connect( j, SIGNAL( finished( QKeychain::Job* ) ),
|
||||
SLOT( keychainJobFinished( QKeychain::Job* ) ) );
|
||||
m_readJobs << j;
|
||||
j->start();
|
||||
tDebug() << "Launching QtKeychain readJob for" << key;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
QStringList
|
||||
QList< CredentialsStorageKey >
|
||||
CredentialsManager::keys() const
|
||||
{
|
||||
QStringList keys = m_credentials.keys();
|
||||
QList< CredentialsStorageKey > keys = m_credentials.keys();
|
||||
return keys;
|
||||
}
|
||||
|
||||
|
||||
QVariantHash
|
||||
CredentialsManager::credentials( const QString& key ) const
|
||||
CredentialsManager::credentials( const CredentialsStorageKey& key ) const
|
||||
{
|
||||
return m_credentials.value( key );
|
||||
}
|
||||
|
||||
|
||||
QVariantHash
|
||||
CredentialsManager::credentials( const QString& serviceName, const QString& key ) const
|
||||
{
|
||||
return credentials( CredentialsStorageKey( serviceName, key ) );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
CredentialsManager::setCredentials( const QString& key, const QVariantHash& value )
|
||||
CredentialsManager::setCredentials( const CredentialsStorageKey& csKey, const QVariantHash& value )
|
||||
{
|
||||
QMutexLocker locker( &m_mutex );
|
||||
|
||||
QKeychain::Job* j;
|
||||
if ( value.isEmpty() )
|
||||
{
|
||||
if ( !m_credentials.contains( key ) ) //if we don't have any credentials for this key, we bail
|
||||
if ( !m_credentials.contains( csKey ) ) //if we don't have any credentials for this key, we bail
|
||||
return;
|
||||
|
||||
m_credentials.remove( key );
|
||||
m_credentials.remove( csKey );
|
||||
|
||||
QKeychain::DeletePasswordJob* dj = new QKeychain::DeletePasswordJob( TOMAHAWK_KEYCHAINSVC, this );
|
||||
dj->setKey( key );
|
||||
QKeychain::DeletePasswordJob* dj = new QKeychain::DeletePasswordJob( csKey.service(), this );
|
||||
dj->setKey( csKey.key() );
|
||||
j = dj;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( value == m_credentials.value( key ) ) //if the credentials haven't actually changed, we bail
|
||||
if ( value == m_credentials.value( csKey ) ) //if the credentials haven't actually changed, we bail
|
||||
return;
|
||||
|
||||
m_credentials.insert( key, value );
|
||||
m_credentials.insert( csKey, value );
|
||||
|
||||
QByteArray data;
|
||||
{
|
||||
@@ -107,8 +141,8 @@ CredentialsManager::setCredentials( const QString& key, const QVariantHash& valu
|
||||
ds << value;
|
||||
}
|
||||
|
||||
QKeychain::WritePasswordJob* wj = new QKeychain::WritePasswordJob( TOMAHAWK_KEYCHAINSVC, this );
|
||||
wj->setKey( key );
|
||||
QKeychain::WritePasswordJob* wj = new QKeychain::WritePasswordJob( csKey.service(), this );
|
||||
wj->setKey( csKey.key() );
|
||||
wj->setBinaryData( data );
|
||||
j = wj;
|
||||
}
|
||||
@@ -123,6 +157,13 @@ CredentialsManager::setCredentials( const QString& key, const QVariantHash& valu
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
CredentialsManager::setCredentials( const QString& serviceName, const QString& key, const QVariantHash& value )
|
||||
{
|
||||
setCredentials( CredentialsStorageKey( serviceName, key ), value );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
CredentialsManager::keychainJobFinished( QKeychain::Job* j )
|
||||
{
|
||||
@@ -137,7 +178,7 @@ CredentialsManager::keychainJobFinished( QKeychain::Job* j )
|
||||
QDataStream dataStream( readJob->binaryData() );
|
||||
dataStream >> creds;
|
||||
|
||||
m_credentials.insert( readJob->key(), creds );
|
||||
m_credentials.insert( CredentialsStorageKey( readJob->service(), readJob->key() ), creds );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -164,7 +205,6 @@ CredentialsManager::keychainJobFinished( QKeychain::Job* j )
|
||||
j->deleteLater();
|
||||
}
|
||||
|
||||
|
||||
} //namespace Accounts
|
||||
|
||||
} //namespace Tomahawk
|
||||
|
@@ -22,6 +22,7 @@
|
||||
#include <QObject>
|
||||
#include <QVariantHash>
|
||||
#include <QMutex>
|
||||
#include <QStringList>
|
||||
|
||||
|
||||
namespace QKeychain
|
||||
@@ -30,13 +31,25 @@ class Job;
|
||||
class ReadPasswordJob;
|
||||
}
|
||||
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
namespace Accounts
|
||||
{
|
||||
|
||||
class CredentialsStorageKey
|
||||
{
|
||||
public:
|
||||
explicit CredentialsStorageKey( const QString &service, const QString &key );
|
||||
bool operator ==( const CredentialsStorageKey& other ) const;
|
||||
bool operator !=( const CredentialsStorageKey& other ) const;
|
||||
QString service() const { return m_service; }
|
||||
QString key() const { return m_key; }
|
||||
private:
|
||||
QString m_service;
|
||||
QString m_key;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief The CredentialsManager class holds an in-memory cache of whatever credentials are stored
|
||||
* in the system's QtKeychain-accessible credentials storage.
|
||||
@@ -51,14 +64,22 @@ class CredentialsManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
struct Service
|
||||
{
|
||||
QString name;
|
||||
QStringList keys;
|
||||
};
|
||||
|
||||
explicit CredentialsManager( QObject* parent = 0 );
|
||||
|
||||
void loadCredentials( QStringList keys );
|
||||
void loadCredentials( QList< Service > keysByService );
|
||||
|
||||
QStringList keys() const;
|
||||
QList< CredentialsStorageKey > keys() const;
|
||||
|
||||
QVariantHash credentials( const QString& key ) const;
|
||||
void setCredentials( const QString& key, const QVariantHash& value );
|
||||
QVariantHash credentials( const CredentialsStorageKey& key ) const;
|
||||
QVariantHash credentials( const QString& serviceName, const QString& key ) const;
|
||||
void setCredentials( const CredentialsStorageKey& key, const QVariantHash& value );
|
||||
void setCredentials( const QString& serviceName, const QString& key, const QVariantHash& value );
|
||||
|
||||
signals:
|
||||
void ready();
|
||||
@@ -67,12 +88,15 @@ private slots:
|
||||
void keychainJobFinished( QKeychain::Job* );
|
||||
|
||||
private:
|
||||
QHash< QString, QVariantHash > m_credentials;
|
||||
QHash< CredentialsStorageKey, QVariantHash > m_credentials;
|
||||
QList< QKeychain::ReadPasswordJob* > m_readJobs;
|
||||
QMutex m_mutex;
|
||||
};
|
||||
|
||||
uint qHash( const Tomahawk::Accounts::CredentialsStorageKey& key );
|
||||
|
||||
} //namespace Accounts
|
||||
|
||||
} //namespace Tomahawk
|
||||
|
||||
#endif // CREDENTIALSMANAGER_H
|
||||
|
Reference in New Issue
Block a user