Skip to content

Commit

Permalink
Fix TouchID not being shown after lid close
Browse files Browse the repository at this point in the history
Fixes #8945
Fixes #10315
  • Loading branch information
phoerious committed Feb 22, 2024
1 parent 14265f5 commit 7d46880
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 55 deletions.
104 changes: 59 additions & 45 deletions src/gui/DatabaseOpenWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <QCloseEvent>
#include <QDesktopServices>
#include <QFont>

namespace
{
constexpr int clearFormsDelay = 30000;
Expand All @@ -45,15 +46,6 @@ namespace
}
return false;
}

bool canPerformQuickUnlock(const QUuid& dbUuid)
{
if (isQuickUnlockAvailable()) {
return getQuickUnlock()->hasKey(dbUuid);
}
Q_UNUSED(dbUuid);
return false;
}
} // namespace

DatabaseOpenWidget::DatabaseOpenWidget(QWidget* parent)
Expand Down Expand Up @@ -130,9 +122,7 @@ DatabaseOpenWidget::DatabaseOpenWidget(QWidget* parent)

m_ui->noHardwareKeysFoundLabel->setVisible(false);
m_ui->refreshHardwareKeys->setIcon(icons()->icon("yubikey-refresh", true));
connect(m_ui->refreshHardwareKeys, &QPushButton::clicked, this, [this] {
pollHardwareKey(true);
});
connect(m_ui->refreshHardwareKeys, &QPushButton::clicked, this, [this] { pollHardwareKey(true); });
m_hideNoHardwareKeysFoundTimer.setInterval(2000);
connect(&m_hideNoHardwareKeysFoundTimer, &QTimer::timeout, this, [this] {
m_ui->noHardwareKeysFoundLabel->setVisible(false);
Expand Down Expand Up @@ -176,44 +166,57 @@ void DatabaseOpenWidget::toggleHardwareKeyComponent(bool state)
}
}

void DatabaseOpenWidget::showEvent(QShowEvent* event)
bool DatabaseOpenWidget::event(QEvent* event)
{
DialogyWidget::showEvent(event);
if (isOnQuickUnlockScreen()) {
m_ui->quickUnlockButton->setFocus();
if (m_db.isNull() || !canPerformQuickUnlock(m_db->publicUuid())) {
bool ret = DialogyWidget::event(event);

switch (event->type()) {
case QEvent::Show:
case QEvent::WindowActivate: {
if (isOnQuickUnlockScreen() && (m_db.isNull() || !canPerformQuickUnlock())) {
resetQuickUnlock();
}
} else {
m_ui->editPassword->setFocus();
}
m_hideTimer.stop();
toggleQuickUnlockScreen();
m_hideTimer.stop();

#ifdef WITH_XC_YUBIKEY
#ifdef Q_OS_WIN
m_deviceListener->registerHotplugCallback(
true, true, YubiKeyInterfaceUSB::YUBICO_USB_VID, DeviceListener::MATCH_ANY, &DeviceListenerWin::DEV_CLS_KEYBOARD);
m_deviceListener->registerHotplugCallback(
true, true, YubiKeyInterfaceUSB::ONLYKEY_USB_VID, DeviceListener::MATCH_ANY, &DeviceListenerWin::DEV_CLS_KEYBOARD);
m_deviceListener->registerHotplugCallback(true,
true,
YubiKeyInterfaceUSB::YUBICO_USB_VID,
DeviceListener::MATCH_ANY,
&DeviceListenerWin::DEV_CLS_KEYBOARD);
m_deviceListener->registerHotplugCallback(true,
true,
YubiKeyInterfaceUSB::ONLYKEY_USB_VID,
DeviceListener::MATCH_ANY,
&DeviceListenerWin::DEV_CLS_KEYBOARD);
#else
m_deviceListener->registerHotplugCallback(true, true, YubiKeyInterfaceUSB::YUBICO_USB_VID);
m_deviceListener->registerHotplugCallback(true, true, YubiKeyInterfaceUSB::ONLYKEY_USB_VID);
m_deviceListener->registerHotplugCallback(true, true, YubiKeyInterfaceUSB::YUBICO_USB_VID);
m_deviceListener->registerHotplugCallback(true, true, YubiKeyInterfaceUSB::ONLYKEY_USB_VID);
#endif
#endif
}

void DatabaseOpenWidget::hideEvent(QHideEvent* event)
{
DialogyWidget::hideEvent(event);

// Schedule form clearing if we are hidden
if (!isVisible()) {
m_hideTimer.start();
return true;
}

case QEvent::Hide: {
// Schedule form clearing if we are hidden
if (!isVisible()) {
m_hideTimer.start();
}

#ifdef WITH_XC_YUBIKEY
m_deviceListener->deregisterAllHotplugCallbacks();
m_deviceListener->deregisterAllHotplugCallbacks();
#endif

return true;
}

default:;
}

return ret;
}

bool DatabaseOpenWidget::unlockingDatabase()
Expand Down Expand Up @@ -242,12 +245,7 @@ void DatabaseOpenWidget::load(const QString& filename)
}
}

