GuardAuthenticationProviderTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Guard\Tests\Provider;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Security\Guard\Provider\GuardAuthenticationProvider;
  13. use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
  14. use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
  15. /**
  16. * @author Ryan Weaver <weaverryan@gmail.com>
  17. */
  18. class GuardAuthenticationProviderTest extends TestCase
  19. {
  20. private $userProvider;
  21. private $userChecker;
  22. private $preAuthenticationToken;
  23. public function testAuthenticate()
  24. {
  25. $providerKey = 'my_cool_firewall';
  26. $authenticatorA = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();
  27. $authenticatorB = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();
  28. $authenticatorC = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();
  29. $authenticators = array($authenticatorA, $authenticatorB, $authenticatorC);
  30. // called 2 times - for authenticator A and B (stops on B because of match)
  31. $this->preAuthenticationToken->expects($this->exactly(2))
  32. ->method('getGuardProviderKey')
  33. // it will return the "1" index, which will match authenticatorB
  34. ->will($this->returnValue('my_cool_firewall_1'));
  35. $enteredCredentials = array(
  36. 'username' => '_weaverryan_test_user',
  37. 'password' => 'guard_auth_ftw',
  38. );
  39. $this->preAuthenticationToken->expects($this->atLeastOnce())
  40. ->method('getCredentials')
  41. ->will($this->returnValue($enteredCredentials));
  42. // authenticators A and C are never called
  43. $authenticatorA->expects($this->never())
  44. ->method('getUser');
  45. $authenticatorC->expects($this->never())
  46. ->method('getUser');
  47. $mockedUser = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  48. $authenticatorB->expects($this->once())
  49. ->method('getUser')
  50. ->with($enteredCredentials, $this->userProvider)
  51. ->will($this->returnValue($mockedUser));
  52. // checkCredentials is called
  53. $authenticatorB->expects($this->once())
  54. ->method('checkCredentials')
  55. ->with($enteredCredentials, $mockedUser)
  56. // authentication works!
  57. ->will($this->returnValue(true));
  58. $authedToken = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  59. $authenticatorB->expects($this->once())
  60. ->method('createAuthenticatedToken')
  61. ->with($mockedUser, $providerKey)
  62. ->will($this->returnValue($authedToken));
  63. // user checker should be called
  64. $this->userChecker->expects($this->once())
  65. ->method('checkPreAuth')
  66. ->with($mockedUser);
  67. $this->userChecker->expects($this->once())
  68. ->method('checkPostAuth')
  69. ->with($mockedUser);
  70. $provider = new GuardAuthenticationProvider($authenticators, $this->userProvider, $providerKey, $this->userChecker);
  71. $actualAuthedToken = $provider->authenticate($this->preAuthenticationToken);
  72. $this->assertSame($authedToken, $actualAuthedToken);
  73. }
  74. /**
  75. * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
  76. */
  77. public function testCheckCredentialsReturningNonTrueFailsAuthentication()
  78. {
  79. $providerKey = 'my_uncool_firewall';
  80. $authenticator = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();
  81. // make sure the authenticator is used
  82. $this->preAuthenticationToken->expects($this->any())
  83. ->method('getGuardProviderKey')
  84. // the 0 index, to match the only authenticator
  85. ->will($this->returnValue('my_uncool_firewall_0'));
  86. $this->preAuthenticationToken->expects($this->atLeastOnce())
  87. ->method('getCredentials')
  88. ->will($this->returnValue('non-null-value'));
  89. $mockedUser = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  90. $authenticator->expects($this->once())
  91. ->method('getUser')
  92. ->will($this->returnValue($mockedUser));
  93. // checkCredentials is called
  94. $authenticator->expects($this->once())
  95. ->method('checkCredentials')
  96. // authentication fails :(
  97. ->will($this->returnValue(null));
  98. $provider = new GuardAuthenticationProvider(array($authenticator), $this->userProvider, $providerKey, $this->userChecker);
  99. $provider->authenticate($this->preAuthenticationToken);
  100. }
  101. /**
  102. * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationExpiredException
  103. */
  104. public function testGuardWithNoLongerAuthenticatedTriggersLogout()
  105. {
  106. $providerKey = 'my_firewall_abc';
  107. // create a token and mark it as NOT authenticated anymore
  108. // this mimics what would happen if a user "changed" between request
  109. $mockedUser = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  110. $token = new PostAuthenticationGuardToken($mockedUser, $providerKey, array('ROLE_USER'));
  111. $token->setAuthenticated(false);
  112. $provider = new GuardAuthenticationProvider(array(), $this->userProvider, $providerKey, $this->userChecker);
  113. $actualToken = $provider->authenticate($token);
  114. }
  115. public function testSupportsChecksGuardAuthenticatorsTokenOrigin()
  116. {
  117. $authenticatorA = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();
  118. $authenticatorB = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();
  119. $authenticators = array($authenticatorA, $authenticatorB);
  120. $mockedUser = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  121. $provider = new GuardAuthenticationProvider($authenticators, $this->userProvider, 'first_firewall', $this->userChecker);
  122. $token = new PreAuthenticationGuardToken($mockedUser, 'first_firewall_1');
  123. $supports = $provider->supports($token);
  124. $this->assertTrue($supports);
  125. $token = new PreAuthenticationGuardToken($mockedUser, 'second_firewall_0');
  126. $supports = $provider->supports($token);
  127. $this->assertFalse($supports);
  128. }
  129. /**
  130. * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
  131. * @expectedExceptionMessageRegExp /second_firewall_0/
  132. */
  133. public function testAuthenticateFailsOnNonOriginatingToken()
  134. {
  135. $authenticatorA = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();
  136. $authenticators = array($authenticatorA);
  137. $mockedUser = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  138. $provider = new GuardAuthenticationProvider($authenticators, $this->userProvider, 'first_firewall', $this->userChecker);
  139. $token = new PreAuthenticationGuardToken($mockedUser, 'second_firewall_0');
  140. $provider->authenticate($token);
  141. }
  142. protected function setUp()
  143. {
  144. $this->userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock();
  145. $this->userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
  146. $this->preAuthenticationToken = $this->getMockBuilder('Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken')
  147. ->disableOriginalConstructor()
  148. ->getMock();
  149. }
  150. protected function tearDown()
  151. {
  152. $this->userProvider = null;
  153. $this->userChecker = null;
  154. $this->preAuthenticationToken = null;
  155. }
  156. }