Skip to content

Commit

Permalink
Add password strength indicator to PasswordEditWidget
Browse files Browse the repository at this point in the history
Fixes #7437
Fixes #5220
  • Loading branch information
jmdana committed Apr 13, 2022
1 parent 692c95b commit 23dabb7
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
68 changes: 68 additions & 0 deletions src/gui/databasekey/PasswordEditWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
#include "PasswordEditWidget.h"
#include "ui_KeyComponentWidget.h"
#include "ui_PasswordEditWidget.h"
#include <QRegularExpression>

#include "core/PasswordHealth.h"
#include "gui/styles/StateColorPalette.h"
#include "keys/CompositeKey.h"
#include "keys/PasswordKey.h"

Expand Down Expand Up @@ -70,6 +73,17 @@ QWidget* PasswordEditWidget::componentEditWidget()
m_compUi->setupUi(m_compEditWidget);
m_compUi->enterPasswordEdit->enablePasswordGenerator();
m_compUi->enterPasswordEdit->setRepeatPartner(m_compUi->repeatPasswordEdit);

// set font size of password quality and entropy labels dynamically to 80% of
// the default font size, but make it no smaller than 8pt
QFont defaultFont;
int smallerSize = static_cast<int>(defaultFont.pointSize() * 0.8f);
if (smallerSize >= 8) {
defaultFont.setPointSize(smallerSize);
m_compUi->qualityLabel->setFont(defaultFont);
}

connect(m_compUi->enterPasswordEdit, SIGNAL(textChanged(QString)), this, SLOT(updatePasswordStrength(QString)));
return m_compEditWidget;
}

Expand Down Expand Up @@ -121,3 +135,57 @@ void PasswordEditWidget::setPassword(const QString& password)
m_compUi->enterPasswordEdit->setText(password);
m_compUi->repeatPasswordEdit->setText(password);
}

void PasswordEditWidget::updatePasswordStrength(const QString& password)
{
if (password.isEmpty()) {
m_compUi->qualityProgressBar->setValue(0);
m_compUi->qualityLabel->setText(tr(""));
return;
}

PasswordHealth health(password);

m_compUi->qualityProgressBar->setValue(std::min(int(health.entropy()), m_compUi->qualityProgressBar->maximum()));

QString style = m_compUi->qualityProgressBar->styleSheet();
QRegularExpression re("(QProgressBar::chunk\\s*\\{.*?background-color:)[^;]+;",
QRegularExpression::CaseInsensitiveOption | QRegularExpression::DotMatchesEverythingOption);
style.replace(re, "\\1 %1;");

StateColorPalette qualityPalette;

switch (health.quality()) {
case PasswordHealth::Quality::Bad:
case PasswordHealth::Quality::Poor:
m_compUi->qualityProgressBar->setStyleSheet(
style.arg(qualityPalette.color(StateColorPalette::HealthCritical).name()));

m_compUi->qualityLabel->setText(tr("Quality: %1").arg(tr("Poor", "Password quality")));

break;

case PasswordHealth::Quality::Weak:
m_compUi->qualityProgressBar->setStyleSheet(
style.arg(qualityPalette.color(StateColorPalette::HealthBad).name()));

m_compUi->qualityLabel->setText(tr("Quality: %1").arg(tr("Weak", "Password quality")));

break;
case PasswordHealth::Quality::Good:
m_compUi->qualityProgressBar->setStyleSheet(
style.arg(qualityPalette.color(StateColorPalette::HealthOk).name()));

m_compUi->qualityLabel->setText(tr("Quality: %1").arg(tr("Good", "Password quality")));

break;
case PasswordHealth::Quality::Excellent:

m_compUi->qualityProgressBar->setStyleSheet(
style.arg(qualityPalette.color(StateColorPalette::HealthExcellent).name()));

m_compUi->qualityLabel->setText(tr("Quality: %1").arg(tr("Excellent", "Password quality")));

break;
}
}
1 change: 1 addition & 0 deletions src/gui/databasekey/PasswordEditWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class PasswordEditWidget : public KeyComponentWidget

private slots:
void setPassword(const QString& password);
void updatePasswordStrength(const QString& password);

private:
const QScopedPointer<Ui::PasswordEditWidget> m_compUi;
Expand Down
55 changes: 50 additions & 5 deletions src/gui/databasekey/PasswordEditWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>571</width>
<height>78</height>
<height>82</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
Expand All @@ -23,14 +23,14 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<item row="1" column="0">
<widget class="QLabel" name="enterPasswordLabel">
<property name="text">
<string>Enter password:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="1" column="1">
<widget class="PasswordEdit" name="enterPasswordEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
Expand All @@ -52,14 +52,14 @@
</property>
</widget>
</item>
<item row="1" column="0">
<item row="3" column="0">
<widget class="QLabel" name="repeatPasswordLabel">
<property name="text">
<string>Confirm password:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="3" column="1">
<widget class="PasswordEdit" name="repeatPasswordEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
Expand All @@ -81,6 +81,51 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QProgressBar" name="qualityProgressBar">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>300</width>
<height>5</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar {
border: none;
height: 2px;
font-size: 1px;
background-color: transparent;
padding: 0 1px;
}
QProgressBar::chunk {
background-color: #c0392b;
border-radius: 2px;
}</string>
</property>
<property name="maximum">
<number>200</number>
</property>
<property name="value">
<number>0</number>
</property>
<property name="format">
<string/>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="qualityLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
Expand Down

0 comments on commit 23dabb7

Please sign in to comment.