File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed
src/Symfony/Bundle/FrameworkBundle Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change 11CHANGELOG
22=========
33
4+ 2.6.0
5+ -----
6+
7+ * Added ` Controller::isCsrfTokenValid ` helper
8+
492.5.0
510-----
611
Original file line number Diff line number Diff line change 1919use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
2020use Symfony \Component \HttpKernel \HttpKernelInterface ;
2121use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
22+ use Symfony \Component \Security \Csrf \CsrfToken ;
2223use Symfony \Component \Form \FormTypeInterface ;
2324use Symfony \Component \Form \Form ;
2425use 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}
You can’t perform that action at this time.
0 commit comments