|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Schnittstabil\Psr7\Csrf; |
|
4
|
|
|
|
|
5
|
|
|
use Schnittstabil\Csrf\TokenService\TokenService; |
|
6
|
|
|
use Schnittstabil\Csrf\TokenService\TokenServiceInterface; |
|
7
|
|
|
use Schnittstabil\Psr7\Csrf\Middlewares\AcceptHeaderToken; |
|
8
|
|
|
use Schnittstabil\Psr7\Csrf\Middlewares\AcceptMethods; |
|
9
|
|
|
use Schnittstabil\Psr7\Csrf\Middlewares\AcceptParsedBodyToken; |
|
10
|
|
|
use Schnittstabil\Psr7\Csrf\Middlewares\Guard; |
|
11
|
|
|
use Schnittstabil\Psr7\Csrf\Middlewares\GuardInterface; |
|
12
|
|
|
use Schnittstabil\Psr7\Csrf\Middlewares\RespondWithCookieToken; |
|
13
|
|
|
use Schnittstabil\Psr7\Csrf\Middlewares\RespondWithHeaderToken; |
|
14
|
|
|
use Schnittstabil\Psr7\MiddlewareStack\CallableMiddlewareStackTrait; |
|
15
|
|
|
use Schnittstabil\Psr7\MiddlewareStack\MiddlewareStackInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* CSRF protection middleware. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Middleware implements MiddlewareStackInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use CallableMiddlewareStackTrait; |
|
23
|
|
|
|
|
24
|
|
|
protected $isGuarded; |
|
25
|
|
|
protected $tokenService; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a new Middleware. |
|
29
|
|
|
* |
|
30
|
|
|
* @param TokenServiceInterface $tokenService A token service |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(TokenServiceInterface $tokenService) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->isGuarded = false; |
|
35
|
|
|
$this->tokenService = $tokenService; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get the token service. |
|
40
|
|
|
* |
|
41
|
|
|
* @return TokenServiceInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getTokenService() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->tokenService; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Push a middleware onto the top of a new Stack instance. |
|
50
|
|
|
* |
|
51
|
|
|
* @param callable $newTopMiddleware the middleware to be pushed onto the top |
|
52
|
|
|
* |
|
53
|
|
|
* @return static the new instance |
|
54
|
|
|
* |
|
55
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
|
56
|
|
|
*/ |
|
57
|
|
|
public function add(callable $newTopMiddleware) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->isGuarded) { |
|
60
|
|
|
if ($newTopMiddleware instanceof GuardInterface) { |
|
61
|
|
|
throw new \RuntimeException('Invalid state: already guarded'); |
|
62
|
|
|
} |
|
63
|
|
|
} else { |
|
64
|
|
|
if (!($newTopMiddleware instanceof GuardInterface)) { |
|
65
|
|
|
throw new \RuntimeException('Invalid state: not guarded'); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$clone = clone $this; |
|
70
|
|
|
$clone->isGuarded = true; |
|
71
|
|
|
|
|
72
|
|
|
return $clone->push($newTopMiddleware); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Add new Guard middleware. |
|
77
|
|
|
* |
|
78
|
|
|
* @param callable $rejectMiddleware Defaults to `new Reject()` |
|
79
|
|
|
* |
|
80
|
|
|
* @return static |
|
81
|
|
|
*/ |
|
82
|
|
|
public function withGuard(callable $rejectMiddleware = null) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->add(new Guard($rejectMiddleware)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Add new AcceptHeaderToken middleware. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $headerName Header field name |
|
91
|
|
|
* |
|
92
|
|
|
* @return static |
|
93
|
|
|
*/ |
|
94
|
|
|
public function withAcceptHeaderToken($headerName = 'X-XSRF-TOKEN') |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->add(new AcceptHeaderToken([$this->tokenService, 'getConstraintViolations'], $headerName)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Add new AcceptMethods middleware. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string[] $methods HTTP methods allowed to bypass CSRF protection |
|
103
|
|
|
* |
|
104
|
|
|
* @return static |
|
105
|
|
|
*/ |
|
106
|
|
|
public function withAcceptMethods(array $methods = array('GET', 'OPTIONS')) |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->add(new AcceptMethods($methods)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Add new AcceptParsedBodyToken middleware. |
|
113
|
|
|
* |
|
114
|
|
|
* @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValue` |
|
115
|
|
|
* |
|
116
|
|
|
* @param string|int|mixed[] $path a `Schnittstabil\Get\getValue` path |
|
117
|
|
|
* |
|
118
|
|
|
* @return static |
|
119
|
|
|
*/ |
|
120
|
|
|
public function withAcceptParsedBodyToken($path = 'X-XSRF-TOKEN') |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->add(new AcceptParsedBodyToken([$this->tokenService, 'getConstraintViolations'], $path)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Add new RespondWithCookieToken middleware. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $cookieName Cookie name |
|
129
|
|
|
* @param callable $modify Allows to modify the cookie; same signature as `$this->modifyCookie` |
|
130
|
|
|
* |
|
131
|
|
|
* @return static |
|
132
|
|
|
*/ |
|
133
|
|
|
public function withRespondWithCookieToken($cookieName = 'XSRF-TOKEN', callable $modify = null) |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->add(new RespondWithCookieToken([$this->tokenService, 'generate'], $cookieName, $modify)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Add new RespondWithHeaderToken middleware. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $headerName Header field name |
|
142
|
|
|
* |
|
143
|
|
|
* @return static |
|
144
|
|
|
*/ |
|
145
|
|
|
public function withRespondWithHeaderToken($headerName = 'XSRF-TOKEN') |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->add(new RespondWithHeaderToken([$this->tokenService, 'generate'], $headerName)); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|