예제 #1
2
 /**
  * Set the cookie in the response.
  *
  * Also sets the request->params['_csrfToken'] so the newly minted
  * token is available in the request data.
  *
  * @param \Cake\Network\Request $request The request object.
  * @param \Cake\Network\Response $response The response object.
  * @return void
  */
 protected function _setCookie(Request $request, Response $response)
 {
     $expiry = new Time($this->_config['expiry']);
     $value = hash('sha512', Security::randomBytes(16), false);
     $request->params['_csrfToken'] = $value;
     $response->cookie(['name' => $this->_config['cookieName'], 'value' => $value, 'expire' => $expiry->format('U'), 'path' => $request->webroot, 'secure' => $this->_config['secure'], 'httpOnly' => $this->_config['httpOnly']]);
 }
예제 #2
1
 /**
  * Asserts cookie values
  *
  * @param string $expected The expected contents.
  * @param string $name The cookie name.
  * @param string $message The failure message that will be appended to the generated message.
  * @return void
  */
 public function assertCookie($expected, $name, $message = '')
 {
     if (empty($this->_response)) {
         $this->fail('Not response set, cannot assert cookies.');
     }
     $result = $this->_response->cookie($name);
     $this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
 }
예제 #3
1
 /**
  * Filters the cake response to the BrowserKit one.
  *
  * @param \Cake\Network\Response $response Cake response.
  * @return \Symfony\Component\BrowserKit\Response BrowserKit response.
  */
 protected function filterResponse($response)
 {
     $this->cake['response'] = $response;
     foreach ($response->cookie() as $cookie) {
         $this->getCookieJar()->set(new Cookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httpOnly']));
     }
     $response->sendHeaders();
     return new BrowserKitResponse($response->body(), $response->statusCode(), $response->header());
 }
예제 #4
0
 /**
  * Asserts cookie values which are encrypted by the
  * CookieComponent.
  *
  * The difference from assertCookie() is this decrypts the cookie
  * value like the CookieComponent for this assertion.
  *
  * @param string $expected The expected contents.
  * @param string $name The cookie name.
  * @param string|bool $encrypt Encryption mode to use.
  * @param string|null $key Encryption key used. Defaults
  *   to Security.salt.
  * @param string $message The failure message that will be appended to the generated message.
  * @return void
  * @see \Cake\Utility\CookieCryptTrait::_encrypt()
  */
 public function assertCookieEncrypted($expected, $name, $encrypt = 'aes', $key = null, $message = '')
 {
     if (empty($this->_response)) {
         $this->fail('No response set, cannot assert cookies.');
     }
     $result = $this->_response->cookie($name);
     $this->_cookieEncriptionKey = $key;
     $result['value'] = $this->_decrypt($result['value'], $encrypt);
     $this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
 }
예제 #5
0
 /**
  * Convert a PSR7 Response into a CakePHP one.
  *
  * @param \Psr\Http\Message\ResponseInterface $response The response to convert.
  * @return \Cake\Network\Response The equivalent CakePHP response
  */
 public static function toCake(PsrResponse $response)
 {
     $body = static::getBody($response);
     $data = ['status' => $response->getStatusCode(), 'body' => $body['body']];
     $cake = new CakeResponse($data);
     if ($body['file']) {
         $cake->file($body['file']);
     }
     $cookies = static::parseCookies($response->getHeader('Set-Cookie'));
     foreach ($cookies as $cookie) {
         $cake->cookie($cookie);
     }
     $headers = static::collapseHeaders($response);
     $cake->header($headers);
     if (!empty($headers['Content-Type'])) {
         $cake->type($headers['Content-Type']);
     }
     return $cake;
 }
예제 #6
0
 /**
  * Test cookie setting
  *
  * @return void
  */
 public function testCookieSettings()
 {
     $response = new Response();
     $cookie = ['name' => 'CakeTestCookie[Testing]'];
     $response->cookie($cookie);
     $expected = ['name' => 'CakeTestCookie[Testing]', 'value' => '', 'expire' => 0, 'path' => '/', 'domain' => '', 'secure' => false, 'httpOnly' => false];
     $result = $response->cookie('CakeTestCookie[Testing]');
     $this->assertEquals($expected, $result);
     $cookie = ['name' => 'CakeTestCookie[Testing2]', 'value' => '[a,b,c]', 'expire' => 1000, 'path' => '/test', 'secure' => true];
     $response->cookie($cookie);
     $expected = ['CakeTestCookie[Testing]' => ['name' => 'CakeTestCookie[Testing]', 'value' => '', 'expire' => 0, 'path' => '/', 'domain' => '', 'secure' => false, 'httpOnly' => false], 'CakeTestCookie[Testing2]' => ['name' => 'CakeTestCookie[Testing2]', 'value' => '[a,b,c]', 'expire' => 1000, 'path' => '/test', 'domain' => '', 'secure' => true, 'httpOnly' => false]];
     $result = $response->cookie();
     $this->assertEquals($expected, $result);
     $cookie = $expected['CakeTestCookie[Testing]'];
     $cookie['value'] = 'test';
     $response->cookie($cookie);
     $expected = ['CakeTestCookie[Testing]' => ['name' => 'CakeTestCookie[Testing]', 'value' => 'test', 'expire' => 0, 'path' => '/', 'domain' => '', 'secure' => false, 'httpOnly' => false], 'CakeTestCookie[Testing2]' => ['name' => 'CakeTestCookie[Testing2]', 'value' => '[a,b,c]', 'expire' => 1000, 'path' => '/test', 'domain' => '', 'secure' => true, 'httpOnly' => false]];
     $result = $response->cookie();
     $this->assertEquals($expected, $result);
 }
예제 #7
0
 /**
  * Set the cookie in the response.
  *
  * Also sets the request->params['_csrfToken'] so the newly minted
  * token is available in the request data.
  *
  * @param \Cake\Network\Request $request The request object.
  * @param \Cake\Network\Response $response The response object.
  * @return void
  */
 protected function _setCookie(Request $request, Response $response)
 {
     $value = Security::hash(Text::uuid(), 'sha1', true);
     $request->params['_csrfToken'] = $value;
     $response->cookie(['name' => $this->_config['cookieName'], 'value' => $value, 'expiry' => $this->_config['expiry'], 'path' => $request->webroot, 'secure' => $this->_config['secure']]);
 }
예제 #8
0
 /**
  * Sets a cookie expire time to remove cookie value
  *
  * @param string $name Name of cookie
  * @return void
  */
 protected function _delete($name)
 {
     $config = $this->config();
     $this->_response->cookie(array('name' => $config['name'] . $name, 'value' => '', 'expire' => time() - 42000, 'path' => $config['path'], 'domain' => $config['domain'], 'secure' => $config['secure'], 'httpOnly' => $config['httpOnly']));
 }
예제 #9
-1
 /**
  * Sets a cookie expire time to remove cookie value.
  *
  * This is only done once all values in a cookie key have been
  * removed with delete.
  *
  * @param string $name Name of cookie
  * @return void
  */
 protected function _delete($name)
 {
     $config = $this->configKey($name);
     $expires = new Time('now');
     $this->_response->cookie(array('name' => $name, 'value' => '', 'expire' => $expires->format('U') - 42000, 'path' => $config['path'], 'domain' => $config['domain'], 'secure' => $config['secure'], 'httpOnly' => $config['httpOnly']));
 }