Skip to content

Instantly share code, notes, and snippets.

@SamJUK
Last active June 4, 2019 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamJUK/50e3404e9008b0a42dcfae007613f046 to your computer and use it in GitHub Desktop.
Save SamJUK/50e3404e9008b0a42dcfae007613f046 to your computer and use it in GitHub Desktop.
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Stdlib\Cookie\CookieMetadata;
/**
* Class CookieMetadata
* @api
*/
class CookieMetadata
{
/**#@+
* Constant for metadata value key.
*/
const KEY_DOMAIN = 'domain';
const KEY_PATH = 'path';
const KEY_SECURE = 'secure';
const KEY_HTTP_ONLY = 'http_only';
const KEY_DURATION = 'duration';
/**#@-*/
/**#@-*/
private $metadata;
// MOD START: SET COOKIE SECURE VALUE TO MATCH REQUEST TYPE
private $_request;
// MOD END: SET COOKIE SECURE VALUE TO MATCH REQUEST TYPE
/**
* @param array $metadata
*/
// MOD: Additiona Construct param
public function __construct(RequestInterface $request, $metadata = [])
{
if (!is_array($metadata)) {
$metadata = [];
}
$this->metadata = $metadata;
// MOD START: SET COOKIE SECURE VALUE TO MATCH REQUEST TYPE
$this->_request = $request;
// MOD END: SET COOKIE SECURE VALUE TO MATCH REQUEST TYPE
}
/**
* Returns an array representation of this metadata.
*
* If a value has not yet been set then the key will not show up in the array.
*
* @return array
*/
public function __toArray()
{
return $this->metadata;
}
/**
* Set the domain for the cookie
*
* @param string $domain
* @return $this
*/
public function setDomain($domain)
{
return $this->set(self::KEY_DOMAIN, $domain);
}
/**
* Get the domain for the cookie
*
* @return string|null
*/
public function getDomain()
{
return $this->get(self::KEY_DOMAIN);
}
/**
* Set path of the cookie
*
* @param string $path
* @return $this
*/
public function setPath($path)
{
return $this->set(self::KEY_PATH, $path);
}
/**
* Get the path of the cookie
*
* @return string|null
*/
public function getPath()
{
return $this->get(self::KEY_PATH);
}
/**
* Get a value from the metadata storage.
*
* @param string $name
* @return int|float|string|bool|null
*/
protected function get($name)
{
if (isset($this->metadata[$name])) {
return $this->metadata[$name];
}
return null;
}
/**
* Set a value to the metadata storage.
*
* @param string $name
* @param int|float|string|bool|null $value
* @return $this
*/
protected function set($name, $value)
{
$this->metadata[$name] = $value;
return $this;
}
/**
* Get HTTP Only flag
*
* @return bool|null
*/
public function getHttpOnly()
{
return $this->get(self::KEY_HTTP_ONLY);
}
/**
* Get whether the cookie is only available under HTTPS
*
* @return bool|null
*/
public function getSecure()
{
// MOD START: SET COOKIE SECURE VALUE TO MATCH REQUEST TYPE
return $this->_request->isSecure();
// MOD END: SET COOKIE SECURE VALUE TO MATCH REQUEST TYPE
return $this->get(self::KEY_SECURE);
}
}
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Stdlib\Cookie;
use Magento\Framework\App\RequestInterface;
/**
* Class SensitiveCookieMetadata
*
* The class has only methods extended from CookieMetadata
* as path and domain are only data to be exposed by SensitiveCookieMetadata
*
* @api
*/
class SensitiveCookieMetadata extends CookieMetadata
{
/**
* @var RequestInterface
*/
protected $request;
/**
* @param RequestInterface $request
* @param array $metadata
*/
public function __construct(RequestInterface $request, $metadata = [])
{
if (!isset($metadata[self::KEY_HTTP_ONLY])) {
$metadata[self::KEY_HTTP_ONLY] = true;
}
$this->request = $request;
parent::__construct($metadata);
}
/**
* {@inheritdoc}
*/
public function getSecure()
{
// MOD START: SET COOKIE SECURE VALUE TO MATCH REQUEST TYPE
return $this->request->isSecure();
// MOD END: SET COOKIE SECURE VALUE TO MATCH REQUEST TYPE
$this->updateSecureValue();
return $this->get(self::KEY_SECURE);
}
/**
* {@inheritdoc}
*/
public function __toArray()
{
$this->updateSecureValue();
return parent::__toArray();
}
/**
* Update secure value, set it to request setting if it has no explicit value assigned.
*
* @return void
*/
private function updateSecureValue()
{
if (null === $this->get(self::KEY_SECURE)) {
$this->set(self::KEY_SECURE, $this->request->isSecure());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment