1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-01 20:00:13 +02:00

CredentialsManager can now read/write under different service names.

This commit is contained in:
Teo Mrnjavac
2013-05-30 15:24:13 +02:00
parent 6fa9379dae
commit 09a20e98d1
4 changed files with 108 additions and 38 deletions

View File

@@ -143,7 +143,7 @@ Account::syncConfig()
s->sync(); s->sync();
CredentialsManager* c = AccountManager::instance()->credentialsManager(); 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(); s->endGroup();
CredentialsManager* c = AccountManager::instance()->credentialsManager(); 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 ); s->remove( "accounts/" + m_accountId );
CredentialsManager* c = AccountManager::instance()->credentialsManager(); CredentialsManager* c = AccountManager::instance()->credentialsManager();
c->setCredentials( m_accountId, QVariantHash() ); c->setCredentials( "Tomahawk", m_accountId, QVariantHash() );
} }

View File

@@ -291,7 +291,13 @@ AccountManager::loadFromConfig()
m_creds = new CredentialsManager( this ); m_creds = new CredentialsManager( this );
NewClosure( m_creds, SIGNAL( ready() ), NewClosure( m_creds, SIGNAL( ready() ),
this, SLOT( finishLoadingFromConfig( QStringList ) ), accountIds ); 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 );
} }

View File

@@ -24,7 +24,6 @@
#include <QStringList> #include <QStringList>
#define TOMAHAWK_KEYCHAINSVC QLatin1String("Tomahawk")
namespace Tomahawk namespace Tomahawk
{ {
@@ -32,6 +31,32 @@ namespace Tomahawk
namespace Accounts 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 ) CredentialsManager::CredentialsManager( QObject* parent )
: QObject( parent ) : QObject( parent )
@@ -41,65 +66,74 @@ CredentialsManager::CredentialsManager( QObject* parent )
void void
CredentialsManager::loadCredentials( QStringList keys ) CredentialsManager::loadCredentials( QList< Service > keysByService )
{ {
tDebug() << Q_FUNC_INFO << "keys:" << keys; foreach ( const Service svc, keysByService )
foreach ( QString key, keys )
{ {
QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( TOMAHAWK_KEYCHAINSVC, this ); tDebug() << Q_FUNC_INFO << "keys for service" << svc.name << ":" << svc.keys;
j->setKey( key ); foreach ( QString key, svc.keys )
j->setAutoDelete( false ); {
QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( svc.name, this );
j->setKey( key );
j->setAutoDelete( false );
#if defined( Q_OS_UNIX ) && !defined( Q_OS_MAC ) #if defined( Q_OS_UNIX ) && !defined( Q_OS_MAC )
j->setInsecureFallback( true ); j->setInsecureFallback( true );
#endif #endif
connect( j, SIGNAL( finished( QKeychain::Job* ) ), connect( j, SIGNAL( finished( QKeychain::Job* ) ),
SLOT( keychainJobFinished( QKeychain::Job* ) ) ); SLOT( keychainJobFinished( QKeychain::Job* ) ) );
m_readJobs << j; m_readJobs << j;
j->start(); j->start();
tDebug() << "Launching QtKeychain readJob for" << key; tDebug() << "Launching QtKeychain readJob for" << key;
}
} }
} }
QStringList QList< CredentialsStorageKey >
CredentialsManager::keys() const CredentialsManager::keys() const
{ {
QStringList keys = m_credentials.keys(); QList< CredentialsStorageKey > keys = m_credentials.keys();
return keys; return keys;
} }
QVariantHash QVariantHash
CredentialsManager::credentials( const QString& key ) const CredentialsManager::credentials( const CredentialsStorageKey& key ) const
{ {
return m_credentials.value( key ); return m_credentials.value( key );
} }
QVariantHash
CredentialsManager::credentials( const QString& serviceName, const QString& key ) const
{
return credentials( CredentialsStorageKey( serviceName, key ) );
}
void void
CredentialsManager::setCredentials( const QString& key, const QVariantHash& value ) CredentialsManager::setCredentials( const CredentialsStorageKey& csKey, const QVariantHash& value )
{ {
QMutexLocker locker( &m_mutex ); QMutexLocker locker( &m_mutex );
QKeychain::Job* j; QKeychain::Job* j;
if ( value.isEmpty() ) 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; return;
m_credentials.remove( key ); m_credentials.remove( csKey );
QKeychain::DeletePasswordJob* dj = new QKeychain::DeletePasswordJob( TOMAHAWK_KEYCHAINSVC, this ); QKeychain::DeletePasswordJob* dj = new QKeychain::DeletePasswordJob( csKey.service(), this );
dj->setKey( key ); dj->setKey( csKey.key() );
j = dj; j = dj;
} }
else 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; return;
m_credentials.insert( key, value ); m_credentials.insert( csKey, value );
QByteArray data; QByteArray data;
{ {
@@ -107,8 +141,8 @@ CredentialsManager::setCredentials( const QString& key, const QVariantHash& valu
ds << value; ds << value;
} }
QKeychain::WritePasswordJob* wj = new QKeychain::WritePasswordJob( TOMAHAWK_KEYCHAINSVC, this ); QKeychain::WritePasswordJob* wj = new QKeychain::WritePasswordJob( csKey.service(), this );
wj->setKey( key ); wj->setKey( csKey.key() );
wj->setBinaryData( data ); wj->setBinaryData( data );
j = wj; 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 void
CredentialsManager::keychainJobFinished( QKeychain::Job* j ) CredentialsManager::keychainJobFinished( QKeychain::Job* j )
{ {
@@ -137,7 +178,7 @@ CredentialsManager::keychainJobFinished( QKeychain::Job* j )
QDataStream dataStream( readJob->binaryData() ); QDataStream dataStream( readJob->binaryData() );
dataStream >> creds; dataStream >> creds;
m_credentials.insert( readJob->key(), creds ); m_credentials.insert( CredentialsStorageKey( readJob->service(), readJob->key() ), creds );
} }
else else
{ {
@@ -164,7 +205,6 @@ CredentialsManager::keychainJobFinished( QKeychain::Job* j )
j->deleteLater(); j->deleteLater();
} }
} //namespace Accounts } //namespace Accounts
} //namespace Tomahawk } //namespace Tomahawk

