-
-
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.
On password reset require 2FA code before storing new password (#1403)
* Two-Factor: extract code validation into constraint and add test to enable two-factor * Two-Factor: password reset for a user with 2FA enabled should ask for the 2FA code * Apply review feedback: - better form options - define listener event on method instead of class - avoid initializing new totp secret on every request * TwoFactor: move constraint and validator to existing classes
- Loading branch information
Showing
14 changed files
with
409 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ PANTHER_APP_ENV=panther | |
DATABASE_URL="mysql://[email protected]:3306/packagist?serverVersion=8.0.28" | ||
MAILER_DSN=null://null | ||
REDIS_URL=redis://localhost/14 | ||
APP_MAILER_FROM_EMAIL=[email protected] |
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
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; | ||
|
||
class ResolvedTwoFactorCodeCredentialsListener | ||
{ | ||
#[AsEventListener(event: AuthenticationTokenCreatedEvent::class, priority: 512)] | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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\Validator; | ||
|
||
use App\Entity\User; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
class TwoFactorCode extends Constraint | ||
{ | ||
public function __construct( | ||
public readonly User $user, | ||
public readonly string $message = 'Invalid authenticator code', | ||
) { | ||
parent::__construct(); | ||
} | ||
} |
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,34 @@ | ||
<?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\Validator; | ||
|
||
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpAuthenticatorInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
class TwoFactorCodeValidator extends ConstraintValidator | ||
{ | ||
public function __construct( | ||
private readonly TotpAuthenticatorInterface $totpAuthenticator, | ||
) {} | ||
|
||
/** | ||
* @param TwoFactorCode $constraint | ||
*/ | ||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$this->totpAuthenticator->checkCode($constraint->user, (string) $value)) { | ||
$this->context->addViolation($constraint->message); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.