|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Schnittstabil\Psr7\Csrf\Middlewares; |
|
4
|
|
|
|
|
5
|
|
|
use Dflydev\FigCookies\FigResponseCookies; |
|
6
|
|
|
use Dflydev\FigCookies\SetCookie; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Middleware for sending CSRF tokens by cookies. |
|
12
|
|
|
*/ |
|
13
|
|
|
class RespondWithCookieToken |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Used to generate tokens. |
|
17
|
|
|
* |
|
18
|
|
|
* @var callable |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $tokenGenerator; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Cookie name. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $cookieName; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Additional SetCookie modifier. |
|
31
|
|
|
* |
|
32
|
|
|
* @var callable|null |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $modify; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create new RespondWithCookieToken middleware. |
|
38
|
|
|
* |
|
39
|
|
|
* @param callable $tokenGenerator Used to generate tokens |
|
40
|
|
|
* @param string $cookieName Cookie name |
|
41
|
|
|
* @param callable $modify Allows to modify the cookie; same signature as `$this->modifyCookie` |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(callable $tokenGenerator, $cookieName = 'XSRF-TOKEN', callable $modify = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->tokenGenerator = $tokenGenerator; |
|
46
|
|
|
$this->cookieName = $cookieName; |
|
47
|
|
|
$this->modify = $modify; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Invoke middleware. |
|
52
|
|
|
* |
|
53
|
|
|
* @param ServerRequestInterface $request request object |
|
54
|
|
|
* @param ResponseInterface $response response object |
|
55
|
|
|
* @param callable $next next middleware |
|
56
|
|
|
* |
|
57
|
|
|
* @return ResponseInterface response object |
|
58
|
|
|
* |
|
59
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
62
|
|
|
{ |
|
63
|
|
|
$response = FigResponseCookies::modify( |
|
64
|
|
|
$response, |
|
65
|
|
|
$this->cookieName, |
|
66
|
|
|
function (SetCookie $setCookie) use ($request, $response) { |
|
67
|
|
|
return $this->modifyCookie($request, $response, $setCookie); |
|
68
|
|
|
} |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return $next($request, $response); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Modify the `$response` cookie. |
|
76
|
|
|
* |
|
77
|
|
|
* @param ServerRequestInterface $request request object |
|
78
|
|
|
* @param ResponseInterface $response response object |
|
79
|
|
|
* @param SetCookie $setCookie the cookie to modify |
|
80
|
|
|
* |
|
81
|
|
|
* @return SetCookie the modified cookie |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function modifyCookie(ServerRequestInterface $request, ResponseInterface $response, SetCookie $setCookie) |
|
84
|
|
|
{ |
|
85
|
|
|
$setCookie = $setCookie->withValue(call_user_func($this->tokenGenerator)); |
|
86
|
|
|
|
|
87
|
|
|
if ($setCookie->getPath() === null) { |
|
88
|
|
|
$setCookie = $setCookie->withPath('/'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($this->modify !== null) { |
|
92
|
|
|
$setCookie = call_user_func($this->modify, $request, $response, $setCookie); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $setCookie; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|