Skip to content

Commit

Permalink
Cleanup and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Dec 2, 2019
1 parent fa57e54 commit 0e10e61
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
5 changes: 1 addition & 4 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
<ruleset name="Auth0-PHP" namespace="Auth0PHP\CS\Standard">
<description>A custom coding standard for the Auth0 PHP SDK</description>

<file>.</file>

<exclude-pattern>/examples/*</exclude-pattern>
<exclude-pattern>/vendor/*</exclude-pattern>
<file>./src</file>

<!-- Only check PHP files. -->
<arg name="extensions" value="php"/>
Expand Down
1 change: 1 addition & 0 deletions src/Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ public function __construct(array $config)
$transientStore = $config['transient_store'] ?? null;
if (! $transientStore instanceof StoreInterface) {
$transientStore = new CookieStore([
// Use configuration option or class default.
'legacy_samesite_none' => $config['legacy_samesite_none_cookie'] ?? null,
'samesite' => 'form_post' === $this->responseMode ? 'None' : 'Lax',
]);
Expand Down
11 changes: 7 additions & 4 deletions src/Store/CookieStore.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Auth0\SDK\Store;

Expand Down Expand Up @@ -93,11 +94,13 @@ public function set($key, $value)
$_COOKIE[$key_name] = $value;

if ($this->sameSite) {
// Core setcookie() does not handle SameSite before PHP 7.3.
$this->setCookieHeader($key_name, $value, $this->getExpTimecode());
} else {
$this->setCookie($key_name, $value, $this->getExpTimecode());
}

// If we're using SameSite=None, set a fallback cookie with no SameSite attribute.
if ($this->legacySameSiteNone && 'None' === $this->sameSite) {
$_COOKIE['_'.$key_name] = $value;
$this->setCookie('_'.$key_name, $value, $this->getExpTimecode());
Expand All @@ -118,7 +121,7 @@ public function get($key, $default = null)
$key_name = $this->getCookieName($key);
$value = $default;

// If we're handling legacy browsers, check for fallback value first.
// If handling legacy browsers, check for fallback value.
if ($this->legacySameSiteNone) {
$value = $_COOKIE['_'.$key_name] ?? $value;
}
Expand All @@ -139,6 +142,7 @@ public function delete($key)
unset($_COOKIE[$key_name]);
$this->setCookie( $key_name, '', 0 );

// If we set a legacy fallback value, remove that as well.
if ($this->legacySameSiteNone) {
unset($_COOKIE['_'.$key_name]);
$this->setCookie( '_'.$key_name, '', 0 );
Expand All @@ -147,15 +151,14 @@ public function delete($key)

/**
* Build the header to use when setting SameSite cookies.
* Core setcookie() function does not handle SameSite before PHP 7.3.
*
* @param string $name Cookie name.
* @param string $value Cookie value.
* @param integer $expire Cookie expiration timecode.
*
* @return string
*/
public function getSameSiteCookieHeader(string $name, string $value, int $expire) : string
protected function getSameSiteCookieHeader(string $name, string $value, int $expire) : string
{
$date = new \Datetime();
$date->setTimestamp($expire)
Expand All @@ -176,7 +179,7 @@ public function getSameSiteCookieHeader(string $name, string $value, int $expire
*
* @return integer
*/
private function getExpTimecode() : int
protected function getExpTimecode() : int
{
return ($this->now ?? time()) + $this->expiration;
}
Expand Down
16 changes: 10 additions & 6 deletions tests/Store/CookieStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,26 @@ public function testDeleteNoLegacy()

public function testGetSetCookieHeaderStrict()
{
$store = new CookieStore(['now' => 303943620, 'expiration' => 0, 'samesite' => 'lax']);
$store = new CookieStore(['now' => 303943620, 'expiration' => 0, 'samesite' => 'lax']);
$method = new \ReflectionMethod(CookieStore::class, 'getSameSiteCookieHeader');
$method->setAccessible(true);
$header = $method->invokeArgs($store, ['__test_name_1__', '__test_value_1__', 303943620]);

$header = $store->getSameSiteCookieHeader('__test_name_1__', '__test_value_1__', 303943620);
$this->assertEquals(
'Set-Cookie: __test_name_1__=__test_value_1__; path=/; expires=Sunday, 19-Aug-1979 20:47:00 GMT; HttpOnly; SameSite=Lax',
'Set-Cookie: __test_name_1__=__test_value_1__; path=/; '.'expires=Sunday, 19-Aug-1979 20:47:00 GMT; HttpOnly; SameSite=Lax',
$header
);
}

public function testGetSetCookieHeaderNone()
{
$store = new CookieStore(['now' => 303943620, 'expiration' => 0, 'samesite' => 'none']);
$store = new CookieStore(['now' => 303943620, 'expiration' => 0, 'samesite' => 'none']);
$method = new \ReflectionMethod(CookieStore::class, 'getSameSiteCookieHeader');
$method->setAccessible(true);
$header = $method->invokeArgs($store, ['__test_name_2__', '__test_value_2__', 303943620]);

$header = $store->getSameSiteCookieHeader('__test_name_1__', '__test_value_1__', 303943620);
$this->assertEquals(
'Set-Cookie: __test_name_1__=__test_value_1__; path=/; expires=Sunday, 19-Aug-1979 20:47:00 GMT; HttpOnly; SameSite=None; Secure',
'Set-Cookie: __test_name_2__=__test_value_2__; path=/; '.'expires=Sunday, 19-Aug-1979 20:47:00 GMT; HttpOnly; SameSite=None; Secure',
$header
);
}
Expand Down

0 comments on commit 0e10e61

Please sign in to comment.