MaintenanceModeBypassCookie.php
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace Illuminate\Foundation\Http;
use Illuminate\Support\Carbon;
use Symfony\Component\HttpFoundation\Cookie;
class MaintenanceModeBypassCookie
{
/**
* Create a new maintenance mode bypass cookie.
*
* @param string $key
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public static function create(string $key)
{
$expiresAt = Carbon::now()->addHours(12);
return new Cookie('laravel_maintenance', base64_encode(json_encode([
'expires_at' => $expiresAt->getTimestamp(),
'mac' => hash_hmac('sha256', $expiresAt->getTimestamp(), $key),
])), $expiresAt);
}
/**
* Determine if the given maintenance mode bypass cookie is valid.
*
* @param string $cookie
* @param string $key
* @return bool
*/
public static function isValid(string $cookie, string $key)
{
$payload = json_decode(base64_decode($cookie), true);
return is_array($payload) &&
is_numeric($payload['expires_at'] ?? null) &&
isset($payload['mac']) &&
hash_equals(hash_hmac('sha256', $payload['expires_at'], $key), $payload['mac']) &&
(int) $payload['expires_at'] >= Carbon::now()->getTimestamp();
}
}