PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php

https://gitlab.com/jjpa2018/dashboard
PHP | 194 lines | 93 code | 23 blank | 78 comment | 4 complexity | 017a73ba263671ad78c1eba28a0aaf0d MD5 | raw file
  1. <?php
  2. namespace Illuminate\Cookie\Middleware;
  3. use Closure;
  4. use Illuminate\Contracts\Encryption\DecryptException;
  5. use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
  6. use Illuminate\Cookie\CookieValuePrefix;
  7. use Symfony\Component\HttpFoundation\Cookie;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. class EncryptCookies
  11. {
  12. /**
  13. * The encrypter instance.
  14. *
  15. * @var \Illuminate\Contracts\Encryption\Encrypter
  16. */
  17. protected $encrypter;
  18. /**
  19. * The names of the cookies that should not be encrypted.
  20. *
  21. * @var array
  22. */
  23. protected $except = [];
  24. /**
  25. * Indicates if cookies should be serialized.
  26. *
  27. * @var bool
  28. */
  29. protected static $serialize = false;
  30. /**
  31. * Create a new CookieGuard instance.
  32. *
  33. * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
  34. * @return void
  35. */
  36. public function __construct(EncrypterContract $encrypter)
  37. {
  38. $this->encrypter = $encrypter;
  39. }
  40. /**
  41. * Disable encryption for the given cookie name(s).
  42. *
  43. * @param string|array $name
  44. * @return void
  45. */
  46. public function disableFor($name)
  47. {
  48. $this->except = array_merge($this->except, (array) $name);
  49. }
  50. /**
  51. * Handle an incoming request.
  52. *
  53. * @param \Illuminate\Http\Request $request
  54. * @param \Closure $next
  55. * @return \Symfony\Component\HttpFoundation\Response
  56. */
  57. public function handle($request, Closure $next)
  58. {
  59. return $this->encrypt($next($this->decrypt($request)));
  60. }
  61. /**
  62. * Decrypt the cookies on the request.
  63. *
  64. * @param \Symfony\Component\HttpFoundation\Request $request
  65. * @return \Symfony\Component\HttpFoundation\Request
  66. */
  67. protected function decrypt(Request $request)
  68. {
  69. foreach ($request->cookies as $key => $cookie) {
  70. if ($this->isDisabled($key) || is_array($cookie)) {
  71. continue;
  72. }
  73. try {
  74. $value = $this->decryptCookie($key, $cookie);
  75. $hasValidPrefix = strpos($value, CookieValuePrefix::create($key, $this->encrypter->getKey())) === 0;
  76. $request->cookies->set(
  77. $key, $hasValidPrefix ? CookieValuePrefix::remove($value) : null
  78. );
  79. } catch (DecryptException $e) {
  80. $request->cookies->set($key, null);
  81. }
  82. }
  83. return $request;
  84. }
  85. /**
  86. * Decrypt the given cookie and return the value.
  87. *
  88. * @param string $name
  89. * @param string|array $cookie
  90. * @return string|array
  91. */
  92. protected function decryptCookie($name, $cookie)
  93. {
  94. return is_array($cookie)
  95. ? $this->decryptArray($cookie)
  96. : $this->encrypter->decrypt($cookie, static::serialized($name));
  97. }
  98. /**
  99. * Decrypt an array based cookie.
  100. *
  101. * @param array $cookie
  102. * @return array
  103. */
  104. protected function decryptArray(array $cookie)
  105. {
  106. $decrypted = [];
  107. foreach ($cookie as $key => $value) {
  108. if (is_string($value)) {
  109. $decrypted[$key] = $this->encrypter->decrypt($value, static::serialized($key));
  110. }
  111. }
  112. return $decrypted;
  113. }
  114. /**
  115. * Encrypt the cookies on an outgoing response.
  116. *
  117. * @param \Symfony\Component\HttpFoundation\Response $response
  118. * @return \Symfony\Component\HttpFoundation\Response
  119. */
  120. protected function encrypt(Response $response)
  121. {
  122. foreach ($response->headers->getCookies() as $cookie) {
  123. if ($this->isDisabled($cookie->getName())) {
  124. continue;
  125. }
  126. $response->headers->setCookie($this->duplicate(
  127. $cookie,
  128. $this->encrypter->encrypt(
  129. CookieValuePrefix::create($cookie->getName(), $this->encrypter->getKey()).$cookie->getValue(),
  130. static::serialized($cookie->getName())
  131. )
  132. ));
  133. }
  134. return $response;
  135. }
  136. /**
  137. * Duplicate a cookie with a new value.
  138. *
  139. * @param \Symfony\Component\HttpFoundation\Cookie $cookie
  140. * @param mixed $value
  141. * @return \Symfony\Component\HttpFoundation\Cookie
  142. */
  143. protected function duplicate(Cookie $cookie, $value)
  144. {
  145. return new Cookie(
  146. $cookie->getName(), $value, $cookie->getExpiresTime(),
  147. $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(),
  148. $cookie->isHttpOnly(), $cookie->isRaw(), $cookie->getSameSite()
  149. );
  150. }
  151. /**
  152. * Determine whether encryption has been disabled for the given cookie.
  153. *
  154. * @param string $name
  155. * @return bool
  156. */
  157. public function isDisabled($name)
  158. {
  159. return in_array($name, $this->except);
  160. }
  161. /**
  162. * Determine if the cookie contents should be serialized.
  163. *
  164. * @param string $name
  165. * @return bool
  166. */
  167. public static function serialized($name)
  168. {
  169. return static::$serialize;
  170. }
  171. }