Skip to content
Permalink
Browse files

Update typehints.

ServerRequestInterface::getData() and ResponseInterface::withCookie() don't exist.
  • Loading branch information...
ADmad committed Aug 5, 2017
1 parent b096744 commit c273a023ee7aa8513c69726a22a3637ccd1acc1c
Showing with 148 additions and 18 deletions.
  1. +130 −0 src/Cache/SimpleCache.php
  2. +18 −18 src/Http/Middleware/CsrfProtectionMiddleware.php
@@ -0,0 +1,130 @@
<?php
namespace Cake\Cache;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException;
use Cake\Cache\CacheEngine;
class SimpleCache implements CacheInterface
{
/**
* @var \Cake\Cache\CacheEngine
*/
protected $engine;
public function __construct(CacheEngine $engine)
{
$this->engine = $engine;
}
public function get($key, $default = null)
{
$result = $engine->read($key);
return $result === false ? $default : $result;
}
public function set($key, $value, $ttl = null)
{
if ($ttl !== null) {
$duration = $this->engine->getConfig('duration');
$this->engine->setConfig('duration', $this->ttlToSeconds($ttl));
}
try {
$result = $this->engine->write($key, $value);
} finally {
if ($ttl !== null) {
$this->engine->setConfig('duration', $duration);
}
}
return $result;
}
public function delete($key)
{
return $this->engine->delete($key);
}
public function clear()
{
return $this->engine->clear(false);
}
public function getMultiple($keys, $default = null)
{
$keys = $this->getAsArray($keys);
$result = [];
foreach ($keys as $key) {
$value = $this->engine->get($key, $default);
}
return $result;
}
public function setMultiple($values, $ttl = null)
{
if ($ttl !== null) {
$duration = $this->engine->getConfig('duration');
$this->engine->setConfig('duration', $this->ttlToSeconds($ttl));
}
try {
$result = true;
foreach ($values as $key => $value) {
$result = $this->engine->set($key, $value) && $result;
}
} finally {
if ($ttl !== null) {
$this->engine->setConfig('duration', $duration);
}
}
return $result;
}
public function deleteMultiple($keys)
{
$keys = $this->getAsArray($keys);
$result = true;
foreach ($keys as $key) {
$result = $this->engine->delete($key) && $result;
}
return $result;
}
public function has($key)
{
return $this->get($key) === null ? false : true;
}
protected function getAsArray($keys)
{
if ($keys instanceof \Traversable) {
return iterator_to_array($keys);
}
if (is_array($keys)) {
return $keys;
}
throw new InvalidArgumentException('"$keys" must be an array or instanceof Traversable');
}
/**
* @param int|\DateInterval $ttl
* @return int seconds
*/
function ttlToSeconds($ttl)
{
if (is_int($ttl)) {
return $ttl;
}
return $ttl->days * 86400 + $ttl->h * 3600 + $ttl->i * 60 + $ttl->s;
}
}
@@ -14,12 +14,12 @@
*/
namespace Cake\Http\Middleware;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\I18n\Time;
use Cake\Network\Exception\InvalidCsrfTokenException;
use Cake\Utility\Hash;
use Cake\Utility\Security;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Provides CSRF protection & validation.
@@ -77,12 +77,12 @@ public function __construct(array $config = [])
/**
* Checks and sets the CSRF token depending on the HTTP verb.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @param \Psr\Http\Message\ResponseInterface $response The response.
* @param \Cake\Http\ServerRequest $request The request.
* @param \Cake\Http\Response $response The response.
* @param callable $next Callback to invoke the next middleware.
* @return \Psr\Http\Message\ResponseInterface A response
* @return \Cake\Http\Response A response
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
public function __invoke(ServerRequest $request, Response $response, $next)
{
$cookies = $request->getCookieParams();
$cookieData = Hash::get($cookies, $this->_config['cookieName']);
@@ -109,10 +109,10 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
/**
* Checks if the request is POST, PUT, DELETE or PATCH and validates the CSRF token
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request object.
* @return \Psr\Http\Message\ServerRequestInterface
* @param \Cake\Http\ServerRequest $request The request object.
* @return \Cake\Http\ServerRequest
*/
protected function _validateAndUnsetTokenField(ServerRequestInterface $request)
protected function _validateAndUnsetTokenField(ServerRequest $request)
{
if (in_array($request->getMethod(), ['PUT', 'POST', 'DELETE', 'PATCH']) || $request->getData()) {
$this->_validateToken($request);
@@ -140,10 +140,10 @@ protected function _createToken()
* Add a CSRF token to the request parameters.
*
* @param string $token The token to add.
* @param \Psr\Http\Message\ServerRequestInterface $request The request to augment
* @return \Psr\Http\Message\ServerRequestInterface Modified request
* @param \Cake\Http\ServerRequest $request The request to augment
* @return \Cake\Http\ServerRequest Modified request
*/
protected function _addTokenToRequest($token, ServerRequestInterface $request)
protected function _addTokenToRequest($token, ServerRequest $request)
{
$params = $request->getAttribute('params');
$params['_csrfToken'] = $token;
@@ -155,11 +155,11 @@ protected function _addTokenToRequest($token, ServerRequestInterface $request)
* Add a CSRF token to the response cookies.
*
* @param string $token The token to add.
* @param \Psr\Http\Message\ServerRequestInterface $request The request to validate against.
* @param \Psr\Http\Message\ResponseInterface $response The response.
* @return @param \Psr\Http\Message\ResponseInterface $response Modified response.
* @param \Cake\Http\ServerRequest $request The request to validate against.
* @param \Cake\Http\Response $response The response.
* @return @param \Cake\Http\Response $response Modified response.
*/
protected function _addTokenCookie($token, ServerRequestInterface $request, ResponseInterface $response)
protected function _addTokenCookie($token, ServerRequest $request, Response $response)
{
$expiry = new Time($this->_config['expiry']);
@@ -175,11 +175,11 @@ protected function _addTokenCookie($token, ServerRequestInterface $request, Resp
/**
* Validate the request data against the cookie token.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request to validate against.
* @param \Cake\Http\ServerRequest $request The request to validate against.
* @return void
* @throws \Cake\Network\Exception\InvalidCsrfTokenException When the CSRF token is invalid or missing.
*/
protected function _validateToken(ServerRequestInterface $request)
protected function _validateToken(ServerRequest $request)
{
$cookies = $request->getCookieParams();
$cookie = Hash::get($cookies, $this->_config['cookieName']);

0 comments on commit c273a02

Please sign in to comment.
You can’t perform that action at this time.