diff --git a/composer.json b/composer.json index 9d48b932..673f3c70 100644 --- a/composer.json +++ b/composer.json @@ -14,17 +14,18 @@ ], "require": { "php": ">=8.1", - "symfony/framework-bundle": "^5.4|^6.0|^7.0", - "symfony/dependency-injection": "^5.4|^6.0|^7.0", - "symfony/routing": "^5.4|^6.0|^7.0", - "symfony/http-foundation": "^5.4|^6.0|^7.0", - "league/oauth2-client": "^2.0" + "symfony/framework-bundle": "^6.4|^7.3", + "symfony/dependency-injection": "^6.4|^7.3", + "symfony/routing": "^6.4|^7.3", + "symfony/http-foundation": "^6.4|^7.3", + "league/oauth2-client": "^2.0", + "symfony/security-core": "^6.4|^7.3", + "symfony/security-http": "^6.4|^7.3" }, "require-dev": { "league/oauth2-facebook": "^1.1|^2.0", - "symfony/phpunit-bridge": "^5.4|^6.0|^7.0", - "symfony/security-guard": "^5.4", - "symfony/yaml": "^5.4|^6.0|^7.0" + "symfony/phpunit-bridge": "^7.3", + "symfony/yaml": "^6.4|^7.3" }, "autoload": { "psr-4": { "KnpU\\OAuth2ClientBundle\\": "src/" } diff --git a/src/Security/Authenticator/SocialAuthenticator.php b/src/Security/Authenticator/SocialAuthenticator.php deleted file mode 100644 index e4133104..00000000 --- a/src/Security/Authenticator/SocialAuthenticator.php +++ /dev/null @@ -1,59 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace KnpU\OAuth2ClientBundle\Security\Authenticator; - -use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface; -use KnpU\OAuth2ClientBundle\Exception\InvalidStateException; -use KnpU\OAuth2ClientBundle\Exception\MissingAuthorizationCodeException; -use KnpU\OAuth2ClientBundle\Security\Exception\IdentityProviderAuthenticationException; -use KnpU\OAuth2ClientBundle\Security\Exception\InvalidStateAuthenticationException; -use KnpU\OAuth2ClientBundle\Security\Exception\NoAuthCodeAuthenticationException; -use KnpU\OAuth2ClientBundle\Security\Helper\FinishRegistrationBehavior; -use KnpU\OAuth2ClientBundle\Security\Helper\PreviousUrlHelper; -use KnpU\OAuth2ClientBundle\Security\Helper\SaveAuthFailureMessage; -use League\OAuth2\Client\Provider\Exception\IdentityProviderException; -use League\OAuth2\Client\Token\AccessToken; -use Symfony\Component\Security\Core\User\UserInterface; -use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; - -abstract class SocialAuthenticator extends AbstractGuardAuthenticator -{ - use FinishRegistrationBehavior; - use PreviousUrlHelper; - use SaveAuthFailureMessage; - - /** - * @return AccessToken - */ - protected function fetchAccessToken(OAuth2ClientInterface $client, array $options = []) - { - try { - return $client->getAccessToken($options); - } catch (MissingAuthorizationCodeException $e) { - throw new NoAuthCodeAuthenticationException(); - } catch (IdentityProviderException $e) { - throw new IdentityProviderAuthenticationException($e); - } catch (InvalidStateException $e) { - throw new InvalidStateAuthenticationException($e); - } - } - - public function checkCredentials($credentials, UserInterface $user): bool - { - // do nothing - the fact that the access token works is enough - return true; - } - - public function supportsRememberMe(): bool - { - return true; - } -} diff --git a/src/Security/Helper/SaveAuthFailureMessage.php b/src/Security/Helper/SaveAuthFailureMessage.php index 243a032a..cee52290 100644 --- a/src/Security/Helper/SaveAuthFailureMessage.php +++ b/src/Security/Helper/SaveAuthFailureMessage.php @@ -13,7 +13,6 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; -use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Http\SecurityRequestAttributes; trait SaveAuthFailureMessage @@ -24,10 +23,6 @@ protected function saveAuthenticationErrorToSession(Request $request, Authentica throw new \LogicException('In order to save an authentication error, you must have a session available.'); } - $authenticationError = class_exists(Security::class) - ? Security::AUTHENTICATION_ERROR - : SecurityRequestAttributes::AUTHENTICATION_ERROR; - - $request->getSession()->set($authenticationError, $exception); + $request->getSession()->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, $exception); } } diff --git a/tests/Security/Authenticator/SocialAuthenticatorTest.php b/tests/Security/Authenticator/SocialAuthenticatorTest.php deleted file mode 100644 index 8ff27b83..00000000 --- a/tests/Security/Authenticator/SocialAuthenticatorTest.php +++ /dev/null @@ -1,151 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace KnpU\OAuth2ClientBundle\Tests\Security\Authenticator; - -use KnpU\OAuth2ClientBundle\Exception\InvalidStateException; -use KnpU\OAuth2ClientBundle\Exception\MissingAuthorizationCodeException; -use KnpU\OAuth2ClientBundle\Security\Authenticator\SocialAuthenticator; -use KnpU\OAuth2ClientBundle\Client\OAuth2Client; -use KnpU\OAuth2ClientBundle\Security\Exception\IdentityProviderAuthenticationException; -use KnpU\OAuth2ClientBundle\Security\Exception\InvalidStateAuthenticationException; -use KnpU\OAuth2ClientBundle\Security\Exception\NoAuthCodeAuthenticationException; -use League\OAuth2\Client\Provider\Exception\IdentityProviderException; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; -use Symfony\Component\Security\Core\Exception\AuthenticationException; -use Symfony\Component\Security\Core\User\UserInterface; -use Symfony\Component\Security\Core\User\UserProviderInterface; -use PHPUnit\Framework\TestCase; - -/** - * @group legacy - */ -class SocialAuthenticatorTest extends TestCase -{ - public function testFetchAccessTokenSimplyReturns() - { - $authenticator = new StubSocialAuthenticator(); - $client = $this->createMock(OAuth2Client::class); - $client->method('getAccessToken') - ->with([]) - ->willReturn('expected_access_token'); - - $actualToken = $authenticator->doFetchAccessToken($client); - $this->assertEquals('expected_access_token', $actualToken); - } - - public function testFetchAccessTokenThrowsAuthenticationException() - { - $this->expectException(NoAuthCodeAuthenticationException::class); - $authenticator = new StubSocialAuthenticator(); - $client = $this->createMock(OAuth2Client::class); - $client->method('getAccessToken') - ->with([]) - ->willThrowException(new MissingAuthorizationCodeException()); - - $authenticator->doFetchAccessToken($client); - } - - public function testFetchAccessTokenThrowsIdentityProviderException() - { - $this->expectException(IdentityProviderAuthenticationException::class); - $authenticator = new StubSocialAuthenticator(); - $client = $this->createMock(OAuth2Client::class); - $client->method('getAccessToken') - ->with([]) - ->willThrowException(new IdentityProviderException("message", 42, "response")); - - $authenticator->doFetchAccessToken($client); - } - - public function testFetchAccessTokenThrowsInvalidStateException() - { - $this->expectException(InvalidStateAuthenticationException::class); - $authenticator = new StubSocialAuthenticator(); - $client = $this->createMock(OAuth2Client::class); - $client->method('getAccessToken') - ->with([]) - ->willThrowException(new InvalidStateException()); - - $authenticator->doFetchAccessToken($client); - } - - public function testCheckCredentials() - { - $authenticator = new StubSocialAuthenticator(); - $user = new SomeUser(); - $this->assertEquals(true, $authenticator->checkCredentials('', $user)); - } - - public function testSupportsRememberMe() - { - $authenticator = new StubSocialAuthenticator(); - $this->assertEquals(true, $authenticator->supportsRememberMe()); - } -} - -class StubSocialAuthenticator extends SocialAuthenticator -{ - public function doFetchAccessToken(OAuth2Client $client) - { - return $this->fetchAccessToken($client); - } - - public function start(Request $request, ?AuthenticationException $authException = null): Response - { - } - public function supports(Request $request): bool - { - } - - /** @return mixed */ - public function getCredentials(Request $request) - { - } - public function getUser($credentials, UserProviderInterface $userProvider): ?UserInterface - { - } - public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response - { - } - public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): ?Response - { - } -} - -class SomeUser implements UserInterface -{ - public function getRoles(): array - { - } - - public function getPassword(): ?string - { - } - - public function getSalt(): ?string - { - } - - // to be removed when Symfony 5.2 supported is dropped - public function getUsername(): string - { - } - - public function eraseCredentials(): void - { - } - - public function getUserIdentifier(): string - { - } -} diff --git a/tests/Security/Helper/SaveAuthFailureMessageTest.php b/tests/Security/Helper/SaveAuthFailureMessageTest.php index 7432051c..a75554fb 100644 --- a/tests/Security/Helper/SaveAuthFailureMessageTest.php +++ b/tests/Security/Helper/SaveAuthFailureMessageTest.php @@ -12,9 +12,8 @@ use KnpU\OAuth2ClientBundle\Security\Helper\SaveAuthFailureMessage; use PHPUnit\Framework\TestCase; -use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; -use Symfony\Component\Security\Core\Security; +use Symfony\Component\Security\Http\SecurityRequestAttributes; class SaveAuthFailureMessageTest extends TestCase { @@ -41,7 +40,7 @@ public function testShouldUpdateSessionErrorIfSessionExists() $testFailureMessage->callSaveAuthenticationErrorToSession($request, $mockAuthException); $session = $request->getSession(); - $this->assertInstanceOf(AuthenticationException::class, $session->get(Security::AUTHENTICATION_ERROR)); + $this->assertInstanceOf(AuthenticationException::class, $session->get(SecurityRequestAttributes::AUTHENTICATION_ERROR)); } } diff --git a/tests/app/TestKernel.php b/tests/app/TestKernel.php index b523d440..2ad6fc57 100644 --- a/tests/app/TestKernel.php +++ b/tests/app/TestKernel.php @@ -43,9 +43,6 @@ public function registerContainerConfiguration(LoaderInterface $loader): void 'resource' => __DIR__ . '/routing.yml', 'utf8' => true, ], - // turn this off - otherwise we need doctrine/annotation - // the change that required this was in Symfony 3.2.0 - 'annotations' => Kernel::VERSION_ID >= 30200 ? false : [], ]); $container->loadFromExtension('knpu_oauth2_client', [