Skip to content
Permalink
Browse files Browse the repository at this point in the history
Add cross-platform keychain support, fixes #70
  • Loading branch information
kraxarn committed Sep 17, 2022
1 parent 5704a47 commit afa7323
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 146 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -80,6 +80,7 @@ target_compile_definitions(spotify-qt PRIVATE APP_VERSION="v${PROJECT_VERSION}")
target_compile_definitions(spotify-qt PRIVATE APP_NAME="${PROJECT_NAME}")
target_compile_definitions(spotify-qt PRIVATE APP_ICON="${PROJECT_NAME}")
target_compile_definitions(spotify-qt PRIVATE ORG_NAME="kraxarn")
target_compile_definitions(spotify-qt PRIVATE PKG_NAME="com.kraxarn.${PROJECT_NAME}")
target_compile_definitions(spotify-qt PRIVATE BUILD_TYPE="${CMAKE_BUILD_TYPE}")

# Install icons and desktop shortcut on unix-like
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
@@ -1,6 +1,5 @@
add_subdirectory(commandline)
add_subdirectory(dialog)
add_subdirectory(keyring)
add_subdirectory(lib)
add_subdirectory(list)
add_subdirectory(listitem)
Expand Down
1 change: 0 additions & 1 deletion src/dialog/settings.hpp
@@ -1,6 +1,5 @@
#pragma once

#include "keyring/kwallet.hpp"
#include "mainwindow.hpp"
#include "settingspage/base.hpp"
#include "spotifyclient/runner.hpp"
Expand Down
2 changes: 0 additions & 2 deletions src/keyring/CMakeLists.txt

This file was deleted.

94 changes: 0 additions & 94 deletions src/keyring/kwallet.cpp

This file was deleted.

32 changes: 0 additions & 32 deletions src/keyring/kwallet.hpp

This file was deleted.

15 changes: 6 additions & 9 deletions src/settingspage/spotify.cpp
Expand Up @@ -231,15 +231,12 @@ auto SettingsPage::Spotify::config() -> QWidget *
sptDeviceType->addItem(QStringLiteral("Default"));
sptLayout->addWidget(sptDeviceType, 3, 1);

// KWallet keyring for password
#ifdef USE_DBUS
if (KWallet(QString::fromStdString(settings.spotify.username)).isEnabled())
{
sptKeyring = new QCheckBox("Save password in keyring", this);
sptKeyring->setToolTip("Store password in keyring (using KWallet)");
sptKeyring->setChecked(settings.spotify.keyring_password);
sptLayout->addWidget(sptKeyring, 4, 0);
}
// Keychain for password
#ifdef USE_KEYCHAIN
sptKeyring = new QCheckBox(QStringLiteral("Save password in keyring"), this);
sptKeyring->setToolTip(QStringLiteral("Store password in system keychain"));
sptKeyring->setChecked(settings.spotify.keyring_password);
sptLayout->addWidget(sptKeyring, 4, 0);
#endif

// librespot discovery
Expand Down
15 changes: 9 additions & 6 deletions src/spotifyclient/runner.cpp
@@ -1,6 +1,10 @@
#include "spotifyclient/runner.hpp"
#include "mainwindow.hpp"

#ifdef USE_KEYCHAIN
#include "util/keychain.hpp"
#endif

std::vector<lib::log_message> SpotifyClient::Runner::log;

SpotifyClient::Runner::Runner(const lib::settings &settings,
Expand Down Expand Up @@ -60,11 +64,10 @@ auto SpotifyClient::Runner::start() -> QString

// Get password from keyring if set
QString password;
#ifdef USE_DBUS
KWallet keyring(username);
if (settings.spotify.keyring_password && keyring.unlock())
#ifdef USE_KEYCHAIN
if (settings.spotify.keyring_password)
{
password = keyring.readPassword();
password = Keychain::getPassword(username);
}
#endif

Expand All @@ -81,10 +84,10 @@ auto SpotifyClient::Runner::start() -> QString
return "no password provided";
}

#ifdef USE_DBUS
#ifdef USE_KEYCHAIN
if (settings.spotify.keyring_password)
{
keyring.writePassword(password);
Keychain::setPassword(username, password);
}
#endif
}
Expand Down
1 change: 0 additions & 1 deletion src/spotifyclient/runner.hpp
Expand Up @@ -5,7 +5,6 @@
#include "lib/logmessage.hpp"

#include "spotifyclient/helper.hpp"
#include "keyring/kwallet.hpp"

#include <QDateTime>
#include <QFileInfo>
Expand Down
1 change: 1 addition & 0 deletions src/util/CMakeLists.txt
Expand Up @@ -5,6 +5,7 @@ target_sources(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/http.cpp
${CMAKE_CURRENT_SOURCE_DIR}/icon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/image.cpp
${CMAKE_CURRENT_SOURCE_DIR}/keychain.cpp
${CMAKE_CURRENT_SOURCE_DIR}/menuaction.cpp
${CMAKE_CURRENT_SOURCE_DIR}/refresher.cpp
${CMAKE_CURRENT_SOURCE_DIR}/shortcut.cpp
Expand Down
35 changes: 35 additions & 0 deletions src/util/keychain.cpp
@@ -0,0 +1,35 @@
#ifdef USE_KEYCHAIN

#include "util/keychain.hpp"
#include "thirdparty/keychain.h"
#include "lib/log.hpp"

auto Keychain::getPassword(const QString &user) -> QString
{
keychain::Error error;
const auto password = keychain::getPassword(PKG_NAME, APP_NAME, user.toStdString(), error);

if (error)
{
lib::log::warn("Failed to get password: {}", error.message);
return {};
}

return QString::fromStdString(password);
}

auto Keychain::setPassword(const QString &user, const QString &password) -> bool
{
keychain::Error error;
keychain::setPassword(PKG_NAME, APP_NAME, user.toStdString(), password.toStdString(), error);

if (error)
{
lib::log::warn("Failed to set password: {}", error.message);
return false;
}

return true;
}

#endif
17 changes: 17 additions & 0 deletions src/util/keychain.hpp
@@ -0,0 +1,17 @@
#pragma once

#ifdef USE_KEYCHAIN

#include <QString>

class Keychain
{
public:
static auto getPassword(const QString &user) -> QString;
static auto setPassword(const QString &user, const QString &password) -> bool;

private:
Keychain() = default;
};

#endif

0 comments on commit afa7323

Please sign in to comment.