Remove keychain from 3rdparty.

This commit is contained in:
John Maguire 2012-10-29 13:26:14 +00:00
parent 6cadc9321d
commit 3a502ef902
11 changed files with 0 additions and 456 deletions

View File

@ -1,73 +0,0 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR)
FIND_PACKAGE(Qt4 REQUIRED)
FIND_PACKAGE(Boost REQUIRED)
SET(KEYCHAIN-SOURCES
keychain.cpp
default_keychain.cpp)
INCLUDE(${QT_USE_FILE})
IF(APPLE)
FIND_LIBRARY(SECURITY Security)
LIST(APPEND KEYCHAIN-SOURCES mac_keychain.cpp)
ELSEIF(UNIX)
# Find Gnome Keyring
FIND_PACKAGE(PkgConfig REQUIRED)
pkg_check_modules(GNOME_KEYRING gnome-keyring-1>=0.8)
IF(NOT GNOME_KEYRING_FOUND)
MESSAGE("Gnome keyring not found")
ADD_DEFINITIONS(-DNO_GNOME_KEYRING)
ELSE(NOT GNOME_KEYRING_FOUND)
pkg_check_modules(GLIB2 REQUIRED glib-2.0)
LIST(APPEND KEYCHAIN-SOURCES gnome_keychain.cpp)
ENDIF(NOT GNOME_KEYRING_FOUND)
# Find KDE4 KWallet dbus interface
FIND_FILE(KWALLET_INTERFACE
org.kde.KWallet.xml
PATHS /usr/share/dbus-1/interfaces)
IF(${KWALLET_INTERFACE} MATCHES "KWALLET_INTERFACE-NOTFOUND")
SET(KWALLET_INTERFACE_NOTFOUND TRUE)
ENDIF(${KWALLET_INTERFACE} MATCHES "KWALLET_INTERFACE-NOTFOUND")
IF(KWALLET_INTERFACE_NOTFOUND)
MESSAGE("KWallet interface not found")
ADD_DEFINITIONS(-DNO_KWALLET)
ELSE(KWALLET_INTERFACE_NOTFOUND)
LIST(APPEND KEYCHAIN-SOURCES kwallet_keychain.cpp)
SET(QT_USE_QTDBUS 1)
QT4_ADD_DBUS_INTERFACE(KWALLET_INTERFACE-SOURCES ${KWALLET_INTERFACE} kwallet)
ENDIF(KWALLET_INTERFACE_NOTFOUND)
ENDIF(APPLE)
INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${GNOME_KEYRING_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/..
${CMAKE_CURRENT_BINARY_DIR}/..
)
ADD_LIBRARY(keychain STATIC
${KEYCHAIN-SOURCES}
${KWALLET_INTERFACE-SOURCES})
TARGET_LINK_LIBRARIES(keychain
${QT_LIBRARIES}
${SECURITY}
${GNOME_KEYRING_LIBRARIES}
)
IF(NOT KWALLET_INTERFACE_NOTFOUND)
TARGET_LINK_LIBRARIES(keychain
${QT_DBUS_LIBRARY}
${QT_QTDBUS_LIBRARY}
)
INCLUDE_DIRECTORIES(${QT_QTDBUS_INCLUDE_DIR})
ENDIF(NOT KWALLET_INTERFACE_NOTFOUND)
LINK_DIRECTORIES(${GNOME_KEYRING_LIBRARY_DIRS})

View File

@ -1,14 +0,0 @@
#include "default_keychain.h"
const QString DefaultKeychain::kImplementationName = "Default";
const QString DefaultKeychain::getPassword(const QString& account) {
Q_UNUSED(account);
return password_;
}
bool DefaultKeychain::setPassword(const QString& account, const QString& password) {
Q_UNUSED(account);
password_ = password;
return true;
}

View File

@ -1,21 +0,0 @@
#ifndef DEFAULT_KEYCHAIN_H
#define DEFAULT_KEYCHAIN_H
#include "keychain.h"
class DefaultKeychain : public Keychain {
public:
virtual ~DefaultKeychain() {}
virtual bool isAvailable() { return true; }
virtual const QString getPassword(const QString& account);
virtual bool setPassword(const QString& account, const QString& password);
virtual const QString& implementationName() const { return kImplementationName; }
static const QString kImplementationName;
private:
QString password_;
};
#endif

View File