View File

@@ -22,6 +22,7 @@
#include <QObject> #include <QObject>
#include <QVariantHash> #include <QVariantHash>
#include <QMutex> #include <QMutex>
#include <QStringList>
namespace QKeychain namespace QKeychain
@@ -30,13 +31,25 @@ class Job;
class ReadPasswordJob; class ReadPasswordJob;
} }
namespace Tomahawk namespace Tomahawk
{ {
namespace Accounts 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 * @brief The CredentialsManager class holds an in-memory cache of whatever credentials are stored
* in the system's QtKeychain-accessible credentials storage. * in the system's QtKeychain-accessible credentials storage.
@@ -51,14 +64,22 @@ class CredentialsManager : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
struct Service
{
QString name;
QStringList keys;
};
explicit CredentialsManager( QObject* parent = 0 ); 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; QVariantHash credentials( const CredentialsStorageKey& key ) const;
void setCredentials( const QString& key, const QVariantHash& value ); 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: signals:
void ready(); void ready();
@@ -67,12 +88,15 @@ private slots:
void keychainJobFinished( QKeychain::Job* ); void keychainJobFinished( QKeychain::Job* );
private: private:
QHash< QString, QVariantHash > m_credentials; QHash< CredentialsStorageKey, QVariantHash > m_credentials;
QList< QKeychain::ReadPasswordJob* > m_readJobs; QList< QKeychain::ReadPasswordJob* > m_readJobs;
QMutex m_mutex; QMutex m_mutex;
}; };
uint qHash( const Tomahawk::Accounts::CredentialsStorageKey& key );
} //namespace Accounts } //namespace Accounts
} //namespace Tomahawk } //namespace Tomahawk
#endif // CREDENTIALSMANAGER_H #endif // CREDENTIALSMANAGER_H