Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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