-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Two-Factor: password reset for a user with 2FA enabled should ask for…
… the 2FA code
- Loading branch information
Showing
6 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/EventListener/ResolvedTwoFactorCodeCredentialsListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <[email protected]> | ||
* Nils Adermann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\EventListener; | ||
|
||
use App\Security\Passport\Badge\ResolvedTwoFactorCodeCredentials; | ||
use Scheb\TwoFactorBundle\Security\Http\Authenticator\TwoFactorAuthenticator; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
use Symfony\Component\Security\Http\Event\AuthenticationTokenCreatedEvent; | ||
|
||
#[AsEventListener(event: AuthenticationTokenCreatedEvent::class, method: 'onAuthenticationTokenCreated', priority: 512)] | ||
class ResolvedTwoFactorCodeCredentialsListener | ||
{ | ||
public function onAuthenticationTokenCreated(AuthenticationTokenCreatedEvent $event): void | ||
{ | ||
if ($event->getPassport()->getBadge(ResolvedTwoFactorCodeCredentials::class)) { | ||
$event->getAuthenticatedToken()->setAttribute(TwoFactorAuthenticator::FLAG_2FA_COMPLETE, true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Security/Passport/Badge/ResolvedTwoFactorCodeCredentials.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <[email protected]> | ||
* Nils Adermann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Security\Passport\Badge; | ||
|
||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\BadgeInterface; | ||
|
||
class ResolvedTwoFactorCodeCredentials implements BadgeInterface | ||
{ | ||
public function isResolved(): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <[email protected]> | ||
* Nils Adermann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Tests\Controller; | ||
|
||
use App\Entity\User; | ||
use App\Tests\Mock\TotpAuthenticatorStub; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Scheb\TwoFactorBundle\Security\Http\Authenticator\TwoFactorAuthenticator; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
|
||
class ResetPasswordControllerTest extends WebTestCase | ||
{ | ||
private KernelBrowser $client; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->client = self::createClient(); | ||
$this->client->disableReboot(); // Prevent reboot between requests | ||
static::getContainer()->get(Connection::class)->beginTransaction(); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
static::getContainer()->get(Connection::class)->rollBack(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testResetPassword(): void | ||
{ | ||
$user = $this->setupUserWithPasswordResetRequest(false); | ||
$oldPassword = $user->getPassword(); | ||
|
||
$crawler = $this->client->request('GET', '/reset-password/reset/' . $user->getConfirmationToken()); | ||
$this->assertResponseStatusCodeSame(200); | ||
|
||
$this->submitPasswordResetFormAndAsserStatusCode($crawler, 'new-password', 302); | ||
$this->assertUserHasNewPassword($user, $oldPassword); | ||
} | ||
|
||
public function testResetPasswordWithTwoFactor(): void | ||
{ | ||
$user = $this->setupUserWithPasswordResetRequest(true); | ||
$oldPassword = $user->getPassword(); | ||
|
||
$crawler = $this->client->request('GET', '/reset-password/reset/' . $user->getConfirmationToken()); | ||
$this->assertResponseStatusCodeSame(200); | ||
|
||
$this->submitPasswordResetFormAndAsserStatusCode($crawler, 'new-password', 422); | ||
$this->submitPasswordResetFormAndAsserStatusCode($crawler, 'new-password', 302, TotpAuthenticatorStub::MOCKED_VALID_CODE); | ||
$this->assertUserHasNewPassword($user, $oldPassword); | ||
|
||
$this->assertTrue(self::getContainer()->get(TokenStorageInterface::class)->getToken()?->getAttribute(TwoFactorAuthenticator::FLAG_2FA_COMPLETE)); | ||
} | ||
|
||
private function setupUserWithPasswordResetRequest(bool $withTwoFactor): User | ||
{ | ||
$user = new User; | ||
$user->setEnabled(true); | ||
$user->setUsername('test'); | ||
$user->setEmail('[email protected]'); | ||
$user->setPassword('testtest'); | ||
$user->setApiToken('token'); | ||
$user->initializeConfirmationToken(); | ||
$user->setPasswordRequestedAt(new \DateTime()); | ||
|
||
if ($withTwoFactor) { | ||
$user->setTotpSecret('secret'); | ||
} | ||
|
||
$em = static::getContainer()->get(ManagerRegistry::class)->getManager(); | ||
$em->persist($user); | ||
$em->flush(); | ||
|
||
return $user; | ||
} | ||
|
||
private function submitPasswordResetFormAndAsserStatusCode(Crawler $crawler, string $newPassword, int $expectedStatusCode, ?string $mfaCode = null): void | ||
{ | ||
$form = $crawler->selectButton('Reset password')->form(); | ||
$form->setValues(array_filter([ | ||
'reset_password_form[plainPassword]' => $newPassword, | ||
'reset_password_form[twoFactorCode]' => $mfaCode, | ||
])); | ||
|
||
$this->client->submit($form); | ||
$this->assertResponseStatusCodeSame($expectedStatusCode); | ||
} | ||
|
||
private function assertUserHasNewPassword(User $user, ?string $oldPassword): void | ||
{ | ||
$em = static::getContainer()->get(ManagerRegistry::class)->getManager(); | ||
$em->clear(); | ||
|
||
$user = $em->getRepository(User::class)->find($user->getId()); | ||
$this->assertNotNull($user); | ||
$this->assertNotSame($oldPassword, $user->getPassword()); | ||
} | ||
} |