Skip to content

Commit

Permalink
update to Magento2.4.1 compatible implementation (beta version).
Browse files Browse the repository at this point in the history
  • Loading branch information
HirokazuNishi committed Oct 27, 2020
1 parent b4c3a5d commit d3a948a
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 486 deletions.
17 changes: 17 additions & 0 deletions Model/Config/Source/SameSite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Veriteworks\CookieFix\Model\Config\Source;

class SameSite
{
/**
* @return array
*/
public function toOptionArray()
{
return [
['value' => 'Lax','label' => __('Lax')],
['value' => 'Strict','label' => __('Strict')],
['value' => 'None','label' => __('None')]
];
}
}
64 changes: 0 additions & 64 deletions Plugin/Session/AddSameSite.php

This file was deleted.

83 changes: 83 additions & 0 deletions Plugin/SwitchSameSite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
namespace Veriteworks\CookieFix\Plugin;


use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\Header;
use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
use Magento\Framework\Stdlib\Cookie\PublicCookieMetadata;
use Magento\Store\Model\ScopeInterface;
use Veriteworks\CookieFix\Validator\SameSite;

class SwitchSameSite
{
const CONFIG_PATH = 'web/cookie/samesite';
const CONFIG_AFFECTED_KEYS = 'web/cookie/affected_keys';
/**
* @var SameSite
*/
private $validator;
/**
* @var Header
*/
private $header;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

private $affectedKeys = [];

/**
* SwitchSameSite constructor.
* @param Header $header
* @param ScopeConfigInterface $scopeConfig
* @param SameSite $validator
*/
public function __construct(
Header $header,
ScopeConfigInterface $scopeConfig,
SameSite $validator
) {
$this->validator = $validator;
$this->header = $header;
$this->scopeConfig = $scopeConfig;
}

/**
* @param PhpCookieManager $subject
* @param string $name
* @param string $value
* @param PublicCookieMetadata|null $metadata
* @return array
*/
public function beforeSetPublicCookie(
PhpCookieManager $subject,
$name,
$value,
PublicCookieMetadata $metadata = null
) {
if ($this->isAffectedKeys($name)) {
$agent = $this->header->getHttpUserAgent();
$sameSite = $this->validator->shouldSendSameSiteNone($agent);
if ($sameSite === false) {
$metadata->setSameSite('None');
} else {
$config = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE);
$metadata->setSameSite($config);
}
}

return [$name, $value, $metadata];
}

private function isAffectedKeys($name)
{
if (!count($this->affectedKeys)) {
$affectedKeys = $this->scopeConfig->getValue(self::CONFIG_AFFECTED_KEYS, ScopeInterface::SCOPE_STORE);
$this->affectedKeys = explode(',', strtolower($affectedKeys));
}

return in_array(strtolower($name), $this->affectedKeys);
}
}
21 changes: 0 additions & 21 deletions Plugin/View/Element/Js/AdjustPath.php

This file was deleted.

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ This extension set SameSite=None and make customers place order via 3DS payment
+ composer require veriteworks/cookiefix
+ bin/magento module:enable Veriteworks_CookieFix
+ bin/magento setup:upgrade

# Configuration

After 3.0.0-beta1, you can change SameSite cookie configuration from admin panel. Go to "Stores > configuration > web" and open cookie section. You can see "SameSite" field.
By default, this extension sets SameSite configuration value to limited cookies. If you hope to update the list, please update web/cookie/affected_keys configuration value.

# Support

Expand Down
24 changes: 0 additions & 24 deletions Rewrite/Stdlib/Cookie/PublicCookieMetadata.php

This file was deleted.

Loading

0 comments on commit d3a948a

Please sign in to comment.