From 9ca65eae0947c8253fe33c3e6b08356585a63475 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Sun, 20 Aug 2023 19:25:13 +0200 Subject: [PATCH] Further cleanup --- .../Exception/AccountNotLinkedException.php | 306 ++++++------------ src/Security/Core/User/EntityUserProvider.php | 33 +- .../User/OAuthAwareUserProviderInterface.php | 3 +- tests/Functional/IntegrationTest.php | 1 + 4 files changed, 106 insertions(+), 237 deletions(-) diff --git a/src/Security/Core/Exception/AccountNotLinkedException.php b/src/Security/Core/Exception/AccountNotLinkedException.php index ddab8ca2a..a1b870865 100644 --- a/src/Security/Core/Exception/AccountNotLinkedException.php +++ b/src/Security/Core/Exception/AccountNotLinkedException.php @@ -12,217 +12,109 @@ namespace HWI\Bundle\OAuthBundle\Security\Core\Exception; use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\AbstractOAuthToken; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\Exception\UserNotFoundException; -if (class_exists(UserNotFoundException::class)) { - final class AccountNotLinkedException extends UserNotFoundException implements OAuthAwareExceptionInterface +final class AccountNotLinkedException extends UserNotFoundException implements OAuthAwareExceptionInterface +{ + private ?string $resourceOwnerName = null; + + /** + * {@inheritdoc} + */ + public function __serialize(): array + { + return [ + $this->resourceOwnerName, + parent::__serialize(), + ]; + } + + /** + * {@inheritdoc} + */ + public function __unserialize(array $data): void + { + [ + $this->resourceOwnerName, + $parentData + ] = $data; + + parent::__unserialize($parentData); + } + + /** + * {@inheritdoc} + */ + public function getMessageKey(): string + { + return 'Account could not be linked correctly.'; + } + + /** + * {@inheritdoc} + */ + public function getAccessToken(): string + { + /** @var AbstractOAuthToken $token */ + $token = $this->getToken(); + + return $token->getAccessToken(); + } + + public function getRawToken(): array + { + /** @var AbstractOAuthToken $token */ + $token = $this->getToken(); + + return $token->getRawToken(); + } + + /** + * {@inheritdoc} + */ + public function getRefreshToken(): ?string + { + /** @var AbstractOAuthToken $token */ + $token = $this->getToken(); + + return $token->getRefreshToken(); + } + + /** + * {@inheritdoc} + */ + public function getExpiresIn(): ?int + { + /** @var AbstractOAuthToken $token */ + $token = $this->getToken(); + + return $token->getExpiresIn(); + } + + /** + * {@inheritdoc} + */ + public function getTokenSecret(): ?string + { + /** @var AbstractOAuthToken $token */ + $token = $this->getToken(); + + return $token->getTokenSecret(); + } + + /** + * {@inheritdoc} + */ + public function getResourceOwnerName(): ?string { - private ?string $resourceOwnerName = null; - - /** - * {@inheritdoc} - */ - public function __serialize(): array - { - return [ - $this->resourceOwnerName, - parent::__serialize(), - ]; - } - - /** - * {@inheritdoc} - */ - public function __unserialize(array $data): void - { - [ - $this->resourceOwnerName, - $parentData - ] = $data; - - parent::__unserialize($parentData); - } - - /** - * {@inheritdoc} - */ - public function getMessageKey(): string - { - return 'Account could not be linked correctly.'; - } - - /** - * {@inheritdoc} - */ - public function getAccessToken(): string - { - /** @var AbstractOAuthToken $token */ - $token = $this->getToken(); - - return $token->getAccessToken(); - } - - public function getRawToken(): array - { - /** @var AbstractOAuthToken $token */ - $token = $this->getToken(); - - return $token->getRawToken(); - } - - /** - * {@inheritdoc} - */ - public function getRefreshToken(): ?string - { - /** @var AbstractOAuthToken $token */ - $token = $this->getToken(); - - return $token->getRefreshToken(); - } - - /** - * {@inheritdoc} - */ - public function getExpiresIn(): ?int - { - /** @var AbstractOAuthToken $token */ - $token = $this->getToken(); - - return $token->getExpiresIn(); - } - - /** - * {@inheritdoc} - */ - public function getTokenSecret(): ?string - { - /** @var AbstractOAuthToken $token */ - $token = $this->getToken(); - - return $token->getTokenSecret(); - } - - /** - * {@inheritdoc} - */ - public function getResourceOwnerName(): ?string - { - return $this->resourceOwnerName; - } - - /** - * {@inheritdoc} - */ - public function setResourceOwnerName($resourceOwnerName): void - { - $this->resourceOwnerName = $resourceOwnerName; - } + return $this->resourceOwnerName; } -} else { - final class AccountNotLinkedException extends UsernameNotFoundException implements OAuthAwareExceptionInterface + + /** + * {@inheritdoc} + */ + public function setResourceOwnerName($resourceOwnerName): void { - private ?string $resourceOwnerName = null; - - /** - * {@inheritdoc} - */ - public function __serialize(): array - { - return [ - $this->resourceOwnerName, - parent::__serialize(), - ]; - } - - /** - * {@inheritdoc} - */ - public function __unserialize(array $data): void - { - [ - $this->resourceOwnerName, - $parentData - ] = $data; - - parent::__unserialize($parentData); - } - - /** - * {@inheritdoc} - */ - public function getMessageKey(): string - { - return 'Account could not be linked correctly.'; - } - - /** - * {@inheritdoc} - */ - public function getAccessToken(): string - { - /** @var AbstractOAuthToken $token */ - $token = $this->getToken(); - - return $token->getAccessToken(); - } - - public function getRawToken(): array - { - /** @var AbstractOAuthToken $token */ - $token = $this->getToken(); - - return $token->getRawToken(); - } - - /** - * {@inheritdoc} - */ - public function getRefreshToken(): ?string - { - /** @var AbstractOAuthToken $token */ - $token = $this->getToken(); - - return $token->getRefreshToken(); - } - - /** - * {@inheritdoc} - */ - public function getExpiresIn(): ?int - { - /** @var AbstractOAuthToken $token */ - $token = $this->getToken(); - - return $token->getExpiresIn(); - } - - /** - * {@inheritdoc} - */ - public function getTokenSecret(): ?string - { - /** @var AbstractOAuthToken $token */ - $token = $this->getToken(); - - return $token->getTokenSecret(); - } - - /** - * {@inheritdoc} - */ - public function getResourceOwnerName(): ?string - { - return $this->resourceOwnerName; - } - - /** - * {@inheritdoc} - */ - public function setResourceOwnerName($resourceOwnerName): void - { - $this->resourceOwnerName = $resourceOwnerName; - } + $this->resourceOwnerName = $resourceOwnerName; } } diff --git a/src/Security/Core/User/EntityUserProvider.php b/src/Security/Core/User/EntityUserProvider.php index 314ddd710..25aa93940 100644 --- a/src/Security/Core/User/EntityUserProvider.php +++ b/src/Security/Core/User/EntityUserProvider.php @@ -17,7 +17,6 @@ use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\Security\Core\Exception\UnsupportedUserException; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\Exception\UserNotFoundException; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserProviderInterface; @@ -57,10 +56,7 @@ public function loadUserByIdentifier(string $identifier): UserInterface $user = $this->findUser(['username' => $identifier]); if (!$user) { - $exception = new UserNotFoundException(sprintf("User '%s' not found.", $identifier)); - $exception->setUserIdentifier($identifier); - - throw $exception; + throw $this->createUserNotFoundException($identifier, sprintf("User '%s' not found.", $identifier)); } return $user; @@ -75,12 +71,7 @@ public function loadUserByIdentifier(string $identifier): UserInterface */ public function loadUserByUsername($username) { - $user = $this->findUser(['username' => $username]); - if (!$user) { - throw $this->createUserNotFoundException($username, sprintf("User '%s' not found.", $username)); - } - - return $user; + return $this->loadUserByIdentifier($username); } public function loadUserByOAuthUserResponse(UserResponseInterface $response): ?UserInterface @@ -133,24 +124,10 @@ private function findUser(array $criteria): ?UserInterface return $this->repository->findOneBy($criteria); } - /** - * @return UsernameNotFoundException|UserNotFoundException - */ - private function createUserNotFoundException(string $username, string $message) + private function createUserNotFoundException(string $username, string $message): UserNotFoundException { - if (class_exists(UserNotFoundException::class)) { - $exception = new UserNotFoundException($message); - $exception->setUserIdentifier($username); - } else { - if (!class_exists(UsernameNotFoundException::class)) { - throw new \RuntimeException('Unsupported Symfony version used!'); - } - - $exception = new UsernameNotFoundException($message); - if (method_exists($exception, 'setUsername')) { - $exception->setUsername($username); // @phpstan-ignore-this-line Symfony <5.4 BC layer - } - } + $exception = new UserNotFoundException($message); + $exception->setUserIdentifier($username); return $exception; } diff --git a/src/Security/Core/User/OAuthAwareUserProviderInterface.php b/src/Security/Core/User/OAuthAwareUserProviderInterface.php index f2b222b78..759722c64 100644 --- a/src/Security/Core/User/OAuthAwareUserProviderInterface.php +++ b/src/Security/Core/User/OAuthAwareUserProviderInterface.php @@ -12,7 +12,6 @@ namespace HWI\Bundle\OAuthBundle\Security\Core\User; use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\Exception\UserNotFoundException; use Symfony\Component\Security\Core\User\UserInterface; @@ -28,7 +27,7 @@ interface OAuthAwareUserProviderInterface * * @return UserInterface * - * @throws UsernameNotFoundException|UserNotFoundException if the user is not found + * @throws UserNotFoundException if the user is not found */ public function loadUserByOAuthUserResponse(UserResponseInterface $response); } diff --git a/tests/Functional/IntegrationTest.php b/tests/Functional/IntegrationTest.php index 948c07dc7..ab3c22144 100644 --- a/tests/Functional/IntegrationTest.php +++ b/tests/Functional/IntegrationTest.php @@ -149,6 +149,7 @@ function ($method, $url, $options) { public function testRequestCheckApi(): void { + // @phpstan-ignore-next-line if (Kernel::MAJOR_VERSION >= 6) { $this->markTestSkipped('Skipped due to bug in Symfony Security component: https://github.com/symfony/symfony/issues/51319'); }