feat: detect if biometrics was enabled

This commit is contained in:
Igor Sirotin 2025-02-22 12:44:35 +03:00
parent 1f0d65ada5
commit dcdb910f4f
No known key found for this signature in database
GPG Key ID: 425E227CAAB81F95
2 changed files with 21 additions and 5 deletions

View File

@ -14,7 +14,7 @@ class Keychain : public QObject {
Q_PROPERTY(QString service READ service WRITE setService NOTIFY serviceChanged) Q_PROPERTY(QString service READ service WRITE setService NOTIFY serviceChanged)
Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged) Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
Q_PROPERTY(bool available READ available CONSTANT) Q_PROPERTY(bool available READ available NOTIFY availableChanged)
public: public:
explicit Keychain(QObject *parent = nullptr); explicit Keychain(QObject *parent = nullptr);
@ -50,6 +50,7 @@ signals:
void serviceChanged(); void serviceChanged();
void reasonChanged(); void reasonChanged();
void loadingChanged(); void loadingChanged();
void availableChanged();
private: private:
QString m_service; QString m_service;

View File

@ -3,6 +3,7 @@
#include <QDebug> #include <QDebug>
#include <QEventLoop> #include <QEventLoop>
#include <QFuture> #include <QFuture>
#include <QGuiApplication>
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
#include <Foundation/Foundation.h> #include <Foundation/Foundation.h>
@ -50,6 +51,15 @@ Keychain::Keychain(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
reevaluateAvailability(); reevaluateAvailability();
connect(qApp,
&QGuiApplication::applicationStateChanged,
this,
[this](Qt::ApplicationState state) {
if (state == Qt::ApplicationActive) {
reevaluateAvailability();
}
});
} }
Keychain::~Keychain() Keychain::~Keychain()
@ -213,12 +223,17 @@ void Keychain::reevaluateAvailability()
auto context = [[LAContext alloc] init]; auto context = [[LAContext alloc] init];
NSError *authError = nil; NSError *authError = nil;
m_available = [context canEvaluatePolicy:authPolicy error:&authError]; const auto available = [context canEvaluatePolicy:authPolicy error:&authError];
if (!m_available) { // Later this description can be used if needed:
const auto description = QString::fromNSString(authError.localizedDescription); // const auto description = QString::fromNSString(authError.localizedDescription);
qDebug() << "Keychain is not available" << description;
if (m_available == available) {
return;
} }
m_available = available;
emit availableChanged();
} }
Keychain::Status Keychain::hasCredential(const QString &account) const Keychain::Status Keychain::hasCredential(const QString &account) const