@ -1,58 +0,0 @@
#include "gnome_keychain.h"
#include <glib.h>
#include <QCoreApplication>
#include <QtDebug>
const QString GnomeKeychain::kImplementationName = "Gnome Keyring";
const GnomeKeyringPasswordSchema GnomeKeychain::kOurSchema = {
GNOME_KEYRING_ITEM_GENERIC_SECRET,
{
{ "username", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
{ "service", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
{ NULL },
},
};
bool GnomeKeychain::isAvailable() {
return gnome_keyring_is_available();
}
const QString GnomeKeychain::getPassword(const QString& account) {
Q_ASSERT(isAvailable());
char* password;
GnomeKeyringResult result = gnome_keyring_find_password_sync(
&kOurSchema,
&password,
"username", account.toUtf8().constData(),
"service", kServiceName.toUtf8().constData(),
NULL);
if (result == GNOME_KEYRING_RESULT_OK) {
QString pass(password);
gnome_keyring_free_password(password);
return pass;
}
qWarning() << "Failed to get password from keychain for account" << account;
return QString::null;
}
bool GnomeKeychain::setPassword(const QString& account, const QString& password) {
Q_ASSERT(isAvailable());
QString displayName = QString("%1 account for %2").arg(
QCoreApplication::applicationName(), account);
GnomeKeyringResult result = gnome_keyring_store_password_sync(
&kOurSchema,
NULL,
displayName.toUtf8().constData(),
password.toUtf8().constData(),
"username", account.toUtf8().constData(),
"service", kServiceName.toUtf8().constData(),
NULL);
return result == GNOME_KEYRING_RESULT_OK;
}

View File

@ -1,24 +0,0 @@
#ifndef GNOME_KEYCHAIN_H
#define GNOME_KEYCHAIN_H
#include "keychain.h"
extern "C" {
#include <gnome-keyring.h>
}
class GnomeKeychain : public Keychain {
public:
virtual ~GnomeKeychain() {}
virtual bool isAvailable();
virtual const QString getPassword(const QString& account);
virtual bool setPassword(const QString& account, const QString& password);
virtual const QString& implementationName() const { return kImplementationName; }
static const QString kImplementationName;
private:
static const GnomeKeyringPasswordSchema kOurSchema;
};
#endif

View File

@ -1,46 +0,0 @@
#include "keychain.h"
#include "default_keychain.h"
#ifdef Q_OS_DARWIN
#include "mac_keychain.h"
#elif defined(Q_OS_UNIX)
#ifndef NO_KWALLET
#include "kwallet_keychain.h"
#endif
#ifndef NO_GNOME_KEYRING
#include "gnome_keychain.h"
#endif
#endif // Q_OS_UNIX
// TODO: Make this configurable.
const QString Keychain::kServiceName = "Google Account";
const Keychain::KeychainDefinition* Keychain::kCompiledImplementations[] = {
#ifdef Q_OS_DARWIN
new KeychainImpl<MacKeychain>(),
#elif defined(Q_OS_LINUX)
#ifndef NO_KWALLET
new KeychainImpl<KWalletKeychain>(),
#endif
#ifndef NO_GNOME_KEYRING
new KeychainImpl<GnomeKeychain>(),
#endif
#endif
new KeychainImpl<DefaultKeychain>(),
NULL
};
Keychain* Keychain::getDefault() {
const KeychainDefinition** ptr = kCompiledImplementations;
while (*ptr != NULL) {
Keychain* keychain = (*ptr)->getInstance();
if (keychain->isAvailable()) {
return keychain;
} else {
delete keychain;
}
}
return NULL;
}

View File

@ -1,44 +0,0 @@
#ifndef KEYCHAIN_H
#define KEYCHAIN_H
#include <boost/utility.hpp>
#include <QString>
class Keychain : boost::noncopyable {
public:
virtual ~Keychain() {}
virtual bool isAvailable() = 0;
virtual const QString getPassword(const QString& account) = 0;
virtual bool setPassword(const QString& account, const QString& password) = 0;
virtual const QString& implementationName() const = 0;
static Keychain* getDefault();
protected:
static const QString kServiceName;
private:
// Fun for all the family.
struct KeychainDefinition {
KeychainDefinition(const QString& name) : name_(name) {}
virtual ~KeychainDefinition() {}
const QString& getName() const { return name_; }
virtual Keychain* getInstance() const = 0;
protected:
const QString& name_;
};
template<typename T>
struct KeychainImpl : public KeychainDefinition {
KeychainImpl() : KeychainDefinition(T::kImplementationName) { }
virtual Keychain* getInstance() const {
return new T();
}
};
static const KeychainDefinition* kCompiledImplementations[];
};
#endif

View File

@ -1,60 +0,0 @@
#include "kwallet_keychain.h"
#include <QDBusConnection>
#include <QDBusPendingReply>
const QString KWalletKeychain::kImplementationName = "KWallet";
const QString KWalletKeychain::kKWalletServiceName = "org.kde.kwalletd";
const QString KWalletKeychain::kKWalletPath = "/modules/kwalletd";
const QString KWalletKeychain::kKWalletFolder = "Passwords";
KWalletKeychain::KWalletKeychain()
: kwallet_(kKWalletServiceName, kKWalletPath, QDBusConnection::sessionBus())
{
if (isAvailable()) {
QDBusPendingReply<QString> wallet_name = kwallet_.networkWallet();
wallet_name.waitForFinished();
if (wallet_name.isValid()) {
wallet_name_ = wallet_name.value();
QDBusPendingReply<int> open_request = kwallet_.open(wallet_name_, 0, kServiceName);
open_request.waitForFinished();
if (open_request.isValid()) {
handle_ = open_request.value();
}
}
}
}
KWalletKeychain::~KWalletKeychain() {
}
bool KWalletKeychain::isAvailable() {
if(!kwallet_.isValid())
return false;
QDBusPendingReply<bool> check = kwallet_.isEnabled();
check.waitForFinished();
return check.isValid() && check.value();
}
const QString KWalletKeychain::getPassword(const QString& account) {
QDBusPendingReply<QString> password =
kwallet_.readPassword(handle_, kKWalletFolder, account, kServiceName);
password.waitForFinished();
if (password.isValid()) {
return password.value();
}
return QString::null;
}
bool KWalletKeychain::setPassword(const QString& account, const QString& password) {
QDBusPendingReply<int> ret =
kwallet_.writePassword(handle_, kKWalletFolder, account, password, kServiceName);
ret.waitForFinished();
if (ret.isValid() && ret.value() == 0) {
return true;
}
return false;
}

View File

@ -1,31 +0,0 @@
#ifndef KWALLET_KEYCHAIN_H
#define KWALLET_KEYCHAIN_H
#include "keychain.h"
#include "kwallet.h"
class KWalletKeychain : public Keychain {
public:
KWalletKeychain();
virtual ~KWalletKeychain();
virtual const QString getPassword(const QString& account);
virtual bool setPassword(const QString& account, const QString& password);
virtual bool isAvailable();
virtual const QString& implementationName() const { return kImplementationName; }
static const QString kImplementationName;
private:
org::kde::KWallet kwallet_;
QString wallet_name_;
int handle_;
static const QString kKWalletServiceName;
static const QString kKWalletPath;
static const QString kKWalletFolder;
};
#endif

View File

@ -1,66 +0,0 @@
#include "mac_keychain.h"
#include <Security/Security.h>
const QString MacKeychain::kImplementationName = "Mac Keychain";
bool MacKeychain::isAvailable() {
return true;
}
const QString MacKeychain::getPassword(const QString& account) {
UInt32 password_length;
char* password;
OSStatus ret = SecKeychainFindGenericPassword(
NULL,
kServiceName.length(),
kServiceName.toStdString().c_str(),
account.length(),
account.toStdString().c_str(),
&password_length,
(void**)&password,
NULL);
if (ret == 0) {
QString pass = QString::fromAscii(password, password_length);
SecKeychainItemFreeContent(NULL, password);
return pass;
}
return QString::null;
}
bool MacKeychain::setPassword(const QString& account, const QString& password) {
SecKeychainItemRef item;
OSStatus ret = SecKeychainFindGenericPassword(
NULL,
kServiceName.length(),
kServiceName.toStdString().c_str(),
account.length(),
account.toStdString().c_str(),
NULL,
NULL,
&item);
if (ret == 0) {
ret = SecKeychainItemModifyAttributesAndData(
item,
NULL,
password.length(),
password.toStdString().c_str());
return ret == 0;
} else {
ret = SecKeychainAddGenericPassword(
NULL,
kServiceName.length(),
kServiceName.toStdString().c_str(),
account.length(),
account.toStdString().c_str(),
password.length(),
password.toStdString().c_str(),
NULL);
return ret == 0;
}
}

View File

@ -1,19 +0,0 @@
#ifndef MAC_KEYCHAIN_H
#define MAC_KEYCHAIN_H
#include "keychain.h"
class MacKeychain : public Keychain {
public:
virtual ~MacKeychain() {}
virtual bool isAvailable();
virtual const QString getPassword(const QString& account);
virtual bool setPassword(const QString& account, const QString& password);
virtual const QString& implementationName() const { return kImplementationName; }
static const QString kImplementationName;
};
#endif