Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/Illuminate/Session/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ public function handle(Request $request, $type = HttpKernelInterface::MAIN_REQUE
{
$this->checkRequestForArraySessions($request);

// If a session driver has been configured, we will need to start the session here
// Check if this request is coming from web based auth
$isFromWebBasedAuth = $this->isRequestFromWebBasedAuth($request);

// If a session driver has been configured, we will need to start the session here
// so that the data is ready for an application. Note that the Laravel sessions
// do not make use of PHP "native" sessions in any way since they are crappy.
if ($this->sessionConfigured())

// this api has been modified to prevent request from API starting the
// session, and saving the session as we don't need user session here
if ($isFromWebBasedAuth && $this->sessionConfigured())
{
$session = $this->startSession($request);

Expand All @@ -74,7 +80,10 @@ public function handle(Request $request, $type = HttpKernelInterface::MAIN_REQUE
// Again, if the session has been configured we will need to close out the session
// so that the attributes may be persisted to some storage medium. We will also
// add the session identifier cookie to the application response headers now.
if ($this->sessionConfigured())

// this api has been modified to prevent request from API starting the
// session, and saving the session as we don't need user at backend side here
if ($isFromWebBasedAuth && $this->sessionConfigured())
{
$this->storeCurrentUrl($request, $session);
$this->closeSession($session);
Expand All @@ -85,6 +94,17 @@ public function handle(Request $request, $type = HttpKernelInterface::MAIN_REQUE
return $response;
}

private function isRequestFromWebBasedAuth(Request $request): bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ini metodenya sebenarnya kurang pas. Karena kondisi !$isFromTokenBasedAuthentication tidak hanya berasal dari web auth. Usulku lebih baik dibuat eksplisit saja isTokenBasedRequest().

{
$currentPath = $request->getRequestUri();
$authorizationHeader = $request->headers->get('Authorization');
$isFromTokenBasedAuthentication =
(!empty($authorizationHeader) && preg_match("/(Basic|Bearer) .+/", $authorizationHeader)) ||
starts_with($currentPath, '/api/v1/oauth/');

return !$isFromTokenBasedAuthentication;
}

/**
* Check the request and reject callback for array sessions.
*
Expand Down
60 changes: 60 additions & 0 deletions tests/Session/SessionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,66 @@ public function testSessionIsNotUsedWhenNoDriver()
$this->assertSame($response, $middleResponse);
}

public function testSessionIsNotUsedWhenRequestHasValidAuthorizationHeader(): void
{
$request = Symfony\Component\HttpFoundation\Request::create('http://www.foo.com/api/some-api', 'GET', server: [
'HTTP_AUTHORIZATION' => 'Bearer 1234567890',
]);
$response = new Symfony\Component\HttpFoundation\Response;

$middle = new Illuminate\Session\Middleware(
$app = m::mock(HttpKernelInterface::class),
$manager = m::mock(SessionManager::class)
);

$manager->shouldNotReceive('getSessionConfig');

$manager->shouldReceive('driver')->andReturn($driver = m::mock(Store::class)->makePartial());
$driver->shouldNotReceive('setRequestOnHandler');
$driver->shouldNotReceive('start');
$app->shouldReceive('handle')->once()->with($request, Symfony\Component\HttpKernel\HttpKernelInterface::MAIN_REQUEST, true)->andReturn($response);
$driver->shouldNotReceive('save');
$driver->shouldNotReceive('getHandler');
$driver->shouldNotReceive('getName')->andReturn('name');
$driver->shouldNotReceive('getId')->andReturn(1);
$driver->shouldNotReceive('setPreviousUrl');

$middleResponse = $middle->handle($request);

self::assertSame($response, $middleResponse);
self::assertCount(0, $response->headers->getCookies());
}



public function testSessionIsNotUsedWhenRequestFromOauthApi(): void
{
$request = Symfony\Component\HttpFoundation\Request::create('http://www.foo.com/api/v1/oauth/token', 'POST');
$response = new Symfony\Component\HttpFoundation\Response;

$middle = new Illuminate\Session\Middleware(
$app = m::mock(HttpKernelInterface::class),
$manager = m::mock(SessionManager::class)
);

$manager->shouldNotReceive('getSessionConfig');

$manager->shouldReceive('driver')->andReturn($driver = m::mock(Store::class)->makePartial());
$driver->shouldNotReceive('setRequestOnHandler');
$driver->shouldNotReceive('start');
$app->shouldReceive('handle')->once()->with($request, Symfony\Component\HttpKernel\HttpKernelInterface::MAIN_REQUEST, true)->andReturn($response);
$driver->shouldNotReceive('save');
$driver->shouldNotReceive('getHandler');
$driver->shouldNotReceive('getName')->andReturn('name');
$driver->shouldNotReceive('getId')->andReturn(1);
$driver->shouldNotReceive('setPreviousUrl');

$middleResponse = $middle->handle($request);

self::assertSame($response, $middleResponse);
self::assertCount(0, $response->headers->getCookies());
}


public function testCheckingForRequestUsingArraySessions()
{
Expand Down