Cutelyst  2.13.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
Cutelyst::ValidatorPwQuality Class Reference

Validates an input field with libpwquality to check password quality. More...

#include <Cutelyst/Plugins/Utils/validatorpwquality.h>

Inheritance diagram for Cutelyst::ValidatorPwQuality:
Inheritance graph

Public Member Functions

 ValidatorPwQuality (const QString &field, int threshold=30, const QVariant &options=QVariant(), const QString &userName=QString(), const QString &oldPassword=QString(), const ValidatorMessages &messages=ValidatorMessages())
 Constructs a new ValidatorPwQuality with the given parameters. More...
 ~ValidatorPwQuality () override
 Deconstructs the ValidatorPwQuality.
- Public Member Functions inherited from Cutelyst::ValidatorRule
 ValidatorRule (const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
 Constructs a new ValidatorRule with the given parameters. More...
virtual ~ValidatorRule ()
 Deconstructs the ValidatorRule.

Static Public Member Functions

static QString errorString (Context *c, int returnValue, const QString &label=QString(), int threshold=0)
 Returns a human readable string for the return value of ValidatorPwQuality::validate() More...
static int validate (const QString &value, const QVariant &options=QVariant(), const QString &oldPassword=QString(), const QString &user=QString())
 Returns the password quality score for value. More...

Protected Member Functions

QString genericValidationError (Context *c, const QVariant &errorData) const override
 Returns a generic error message if validation failed. More...
ValidatorReturnType validate (Context *c, const ParamsMultiMap &params) const override
 Performs the validation and returns the result. More...
- Protected Member Functions inherited from Cutelyst::ValidatorRule
 ValidatorRule (ValidatorRulePrivate &dd)
 Constructs a new ValidatorRule object with the given private class. More...
void defaultValue (Context *c, ValidatorReturnType *result, const char *validatorName) const
 I a defValKey has been set in the constructor, this will try to get the default value from the stash and put it into the result. More...
QString field () const
 Returns the name of the field to validate. More...
virtual QString genericParsingError (Context *c, const QVariant &errorData=QVariant()) const
 Returns a generic error message if an error occures while parsing input. More...
virtual QString genericValidationDataError (Context *c, const QVariant &errorData=QVariant()) const
 Returns a generic error message if any validation data is missing or invalid. More...
QString label (Context *c) const
 Returns the human readable field label used for generic error messages. More...
QString parsingError (Context *c, const QVariant &errorData=QVariant()) const
 Returns an error message if an error occured while parsing input. More...
bool trimBefore () const
 Returns true if the field value should be trimmed before validation. More...
QString validationDataError (Context *c, const QVariant &errorData=QVariant()) const
 Returns an error message if any validation data is missing or invalid. More...
QString validationError (Context *c, const QVariant &errorData=QVariant()) const
 Returns a descriptive error message if validation failed. More...
QString value (const ParamsMultiMap &params) const
 Returns the value of the field from the input params.

Detailed Description

This validator uses libpwquality to generate a password quality score that is compared against a threshold. If it is below the threshold, the validation fails. According to libpwquality a score of 0-30 is of low, a score of 30-60 of medium and a score of 60-100 of high quality. Everything below 0 is an error and the password should not be used.


As this validator relies on an external library, it will not be included and build by default. Use either -DPLUGIN_VALIDATOR_PWQUALITY:BOOL=ON or -DBUILD_ALL:BOOL=ON when configuring Cutelyst for build with cmake. In your Cutelyst application you can check if CUTELYST_VALIDATOR_WITH_PWQUALITY has been defined to see if this validator is available.


ValidatorPwQuality can take additional options. To learn more about the available options see man 5 pwquality.conf. The options value can be either a QVariantMap containing the options or a QString specifying a file name that will be read by libpwquality. For the constructor the options will also be searched in the current stash if it is a QString. The stash value should than be either a QVariantMap or a QString pointing to a configuration file. All values in the QVariantMap used to specify options, have to be convertible into QString. The QVariantMap does not have to contain all available option keys, for keys that are not contained, the default values of libpwquality will be used. If the options QVariant is not valid or if a contained QString or QVariantMap is empty, the options from the default libpwquality configuration file will be read.

Unless validation is started with NoTrimming, whitespaces will be removed from the beginning and the end of the input value before validation. If the field's value is empty or if the field is missing in the input data, the validation will succeed without performing the validation itself. Use one of the required validators to require the field to be present and not empty.
See also
Validator for general usage of validators.
Cutelyst 2.0.0

Definition at line 64 of file validatorpwquality.h.

Constructor & Destructor Documentation

◆ ValidatorPwQuality()

ValidatorPwQuality::ValidatorPwQuality ( const QString &  field,
int  threshold = 30,
const QVariant &  options = QVariant(),
const QString &  userName = QString(),
const QString &  oldPassword = QString(),
const ValidatorMessages messages = ValidatorMessages() 
fieldName of the input field to validate.
thresholdThe quality score threshold below the validation fails.
optionsOptions for libpwquality. Use invalid QVariant to omit.
userNameInput params key or stash key containing the user name, used for quality checks. Will first try params, than stash. Leave empty to omit.
oldPasswordInput params key or stash key containing the old password, used for quality checks. Will first try params, than stash. Leave empty to omit.
messagesCustom error messages if validation fails.

Definition at line 25 of file validatorpwquality.cpp.

Member Function Documentation

◆ errorString()

QString ValidatorPwQuality::errorString ( Context c,
int  returnValue,
const QString &  label = QString(),
int  threshold = 0 
cCurrent context, used for translations.
returnValueThe return value of ValidatorPwQuality::validate()
labelOptional label used in the returned string.
thresholdThe threshold below that a password is invalid.
Human readable error string. If returnValue is >= 0 but below threshold, a string explaining the threshold will be returned. If returnValue is >= threshold, an empty string will be returned.

Definition at line 114 of file validatorpwquality.cpp.

References Cutelyst::ValidatorRule::label(), and Cutelyst::Context::translate().

Referenced by genericValidationError().

◆ genericValidationError()

QString ValidatorPwQuality::genericValidationError ( Context c,
const QVariant &  errorData 
) const

The errorData will contain the score returned by ValidatorPqQuality::validate()

Reimplemented from Cutelyst::ValidatorRule.

Definition at line 374 of file validatorpwquality.cpp.

References errorString(), and Cutelyst::ValidatorRule::label().

◆ validate()

ValidatorReturnType ValidatorPwQuality::validate ( Context c,
const ParamsMultiMap params 
) const