Skip to content

Commit

Permalink
Updated deprecated method usage in Components
Browse files Browse the repository at this point in the history
  • Loading branch information
David Yell committed Jun 7, 2018
1 parent 93c9d94 commit 3cbefae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
40 changes: 24 additions & 16 deletions src/Controller/Component/CookieComponent.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Controller\Component;

use Cake\Controller\Component;
use Cake\Http\Cookie\Cookie;
use Cake\Http\ServerRequestFactory;
use Cake\I18n\Time;
use Cake\Utility\CookieCryptTrait;
Expand Down Expand Up @@ -310,14 +311,18 @@ protected function _write($name, $value)
$expires = new Time($config['expires']);

$controller = $this->getController();
$controller->response = $controller->response->withCookie($name, [
'value' => $this->_encrypt($value, $config['encryption'], $config['key']),
'expire' => $expires->format('U'),
'path' => $config['path'],
'domain' => $config['domain'],
'secure' => (bool)$config['secure'],
'httpOnly' => (bool)$config['httpOnly']
]);

$cookie = new Cookie(
$name,
$this->_encrypt($value, $config['encryption'], $config['key']),
$expires,
$config['path'],
$config['domain'],
(bool)$config['secure'],
(bool)$config['httpOnly']
);

$controller->response = $controller->response->withCookie($cookie);
}

/**
Expand All @@ -335,14 +340,17 @@ protected function _delete($name)
$expires = new Time('now');
$controller = $this->getController();

$controller->response = $controller->response->withCookie($name, [
'value' => '',
'expire' => $expires->format('U') - 42000,
'path' => $config['path'],
'domain' => $config['domain'],
'secure' => $config['secure'],
'httpOnly' => $config['httpOnly']
]);
$cookie = new Cookie(
$name,
'',
$expires,
$config['path'],
$config['domain'],
(bool)$config['secure'],
(bool)$config['httpOnly']
);

$controller->response = $controller->response->withExpiredCookie($cookie);
}

/**
Expand Down
20 changes: 13 additions & 7 deletions src/Controller/Component/CsrfComponent.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Controller\Component;
use Cake\Event\Event;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Exception\InvalidCsrfTokenException;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
Expand Down Expand Up @@ -134,13 +135,18 @@ protected function _setCookie(ServerRequest $request, Response $response)
$value = hash('sha512', Security::randomBytes(16), false);

$request = $request->withParam('_csrfToken', $value);
$response = $response->withCookie($this->_config['cookieName'], [
'value' => $value,
'expire' => $expiry->format('U'),
'path' => $request->getAttribute('webroot'),
'secure' => $this->_config['secure'],
'httpOnly' => $this->_config['httpOnly'],
]);

$cookie = new Cookie(
$this->_config['cookieName'],
$value,
$expiry,
$request->getAttribute('webroot'),
'',
(bool)$this->_config['secure'],
(bool)$this->_config['httpOnly']
);

$response = $response->withCookie($cookie);

return [$request, $response];
}
Expand Down

0 comments on commit 3cbefae

Please sign in to comment.