Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Cookie\Middleware;
4
 
5
use Closure;
6
use Illuminate\Contracts\Encryption\DecryptException;
7
use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
8
use Illuminate\Cookie\CookieValuePrefix;
9
use Symfony\Component\HttpFoundation\Cookie;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
 
13
class EncryptCookies
14
{
15
    /**
16
     * The encrypter instance.
17
     *
18
     * @var \Illuminate\Contracts\Encryption\Encrypter
19
     */
20
    protected $encrypter;
21
 
22
    /**
23
     * The names of the cookies that should not be encrypted.
24
     *
399 lars 25
     * @var array<int, string>
148 lars 26
     */
27
    protected $except = [];
28
 
29
    /**
30
     * Indicates if cookies should be serialized.
31
     *
32
     * @var bool
33
     */
34
    protected static $serialize = false;
35
 
36
    /**
37
     * Create a new CookieGuard instance.
38
     *
39
     * @param  \Illuminate\Contracts\Encryption\Encrypter  $encrypter
40
     * @return void
41
     */
42
    public function __construct(EncrypterContract $encrypter)
43
    {
44
        $this->encrypter = $encrypter;
45
    }
46
 
47
    /**
48
     * Disable encryption for the given cookie name(s).
49
     *
50
     * @param  string|array  $name
51
     * @return void
52
     */
53
    public function disableFor($name)
54
    {
55
        $this->except = array_merge($this->except, (array) $name);
56
    }
57
 
58
    /**
59
     * Handle an incoming request.
60
     *
61
     * @param  \Illuminate\Http\Request  $request
62
     * @param  \Closure  $next
63
     * @return \Symfony\Component\HttpFoundation\Response
64
     */
65
    public function handle($request, Closure $next)
66
    {
67
        return $this->encrypt($next($this->decrypt($request)));
68
    }
69
 
70
    /**
71
     * Decrypt the cookies on the request.
72
     *
73
     * @param  \Symfony\Component\HttpFoundation\Request  $request
74
     * @return \Symfony\Component\HttpFoundation\Request
75
     */
76
    protected function decrypt(Request $request)
77
    {
78
        foreach ($request->cookies as $key => $cookie) {
79
            if ($this->isDisabled($key)) {
80
                continue;
81
            }
82
 
83
            try {
84
                $value = $this->decryptCookie($key, $cookie);
85
 
86
                $request->cookies->set($key, $this->validateValue($key, $value));
87
            } catch (DecryptException $e) {
88
                $request->cookies->set($key, null);
89
            }
90
        }
91
 
92
        return $request;
93
    }
94
 
95
    /**
96
     * Validate and remove the cookie value prefix from the value.
97
     *
98
     * @param  string  $key
99
     * @param  string  $value
100
     * @return string|array|null
101
     */
102
    protected function validateValue(string $key, $value)
103
    {
104
        return is_array($value)
105
                    ? $this->validateArray($key, $value)
106
                    : CookieValuePrefix::validate($key, $value, $this->encrypter->getKey());
107
    }
108
 
109
    /**
110
     * Validate and remove the cookie value prefix from all values of an array.
111
     *
112
     * @param  string  $key
113
     * @param  array  $value
114
     * @return array
115
     */
116
    protected function validateArray(string $key, array $value)
117
    {
118
        $validated = [];
119
 
120
        foreach ($value as $index => $subValue) {
121
            $validated[$index] = $this->validateValue("{$key}[{$index}]", $subValue);
122
        }
123
 
124
        return $validated;
125
    }
126
 
127
    /**
128
     * Decrypt the given cookie and return the value.
129
     *
130
     * @param  string  $name
131
     * @param  string|array  $cookie
132
     * @return string|array
133
     */
134
    protected function decryptCookie($name, $cookie)
135
    {
136
        return is_array($cookie)
137
                        ? $this->decryptArray($cookie)
138
                        : $this->encrypter->decrypt($cookie, static::serialized($name));
139
    }
140
 
141
    /**
142
     * Decrypt an array based cookie.
143
     *
144
     * @param  array  $cookie
145
     * @return array
146
     */
147
    protected function decryptArray(array $cookie)
148
    {
149
        $decrypted = [];
150
 
151
        foreach ($cookie as $key => $value) {
152
            if (is_string($value)) {
153
                $decrypted[$key] = $this->encrypter->decrypt($value, static::serialized($key));
154
            }
155
 
156
            if (is_array($value)) {
157
                $decrypted[$key] = $this->decryptArray($value);
158
            }
159
        }
160
 
161
        return $decrypted;
162
    }
163
 
164
    /**
165
     * Encrypt the cookies on an outgoing response.
166
     *
167
     * @param  \Symfony\Component\HttpFoundation\Response  $response
168
     * @return \Symfony\Component\HttpFoundation\Response
169
     */
170
    protected function encrypt(Response $response)
171
    {
172
        foreach ($response->headers->getCookies() as $cookie) {
173
            if ($this->isDisabled($cookie->getName())) {
174
                continue;
175
            }
176
 
177
            $response->headers->setCookie($this->duplicate(
178
                $cookie,
179
                $this->encrypter->encrypt(
180
                    CookieValuePrefix::create($cookie->getName(), $this->encrypter->getKey()).$cookie->getValue(),
181
                    static::serialized($cookie->getName())
182
                )
183
            ));
184
        }
185
 
186
        return $response;
187
    }
188
 
189
    /**
190
     * Duplicate a cookie with a new value.
191
     *
192
     * @param  \Symfony\Component\HttpFoundation\Cookie  $cookie
193
     * @param  mixed  $value
194
     * @return \Symfony\Component\HttpFoundation\Cookie
195
     */
196
    protected function duplicate(Cookie $cookie, $value)
197
    {
198
        return new Cookie(
199
            $cookie->getName(), $value, $cookie->getExpiresTime(),
200
            $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(),
201
            $cookie->isHttpOnly(), $cookie->isRaw(), $cookie->getSameSite()
202
        );
203
    }
204
 
205
    /**
206
     * Determine whether encryption has been disabled for the given cookie.
207
     *
208
     * @param  string  $name
209
     * @return bool
210
     */
211
    public function isDisabled($name)
212
    {
213
        return in_array($name, $this->except);
214
    }
215
 
216
    /**
217
     * Determine if the cookie contents should be serialized.
218
     *
219
     * @param  string  $name
220
     * @return bool
221
     */
222
    public static function serialized($name)
223
    {
224
        return static::$serialize;
225
    }
226
}