Skip to content

Commit a83527d

Browse files
committed
Replace CookieComponent::type() with CookieComponent::encryption().
1 parent de7cf89 commit a83527d

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

src/Controller/Component/CookieComponent.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class CookieComponent extends Component {
5454
* a secure connection exists.
5555
* - `key` - Encryption key.
5656
* - `httpOnly` - Set to true to make HTTP only cookies. Cookies that are HTTP only
57-
* are not accessible in JavaScript. Default false
57+
* are not accessible in JavaScript. Default false.
58+
* - `encryption` - Type of encryption to use. Defaults to 'aes'.
59+
*
5860
* @var array
5961
*/
6062
protected $_defaultConfig = [
@@ -64,7 +66,8 @@ class CookieComponent extends Component {
6466
'domain' => '',
6567
'secure' => false,
6668
'key' => null,
67-
'httpOnly' => false
69+
'httpOnly' => false,
70+
'encryption' => 'aes'
6871
];
6972

7073
/**
@@ -77,15 +80,6 @@ class CookieComponent extends Component {
7780
*/
7881
protected $_values = array();
7982

80-
/**
81-
* Type of encryption to use.
82-
*
83-
* Defaults to Security::encrypt(); or AES encryption.
84-
*
85-
* @var string
86-
*/
87-
protected $_type = 'aes';
88-
8983
/**
9084
* Used to reset cookie time if $expire is passed to CookieComponent::write()
9185
*
@@ -340,23 +334,26 @@ public function destroy() {
340334
}
341335

342336
/**
343-
* Will allow overriding default encryption method. Use this method
344-
* in ex: AppController::beforeFilter() before you have read or
345-
* written any cookies.
337+
* Get / set encryption type. Use this method in ex: AppController::beforeFilter()
338+
* before you have read or written any cookies.
346339
*
347340
* @param string $type Encryption method
348-
* @return void
341+
* @return string
349342
* @throws \Cake\Error\Exception When an unknown type is used.
350343
*/
351-
public function type($type = 'aes') {
344+
public function encryption($type = null) {
345+
if ($type === null) {
346+
return $this->_config['encryption'];
347+
}
348+
352349
$availableTypes = [
353350
'rijndael',
354351
'aes'
355352
];
356353
if (!in_array($type, $availableTypes)) {
357354
throw new Error\Exception('You must use rijndael, or aes for cookie encryption type');
358355
}
359-
$this->_type = $type;
356+
$this->config('encryption', $type);
360357
}
361358

362359
/**
@@ -447,10 +444,10 @@ protected function _encrypt($value) {
447444
return $value;
448445
}
449446
$prefix = "Q2FrZQ==.";
450-
if ($this->_type === 'rijndael') {
447+
if ($this->_config['encryption'] === 'rijndael') {
451448
$cipher = Security::rijndael($value, $this->_config['key'], 'encrypt');
452449
}
453-
if ($this->_type === 'aes') {
450+
if ($this->_config['encryption'] === 'aes') {
454451
$cipher = Security::encrypt($value, $this->_config['key']);
455452
}
456453
return $prefix . base64_encode($cipher);
@@ -464,7 +461,6 @@ protected function _encrypt($value) {
464461
*/
465462
protected function _decrypt($values) {
466463
$decrypted = array();
467-
$type = $this->_type;
468464

469465
foreach ((array)$values as $name => $value) {
470466
if (is_array($value)) {
@@ -491,10 +487,10 @@ protected function _decode($value) {
491487
return $this->_explode($value);
492488
}
493489
$value = base64_decode(substr($value, strlen($prefix)));
494-
if ($this->_type === 'rijndael') {
490+
if ($this->_config['encryption'] === 'rijndael') {
495491
$plain = Security::rijndael($value, $this->_config['key'], 'decrypt');
496492
}
497-
if ($this->_type === 'aes') {
493+
if ($this->_config['encryption'] === 'aes') {
498494
$plain = Security::decrypt($value, $this->_config['key']);
499495
}
500496
return $this->_explode($plain);

tests/TestCase/Controller/Component/CookieComponentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function testWriteSimple() {
182182
* @return void
183183
*/
184184
public function testWriteWithFalseyValue() {
185-
$this->Cookie->type('aes');
185+
$this->Cookie->encryption('aes');
186186
$this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
187187

188188
$this->Cookie->write('Testing');

0 commit comments

Comments
 (0)