Skip to content

Commit 8d9f452

Browse files
jrushlowweaverryan
authored andcommitted
[reset-password] allow anyone to access check email
1 parent 2c3009b commit 8d9f452

File tree

5 files changed

+15
-17
lines changed

5 files changed

+15
-17
lines changed

src/Maker/MakeResetPassword.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
use Symfony\Component\Console\Input\InputInterface;
3333
use Symfony\Component\Mailer\MailerInterface;
3434
use Symfony\Component\Yaml\Yaml;
35-
use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
3635
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
3736
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestTrait;
38-
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordToken;
3937
use SymfonyCasts\Bundle\ResetPassword\Persistence\Repository\ResetPasswordRequestRepositoryTrait;
4038
use SymfonyCasts\Bundle\ResetPassword\Persistence\ResetPasswordRequestRepositoryInterface;
39+
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelper;
4140
use SymfonyCasts\Bundle\ResetPassword\SymfonyCastsResetPasswordBundle;
4241

4342
/**
@@ -96,12 +95,10 @@ public function configureDependencies(DependencyBuilder $dependencies): void
9695

9796
$dependencies->addClassDependency(Annotation::class, 'annotations');
9897

99-
// reset-password-bundle 1.3 includes helpers to get/set a ResetPasswordToken object from the session.
100-
// we need to check that version 1.3 is installed
101-
if (class_exists(ResetPasswordToken::class)) {
102-
if (!method_exists(ResetPasswordControllerTrait::class, 'getTokenObjectFromSession')) {
103-
throw new RuntimeCommandException('Please upgrade symfonycasts/reset-password-bundle to version 1.3 or greater.');
104-
}
98+
// reset-password-bundle 1.6 includes the ability to generate a fake token.
99+
// we need to check that version 1.6 is installed
100+
if (class_exists(ResetPasswordHelper::class) && !method_exists(ResetPasswordHelper::class, 'generateFakeResetToken')) {
101+
throw new RuntimeCommandException('Please run "composer upgrade symfonycasts/reset-password-bundle". Version 1.6 or greater of this bundle is required.');
105102
}
106103
}
107104

src/Resources/skeleton/resetPassword/ResetPasswordController.tpl.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ public function request(Request $request, MailerInterface $mailer): Response
7575
<?php } ?>
7676
public function checkEmail(): Response
7777
{
78-
// We prevent users from directly accessing this page
78+
// Generate a fake token if the user does not exist or someone hit this page directly.
79+
// This prevents exposing whether or not a user was found with the given email address or not
7980
if (null === ($resetToken = $this->getTokenObjectFromSession())) {
80-
return $this->redirectToRoute('app_forgot_password_request');
81+
$resetToken = $this->resetPasswordHelper->generateFakeResetToken();
8182
}
8283

8384
return $this->render('reset_password/check_email.html.twig', [

src/Resources/skeleton/resetPassword/twig_check_email.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{% block body %}
66
<p>
7-
An email has been sent that contains a link that you can click to reset your password.
7+
If an account matching your email exists, then an email was just sent that contains a link that you can use to reset your password.
88
This link will expire in {{ resetToken.expirationMessageKey|trans(resetToken.expirationMessageData, 'ResetPasswordBundle') }}.
99
</p>
1010
<p>If you don't receive an email please check your spam folder or <a href="{{ path('app_forgot_password_request') }}">try again</a>.</p>

src/Resources/skeleton/resetPassword/twig_request.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919

2020
<button class="btn btn-primary">Send password reset email</button>
2121
{{ form_end(requestForm) }}
22-
{% endblock %}
22+
{% endblock %}

tests/fixtures/MakeResetPasswordFunctionalTest/tests/ResetPasswordFunctionalTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public function testResetRequestRoute()
1111
$client = static::createClient();
1212
$client->request('GET', '/reset-password');
1313

14-
$this->assertSame(200, $client->getResponse()->getStatusCode());
14+
self::assertSame(200, $client->getResponse()->getStatusCode());
1515
}
1616

1717
public function testResetRequestRouteDeniesInvalidToken()
1818
{
1919
$client = static::createClient();
2020
$client->request('GET', '/reset-password/reset/badToken1234');
2121

22-
$this->assertSame(302, $client->getResponse()->getStatusCode());
22+
self::assertSame(302, $client->getResponse()->getStatusCode());
2323
}
2424

25-
public function testCheckEmailRouteRedirectsToRequestRouteIfUserNotAllowedToCheckEmail()
25+
public function testCheckEmailPageIsAlwaysAccessible()
2626
{
2727
$client = static::createClient();
2828
$client->request('GET', '/reset-password/check-email');
2929

30-
$this->assertSame(302, $client->getResponse()->getStatusCode());
31-
$this->assertResponseRedirects('/reset-password');
30+
self::assertResponseIsSuccessful();
31+
self::assertPageTitleSame('Password Reset Email Sent');
3232
}
3333
}

0 commit comments

Comments
 (0)