Skip to content

Commit

Permalink
Replace CookieComponent::type() with CookieComponent::encryption().
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Apr 1, 2014
1 parent de7cf89 commit a83527d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
40 changes: 18 additions & 22 deletions src/Controller/Component/CookieComponent.php
Expand Up @@ -54,7 +54,9 @@ class CookieComponent extends Component {
* a secure connection exists.
* - `key` - Encryption key.
* - `httpOnly` - Set to true to make HTTP only cookies. Cookies that are HTTP only
* are not accessible in JavaScript. Default false
* are not accessible in JavaScript. Default false.
* - `encryption` - Type of encryption to use. Defaults to 'aes'.
*
* @var array
*/
protected $_defaultConfig = [
Expand All @@ -64,7 +66,8 @@ class CookieComponent extends Component {
'domain' => '',
'secure' => false,
'key' => null,
'httpOnly' => false
'httpOnly' => false,
'encryption' => 'aes'
];

/**
Expand All @@ -77,15 +80,6 @@ class CookieComponent extends Component {
*/
protected $_values = array();

/**
* Type of encryption to use.
*
* Defaults to Security::encrypt(); or AES encryption.
*
* @var string
*/
protected $_type = 'aes';

/**
* Used to reset cookie time if $expire is passed to CookieComponent::write()
*
Expand Down Expand Up @@ -340,23 +334,26 @@ public function destroy() {
}

/**
* Will allow overriding default encryption method. Use this method
* in ex: AppController::beforeFilter() before you have read or
* written any cookies.
* Get / set encryption type. Use this method in ex: AppController::beforeFilter()
* before you have read or written any cookies.
*
* @param string $type Encryption method
* @return void
* @return string
* @throws \Cake\Error\Exception When an unknown type is used.
*/
public function type($type = 'aes') {
public function encryption($type = null) {
if ($type === null) {
return $this->_config['encryption'];
}

$availableTypes = [
'rijndael',
'aes'
];
if (!in_array($type, $availableTypes)) {
throw new Error\Exception('You must use rijndael, or aes for cookie encryption type');
}
$this->_type = $type;
$this->config('encryption', $type);
}

/**
Expand Down Expand Up @@ -447,10 +444,10 @@ protected function _encrypt($value) {
return $value;
}
$prefix = "Q2FrZQ==.";
if ($this->_type === 'rijndael') {
if ($this->_config['encryption'] === 'rijndael') {
$cipher = Security::rijndael($value, $this->_config['key'], 'encrypt');
}
if ($this->_type === 'aes') {
if ($this->_config['encryption'] === 'aes') {
$cipher = Security::encrypt($value, $this->_config['key']);
}
return $prefix . base64_encode($cipher);
Expand All @@ -464,7 +461,6 @@ protected function _encrypt($value) {
*/
protected function _decrypt($values) {
$decrypted = array();
$type = $this->_type;

foreach ((array)$values as $name => $value) {
if (is_array($value)) {
Expand All @@ -491,10 +487,10 @@ protected function _decode($value) {
return $this->_explode($value);
}
$value = base64_decode(substr($value, strlen($prefix)));
if ($this->_type === 'rijndael') {
if ($this->_config['encryption'] === 'rijndael') {
$plain = Security::rijndael($value, $this->_config['key'], 'decrypt');
}
if ($this->_type === 'aes') {
if ($this->_config['encryption'] === 'aes') {
$plain = Security::decrypt($value, $this->_config['key']);
}
return $this->_explode($plain);
Expand Down
Expand Up @@ -182,7 +182,7 @@ public function testWriteSimple() {
* @return void
*/
public function testWriteWithFalseyValue() {
$this->Cookie->type('aes');
$this->Cookie->encryption('aes');
$this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';

$this->Cookie->write('Testing');
Expand Down

0 comments on commit a83527d

Please sign in to comment.