Skip to content

Commit

Permalink
Merge pull request #60 from nealio82/master
Browse files Browse the repository at this point in the history
Add session handling into `SymfonyAdapter`
  • Loading branch information
mnapoli authored Sep 12, 2018
2 parents 5509ac8 + f92ee66 commit d6be192
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
41 changes: 40 additions & 1 deletion src/Bridge/Symfony/SymfonyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;

Expand All @@ -28,12 +29,18 @@ public function __construct(HttpKernelInterface $httpKernel)
$this->httpKernel = $httpKernel;
}

public function handle(ServerRequestInterface $request) : ResponseInterface
public function handle(ServerRequestInterface $request): ResponseInterface
{
$httpFoundationFactory = new HttpFoundationFactory;

$symfonyRequest = $httpFoundationFactory->createRequest($request);

$this->loadSessionFromCookies($symfonyRequest);

$symfonyResponse = $this->httpKernel->handle($symfonyRequest);

$this->addSessionCookieToResponse($symfonyResponse);

if ($this->httpKernel instanceof TerminableInterface) {
$this->httpKernel->terminate($symfonyRequest, $symfonyResponse);
}
Expand All @@ -43,4 +50,36 @@ public function handle(ServerRequestInterface $request) : ResponseInterface

return $response;
}

/**
* @param $symfonyRequest
*/
private function loadSessionFromCookies($symfonyRequest): void
{
if (!is_null($symfonyRequest->cookies->get(session_name()))) {
$this->httpKernel->getContainer()->get('session')->setId(
$symfonyRequest->cookies->get(session_name())
);
}
}

/**
* @param $symfonyResponse
*/
private function addSessionCookieToResponse($symfonyResponse): void
{
$symfonyResponse->headers->setCookie(
new Cookie(
session_name(),
$this->httpKernel->getContainer()->get('session')->getId(),
0,
"/",
null,
false,
true,
false,
Cookie::SAMESITE_LAX
)
);
}
}
42 changes: 40 additions & 2 deletions tests/Bridge/Symfony/SymfonyAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -45,7 +46,39 @@ public function test 404 are PSR7 responses and not exceptions()
self::assertSame(404, $response->getStatusCode());
}

private function createKernel() : HttpKernelInterface
public function test an active session is created()
{
$adapter = new SymfonyAdapter($this->createKernel());
$response = $adapter->handle(new ServerRequest([], [], '/bar'));

self::assertArrayHasKey('Set-Cookie', $response->getHeaders());
}

public function test an active session is retrieved()
{
$kernel = $this->createKernel();
$kernel->boot();

$adapter = new SymfonyAdapter($kernel);
$symfonyResponse = $adapter->handle(
new ServerRequest(
[],
[],
'/bar',
null,
'php://input',
[],
[\session_name() => 'SESSIONID']
)
);

self::assertSame(
sprintf("%s=SESSIONID; path=/; httponly; samesite=lax", \session_name()),
$symfonyResponse->getHeaders()['Set-Cookie'][0]
);
}

private function createKernel(): HttpKernelInterface
{
return new class('dev', false) extends Kernel implements EventSubscriberInterface {
use MicroKernelTrait;
Expand All @@ -57,8 +90,13 @@ public function registerBundles()

protected function configureContainer(ContainerBuilder $c)
{
$c->register('session_storage', MockArraySessionStorage::class);

$c->loadFromExtension('framework', [
'secret' => 'foo',
'secret' => 'foo',
'session' => [
'storage_id' => 'session_storage'
]
]);
}

Expand Down

0 comments on commit d6be192

Please sign in to comment.