Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #19218 [Security][Guard] check if session exist before using it (…
…pasdeloup) This PR was squashed before being merged into the 2.8 branch (closes #19218). Discussion ---------- [Security][Guard] check if session exist before using it | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18958 | License | MIT | Doc PR | - As stated by @Shekhovtsovy when the Guard component is used without the Symfony full stack (for instance in Laravel), $request->getSession() may be null. An additionnal PR will be needed for 3.1 but it may be better to check this one before. Commits ------- a3f7510 [Security][Guard] check if session exist before using it
- Loading branch information
Showing
2 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
src/Symfony/Component/Security/Guard/Tests/Authenticator/FormLoginAuthenticatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Guard\Tests\Authenticator; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator; | ||
|
||
/** | ||
* @author Jean Pasdeloup <jpasdeloup@sedona.fr> | ||
*/ | ||
class FormLoginAuthenticatorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $requestWithoutSession; | ||
private $requestWithSession; | ||
private $authenticator; | ||
|
||
const LOGIN_URL = 'http://login'; | ||
const DEFAULT_SUCCESS_URL = 'http://defaultsuccess'; | ||
const CUSTOM_SUCCESS_URL = 'http://customsuccess'; | ||
|
||
public function testAuthenticationFailureWithoutSession() | ||
{ | ||
$failureResponse = $this->authenticator->onAuthenticationFailure($this->requestWithoutSession, new AuthenticationException()); | ||
|
||
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $failureResponse); | ||
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl()); | ||
} | ||
|
||
public function testAuthenticationFailureWithSession() | ||
{ | ||
$this->requestWithSession->getSession() | ||
->expects($this->once()) | ||
->method('set'); | ||
|
||
$failureResponse = $this->authenticator->onAuthenticationFailure($this->requestWithSession, new AuthenticationException()); | ||
|
||
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $failureResponse); | ||
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl()); | ||
} | ||
|
||
public function testAuthenticationSuccessWithoutSession() | ||
{ | ||
$token = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$redirectResponse = $this->authenticator->onAuthenticationSuccess($this->requestWithoutSession, $token, 'providerkey'); | ||
|
||
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $redirectResponse); | ||
$this->assertEquals(self::DEFAULT_SUCCESS_URL, $redirectResponse->getTargetUrl()); | ||
} | ||
|
||
public function testAuthenticationSuccessWithSessionButEmpty() | ||
{ | ||
$token = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->requestWithSession->getSession() | ||
->expects($this->once()) | ||
->method('get') | ||
->will($this->returnValue(null)); | ||
|
||
$redirectResponse = $this->authenticator->onAuthenticationSuccess($this->requestWithSession, $token, 'providerkey'); | ||
|
||
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $redirectResponse); | ||
$this->assertEquals(self::DEFAULT_SUCCESS_URL, $redirectResponse->getTargetUrl()); | ||
} | ||
|
||
public function testAuthenticationSuccessWithSessionAndTarget() | ||
{ | ||
$token = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->requestWithSession->getSession() | ||
->expects($this->once()) | ||
->method('get') | ||
->will($this->returnValue(self::CUSTOM_SUCCESS_URL)); | ||
|
||
$redirectResponse = $this->authenticator->onAuthenticationSuccess($this->requestWithSession, $token, 'providerkey'); | ||
|
||
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $redirectResponse); | ||
$this->assertEquals(self::CUSTOM_SUCCESS_URL, $redirectResponse->getTargetUrl()); | ||
} | ||
|
||
public function testRememberMe() | ||
{ | ||
$doSupport = $this->authenticator->supportsRememberMe(); | ||
|
||
$this->assertTrue($doSupport); | ||
} | ||
|
||
public function testStartWithoutSession() | ||
{ | ||
$failureResponse = $this->authenticator->start($this->requestWithoutSession, new AuthenticationException()); | ||
|
||
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $failureResponse); | ||
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl()); | ||
} | ||
|
||
public function testStartWithSession() | ||
{ | ||
$failureResponse = $this->authenticator->start($this->requestWithSession, new AuthenticationException()); | ||
|
||
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $failureResponse); | ||
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl()); | ||
} | ||
|
||
protected function setUp() | ||
{ | ||
$this->requestWithoutSession = new Request(array(), array(), array(), array(), array(), array()); | ||
$this->requestWithSession = new Request(array(), array(), array(), array(), array(), array()); | ||
|
||
$session = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\Session\\SessionInterface') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->requestWithSession->setSession($session); | ||
|
||
$this->authenticator = new TestFormLoginAuthenticator(); | ||
$this->authenticator | ||
->setLoginUrl(self::LOGIN_URL) | ||
->setDefaultSuccessRedirectUrl(self::DEFAULT_SUCCESS_URL) | ||
; | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$this->request = null; | ||
$this->requestWithSession = null; | ||
} | ||
} | ||
|
||
class TestFormLoginAuthenticator extends AbstractFormLoginAuthenticator | ||
{ | ||
private $loginUrl; | ||
private $defaultSuccessRedirectUrl; | ||
|
||
/** | ||
* @param mixed $defaultSuccessRedirectUrl | ||
* | ||
* @return TestFormLoginAuthenticator | ||
*/ | ||
public function setDefaultSuccessRedirectUrl($defaultSuccessRedirectUrl) | ||
{ | ||
$this->defaultSuccessRedirectUrl = $defaultSuccessRedirectUrl; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param mixed $loginUrl | ||
* | ||
* @return TestFormLoginAuthenticator | ||
*/ | ||
public function setLoginUrl($loginUrl) | ||
{ | ||
$this->loginUrl = $loginUrl; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getLoginUrl() | ||
{ | ||
return $this->loginUrl; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getDefaultSuccessRedirectUrl() | ||
{ | ||
return $this->defaultSuccessRedirectUrl; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCredentials(Request $request) | ||
{ | ||
return 'credentials'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getUser($credentials, UserProviderInterface $userProvider) | ||
{ | ||
return $userProvider->loadUserByUsername($credentials); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function checkCredentials($credentials, UserInterface $user) | ||
{ | ||
return true; | ||
} | ||
} |