Skip to content

Commit da1fd19

Browse files
feature #11602 [DX] [FrameworkBundle] Added Crontoller::isCsrfTokenValid (lyrixx)
This PR was merged into the 2.6-dev branch. Discussion ---------- [DX] [FrameworkBundle] Added Crontoller::isCsrfTokenValid | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Commits ------- 479c833 [FrameworkBundle] Added Crontoller::isCsrfTokenValid
2 parents 8b9340c + d55c350 commit da1fd19

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.6.0
5+
-----
6+
7+
* Added `Controller::isCsrfTokenValid` helper
8+
49
2.5.0
510
-----
611

Controller/Controller.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2020
use Symfony\Component\HttpKernel\HttpKernelInterface;
2121
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
22+
use Symfony\Component\Security\Csrf\CsrfToken;
2223
use Symfony\Component\Form\FormTypeInterface;
2324
use Symfony\Component\Form\Form;
2425
use Symfony\Component\Form\FormBuilder;
@@ -273,4 +274,21 @@ public function get($id)
273274
{
274275
return $this->container->get($id);
275276
}
277+
278+
/**
279+
* Checks the validity of a CSRF token
280+
*
281+
* @param string $id The id used when generating the token
282+
* @param string $token The actual token sent with the request that should be validated
283+
*
284+
* @return bool
285+
*/
286+
protected function isCsrfTokenValid($id, $token)
287+
{
288+
if (!$this->container->has('security.csrf.token_manager')) {
289+
throw new \LogicException('CSRF protection is not enabled in your application.');
290+
}
291+
292+
return $this->container->get('security.csrf.token_manager')->isTokenValid(new CsrfToken($id, $token));
293+
}
276294
}

0 commit comments

Comments
 (0)