Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OTP code generation on Linux as requested in #327 #394

Merged
merged 10 commits into from
May 25, 2018
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ ui_*.h
localization/*.qm
build/
.idea
.vscode
15 changes: 15 additions & 0 deletions src/configdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ ConfigDialog::ConfigDialog(MainWindow *parent)
ui->checkBoxAutoPush->setChecked(QtPassSettings::isAutoPush());
ui->checkBoxAlwaysOnTop->setChecked(QtPassSettings::isAlwaysOnTop());

#if defined(Q_OS_WIN ) || defined(__APPLE__)
ui->checkBoxUseOtp->hide();
ui->label_10->hide();
#endif

setProfiles(QtPassSettings::getProfiles(), QtPassSettings::getProfile());
setPwgenPath(QtPassSettings::getPwgenExecutable());
setPasswordConfiguration(QtPassSettings::getPasswordConfiguration());
Expand All @@ -54,6 +59,7 @@ ConfigDialog::ConfigDialog(MainWindow *parent)
useAutoclearPanel(QtPassSettings::isUseAutoclearPanel());
useTrayIcon(QtPassSettings::isUseTrayIcon());
useGit(QtPassSettings::isUseGit());
useOtp(QtPassSettings::isUseOtp());
usePwgen(QtPassSettings::isUsePwgen());
useTemplate(QtPassSettings::isUseTemplate());

Expand Down Expand Up @@ -137,6 +143,7 @@ void ConfigDialog::on_accepted() {
QtPassSettings::setStartMinimized(ui->checkBoxStartMinimized->isChecked());
QtPassSettings::setProfiles(getProfiles());
QtPassSettings::setUseGit(ui->checkBoxUseGit->isChecked());
QtPassSettings::setUseOtp(ui->checkBoxUseOtp->isChecked());
QtPassSettings::setPwgenExecutable(ui->pwgenPath->text());
QtPassSettings::setUsePwgen(ui->checkBoxUsePwgen->isChecked());
QtPassSettings::setAvoidCapitals(ui->checkBoxAvoidCapitals->isChecked());
Expand Down Expand Up @@ -558,6 +565,14 @@ void ConfigDialog::useGit(bool useGit) {
on_checkBoxUseGit_clicked();
}

/**
* @brief ConfigDialog::useOtp set preference for using otp plugin.
* @param useOtp
*/
void ConfigDialog::useOtp(bool useOtp) {
ui->checkBoxUseOtp->setChecked(useOtp);
}

/**
* @brief ConfigDialog::on_checkBoxUseGit_clicked enable or disable related
* checkboxes.
Expand Down
1 change: 1 addition & 0 deletions src/configdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ConfigDialog : public QDialog {
bool hideOnClose();
void useTrayIcon(bool useTrayIdon);
void useGit(bool useGit);
void useOtp(bool useOtp);
void setPwgenPath(QString);
void usePwgen(bool usePwgen);
void setPasswordConfiguration(const PasswordConfiguration &config);
Expand Down
31 changes: 31 additions & 0 deletions src/configdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,37 @@
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="Extensions">
<item>
<widget class="QLabel" name="label_10">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Extensions:</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_14">
<item>
<widget class="QCheckBox" name="checkBoxUseOtp">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Use pass otp extension</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="Layout_System">
<property name="topMargin">
Expand Down
3 changes: 2 additions & 1 deletion src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ enum PROCESS {
GIT_MOVE,
GIT_COPY,
PROCESS_COUNT,
INVALID
INVALID,
PASS_OTP_GENERATE,
};

} // namespace Enums
Expand Down
7 changes: 6 additions & 1 deletion src/imitatepass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ void ImitatePass::GitPush() {
/**
* @brief ImitatePass::Show shows content of file
*/

void ImitatePass::Show(QString file) {
file = QtPassSettings::getPassStore() + file + ".gpg";
QStringList args = {"-d", "--quiet", "--yes", "--no-encrypt-to",
"--batch", "--use-agent", file};
executeGpg(PASS_SHOW, args);

}

/**
* @brief ImitatePass::OtpGenerate generates an otp code
*/
void ImitatePass::OtpGenerate(QString file) {}

