1<?php
2
3/*
4 * This file is part of the Silex framework.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Silex\Provider;
13
14use Pimple\Container;
15use Pimple\ServiceProviderInterface;
16use Symfony\Component\Security\Csrf\CsrfTokenManager;
17use Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator;
18use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
19use Symfony\Component\Security\Csrf\TokenStorage\NativeSessionTokenStorage;
20
21/**
22 * Symfony CSRF Security component Provider.
23 *
24 * @author Fabien Potencier <fabien@symfony.com>
25 */
26class CsrfServiceProvider implements ServiceProviderInterface
27{
28    public function register(Container $app)
29    {
30        $app['csrf.token_manager'] = function ($app) {
31            return new CsrfTokenManager($app['csrf.token_generator'], $app['csrf.token_storage']);
32        };
33
34        $app['csrf.token_storage'] = function ($app) {
35            if (isset($app['session'])) {
36                return new SessionTokenStorage($app['session'], $app['csrf.session_namespace']);
37            }
38
39            return new NativeSessionTokenStorage($app['csrf.session_namespace']);
40        };
41
42        $app['csrf.token_generator'] = function ($app) {
43            return new UriSafeTokenGenerator();
44        };
45
46        $app['csrf.session_namespace'] = '_csrf';
47    }
48}
49