Skip to content

Commit

Permalink
Further cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Aug 20, 2023
1 parent ec9f6fb commit 9ca65ea
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 237 deletions.
306 changes: 99 additions & 207 deletions src/Security/Core/Exception/AccountNotLinkedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
33 changes: 5 additions & 28 deletions src/Security/Core/User/EntityUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Security/Core/User/OAuthAwareUserProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Loading

0 comments on commit 9ca65ea

Please sign in to comment.