1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JwPersistentUser\Service; |
4
|
|
|
|
5
|
|
|
use JwPersistentUser\Model\ModuleOptions; |
6
|
|
|
use JwPersistentUser\Model\SerieTokenInterface; |
7
|
|
|
|
8
|
|
|
use Zend\Http\Request; |
9
|
|
|
use Zend\Http\Response; |
10
|
|
|
use Zend\Http\Header\SetCookie; |
11
|
|
|
|
12
|
|
|
class CookieService |
13
|
|
|
{ |
14
|
|
|
const COOKIE_NAME = 'JwPersistentUser'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ModuleOptions |
18
|
|
|
*/ |
19
|
|
|
protected $moduleOptions; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Request $request |
23
|
|
|
* @param Response|null $response |
24
|
|
|
* @return SerieTokenInterface|null |
25
|
|
|
*/ |
26
|
3 |
|
public function read(Request $request, Response $response = null) |
27
|
|
|
{ |
28
|
3 |
|
$cookie = $request->getCookie(); |
29
|
3 |
|
if (!isset($cookie[self::COOKIE_NAME])) { |
30
|
1 |
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
$parts = explode(':', $cookie[self::COOKIE_NAME]); |
34
|
2 |
|
if (!is_array($parts) || count($parts) !== 3) { |
35
|
1 |
|
if ($response) { |
36
|
1 |
|
$this->writeNull($response); |
37
|
|
|
} |
38
|
1 |
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
$serieTokenEntityClass = $this->getModuleOptions()->getSerieTokenEntityClass(); |
42
|
1 |
|
$serieToken = new $serieTokenEntityClass; |
43
|
1 |
|
$serieToken->setUserId($parts[0]); |
44
|
1 |
|
$serieToken->setSerie($parts[1]); |
45
|
1 |
|
$serieToken->setToken($parts[2]); |
46
|
|
|
|
47
|
1 |
|
return $serieToken; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return SetCookie |
52
|
|
|
*/ |
53
|
2 |
|
public function writeNull(Response $response) |
54
|
|
|
{ |
55
|
2 |
|
$this->setCookie($response, $this->appendWithDefaultParameters(new SetCookie( |
56
|
2 |
|
self::COOKIE_NAME, |
57
|
2 |
|
null, |
58
|
2 |
|
time() - 3600, |
59
|
2 |
|
'/' |
60
|
|
|
))); |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param SerieTokenInterface $serieToken |
65
|
|
|
* @return SetCookie |
66
|
|
|
*/ |
67
|
2 |
|
public function writeSerie(Response $response, SerieTokenInterface $serieToken) |
68
|
|
|
{ |
69
|
|
|
$serieRepresentation = |
70
|
2 |
|
$serieToken->getUserId() . |
71
|
2 |
|
':' . $serieToken->getSerie() . |
72
|
2 |
|
':' . $serieToken->getToken(); |
73
|
|
|
|
74
|
2 |
|
$this->setCookie($response, $this->appendWithDefaultParameters(new SetCookie( |
75
|
2 |
|
self::COOKIE_NAME, |
76
|
2 |
|
$serieRepresentation, |
77
|
2 |
|
$serieToken->getExpiresAt()->getTimestamp(), |
78
|
2 |
|
$this->getModuleOptions()->getCookiePath(), |
79
|
2 |
|
$this->getModuleOptions()->getCookieDomain(), |
80
|
2 |
|
$this->getModuleOptions()->getCookieSecure(), |
|
|
|
|
81
|
2 |
|
$this->getModuleOptions()->getCookieHttpOnly(), |
|
|
|
|
82
|
2 |
|
$this->getModuleOptions()->getCookieMaxAge(), |
83
|
2 |
|
$this->getModuleOptions()->getCookieVersion(), |
84
|
2 |
|
$this->getModuleOptions()->getCookieSameSite() |
85
|
|
|
))); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
4 |
|
private function appendWithDefaultParameters(SetCookie $header) |
89
|
|
|
{ |
90
|
4 |
|
$header->setHttponly(true); |
91
|
4 |
|
return $header; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Response $response |
96
|
|
|
* @param SetCookie $cookie |
97
|
|
|
*/ |
98
|
4 |
|
protected static function setCookie(Response $response, SetCookie $cookie) |
99
|
|
|
{ |
100
|
4 |
|
$response->getHeaders()->addHeader($cookie); |
101
|
4 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return ModuleOptions |
105
|
|
|
*/ |
106
|
3 |
|
public function getModuleOptions() |
107
|
|
|
{ |
108
|
3 |
|
return $this->moduleOptions; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param ModuleOptions $moduleOptions |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
6 |
|
public function setModuleOptions($moduleOptions) |
116
|
|
|
{ |
117
|
6 |
|
$this->moduleOptions = $moduleOptions; |
118
|
6 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.