Skip to content

Commit

Permalink
Merge pull request #154 from zealsolutions/issue153
Browse files Browse the repository at this point in the history
Issue #153: Add support for SameSite cookie flag
  • Loading branch information
paragonie-security authored Dec 3, 2020
2 parents 106d917 + 1e6a93e commit 4c3beb1
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ protected static function getConfig(string $stored): SymmetricConfig
* @param string $domain (defaults to NULL)
* @param bool $secure (defaults to TRUE)
* @param bool $httpOnly (defaults to TRUE)
* @param string $samesite (defaults to ''; PHP >= 7.3.0)
* @return bool
*
* @throws InvalidDigestLength
Expand All @@ -156,16 +157,34 @@ public function store(
string $path = '/',
string $domain = '',
bool $secure = true,
bool $httpOnly = true
bool $httpOnly = true,
string $sameSite = ''
): bool {
$val = Crypto::encrypt(
new HiddenString(
(string) \json_encode($value)
),
$this->key
);
if (\version_compare(PHP_VERSION, '7.3.0') >= 0) {
$options = [
'expires' => (int) $expire,
'path' => (string) $path,
'domain' => (string) $domain,
'secure' => (bool) $secure,
'httponly' => (bool) $httpOnly,
];
if ($sameSite !== '') {
$options['samesite'] = (string) $sameSite;
}
return \setcookie(
$name,
$val,
$options);
}
return \setcookie(
$name,
Crypto::encrypt(
new HiddenString(
(string) \json_encode($value)
),
$this->key
),
$val,
(int) $expire,
(string) $path,
(string) $domain,
Expand Down

0 comments on commit 4c3beb1

Please sign in to comment.