cutelyst  4.4.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
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:

Public Member Functions

 ValidatorPwQuality (const QString &field, int threshold=ValidatorPwQuality::defaultThreshold, const QVariant &options=QVariant(), const QString &userName=QString(), const QString &oldPassword=QString(), const ValidatorMessages &messages=ValidatorMessages())
 
 ~ValidatorPwQuality () override
 
- Public Member Functions inherited from Cutelyst::ValidatorRule
 ValidatorRule (const QString &field, const ValidatorMessages &messages={}, const QString &defValKey={}, QByteArrayView validatorName=nullptr)
 
virtual ~ValidatorRule ()
 Deconstructs the ValidatorRule.
 

Static Public Member Functions

static QString errorString (Context *c, int returnValue, const QString &label=QString(), int threshold=0)
 
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...
 

Static Public Attributes

static constexpr int defaultThreshold
 

Protected Member Functions

QString genericValidationError (Context *c, const QVariant &errorData) const override
 
ValidatorReturnType validate (Context *c, const ParamsMultiMap &params) const override
 
- Protected Member Functions inherited from Cutelyst::ValidatorRule
 ValidatorRule (ValidatorRulePrivate &dd)
 
QString debugString (Context *c) const
 
void defaultValue (Context *c, ValidatorReturnType *result) const
 
QString field () const noexcept
 
virtual QString genericParsingError (Context *c, const QVariant &errorData={}) const
 
virtual QString genericValidationDataError (Context *c, const QVariant &errorData={}) const
 
QString label (Context *c) const
 
QString parsingError (Context *c, const QVariant &errorData={}) const
 
bool trimBefore () const noexcept
 
QString validationDataError (Context *c, const QVariant &errorData={}) const
 
QString validationError (Context *c, const QVariant &errorData={}) const
 
QString value (const ParamsMultiMap &params) const
 

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.

Building

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.

Options

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.

Note
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.
Return type
On success, ValidatorReturnType::value will contain a QString.
See also
Validator for general usage of validators.
Since
Cutelyst 2.0.0

Definition at line 57 of file validatorpwquality.h.

Constructor & Destructor Documentation

◆ ValidatorPwQuality()

ValidatorPwQuality::ValidatorPwQuality ( const QString field,
int  threshold = ValidatorPwQuality::defaultThreshold,
const QVariant options = QVariant(),
const QString userName = QString(),
const QString oldPassword = QString(),
const ValidatorMessages messages = ValidatorMessages() 
)
explicit

Constructs a new ValidatorPwQuality object with the given parameters.

Parameters
fieldName of the input field to validate.
thresholdThe quality score threshold below the validation fails.
options<a hrefd=#options">Options 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 14 of file validatorpwquality.cpp.

◆ ~ValidatorPwQuality()

ValidatorPwQuality::~ValidatorPwQuality ( )
overridedefault

Destroys the ValidatorPwQuality object.

Member Function Documentation

◆ errorString()

QString ValidatorPwQuality::errorString ( Context c,
int  returnValue,
const QString label = QString(),
int  threshold = 0 
)
static

Returns a human readable string for the return value of ValidatorPwQuality::validate().

Parameters
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.
Returns
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 121 of file validatorpwquality.cpp.

References QString::arg(), QString::isEmpty(), Cutelyst::ValidatorRule::label(), QString::number(), and Cutelyst::Context::qtTrId().

Referenced by genericValidationError().

◆ genericValidationError()

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

Returns a generic error message if validation failed.

Reimplemented from Cutelyst::ValidatorRule.

Definition at line 409 of file validatorpwquality.cpp.

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

◆ validate()

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

Member Data Documentation

◆ defaultThreshold

constexpr int Cutelyst::ValidatorPwQuality::defaultThreshold
staticconstexpr

The default quality score threshold below the validation fails.

Definition at line 63 of file validatorpwquality.h.