/**
* @brief ImitatePass::Insert create new file with encrypted content
*
Expand Down
1 change: 1 addition & 0 deletions src/imitatepass.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ImitatePass : public Pass, private simpleTransaction {
virtual void GitPull_b() Q_DECL_OVERRIDE;
virtual void GitPush() Q_DECL_OVERRIDE;
virtual void Show(QString file) Q_DECL_OVERRIDE;
virtual void OtpGenerate(QString file) Q_DECL_OVERRIDE;
virtual void Insert(QString file, QString value,
bool overwrite = false) Q_DECL_OVERRIDE;
virtual void Remove(QString file, bool isDir = false) Q_DECL_OVERRIDE;
Expand Down
37 changes: 37 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void MainWindow::initToolBarButtons() {
connect(ui->actionUpdate, SIGNAL(triggered()), this, SLOT(onUpdate()));
connect(ui->actionUsers, SIGNAL(triggered()), this, SLOT(onUsers()));
connect(ui->actionConfig, SIGNAL(triggered()), this, SLOT(onConfig()));
connect(ui->actionOtp, SIGNAL(triggered()), this, SLOT(onOtp()));

ui->actionAddPassword->setIcon(
QIcon::fromTheme("document-new", QIcon(":/icons/document-new.svg")));
Expand Down Expand Up @@ -196,6 +197,7 @@ void MainWindow::connectPassSignalHandlers(Pass *pass) {
connect(pass, &Pass::finishedGitPull, this, &MainWindow::processFinished);
connect(pass, &Pass::finishedGitPush, this, &MainWindow::processFinished);
connect(pass, &Pass::finishedShow, this, &MainWindow::passShowHandler);
connect(pass, &Pass::finishedOtpGenerate, this, &MainWindow::passOtpHandler);
connect(pass, &Pass::finishedInsert, this, &MainWindow::finishedInsert);
connect(pass, &Pass::finishedRemove, this, &MainWindow::passStoreChanged);
connect(pass, &Pass::finishedInit, this, &MainWindow::passStoreChanged);
Expand Down Expand Up @@ -377,6 +379,7 @@ bool MainWindow::checkConfig() {
QtPassSettings::getAutoclearPanelSeconds());
clearClipboardTimer.setInterval(1000 * QtPassSettings::getAutoclearSeconds());
updateGitButtonVisibility();
updateOtpButtonVisibility();

startupPhase = false;
return true;
Expand Down Expand Up @@ -419,6 +422,7 @@ void MainWindow::config() {
QtPassSettings::getAutoclearSeconds());

updateGitButtonVisibility();
updateOtpButtonVisibility();
if (QtPassSettings::isUseTrayIcon() && tray == NULL)
initTrayIcon();
else if (!QtPassSettings::isUseTrayIcon() && tray != NULL) {
Expand Down Expand Up @@ -592,6 +596,17 @@ void MainWindow::passShowHandler(const QString &p_output) {
enableUiElements(true);
}

void MainWindow::passOtpHandler(const QString &p_output) {
if (!p_output.isEmpty()) {
addToGridLayout(ui->gridLayout->count()+1, tr("OTP Code"), p_output);
copyTextToClipboard(p_output);
}
if (QtPassSettings::isUseAutoclearPanel()) {
clearPanelTimer.start();
}
enableUiElements(true);
}

void MainWindow::passStoreChanged(const QString &p_out, const QString &p_err) {
processFinished(p_out, p_err);
doGitPush();
Expand Down Expand Up @@ -723,6 +738,7 @@ void MainWindow::enableUiElements(bool state) {
ui->actionDelete->setEnabled(state);
ui->actionEdit->setEnabled(state);
updateGitButtonVisibility();
updateOtpButtonVisibility();
}

void MainWindow::restoreWindow() {
Expand Down Expand Up @@ -915,6 +931,17 @@ void MainWindow::onDelete() {
QtPassSettings::getPass()->Remove(file, isDir);
}

/**
* @brief MainWindow::onOTP try and generate (selected) OTP code.
*/
void MainWindow::onOtp() {
QString file = getFile(ui->treeView->currentIndex(), true);
if (!file.isEmpty()) {
if (QtPassSettings::isUseOtp())
QtPassSettings::getPass()->OtpGenerate(file);
}
}

/**
* @brief MainWindow::onEdit try and edit (selected) password.
*/
Expand Down Expand Up @@ -1428,6 +1455,16 @@ void MainWindow::updateGitButtonVisibility() {
}
}

void MainWindow::updateOtpButtonVisibility() {
#if defined(Q_OS_WIN ) || defined(__APPLE__)
ui->actionOtp->setVisible(false);
#endif
if (!QtPassSettings::isUseOtp())
ui->actionOtp->setEnabled(false);
else
ui->actionOtp->setEnabled(true);
}

void MainWindow::enableGitButtons(const bool &state) {
// Following GNOME guidelines is preferable disable buttons instead of hide
ui->actionPush->setEnabled(state);
Expand Down
3 changes: 3 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private slots:
void addFolder();
void onEdit();
void onDelete();
void onOtp();
void onPush();
void onUpdate(bool block = false);
void onUsers();
Expand Down Expand Up @@ -92,6 +93,7 @@ private slots:
void endReencryptPath();
void critical(QString, QString);
void passShowHandler(const QString &);
void passOtpHandler(const QString &);
void passStoreChanged(const QString &, const QString &);
void doGitPush();

Expand Down Expand Up @@ -139,6 +141,7 @@ private slots:
void connectPassSignalHandlers(Pass *pass);

void updateGitButtonVisibility();
void updateOtpButtonVisibility();
void enableGitButtons(const bool &);
};

