9
9
namespace Redbitcz \DebugMode ;
10
10
11
11
use Nette \IOException ;
12
+ use Nette \Utils \DateTime as NetteDateTime ;
12
13
use Nette \Utils \FileSystem ;
13
14
use Nette \Utils \Json ;
14
15
use Nette \Utils \JsonException ;
@@ -20,40 +21,63 @@ class Enabler
20
21
private const DEBUG_COOKIE_NAME = 'app-debug-token ' ;
21
22
private const TOKEN_LENGTH = 30 ;
22
23
private const ID_LENGTH = 15 ;
23
- private const TTL = 3600 ;
24
- // TODO: Make configurable TLS requirements
25
- private const REQUIRE_HTTPS = false ;
24
+ private const DEFAULT_TTL = '1 hour ' ;
26
25
27
- /** @var string */
28
- private $ tempDir ;
26
+ private string $ tempDir ;
27
+ private ? bool $ override ;
29
28
30
- /** @var bool|null */
31
- private $ override ;
29
+ /** @var array<string, string|bool> */
30
+ private array $ cookieOptions = [
31
+ 'path ' => '/ ' ,
32
+ 'domain ' => '' ,
33
+ 'secure ' => true ,
34
+ 'httponly ' => true ,
35
+ 'samesite ' => 'Strict ' ,
36
+ ];
32
37
33
38
public function __construct (string $ tempDir )
34
39
{
35
40
$ this ->tempDir = $ tempDir ;
36
41
}
37
42
38
- public function activate (bool $ isDebug ): void
43
+ /** @return static */
44
+ public function setSecure (bool $ secure = true ): self
45
+ {
46
+ $ this ->cookieOptions ['secure ' ] = $ secure ;
47
+ return $ this ;
48
+ }
49
+
50
+ /**
51
+ * @return $this
52
+ * @see setcookie()
53
+ */
54
+ public function setCookieOptions (
55
+ ?string $ path = null ,
56
+ ?string $ domain = null ,
57
+ ?bool $ secure = null ,
58
+ ?bool $ httponly = null ,
59
+ ?string $ samesite = null
60
+ ): self {
61
+ $ this ->cookieOptions = [
62
+ 'path ' => $ path ?? $ this ->cookieOptions ['path ' ],
63
+ 'domain ' => $ domain ?? $ this ->cookieOptions ['domain ' ],
64
+ 'secure ' => $ secure ?? $ this ->cookieOptions ['secure ' ],
65
+ 'httponly ' => $ httponly ?? $ this ->cookieOptions ['httponly ' ],
66
+ 'samesite ' => $ samesite ?? $ this ->cookieOptions ['samesite ' ],
67
+ ];
68
+ return $ this ;
69
+ }
70
+
71
+ public function activate (bool $ isDebug , ?string $ time = self ::DEFAULT_TTL ): void
39
72
{
40
73
if ($ tokenName = $ this ->getTokenName ()) {
41
74
$ this ->destroyToken ($ tokenName );
42
75
}
43
76
44
- $ tokenName = $ this ->createToken ($ isDebug );
45
- setcookie (
46
- self ::DEBUG_COOKIE_NAME ,
47
- $ tokenName ,
48
- [
49
- 'expires ' => time () + self ::TTL ,
50
- 'path ' => '/ ' ,
51
- 'domain ' => '' ,
52
- 'secure ' => self ::REQUIRE_HTTPS ,
53
- 'httponly ' => true ,
54
- 'samesite ' => 'Strict ' ,
55
- ]
56
- );
77
+ $ tokenExpires = (int )NetteDateTime::from ($ time ?? self ::DEFAULT_TTL )->format ('U ' );
78
+ $ cookieExpires = $ time === null ? 0 : $ tokenExpires ;
79
+ $ tokenName = $ this ->createToken ($ isDebug , $ tokenExpires );
80
+ setcookie (self ::DEBUG_COOKIE_NAME , $ tokenName , ['expires ' => $ cookieExpires ] + $ this ->cookieOptions );
57
81
58
82
$ this ->override = $ isDebug ;
59
83
}
@@ -65,19 +89,7 @@ public function deactivate(): void
65
89
$ this ->destroyToken ($ tokenName );
66
90
}
67
91
68
- setcookie (
69
- self ::DEBUG_COOKIE_NAME ,
70
- '' ,
71
- [
72
- 'expires ' => 0 ,
73
- 'path ' => '/ ' ,
74
- 'domain ' => '' ,
75
- 'secure ' => self ::REQUIRE_HTTPS ,
76
- 'httponly ' => true ,
77
- 'samesite ' => 'Strict ' ,
78
- ]
79
- );
80
-
92
+ setcookie (self ::DEBUG_COOKIE_NAME , '' , ['expires ' => time ()] + $ this ->cookieOptions );
81
93
$ this ->override = null ;
82
94
}
83
95
@@ -117,10 +129,10 @@ private function checkToken(string $name): ?bool
117
129
return $ this ->getListTokenValue ($ name , $ list );
118
130
}
119
131
120
- private function createToken (bool $ value ): string
132
+ private function createToken (bool $ value, int $ expires ): string
121
133
{
122
134
$ list = $ this ->loadList ();
123
- $ name = $ this ->addListToken ($ value , $ list );
135
+ $ name = $ this ->addListToken ($ value , $ expires , $ list );
124
136
$ this ->saveList ($ list );
125
137
126
138
return $ name ;
@@ -144,22 +156,18 @@ private function loadList(): array
144
156
}
145
157
146
158
/**
147
- * @param bool $value
148
159
* @param array<int, array> $list
149
- * @return string
150
160
*/
151
- private function addListToken (bool $ value , array &$ list ): string
161
+ private function addListToken (bool $ value , int $ expires , array &$ list ): string
152
162
{
153
163
/** @var string $name */
154
- [$ name , $ token ] = $ this ->generateToken ($ value );
164
+ [$ name , $ token ] = $ this ->generateToken ($ value, $ expires );
155
165
$ list [] = $ token ;
156
166
return $ name ;
157
167
}
158
168
159
169
/**
160
- * @param string $name
161
170
* @param array<int, array> $list
162
- * @return bool|null
163
171
*/
164
172
private function getListTokenValue (string $ name , array $ list ): ?bool
165
173
{
@@ -175,7 +183,6 @@ private function getListTokenValue(string $name, array $list): ?bool
175
183
176
184
177
185
/**
178
- * @param string $name
179
186
* @param array<int, array> $list
180
187
*/
181
188
private function dropListToken (string $ name , array &$ list ): void
@@ -209,8 +216,6 @@ function ($token) {
209
216
210
217
/**
211
218
* @param array<string, string|bool|int> $token
212
- * @param string|null $name
213
- * @return bool
214
219
*/
215
220
private function isTokenValid (array $ token , ?string $ name = null ): bool
216
221
{
@@ -233,16 +238,15 @@ private function isTokenValid(array $token, ?string $name = null): bool
233
238
}
234
239
235
240
/**
236
- * @param bool $value
237
241
* @return array<int, string|array>
238
242
*/
239
- private function generateToken (bool $ value ): array
243
+ private function generateToken (bool $ value, int $ expires ): array
240
244
{
241
245
$ name = $ this ->generateTokenName ();
242
246
$ token = [
243
247
'id ' => $ this ->getIdByName ($ name ),
244
248
'hash ' => $ this ->getHashByName ($ name ),
245
- 'expire ' => time () + self :: TTL ,
249
+ 'expire ' => $ expires ,
246
250
'value ' => $ value
247
251
];
248
252
0 commit comments