mirror of
https://github.com/status-im/react-native-keychain.git
synced 2025-01-12 22:34:12 +00:00
Added basic support of desktop platform
This commit is contained in:
parent
5a6e5b6ba8
commit
6536405d1c
32
desktop/CMakeLists.txt
Normal file
32
desktop/CMakeLists.txt
Normal file
@ -0,0 +1,32 @@
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
set(REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_TYPE_NAMES ${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_TYPE_NAMES}
|
||||
\"RNKeychainManager\" PARENT_SCOPE)
|
||||
|
||||
set(REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_SRC ${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_SRC}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rnkeychainmanager.cpp PARENT_SCOPE)
|
||||
|
||||
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
|
||||
|
||||
|
||||
set(qtkeychain_ROOT ${CMAKE_CURRENT_BINARY_DIR}/qtkeychain)
|
||||
set(qtkeychain_BUILDDIR ${qtkeychain_ROOT}/build)
|
||||
set(qtkeychain_STATIC_LIB ${qtkeychain_BUILDDIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}qt5keychain${CMAKE_STATIC_LIBRARY_SUFFIX})
|
||||
set(qtkeychain_INCLUDE_DIR ${qtkeychain_BUILDDIR}/include/qt5keychain)
|
||||
|
||||
|
||||
ExternalProject_Add(qtkeychain
|
||||
GIT_REPOSITORY https://github.com/frankosterfeld/qtkeychain.git
|
||||
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${qtkeychain_BUILDDIR} -DQTKEYCHAIN_STATIC=ON
|
||||
BUILD_BYPRODUCTS ${qtkeychain_STATIC_LIB}
|
||||
LOG_BUILD 1
|
||||
LOG_DOWNLOAD 1
|
||||
)
|
||||
|
||||
set(REACT_NATIVE_DESKTOP_EXTERNAL_PROJECT_DEPS ${REACT_NATIVE_DESKTOP_EXTERNAL_PROJECT_DEPS} qtkeychain PARENT_SCOPE)
|
||||
|
||||
set(REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_LIBS ${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_LIBS}
|
||||
${qtkeychain_STATIC_LIB} PARENT_SCOPE)
|
||||
|
||||
set(REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_INCLUDE_DIRS ${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_INCLUDE_DIRS}
|
||||
${qtkeychain_INCLUDE_DIR} PARENT_SCOPE)
|
131
desktop/rnkeychainmanager.cpp
Normal file
131
desktop/rnkeychainmanager.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
/**
|
||||
* Copyright (c) 2017-present, Status Research and Development GmbH.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "rnkeychainmanager.h"
|
||||
#include "bridge.h"
|
||||
#include "eventdispatcher.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QVariant>
|
||||
#include <QByteArray>
|
||||
#include <QEventLoop>
|
||||
|
||||
#include "keychain.h"
|
||||
using namespace QKeychain;
|
||||
|
||||
static const QString keychainJobName("org.status.status-im.keychain");
|
||||
|
||||
namespace {
|
||||
struct RegisterQMLMetaType {
|
||||
RegisterQMLMetaType() {
|
||||
qRegisterMetaType<RNKeychainManager*>();
|
||||
}
|
||||
} registerMetaType;
|
||||
} // namespace
|
||||
|
||||
class RNKeychainManagerPrivate {
|
||||
public:
|
||||
Bridge* bridge = nullptr;
|
||||
};
|
||||
|
||||
RNKeychainManager::RNKeychainManager(QObject* parent) : QObject(parent), d_ptr(new RNKeychainManagerPrivate) {}
|
||||
|
||||
RNKeychainManager::~RNKeychainManager() {}
|
||||
|
||||
void RNKeychainManager::setBridge(Bridge* bridge) {
|
||||
Q_D(RNKeychainManager);
|
||||
d->bridge = bridge;
|
||||
}
|
||||
|
||||
QString RNKeychainManager::moduleName() {
|
||||
return "RNKeychainManager";
|
||||
}
|
||||
|
||||
QList<ModuleMethod*> RNKeychainManager::methodsToExport() {
|
||||
return QList<ModuleMethod*>{};
|
||||
}
|
||||
|
||||
QVariantMap RNKeychainManager::constantsToExport() {
|
||||
return QVariantMap();
|
||||
}
|
||||
|
||||
void RNKeychainManager::getGenericPasswordForOptions(QVariantList options,
|
||||
const ModuleInterface::ListArgumentBlock& resolve,
|
||||
const ModuleInterface::ListArgumentBlock& reject) {
|
||||
Q_D(RNKeychainManager);
|
||||
qDebug()<<"invoked RNKeychainManager::getGenericPasswordForOptions";
|
||||
|
||||
ReadPasswordJob rjob( keychainJobName );
|
||||
rjob.setAutoDelete( false );
|
||||
QEventLoop loop;
|
||||
rjob.connect( &rjob, SIGNAL(finished(QKeychain::Job*)), &loop, SLOT(quit()) );
|
||||
rjob.start();
|
||||
loop.exec();
|
||||
|
||||
const QString pw = rjob.textData();
|
||||
if ( rjob.error() ) {
|
||||
qDebug() << "RNKeychainManager::getGenericPasswordForOptions failed: " << qPrintable(rjob.errorString());
|
||||
resolve(d->bridge, QVariantList{QVariant{false}});
|
||||
return;
|
||||
}
|
||||
qDebug() << "RNKeychainManager::getGenericPasswordForOptions success";
|
||||
resolve(d->bridge, QVariantList{QVariantMap{{"password", pw}}});
|
||||
}
|
||||
|
||||
void RNKeychainManager::setGenericPasswordForOptions(QVariantList options,
|
||||
const QString &username,
|
||||
const QString &password,
|
||||
const ModuleInterface::ListArgumentBlock &resolve,
|
||||
const ModuleInterface::ListArgumentBlock &reject) {
|
||||
Q_D(RNKeychainManager);
|
||||
qDebug()<<"invoked RNKeychainManager::setGenericPasswordForOptions";
|
||||
|
||||
WritePasswordJob wjob( keychainJobName);
|
||||
wjob.setAutoDelete( false );
|
||||
wjob.setKey( username );
|
||||
wjob.setTextData( password );
|
||||
QEventLoop loop;
|
||||
wjob.connect( &wjob, SIGNAL(finished(QKeychain::Job*)), &loop, SLOT(quit()) );
|
||||
wjob.start();
|
||||
loop.exec();
|
||||
if ( wjob.error() ) {
|
||||
qDebug() << "RNKeychainManager::setGenericPasswordForOptions failed: " << qPrintable(wjob.errorString());
|
||||
reject(d->bridge, QVariantList{});
|
||||
return;
|
||||
}
|
||||
qDebug() << "RNKeychainManager::setGenericPasswordForOptions success";
|
||||
resolve(d->bridge, QVariantList{QVariant(true)});
|
||||
}
|
||||
|
||||
void RNKeychainManager::resetGenericPasswordForOptions(QVariantList options,
|
||||
const ModuleInterface::ListArgumentBlock &resolve,
|
||||
const ModuleInterface::ListArgumentBlock &reject){
|
||||
Q_D(RNKeychainManager);
|
||||
qDebug()<<"invoked RNKeychainManager::resetGenericPasswordForOptions";
|
||||
|
||||
DeletePasswordJob wjob( keychainJobName);
|
||||
wjob.setAutoDelete( false );
|
||||
QEventLoop loop;
|
||||
wjob.connect( &wjob, SIGNAL(finished(QKeychain::Job*)), &loop, SLOT(quit()) );
|
||||
wjob.start();
|
||||
loop.exec();
|
||||
if ( wjob.error() ) {
|
||||
qDebug() << "RNKeychainManager::resetGenericPasswordForOptions failed: " << qPrintable(wjob.errorString());
|
||||
reject(d->bridge, QVariantList{});
|
||||
return;
|
||||
}
|
||||
qDebug() << "RNKeychainManager::resetGenericPasswordForOptions success";
|
||||
resolve(d->bridge, QVariantList{});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
55
desktop/rnkeychainmanager.h
Normal file
55
desktop/rnkeychainmanager.h
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright (c) 2017-present, Status Research and Development GmbH.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef RNKEYCHAINMANAGER_H
|
||||
#define RNKEYCHAINMANAGER_H
|
||||
|
||||
#include "moduleinterface.h"
|
||||
|
||||
#include <QVariantMap>
|
||||
#include <QDateTime>
|
||||
|
||||
class RNKeychainManagerPrivate;
|
||||
class RNKeychainManager : public QObject, public ModuleInterface {
|
||||
Q_OBJECT
|
||||
Q_INTERFACES(ModuleInterface)
|
||||
|
||||
Q_DECLARE_PRIVATE(RNKeychainManager)
|
||||
|
||||
public:
|
||||
Q_INVOKABLE RNKeychainManager(QObject* parent = 0);
|
||||
~RNKeychainManager();
|
||||
|
||||
void setBridge(Bridge* bridge) override;
|
||||
|
||||
QString moduleName() override;
|
||||
QList<ModuleMethod*> methodsToExport() override;
|
||||
QVariantMap constantsToExport() override;
|
||||
|
||||
Q_INVOKABLE REACT_PROMISE void getGenericPasswordForOptions(QVariantList options,
|
||||
const ModuleInterface::ListArgumentBlock& resolve,
|
||||
const ModuleInterface::ListArgumentBlock& reject);
|
||||
Q_INVOKABLE REACT_PROMISE void setGenericPasswordForOptions(QVariantList options,
|
||||
const QString& username,
|
||||
const QString& password,
|
||||
const ModuleInterface::ListArgumentBlock& resolve,
|
||||
const ModuleInterface::ListArgumentBlock& reject);
|
||||
Q_INVOKABLE REACT_PROMISE void resetGenericPasswordForOptions(QVariantList options,
|
||||
const ModuleInterface::ListArgumentBlock& resolve,
|
||||
const ModuleInterface::ListArgumentBlock& reject);
|
||||
|
||||
private:
|
||||
QScopedPointer<RNKeychainManagerPrivate> d_ptr;
|
||||
|
||||
bool copyRecursively(const QString& src, const QString& dst);
|
||||
bool removeRecursively(const QString& path);
|
||||
};
|
||||
|
||||
#endif // RNKEYCHAINMANAGER_H
|
Loading…
x
Reference in New Issue
Block a user