Expand Down
12 changes: 12 additions & 0 deletions src/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ p, li { white-space: pre-wrap; }
<addaction name="separator"/>
<addaction name="actionEdit"/>
<addaction name="actionDelete"/>
<addaction name="actionOtp"/>
<addaction name="separator"/>
<addaction name="actionPush"/>
<addaction name="actionUpdate"/>
Expand Down Expand Up @@ -361,6 +362,17 @@ p, li { white-space: pre-wrap; }
<string>Delete</string>
</property>
</action>
<action name="actionOtp">
<property name="text">
<string>OTP</string>
</property>
<property name="toolTip">
<string>Generate OTP and copy to clipboard</string>
</property>
<property name="shortcut">
<string>Ctrl+G</string>
</property>
</action>
<action name="actionPush">
<property name="text">
<string>Push</string>
Expand Down
3 changes: 3 additions & 0 deletions src/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ void Pass::finished(int id, int exitCode, const QString &out,
case PASS_SHOW:
emit finishedShow(out);
break;
case PASS_OTP_GENERATE:
emit finishedOtpGenerate(out);
break;
case PASS_INSERT:
emit finishedInsert(out, err);
break;
Expand Down
2 changes: 2 additions & 0 deletions src/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Pass : public QObject {
virtual void GitPull_b() = 0;
virtual void GitPush() = 0;
virtual void Show(QString file) = 0;
virtual void OtpGenerate(QString file) = 0;
virtual void Insert(QString file, QString value, bool force) = 0;
virtual void Remove(QString file, bool isDir) = 0;
virtual void Move(const QString srcDir, const QString dest,
Expand Down Expand Up @@ -87,6 +88,7 @@ protected slots:
void finishedGitPull(const QString &, const QString &);
void finishedGitPush(const QString &, const QString &);
void finishedShow(const QString &);
void finishedOtpGenerate(const QString &);
void finishedInsert(const QString &, const QString &);
void finishedRemove(const QString &, const QString &);
void finishedInit(const QString &, const QString &);
Expand Down
8 changes: 8 additions & 0 deletions src/qtpasssettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ void QtPassSettings::setUseGit(const bool &useGit) {
getInstance()->setValue(SettingsConstants::useGit, useGit);
}

bool QtPassSettings::isUseOtp(const bool &defaultValue) {
return getInstance()->value(SettingsConstants::useOtp, defaultValue).toBool();
}

void QtPassSettings::setUseOtp(const bool &useOtp) {
getInstance()->setValue(SettingsConstants::useOtp, useOtp);
}

bool QtPassSettings::isUsePwgen(const bool &defaultValue) {
return getInstance()
->value(SettingsConstants::usePwgen, defaultValue)
Expand Down
3 changes: 3 additions & 0 deletions src/qtpasssettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class QtPassSettings : public QSettings {
static bool isUseGit(const bool &defaultValue = QVariant().toBool());
static void setUseGit(const bool &useGit);

static bool isUseOtp(const bool &defaultValue = QVariant().toBool());
static void setUseOtp(const bool &useOtp);

static bool isUsePwgen(const bool &defaultValue = QVariant().toBool());
static void setUsePwgen(const bool &usePwgen);

Expand Down
9 changes: 9 additions & 0 deletions src/realpass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ void RealPass::Show(QString file) {
executePass(PASS_SHOW, {"show", file}, "", true);
}

/**
* @brief RealPass::OtpGenerate pass otp
* @param file file containig OTP uri
*/
void RealPass::OtpGenerate(QString file) {
executePass(PASS_OTP_GENERATE, {"otp", file}, "", true);
}


/**
* @brief RealPass::Insert pass insert
*/
Expand Down
1 change: 1 addition & 0 deletions src/realpass.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class RealPass : public Pass {
virtual void GitPull_b() Q_DECL_OVERRIDE;
virtual void GitPush() Q_DECL_OVERRIDE;
virtual void Show(QString file) Q_DECL_OVERRIDE;
virtual void OtpGenerate(QString file) Q_DECL_OVERRIDE;
virtual void Insert(QString file, QString value,
bool overwrite = false) Q_DECL_OVERRIDE;
virtual void Remove(QString file, bool isDir = false) Q_DECL_OVERRIDE;
Expand Down
1 change: 1 addition & 0 deletions src/settingsconstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const QString SettingsConstants::webDavPassword = "webDavPassword";
const QString SettingsConstants::profile = "profile";
const QString SettingsConstants::groupProfiles = "profiles";
const QString SettingsConstants::useGit = "useGit";
const QString SettingsConstants::useOtp = "useOtp";
const QString SettingsConstants::useClipboard = "useClipboard";
const QString SettingsConstants::usePwgen = "usePwgen";
const QString SettingsConstants::avoidCapitals = "avoidCapitals";
Expand Down
1 change: 1 addition & 0 deletions src/settingsconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class SettingsConstants {
const static QString profile;
const static QString groupProfiles;
const static QString useGit;
const static QString useOtp;
const static QString useClipboard;
const static QString usePwgen;
const static QString avoidCapitals;
Expand Down