PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cakephp/utility/CookieCryptTrait.php

http://github.com/josegonzalez/git-php
PHP | 182 lines | 91 code | 16 blank | 75 comment | 14 complexity | cfec90daad7b0e3a5bc743e6706d2cc7 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.1.6
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Utility;
  16. use RuntimeException;
  17. /**
  18. * Cookie Crypt Trait.
  19. *
  20. * Provides the encrypt/decrypt logic for the CookieComponent.
  21. *
  22. * @link https://book.cakephp.org/3.0/en/controllers/components/cookie.html
  23. */
  24. trait CookieCryptTrait
  25. {
  26. /**
  27. * Valid cipher names for encrypted cookies.
  28. *
  29. * @var array
  30. */
  31. protected $_validCiphers = ['aes', 'rijndael'];
  32. /**
  33. * Returns the encryption key to be used.
  34. *
  35. * @return string
  36. */
  37. abstract protected function _getCookieEncryptionKey();
  38. /**
  39. * Encrypts $value using public $type method in Security class
  40. *
  41. * @param string $value Value to encrypt
  42. * @param string|bool $encrypt Encryption mode to use. False
  43. * disabled encryption.
  44. * @param string|null $key Used as the security salt if specified.
  45. * @return string Encoded values
  46. */
  47. protected function _encrypt($value, $encrypt, $key = null)
  48. {
  49. if (is_array($value)) {
  50. $value = $this->_implode($value);
  51. }
  52. if ($encrypt === false) {
  53. return $value;
  54. }
  55. $this->_checkCipher($encrypt);
  56. $prefix = 'Q2FrZQ==.';
  57. $cipher = null;
  58. if ($key === null) {
  59. $key = $this->_getCookieEncryptionKey();
  60. }
  61. if ($encrypt === 'rijndael') {
  62. $cipher = Security::rijndael($value, $key, 'encrypt');
  63. }
  64. if ($encrypt === 'aes') {
  65. $cipher = Security::encrypt($value, $key);
  66. }
  67. return $prefix . base64_encode($cipher);
  68. }
  69. /**
  70. * Helper method for validating encryption cipher names.
  71. *
  72. * @param string $encrypt The cipher name.
  73. * @return void
  74. * @throws \RuntimeException When an invalid cipher is provided.
  75. */
  76. protected function _checkCipher($encrypt)
  77. {
  78. if (!in_array($encrypt, $this->_validCiphers)) {
  79. $msg = sprintf(
  80. 'Invalid encryption cipher. Must be one of %s.',
  81. implode(', ', $this->_validCiphers)
  82. );
  83. throw new RuntimeException($msg);
  84. }
  85. }
  86. /**
  87. * Decrypts $value using public $type method in Security class
  88. *
  89. * @param array $values Values to decrypt
  90. * @param string|bool $mode Encryption mode
  91. * @param string|null $key Used as the security salt if specified.
  92. * @return string|array Decrypted values
  93. */
  94. protected function _decrypt($values, $mode, $key = null)
  95. {
  96. if (is_string($values)) {
  97. return $this->_decode($values, $mode, $key);
  98. }
  99. $decrypted = [];
  100. foreach ($values as $name => $value) {
  101. $decrypted[$name] = $this->_decode($value, $mode, $key);
  102. }
  103. return $decrypted;
  104. }
  105. /**
  106. * Decodes and decrypts a single value.
  107. *
  108. * @param string $value The value to decode & decrypt.
  109. * @param string|false $encrypt The encryption cipher to use.
  110. * @param string|null $key Used as the security salt if specified.
  111. * @return string|array Decoded values.
  112. */
  113. protected function _decode($value, $encrypt, $key)
  114. {
  115. if (!$encrypt) {
  116. return $this->_explode($value);
  117. }
  118. $this->_checkCipher($encrypt);
  119. $prefix = 'Q2FrZQ==.';
  120. $value = base64_decode(substr($value, strlen($prefix)));
  121. if ($key === null) {
  122. $key = $this->_getCookieEncryptionKey();
  123. }
  124. if ($encrypt === 'rijndael') {
  125. $value = Security::rijndael($value, $key, 'decrypt');
  126. }
  127. if ($encrypt === 'aes') {
  128. $value = Security::decrypt($value, $key);
  129. }
  130. return $this->_explode($value);
  131. }
  132. /**
  133. * Implode method to keep keys are multidimensional arrays
  134. *
  135. * @param array $array Map of key and values
  136. * @return string A json encoded string.
  137. */
  138. protected function _implode(array $array)
  139. {
  140. return json_encode($array);
  141. }
  142. /**
  143. * Explode method to return array from string set in CookieComponent::_implode()
  144. * Maintains reading backwards compatibility with 1.x CookieComponent::_implode().
  145. *
  146. * @param string $string A string containing JSON encoded data, or a bare string.
  147. * @return string|array Map of key and values
  148. */
  149. protected function _explode($string)
  150. {
  151. $first = substr($string, 0, 1);
  152. if ($first === '{' || $first === '[') {
  153. $ret = json_decode($string, true);
  154. return ($ret !== null) ? $ret : $string;
  155. }
  156. $array = [];
  157. foreach (explode(',', $string) as $pair) {
  158. $key = explode('|', $pair);
  159. if (!isset($key[1])) {
  160. return $key[0];
  161. }
  162. $array[$key[0]] = $key[1];
  163. }
  164. return $array;
  165. }
  166. }