CLI password generation options cleanup (#3275)

Summary of changes:
* Extract function for creating password generator from options into
`Generate` command. This function is now reused in `Add` and `Edit`
commands.
* Updated manpage with missing password generation options.
* Updated manpage with missing longer forms of password generation options.
* Added unit tests for new password generation options in `Add` and
`Edit`.
* Handle case when `-g` and `-p` options are used at the same time.

This PR adds password generation functionalities while reducing
code duplication, but at the cost of 2 small breaking changes:
* The password generation option for `Add` and `Edit` for specifying
password length is now `-L` instead of `-l`, to not clash with the
`-l --lower` option.
* The `-u` shorthand for the `--upper` option has to be removed, to not
clash with the `-u --username` option.
* Add -U variant for uppercase.
This commit is contained in:
louib 2019-08-30 22:50:32 -04:00 committed by Jonathan White
parent 79bb991a61
commit eb1882453f
8 changed files with 221 additions and 88 deletions

View file

@ -20,6 +20,7 @@
#include "Add.h"
#include "cli/Generate.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
@ -44,11 +45,6 @@ const QCommandLineOption Add::GenerateOption = QCommandLineOption(QStringList()
<< "generate",
QObject::tr("Generate a password for the entry."));
const QCommandLineOption Add::PasswordLengthOption =
QCommandLineOption(QStringList() << "l"
<< "password-length",
QObject::tr("Length for the generated password."),
QObject::tr("length"));
Add::Add()
{
@ -57,9 +53,19 @@ Add::Add()
options.append(Add::UsernameOption);
options.append(Add::UrlOption);
options.append(Add::PasswordPromptOption);
options.append(Add::GenerateOption);
options.append(Add::PasswordLengthOption);
positionalArguments.append({QString("entry"), QObject::tr("Path of the entry to add."), QString("")});
// Password generation options.
options.append(Add::GenerateOption);
options.append(Generate::PasswordLengthOption);
options.append(Generate::LowerCaseOption);
options.append(Generate::UpperCaseOption);
options.append(Generate::NumbersOption);
options.append(Generate::SpecialCharsOption);
options.append(Generate::ExtendedAsciiOption);
options.append(Generate::ExcludeCharsOption);
options.append(Generate::ExcludeSimilarCharsOption);
options.append(Generate::IncludeEveryGroupOption);
}
int Add::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
@ -72,14 +78,22 @@ int Add::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<Q
const QString& databasePath = args.at(0);
const QString& entryPath = args.at(1);
// Validating the password length here, before we actually create
// the entry.
QString passwordLength = parser->value(Add::PasswordLengthOption);
if (!passwordLength.isEmpty() && !passwordLength.toInt()) {
errorTextStream << QObject::tr("Invalid value for password length %1.").arg(passwordLength) << endl;
// Cannot use those 2 options at the same time!
if (parser->isSet(Add::GenerateOption) && parser->isSet(Add::PasswordPromptOption)) {
errorTextStream << QObject::tr("Cannot generate a password and prompt at the same time!") << endl;
return EXIT_FAILURE;
}
// Validating the password generator here, before we actually create
// the entry.
QSharedPointer<PasswordGenerator> passwordGenerator;
if (parser->isSet(Add::GenerateOption)) {
passwordGenerator = Generate::createGenerator(parser);
if (passwordGenerator.isNull()) {
return EXIT_FAILURE;
}
}
Entry* entry = database->rootGroup()->addEntryWithPath(entryPath);
if (!entry) {
errorTextStream << QObject::tr("Could not create entry with path %1.").arg(entryPath) << endl;
@ -101,17 +115,7 @@ int Add::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<Q
QString password = Utils::getPassword(parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT);
entry->setPassword(password);
} else if (parser->isSet(Add::GenerateOption)) {
PasswordGenerator passwordGenerator;
if (passwordLength.isEmpty()) {
passwordGenerator.setLength(PasswordGenerator::DefaultLength);
} else {
passwordGenerator.setLength(passwordLength.toInt());
}
passwordGenerator.setCharClasses(PasswordGenerator::DefaultCharset);
passwordGenerator.setFlags(PasswordGenerator::DefaultFlags);
QString password = passwordGenerator.generatePassword();
QString password = passwordGenerator->generatePassword();
entry->setPassword(password);
}