4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Extension\Csrf\CsrfProvider
;
15 * Default implementation of CsrfProviderInterface.
17 * This provider uses the session ID returned by session_id() as well as a
18 * user-defined secret value to secure the CSRF token.
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 class DefaultCsrfProvider
implements CsrfProviderInterface
25 * A secret value used for generating the CSRF token
31 * Initializes the provider with a secret value
33 * A recommended value for the secret is a generated value with at least
34 * 32 characters and mixed letters, digits and special characters.
36 * @param string $secret A secret value included in the CSRF token
38 public function __construct($secret)
40 $this->secret
= $secret;
46 public function generateCsrfToken($intention)
48 return sha1($this->secret
.$intention.$this->getSessionId());
54 public function isCsrfTokenValid($intention, $token)
56 return $token === $this->generateCsrfToken($intention);
60 * Returns the ID of the user session.
62 * Automatically starts the session if necessary.
64 * @return string The session ID
66 protected function getSessionId()
68 if (version_compare(PHP_VERSION
, '5.4', '>=')) {
69 if (PHP_SESSION_NONE
=== session_status()) {
72 } elseif (!session_id()) {