-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ThrottlingListener to allow MFA attempt throttling
- Loading branch information
Showing
4 changed files
with
132 additions
and
1 deletion.
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
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
43 changes: 43 additions & 0 deletions
43
src/bundle/Security/TwoFactor/Event/ThrottlingListener.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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Event; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\RateLimiter\RequestRateLimiterInterface; | ||
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class ThrottlingListener implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private RequestRateLimiterInterface $requestRateLimiter, | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
TwoFactorAuthenticationEvents::ATTEMPT => 'onTwoFactorAttempt', | ||
TwoFactorAuthenticationEvents::SUCCESS => 'onTwoFactorSuccess', | ||
]; | ||
} | ||
|
||
public function onTwoFactorAttempt(TwoFactorAuthenticationEvent $event): void | ||
{ | ||
if (!$this->requestRateLimiter->consume($event->getRequest())->isAccepted()) { | ||
throw new TooManyRequestsHttpException(); | ||
} | ||
} | ||
|
||
public function onTwoFactorSuccess(TwoFactorAuthenticationEvent $event): void | ||
{ | ||
$this->requestRateLimiter->reset($event->getRequest()); | ||
} | ||
} |