|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Csrf; |
|
4
|
|
|
|
|
5
|
|
|
use BadMethodCallException; |
|
6
|
|
|
use Bdf\Form\Aggregate\FormBuilderInterface; |
|
7
|
|
|
use Bdf\Form\ElementBuilderInterface; |
|
8
|
|
|
use Bdf\Form\ElementInterface; |
|
9
|
|
|
use LogicException; |
|
10
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Builder for a CsrfElement |
|
14
|
|
|
* |
|
15
|
|
|
* Unlike other elements, some methods are disallowed, due to the CSRF implementation : |
|
16
|
|
|
* - satisfy() : The only performed validation is the CSRF token check |
|
17
|
|
|
* - transformer() : Not implemented |
|
18
|
|
|
* - value() : `import()` value is disabled on the element, and token value is always provided on the view |
|
19
|
|
|
* - default() : A token must be provided by the HTTP request |
|
20
|
|
|
* |
|
21
|
|
|
* <code> |
|
22
|
|
|
* $builder->csrf()->message('invalid token')->invalidate(); |
|
23
|
|
|
* </code> |
|
24
|
|
|
* |
|
25
|
|
|
* @see CsrfElement |
|
26
|
|
|
* @see FormBuilderInterface::csrf() |
|
27
|
|
|
* |
|
28
|
|
|
* @implements ElementBuilderInterface<CsrfElement> |
|
29
|
|
|
*/ |
|
30
|
|
|
class CsrfElementBuilder implements ElementBuilderInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $tokenId = CsrfElement::class; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string|null |
|
39
|
|
|
*/ |
|
40
|
|
|
private $message = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var bool |
|
44
|
|
|
*/ |
|
45
|
|
|
private $invalidate = false; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var CsrfTokenManagerInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $tokenManager; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* CsrfElementBuilder constructor. |
|
54
|
|
|
*/ |
|
55
|
10 |
|
public function __construct() |
|
56
|
|
|
{ |
|
57
|
10 |
|
if (!interface_exists(CsrfTokenManagerInterface::class)) { |
|
58
|
|
|
throw new LogicException(CsrfTokenManagerInterface::class.' cannot be found. The package "symfony/security-csrf" must be installed for use the CsrfElement'); |
|
59
|
|
|
} |
|
60
|
10 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Define the CSRF token id |
|
64
|
|
|
* By default, the token id is CsrfElement class name |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $tokenId |
|
67
|
|
|
* |
|
68
|
|
|
* @return $this |
|
69
|
|
|
* |
|
70
|
|
|
* @see CsrfTokenManagerInterface::getToken() The parameter tokenId will be used as parameter of this method |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function tokenId(string $tokenId): self |
|
73
|
|
|
{ |
|
74
|
1 |
|
$this->tokenId = $tokenId; |
|
75
|
|
|
|
|
76
|
1 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Define the error message |
|
81
|
|
|
* |
|
82
|
|
|
* @param string|null $message The message, or null to use the default one |
|
83
|
|
|
* |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function message(?string $message): self |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->message = $message; |
|
89
|
|
|
|
|
90
|
1 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Does the token should be invalidated after check ? |
|
95
|
|
|
* |
|
96
|
|
|
* @param bool $invalidate If true, the token is for one use, if false, the token can be reused |
|
97
|
|
|
* |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function invalidate(bool $invalidate = true): self |
|
101
|
|
|
{ |
|
102
|
1 |
|
$this->invalidate = $invalidate; |
|
103
|
|
|
|
|
104
|
1 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Define the CSRF token manager |
|
109
|
|
|
* |
|
110
|
|
|
* @param CsrfTokenManagerInterface $tokenManager |
|
111
|
|
|
* |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
8 |
|
public function tokenManager(CsrfTokenManagerInterface $tokenManager): self |
|
115
|
|
|
{ |
|
116
|
8 |
|
$this->tokenManager = $tokenManager; |
|
117
|
|
|
|
|
118
|
8 |
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function satisfy($constraint, $options = null, bool $append = true) |
|
125
|
|
|
{ |
|
126
|
1 |
|
throw new BadMethodCallException(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function transformer($transformer, bool $append = true) |
|
133
|
|
|
{ |
|
134
|
1 |
|
throw new BadMethodCallException(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function value($value) |
|
141
|
|
|
{ |
|
142
|
1 |
|
throw new BadMethodCallException(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
6 |
|
public function buildElement(): ElementInterface |
|
149
|
|
|
{ |
|
150
|
6 |
|
return new CsrfElement( |
|
151
|
6 |
|
$this->tokenId, |
|
152
|
6 |
|
new CsrfValueValidator($this->invalidate, $this->message ? ['message' => $this->message] : []), |
|
153
|
6 |
|
$this->tokenManager |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|