Skip to content

Commit 84348ec

Browse files
committed
PasswordStrength Documentation pages
1 parent e556fb3 commit 84348ec

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

reference/configuration/framework.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,15 @@ metadata of the class. You can define an array of strings with the names of
25872587
several methods. In that case, all of them will be called in that order to load
25882588
the metadata.
25892589

2590+
.. _reference-validation-password-strength:
2591+
2592+
password_strength
2593+
.................
2594+
2595+
The :doc:`PasswordStrength </reference/constraints/PasswordStrength>`
2596+
constraint verifies the submitted string entropy is matching the minimum entropy score.
2597+
The strength of the password is measured using the external library `zxcvbn-php`_.
2598+
25902599
.. _reference-validation-email_validation_mode:
25912600

25922601
email_validation_mode
@@ -3688,3 +3697,4 @@ the ``#[WithLogLevel]`` attribute::
36883697
.. _`utf-8 modifier`: https://www.php.net/reference.pcre.pattern.modifiers
36893698
.. _`Link HTTP header`: https://tools.ietf.org/html/rfc5988
36903699
.. _`SMTP session`: https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol#SMTP_transport_example
3700+
.. _`zxcvbn-php`: https://github.com/bjeavons/zxcvbn-php

reference/constraints.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Validation Constraints Reference
7575
constraints/All
7676
constraints/UserPassword
7777
constraints/NotCompromisedPassword
78+
constraints/PasswordStrength
7879
constraints/Valid
7980
constraints/Traverse
8081
constraints/CssColor

reference/constraints/Compound.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ you can create your own named set or requirements to be reused consistently ever
3737
new Assert\Type('string'),
3838
new Assert\Length(['min' => 12]),
3939
new Assert\NotCompromisedPassword(),
40+
new Assert\PasswordStrength(['minScore' => 4]),
4041
];
4142
}
4243
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
PasswordStrength
2+
================
3+
4+
Validates that the given password has reached the minimum strength required by
5+
the constraint. The strength is measured using the `zxcvbn-php`_. library.
6+
7+
========== ===================================================================
8+
Applies to :ref:`property or method <validation-property-target>`
9+
Class :class:`Symfony\\Component\\Validator\\Constraints\\PasswordStrength`
10+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\PasswordStrengthValidator`
11+
========== ===================================================================
12+
13+
Basic Usage
14+
-----------
15+
16+
The following constraint ensures that the ``rawPassword`` property of the
17+
``User`` class reaches the minimum strength required by the constraint.
18+
By default, the minimum required score is 2.
19+
20+
.. configuration-block::
21+
22+
.. code-block:: php-attributes
23+
24+
// src/Entity/User.php
25+
namespace App\Entity;
26+
27+
use Symfony\Component\Validator\Constraints as Assert;
28+
29+
class User
30+
{
31+
#[Assert\PasswordStrength]
32+
protected $rawPassword;
33+
}
34+
35+
.. code-block:: yaml
36+
37+
# config/validator/validation.yaml
38+
App\Entity\User:
39+
properties:
40+
rawPassword:
41+
- PasswordStrength
42+
43+
.. code-block:: xml
44+
45+
<!-- config/validator/validation.xml -->
46+
<?xml version="1.0" encoding="UTF-8" ?>
47+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
48+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
49+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
50+
51+
<class name="App\Entity\User">
52+
<property name="rawPassword">
53+
<constraint name="PasswordStrength"></constraint>
54+
</property>
55+
</class>
56+
</constraint-mapping>
57+
58+
.. code-block:: php
59+
60+
// src/Entity/User.php
61+
namespace App\Entity;
62+
63+
use Symfony\Component\Validator\Constraints as Assert;
64+
use Symfony\Component\Validator\Mapping\ClassMetadata;
65+
66+
class User
67+
{
68+
public static function loadValidatorMetadata(ClassMetadata $metadata)
69+
{
70+
$metadata->addPropertyConstraint('rawPassword', new Assert\PasswordStrength());
71+
}
72+
}
73+
74+
Available Options
75+
-----------------
76+
77+
.. include:: /reference/constraints/_groups-option.rst.inc
78+
79+
``lowStrengthMessage``
80+
~~~~~~~~~~~
81+
82+
**type**: ``string`` **default**: ``The password strength is too low. Please use a stronger password.``
83+
84+
The default message supplied when the password does not reach the minimum required score.
85+
86+
.. include:: /reference/constraints/_payload-option.rst.inc
87+
88+
``restrictedData``
89+
~~~~~~~~~~~~~~~
90+
91+
**type**: ``string[]`` **default**: ``[]``
92+
93+
It is possible to determine if the submitted password contains restricted data
94+
such as the user given/family name,
95+
96+
``restrictedDataMessage``
97+
~~~~~~~~~~~~~
98+
99+
**type**: ``string`` **default**: ``The password contains the following restricted data: {{ wordList }}.``
100+
101+
The default message supplied when the password contains at least one restricted data.
102+
103+
.. _`zxcvbn-php`: https://github.com/bjeavons/zxcvbn-php

reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ String Constraints
2828
* :doc:`Ulid </reference/constraints/Ulid>`
2929
* :doc:`UserPassword </reference/constraints/UserPassword>`
3030
* :doc:`NotCompromisedPassword </reference/constraints/NotCompromisedPassword>`
31+
* :doc:`PasswordStrength </reference/constraints/PasswordStrength>`
3132
* :doc:`CssColor </reference/constraints/CssColor>`
3233
* :doc:`NoSuspiciousCharacters </reference/constraints/NoSuspiciousCharacters>`
3334

0 commit comments

Comments
 (0)