Extension::generateCsrfToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 2
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Schnittstabil\Csrf\Twig\Helpers;
4
5
/**
6
 * CSRF (Cross-Site Request Forgery) protection Twig extension.
7
 */
8
class Extension extends \Twig_Extension
9
{
10
    /**
11
     * The token generator.
12
     *
13
     * @var callable
14
     */
15
    protected $tokenGenerator;
16
17
    /**
18
     * The token name.
19
     *
20
     * @var string
21
     */
22
    protected $tokenName;
23
24
    /**
25
     * Create a new Extension.
26
     *
27
     * @param callable $tokenGenerator the token generator
28
     * @param string   $tokenName      the token name
29
     */
30
    public function __construct(callable $tokenGenerator, $tokenName = 'X-XSRF-TOKEN')
31
    {
32
        $this->tokenGenerator = $tokenGenerator;
33
        $this->tokenName = $tokenName;
34
    }
35
36
    /**
37
     * Returns the name of the extension.
38
     *
39
     * @return string The extension name
40
     */
41
    public function getName()
42
    {
43
        return 'schnittstabil_csrf_twig_helpers_extension';
44
    }
45
46
    /**
47
     * Returns a list of functions to add to the existing list.
48
     *
49
     * @return array An array of functions
50
     */
51
    public function getFunctions()
52
    {
53
        return [
54
            new \Twig_SimpleFunction('csrf_token_name', [$this, 'getTokenName']),
55
            new \Twig_SimpleFunction('csrf_token', [$this, 'generateCsrfToken']),
56
            new \Twig_SimpleFunction(
57
                'csrf_input_widget',
58
                [$this, 'generateInputWidget'],
59
                ['needs_environment' => true, 'is_safe' => ['html']]
60
            ),
61
            new \Twig_SimpleFunction(
62
                'csrf_meta_widget',
63
                [$this, 'generateMetaWidget'],
64
                ['needs_environment' => true, 'is_safe' => ['html']]
65
            ),
66
        ];
67
    }
68
69
    /**
70
     * Returns the token name.
71
     *
72
     * @return string
73
     */
74
    public function getTokenName()
75
    {
76
        return $this->tokenName;
77
    }
78
79
    /**
80
     * Generate a new token.
81
     *
82
     * @return mixed
83
     */
84
    public function generateCsrfToken()
85
    {
86
        return call_user_func($this->tokenGenerator);
87
    }
88
89
    /**
90
     * Generate a new csrf input widget.
91
     *
92
     * @param \Twig_Environment $env twig environment needed for escaping
93
     *
94
     * @return string
95
     */
96 View Code Duplication
    public function generateInputWidget(\Twig_Environment $env)
97
    {
98
        $token = twig_escape_filter($env, $this->generateCsrfToken(), 'html');
99
        $tokenName = twig_escape_filter($env, $this->getTokenName(), 'html');
100
101
        return "<input name=\"$tokenName\" type=\"hidden\" value=\"$token\" />";
102
    }
103
104
    /**
105
     * Generate a new csrf meta widget.
106
     *
107
     * @param \Twig_Environment $env twig environment needed for escaping
108
     *
109
     * @return string
110
     */
111 View Code Duplication
    public function generateMetaWidget(\Twig_Environment $env)
112
    {
113
        $token = twig_escape_filter($env, $this->generateCsrfToken(), 'html');
114
        $tokenName = twig_escape_filter($env, $this->getTokenName(), 'html');
115
116
        return "<meta name=\"$tokenName\" content=\"$token\" />";
117
    }
118
}
119