Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added automatically CSRF protected authenticators
- Loading branch information
Showing
4 changed files
with
99 additions
and
10 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
34 changes: 34 additions & 0 deletions
34
src/Symfony/Component/Security/Http/Authenticator/CsrfProtectedAuthenticatorInterface.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,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Authenticator; | ||
|
||
/** | ||
* This interface can be implemented to automatically add CSF | ||
* protection to the authenticator. | ||
* | ||
* @author Wouter de Jong <wouter@wouterj.nl> | ||
*/ | ||
interface CsrfProtectedAuthenticatorInterface | ||
{ | ||
/** | ||
* An arbitrary string used to generate the value of the CSRF token. | ||
* Using a different string for each authenticator improves its security. | ||
*/ | ||
public function getCsrfTokenId(): string; | ||
|
||
/** | ||
* Returns the CSRF token contained in credentials if any. | ||
* | ||
* @param mixed $credentials the credentials returned by getCredentials() | ||
*/ | ||
public function getCsrfToken($credentials): ?string; | ||
} |
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
52 changes: 52 additions & 0 deletions
52
src/Symfony/Component/Security/Http/EventListener/CsrfProtectionListener.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,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException; | ||
use Symfony\Component\Security\Csrf\CsrfToken; | ||
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; | ||
use Symfony\Component\Security\Http\Authenticator\CsrfProtectedAuthenticatorInterface; | ||
use Symfony\Component\Security\Http\Event\VerifyAuthenticatorCredentialsEvent; | ||
|
||
class CsrfProtectionListener implements EventSubscriberInterface | ||
{ | ||
private $csrfTokenManager; | ||
|
||
public function __construct(CsrfTokenManagerInterface $csrfTokenManager) | ||
{ | ||
$this->csrfTokenManager = $csrfTokenManager; | ||
} | ||
|
||
public function verifyCredentials(VerifyAuthenticatorCredentialsEvent $event): void | ||
{ | ||
$authenticator = $event->getAuthenticator(); | ||
if (!$authenticator instanceof CsrfProtectedAuthenticatorInterface) { | ||
return; | ||
} | ||
|
||
$csrfTokenValue = $authenticator->getCsrfToken($event->getCredentials()); | ||
if (null === $csrfTokenValue) { | ||
return; | ||
} | ||
|
||
$csrfToken = new CsrfToken($authenticator->getCsrfTokenId(), $csrfTokenValue); | ||
if (false === $this->csrfTokenManager->isTokenValid($csrfToken)) { | ||
throw new InvalidCsrfTokenException('Invalid CSRF token.'); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [VerifyAuthenticatorCredentialsEvent::class => ['verifyCredentials', 256]]; | ||
} | ||
} |