1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Phauthentic\Authentication\Authenticator\Storage; |
5
|
|
|
|
6
|
|
|
use Phauthentic\Authentication\Authenticator\Storage\StorageInterface; |
7
|
|
|
use Cake\Http\Cookie\Cookie; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Storage adapter for the CakePHP Cookie |
13
|
|
|
*/ |
14
|
|
|
class CakeCookieStorage implements StorageInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $cookieData = [ |
20
|
|
|
'name' => 'CookieAuth', |
21
|
|
|
'expire' => null, |
22
|
|
|
'path' => '/', |
23
|
|
|
'domain' => '', |
24
|
|
|
'secure' => false, |
25
|
|
|
'httpOnly' => false |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Sets cookie data. |
30
|
|
|
* |
31
|
|
|
* @param array $data Cookie data. |
32
|
|
|
* @param bool $merge Whether to merge or replace. |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function setCookieData(array $data, bool $merge = true): self |
36
|
|
|
{ |
37
|
|
|
if ($merge) { |
38
|
|
|
$this->cookieData = $data + $this->cookieData; |
39
|
|
|
} else { |
40
|
|
|
$this->cookieData = $data; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Creates a cookie instance with configured defaults. |
48
|
|
|
* |
49
|
|
|
* @param mixed $value Cookie value. |
50
|
|
|
* @return \Cake\Http\Cookie\CookieInterface |
51
|
|
|
*/ |
52
|
3 |
|
protected function _createCookie($value) |
53
|
|
|
{ |
54
|
3 |
|
$data = $this->cookieData; |
55
|
|
|
|
56
|
3 |
|
$cookie = new Cookie( |
57
|
3 |
|
$data['name'], |
58
|
3 |
|
$value, |
59
|
3 |
|
$data['expire'], |
60
|
3 |
|
$data['path'], |
61
|
3 |
|
$data['domain'], |
62
|
3 |
|
$data['secure'], |
63
|
3 |
|
$data['httpOnly'] |
64
|
|
|
); |
65
|
|
|
|
66
|
3 |
|
return $cookie; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
2 |
|
public function read(ServerRequestInterface $request) |
73
|
|
|
{ |
74
|
2 |
|
$cookies = $request->getCookieParams(); |
75
|
2 |
|
$cookieName = $this->cookieData['name']; |
76
|
|
|
|
77
|
2 |
|
if (!isset($cookies[$cookieName])) { |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
$value = $cookies[$cookieName]; |
82
|
|
|
|
83
|
2 |
|
if (is_string($value)) { |
84
|
1 |
|
$value = json_decode($value, true); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
return $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
1 |
|
public function clear(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
94
|
|
|
{ |
95
|
1 |
|
$cookie = $this->_createCookie(null)->withExpired(); |
96
|
|
|
|
97
|
1 |
|
return $response->withAddedHeader('Set-Cookie', $cookie->toHeaderValue()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritDoc} |
102
|
|
|
*/ |
103
|
2 |
|
public function write(ServerRequestInterface $request, ResponseInterface $response, $data): ResponseInterface |
104
|
|
|
{ |
105
|
2 |
|
if (!is_string($data)) { |
106
|
1 |
|
$data = json_encode($data); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
$cookie = $this->_createCookie($data); |
110
|
|
|
|
111
|
2 |
|
return $response->withAddedHeader('Set-Cookie', $cookie->toHeaderValue()); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|