if (canPerformQuickUnlock(m_db->publicUuid())) {
m_ui->centralStack->setCurrentIndex(1);
m_ui->quickUnlockButton->setFocus();
} else {
m_ui->editPassword->setFocus();
}
toggleQuickUnlockScreen();

#ifdef WITH_XC_YUBIKEY
// Do initial auto-poll
Expand All @@ -264,7 +262,7 @@ void DatabaseOpenWidget::clearForms()
m_ui->keyFileLineEdit->setShowPassword(false);
m_ui->keyFileLineEdit->setClearButtonEnabled(true);
m_ui->hardwareKeyCombo->clear();
m_ui->centralStack->setCurrentIndex(0);
toggleQuickUnlockScreen();

QString error;
m_db.reset(new Database());
Expand Down Expand Up @@ -383,7 +381,7 @@ QSharedPointer<CompositeKey> DatabaseOpenWidget::buildDatabaseKey()
{
auto databaseKey = QSharedPointer<CompositeKey>::create();

if (!m_db.isNull() && canPerformQuickUnlock(m_db->publicUuid())) {
if (!m_db.isNull() && canPerformQuickUnlock()) {
// try to retrieve the stored password using Windows Hello
QByteArray keyData;
if (!getQuickUnlock()->getKey(m_db->publicUuid(), keyData)) {
Expand Down Expand Up @@ -570,11 +568,27 @@ void DatabaseOpenWidget::setUserInteractionLock(bool state)
m_unlockingDatabase = state;
}

bool DatabaseOpenWidget::isOnQuickUnlockScreen()
bool DatabaseOpenWidget::canPerformQuickUnlock() const
{
return !m_db.isNull() && isQuickUnlockAvailable() && getQuickUnlock()->hasKey(m_db->publicUuid());
}

bool DatabaseOpenWidget::isOnQuickUnlockScreen() const
{
return m_ui->centralStack->currentIndex() == 1;
}

void DatabaseOpenWidget::toggleQuickUnlockScreen()
{
if (canPerformQuickUnlock()) {
m_ui->centralStack->setCurrentIndex(1);
m_ui->quickUnlockButton->setFocus();
} else {
m_ui->centralStack->setCurrentIndex(0);
m_ui->editPassword->setFocus();
}
}

void DatabaseOpenWidget::triggerQuickUnlock()
{
if (isOnQuickUnlockScreen()) {
Expand Down
7 changes: 4 additions & 3 deletions src/gui/DatabaseOpenWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ class DatabaseOpenWidget : public DialogyWidget
bool unlockingDatabase();

// Quick Unlock helper functions
bool isOnQuickUnlockScreen();
bool canPerformQuickUnlock() const;
bool isOnQuickUnlockScreen() const;
void toggleQuickUnlockScreen();
void triggerQuickUnlock();
void resetQuickUnlock();

signals:
void dialogFinished(bool accepted);

protected:
void showEvent(QShowEvent* event) override;
void hideEvent(QHideEvent* event) override;
bool event(QEvent* event) override;
QSharedPointer<CompositeKey> buildDatabaseKey();
void setUserInteractionLock(bool state);

Expand Down
14 changes: 10 additions & 4 deletions src/gui/databasekey/YubiKeyEditWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,16 @@ void YubiKeyEditWidget::showEvent(QShowEvent* event)

#ifdef WITH_XC_YUBIKEY
#ifdef Q_OS_WIN
m_deviceListener->registerHotplugCallback(
true, true, YubiKeyInterfaceUSB::YUBICO_USB_VID, DeviceListener::MATCH_ANY, &DeviceListenerWin::DEV_CLS_KEYBOARD);
m_deviceListener->registerHotplugCallback(
true, true, YubiKeyInterfaceUSB::ONLYKEY_USB_VID, DeviceListener::MATCH_ANY, &DeviceListenerWin::DEV_CLS_KEYBOARD);
m_deviceListener->registerHotplugCallback(true,
true,
YubiKeyInterfaceUSB::YUBICO_USB_VID,
DeviceListener::MATCH_ANY,
&DeviceListenerWin::DEV_CLS_KEYBOARD);
m_deviceListener->registerHotplugCallback(true,
true,
YubiKeyInterfaceUSB::ONLYKEY_USB_VID,
DeviceListener::MATCH_ANY,
&DeviceListenerWin::DEV_CLS_KEYBOARD);
#else
m_deviceListener->registerHotplugCallback(true, true, YubiKeyInterfaceUSB::YUBICO_USB_VID);
m_deviceListener->registerHotplugCallback(true, true, YubiKeyInterfaceUSB::ONLYKEY_USB_VID);
Expand Down
4 changes: 1 addition & 3 deletions src/quickunlock/TouchID.mm
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,7 @@ inline CFMutableDictionaryRef makeDictionary() {
// note: we cannot cache the check results because the configuration
// is dynamic in its nature. User can close the laptop lid or take off
// the watch, thus making one (or both) of the authentication types unavailable.
const bool watchAvailable = isWatchAvailable();
const bool touchIdAvailable = isTouchIdAvailable();
return watchAvailable || touchIdAvailable;
return isWatchAvailable() || isTouchIdAvailable();
}

/**
Expand Down

0 comments on commit 7d46880

Please sign in to comment.