Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #40 from pine3ree/add-create-session-cookie
Browse files Browse the repository at this point in the history
parse boolean ini settings when creating the response cookie
  • Loading branch information
weierophinney committed Feb 28, 2019
2 parents 5ebdb3c + bc0edcb commit 104cc1d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/PhpSessionPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
use function sprintf;
use function time;

use const FILTER_VALIDATE_BOOLEAN;
use const FILTER_NULL_ON_FAILURE;

/**
* Session persistence using ext-session.
*
Expand Down Expand Up @@ -119,12 +122,7 @@ public function persistSession(SessionInterface $session, ResponseInterface $res
return $response;
}

$sessionCookie = SetCookie::create(session_name())
->withValue($id)
->withPath(ini_get('session.cookie_path'))
->withDomain(ini_get('session.cookie_domain'))
->withSecure(ini_get('session.cookie_secure'))
->withHttpOnly(ini_get('session.cookie_httponly'));
$sessionCookie = $this->createSessionCookie(session_name(), $id);

if ($cookieLifetime = $this->getCookieLifetime($session)) {
$sessionCookie = $sessionCookie->withExpires(time() + $cookieLifetime);
Expand Down Expand Up @@ -182,6 +180,34 @@ private function generateSessionId() : string
return bin2hex(random_bytes(16));
}

/**
* Build a SetCookie parsing boolean ini settings
*
* @param string $name The session name as the cookie name
* @param string $id The session id as the cookie value
* @return SetCookie
*/
private function createSessionCookie(string $name, string $id) : SetCookie
{
$secure = filter_var(
ini_get('session.cookie_secure'),
FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE
);
$httpOnly = filter_var(
ini_get('session.cookie_httponly'),
FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE
);

return SetCookie::create($name)
->withValue($id)
->withPath(ini_get('session.cookie_path'))
->withDomain(ini_get('session.cookie_domain'))
->withSecure($secure)
->withHttpOnly($httpOnly);
}

/**
* Generate cache http headers for this instance's session cache_limiter and
* cache_expire values
Expand Down
48 changes: 48 additions & 0 deletions test/PhpSessionPersistenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,52 @@ public function testOnlyOneSessionFileIsCreatedIfNoSessionCookiePresentINFirstRe

$this->restoreOriginalSessionIniSettings($ini);
}

/**
* @dataProvider cookieSettingsProvider
*/
public function testThatSetCookieCorrectlyInterpretsIniSettings(
$secureIni,
$httpOnlyIni,
$expectedSecure,
$expectedHttpOnly
) {
$ini = $this->applyCustomSessionOptions([
'cookie_secure' => $secureIni,
'cookie_httponly' => $httpOnlyIni,
]);

$persistence = new PhpSessionPersistence();

$createSessionCookie = new ReflectionMethod($persistence, 'createSessionCookie');
$createSessionCookie->setAccessible(true);

$setCookie = $createSessionCookie->invokeArgs(
$persistence,
['SETCOOKIESESSIONID', 'set-cookie-test-value']
);

$this->assertSame($expectedSecure, $setCookie->getSecure());
$this->assertSame($expectedHttpOnly, $setCookie->getHttpOnly());

$this->restoreOriginalSessionIniSettings($ini);
}

public function cookieSettingsProvider()
{
// obvious input/results data are left (commented out) for reference
return [
//[false, false, false, false],
//[0, 0, false, false],
//['0', '0', false, false],
//['', '', false, false],
['off', 'off', false, false],
['Off', 'Off', false, false],
//[true, true, true, true],
//[1, 1, true, true],
//['1', '1', true, true],
//['on', 'on', true, true],
//['On', 'On', true, true],
];
}
}

0 comments on commit 104cc1d

Please sign in to comment.