Skip to content

Commit 998d7e2

Browse files
Merge pull request #20 from sumocoders/297-password-reset-weak-password
297 password reset weak password
2 parents 7f409da + 6b9129a commit 998d7e2

File tree

9 files changed

+56
-155
lines changed

9 files changed

+56
-155
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Controller\User\Ajax;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Component\HttpFoundation\Response;
8+
use SumoCoders\FrameworkCoreBundle\Security\PasswordStrengthService;
9+
10+
class PasswordStrengthController extends AbstractController
11+
{
12+
public function __invoke(
13+
Request $request,
14+
PasswordStrengthService $passwordStrengthService
15+
): Response {
16+
$password = json_decode($request->getContent(), true)['password'] ?? '';
17+
18+
return $this->json([
19+
'strength' => $passwordStrengthService->estimateStrength($password),
20+
]);
21+
}
22+
}

src/DataTransferObject/User/UserDataTransferObject.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use App\Validator\User\UniqueEmail;
66
use Symfony\Component\Validator\Constraints\Email;
7+
use Symfony\Component\Validator\Constraints\NoSuspiciousCharacters;
78
use Symfony\Component\Validator\Constraints\NotBlank;
89

910
class UserDataTransferObject
1011
{
1112
#[Email]
1213
#[NotBlank]
1314
#[UniqueEmail]
15+
#[NoSuspiciousCharacters]
1416
public string $email;
1517

1618
/**

src/Message/User/RegisterUser.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
namespace App\Message\User;
44

55
use App\DataTransferObject\User\UserDataTransferObject;
6+
use Symfony\Component\Validator\Constraints as Assert;
67
use Symfony\Component\Validator\Constraints\NotBlank;
78

89
class RegisterUser extends UserDataTransferObject
910
{
1011
#[NotBlank]
12+
#[Assert\PasswordStrength([
13+
'minScore' => Assert\PasswordStrength::STRENGTH_STRONG,
14+
])]
15+
#[Assert\NotCompromisedPassword()]
16+
#[Assert\Length(min: 12)]
1117
public string $password;
1218
}

src/Message/User/ResetPassword.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
namespace App\Message\User;
44

55
use App\Entity\User\User;
6+
use Symfony\Component\Validator\Constraints as Assert;
67

78
class ResetPassword
89
{
10+
#[Assert\PasswordStrength([
11+
'minScore' => Assert\PasswordStrength::STRENGTH_STRONG,
12+
])]
13+
#[Assert\NotCompromisedPassword()]
14+
#[Assert\Length(min: 12)]
915
public string $password;
1016

1117
public function __construct(private readonly User $user)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="password-strength-meter">
2+
<div class="meter-section rounded me-2"></div>
3+
<div class="meter-section rounded me-2"></div>
4+
<div class="meter-section rounded me-2"></div>
5+
<div class="meter-section rounded me-2"></div>
6+
<div class="meter-section rounded"></div>
7+
</div>
8+
<div class="mb-3">
9+
<small>{{ 'Use 12 or more characters with a mix of letters, numbers & symbols'|trans }}</small>
10+
</div>

templates/user/profile.html.twig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
<h1>{{ 'Change your password'|trans }}</h1>
99
{{ form_start(form) }}
1010
{{ form_errors(form) }}
11-
{{ form_row(form.password) }}
11+
<div data-role="check-password" data-route="{{ path('admin_user_ajax_password_strength') }}">
12+
{{ form_row(form.password.first) }}
13+
{% include '/user/password-strength-meter.html.twig' %}
14+
</div>
15+
{{ form_row(form.password.second) }}
1216

1317
<input type="submit" class="btn btn-secondary" value="{{ 'Confirm'|trans }}" />
1418
{{ form_end(form) }}

templates/user/reset.html.twig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
{% block main %}
44
{{ form_start(form) }}
55
{{ form_errors(form) }}
6-
{{ form_row(form.password) }}
6+
<div data-role="check-password" data-route="{{ path('admin_user_ajax_password_strength') }}">
7+
{{ form_row(form.password.first) }}
8+
{% include '/user/password-strength-meter.html.twig' %}
9+
</div>
10+
{{ form_row(form.password.second) }}
711

812
<input type="submit" class="btn btn-secondary" value="{{ 'Confirm'|trans }}" />
913
{{ form_end(form) }}

translations/security.nl.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.

translations/validators.nl.yaml

Lines changed: 0 additions & 134 deletions
This file was deleted.

0 commit comments

Comments
 (0)