From 547e571d9b608279d154283b58f8c73a804a05c7 Mon Sep 17 00:00:00 2001 From: pistej Date: Thu, 9 Mar 2023 11:48:41 +0100 Subject: [PATCH] Decoupled models from managers issue --- src/Event/PreSaveClientEvent.php | 10 ++--- src/Manager/AccessTokenManagerInterface.php | 6 +-- .../AuthorizationCodeManagerInterface.php | 6 +-- src/Manager/ClientManagerInterface.php | 10 ++--- src/Manager/Doctrine/AccessTokenManager.php | 5 ++- .../Doctrine/AuthorizationCodeManager.php | 5 ++- src/Manager/Doctrine/ClientManager.php | 7 ++-- src/Manager/Doctrine/RefreshTokenManager.php | 5 ++- src/Manager/InMemory/AccessTokenManager.php | 10 ++--- .../InMemory/AuthorizationCodeManager.php | 10 ++--- src/Manager/InMemory/ClientManager.php | 14 +++---- src/Manager/InMemory/RefreshTokenManager.php | 10 ++--- src/Manager/RefreshTokenManagerInterface.php | 6 +-- src/Model/AbstractClient.php | 14 +++---- src/Model/AccessToken.php | 10 ++--- src/Model/AccessTokenInterface.php | 26 +++++++++++++ src/Model/AuthorizationCode.php | 10 ++--- src/Model/AuthorizationCodeInterface.php | 26 +++++++++++++ src/Model/ClientInterface.php | 38 +++++++++++++++++++ src/Model/RefreshToken.php | 6 +-- src/Model/RefreshTokenInterface.php | 22 +++++++++++ 21 files changed, 186 insertions(+), 70 deletions(-) create mode 100644 src/Model/AccessTokenInterface.php create mode 100644 src/Model/AuthorizationCodeInterface.php create mode 100644 src/Model/ClientInterface.php create mode 100644 src/Model/RefreshTokenInterface.php diff --git a/src/Event/PreSaveClientEvent.php b/src/Event/PreSaveClientEvent.php index 2d9ac7d8..ca97d4f0 100644 --- a/src/Event/PreSaveClientEvent.php +++ b/src/Event/PreSaveClientEvent.php @@ -4,7 +4,7 @@ namespace League\Bundle\OAuth2ServerBundle\Event; -use League\Bundle\OAuth2ServerBundle\Model\AbstractClient; +use League\Bundle\OAuth2ServerBundle\Model\ClientInterface; use Symfony\Contracts\EventDispatcher\Event; /** @@ -13,21 +13,21 @@ class PreSaveClientEvent extends Event { /** - * @var AbstractClient + * @var ClientInterface */ private $client; - public function __construct(AbstractClient $client) + public function __construct(ClientInterface $client) { $this->client = $client; } - public function getClient(): AbstractClient + public function getClient(): ClientInterface { return $this->client; } - public function setClient(AbstractClient $client): void + public function setClient(ClientInterface $client): void { $this->client = $client; } diff --git a/src/Manager/AccessTokenManagerInterface.php b/src/Manager/AccessTokenManagerInterface.php index d680ac1b..256f74f3 100644 --- a/src/Manager/AccessTokenManagerInterface.php +++ b/src/Manager/AccessTokenManagerInterface.php @@ -4,13 +4,13 @@ namespace League\Bundle\OAuth2ServerBundle\Manager; -use League\Bundle\OAuth2ServerBundle\Model\AccessToken; +use League\Bundle\OAuth2ServerBundle\Model\AccessTokenInterface; interface AccessTokenManagerInterface { - public function find(string $identifier): ?AccessToken; + public function find(string $identifier): ?AccessTokenInterface; - public function save(AccessToken $accessToken): void; + public function save(AccessTokenInterface $accessToken): void; public function clearExpired(): int; } diff --git a/src/Manager/AuthorizationCodeManagerInterface.php b/src/Manager/AuthorizationCodeManagerInterface.php index 69f608a6..ad810310 100644 --- a/src/Manager/AuthorizationCodeManagerInterface.php +++ b/src/Manager/AuthorizationCodeManagerInterface.php @@ -4,13 +4,13 @@ namespace League\Bundle\OAuth2ServerBundle\Manager; -use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCode; +use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCodeInterface; interface AuthorizationCodeManagerInterface { - public function find(string $identifier): ?AuthorizationCode; + public function find(string $identifier): ?AuthorizationCodeInterface; - public function save(AuthorizationCode $authCode): void; + public function save(AuthorizationCodeInterface $authCode): void; public function clearExpired(): int; } diff --git a/src/Manager/ClientManagerInterface.php b/src/Manager/ClientManagerInterface.php index 3e9da70a..edd66b6e 100644 --- a/src/Manager/ClientManagerInterface.php +++ b/src/Manager/ClientManagerInterface.php @@ -4,18 +4,18 @@ namespace League\Bundle\OAuth2ServerBundle\Manager; -use League\Bundle\OAuth2ServerBundle\Model\AbstractClient; +use League\Bundle\OAuth2ServerBundle\Model\ClientInterface; interface ClientManagerInterface { - public function save(AbstractClient $client): void; + public function save(ClientInterface $client): void; - public function remove(AbstractClient $client): void; + public function remove(ClientInterface $client): void; - public function find(string $identifier): ?AbstractClient; + public function find(string $identifier): ?ClientInterface; /** - * @return list + * @return list */ public function list(?ClientFilter $clientFilter): array; } diff --git a/src/Manager/Doctrine/AccessTokenManager.php b/src/Manager/Doctrine/AccessTokenManager.php index 41059256..607b3312 100644 --- a/src/Manager/Doctrine/AccessTokenManager.php +++ b/src/Manager/Doctrine/AccessTokenManager.php @@ -7,6 +7,7 @@ use Doctrine\ORM\EntityManagerInterface; use League\Bundle\OAuth2ServerBundle\Manager\AccessTokenManagerInterface; use League\Bundle\OAuth2ServerBundle\Model\AccessToken; +use League\Bundle\OAuth2ServerBundle\Model\AccessTokenInterface; final class AccessTokenManager implements AccessTokenManagerInterface { @@ -24,7 +25,7 @@ public function __construct(EntityManagerInterface $entityManager, bool $persist $this->persistAccessToken = $persistAccessToken; } - public function find(string $identifier): ?AccessToken + public function find(string $identifier): ?AccessTokenInterface { if (!$this->persistAccessToken) { return null; @@ -33,7 +34,7 @@ public function find(string $identifier): ?AccessToken return $this->entityManager->find(AccessToken::class, $identifier); } - public function save(AccessToken $accessToken): void + public function save(AccessTokenInterface $accessToken): void { if (!$this->persistAccessToken) { return; diff --git a/src/Manager/Doctrine/AuthorizationCodeManager.php b/src/Manager/Doctrine/AuthorizationCodeManager.php index ac1150ce..3a1ffdee 100644 --- a/src/Manager/Doctrine/AuthorizationCodeManager.php +++ b/src/Manager/Doctrine/AuthorizationCodeManager.php @@ -7,6 +7,7 @@ use Doctrine\ORM\EntityManagerInterface; use League\Bundle\OAuth2ServerBundle\Manager\AuthorizationCodeManagerInterface; use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCode; +use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCodeInterface; final class AuthorizationCodeManager implements AuthorizationCodeManagerInterface { @@ -20,12 +21,12 @@ public function __construct(EntityManagerInterface $entityManager) $this->entityManager = $entityManager; } - public function find(string $identifier): ?AuthorizationCode + public function find(string $identifier): ?AuthorizationCodeInterface { return $this->entityManager->find(AuthorizationCode::class, $identifier); } - public function save(AuthorizationCode $authCode): void + public function save(AuthorizationCodeInterface $authCode): void { $this->entityManager->persist($authCode); $this->entityManager->flush(); diff --git a/src/Manager/Doctrine/ClientManager.php b/src/Manager/Doctrine/ClientManager.php index 154287ef..32d2bfd9 100644 --- a/src/Manager/Doctrine/ClientManager.php +++ b/src/Manager/Doctrine/ClientManager.php @@ -9,6 +9,7 @@ use League\Bundle\OAuth2ServerBundle\Manager\ClientFilter; use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface; use League\Bundle\OAuth2ServerBundle\Model\AbstractClient; +use League\Bundle\OAuth2ServerBundle\Model\ClientInterface; use League\Bundle\OAuth2ServerBundle\OAuth2Events; use League\Bundle\OAuth2ServerBundle\ValueObject\Grant; use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri; @@ -45,14 +46,14 @@ public function __construct( $this->clientFqcn = $clientFqcn; } - public function find(string $identifier): ?AbstractClient + public function find(string $identifier): ?ClientInterface { $repository = $this->entityManager->getRepository($this->clientFqcn); return $repository->findOneBy(['identifier' => $identifier]); } - public function save(AbstractClient $client): void + public function save(ClientInterface $client): void { $event = $this->dispatcher->dispatch(new PreSaveClientEvent($client), OAuth2Events::PRE_SAVE_CLIENT); $client = $event->getClient(); @@ -61,7 +62,7 @@ public function save(AbstractClient $client): void $this->entityManager->flush(); } - public function remove(AbstractClient $client): void + public function remove(ClientInterface $client): void { $this->entityManager->remove($client); $this->entityManager->flush(); diff --git a/src/Manager/Doctrine/RefreshTokenManager.php b/src/Manager/Doctrine/RefreshTokenManager.php index dd9a8ef5..1bd7ce5e 100644 --- a/src/Manager/Doctrine/RefreshTokenManager.php +++ b/src/Manager/Doctrine/RefreshTokenManager.php @@ -7,6 +7,7 @@ use Doctrine\ORM\EntityManagerInterface; use League\Bundle\OAuth2ServerBundle\Manager\RefreshTokenManagerInterface; use League\Bundle\OAuth2ServerBundle\Model\RefreshToken; +use League\Bundle\OAuth2ServerBundle\Model\RefreshTokenInterface; final class RefreshTokenManager implements RefreshTokenManagerInterface { @@ -20,12 +21,12 @@ public function __construct(EntityManagerInterface $entityManager) $this->entityManager = $entityManager; } - public function find(string $identifier): ?RefreshToken + public function find(string $identifier): ?RefreshTokenInterface { return $this->entityManager->find(RefreshToken::class, $identifier); } - public function save(RefreshToken $refreshToken): void + public function save(RefreshTokenInterface $refreshToken): void { $this->entityManager->persist($refreshToken); $this->entityManager->flush(); diff --git a/src/Manager/InMemory/AccessTokenManager.php b/src/Manager/InMemory/AccessTokenManager.php index 220a2392..4ad9f143 100644 --- a/src/Manager/InMemory/AccessTokenManager.php +++ b/src/Manager/InMemory/AccessTokenManager.php @@ -5,12 +5,12 @@ namespace League\Bundle\OAuth2ServerBundle\Manager\InMemory; use League\Bundle\OAuth2ServerBundle\Manager\AccessTokenManagerInterface; -use League\Bundle\OAuth2ServerBundle\Model\AccessToken; +use League\Bundle\OAuth2ServerBundle\Model\AccessTokenInterface; final class AccessTokenManager implements AccessTokenManagerInterface { /** - * @var array + * @var array */ private $accessTokens = []; @@ -25,7 +25,7 @@ public function __construct(bool $persistAccessToken) /** * @psalm-mutation-free */ - public function find(string $identifier): ?AccessToken + public function find(string $identifier): ?AccessTokenInterface { if (!$this->persistAccessToken) { return null; @@ -34,7 +34,7 @@ public function find(string $identifier): ?AccessToken return $this->accessTokens[$identifier] ?? null; } - public function save(AccessToken $accessToken): void + public function save(AccessTokenInterface $accessToken): void { if (!$this->persistAccessToken) { return; @@ -52,7 +52,7 @@ public function clearExpired(): int $count = \count($this->accessTokens); $now = new \DateTimeImmutable(); - $this->accessTokens = array_filter($this->accessTokens, static function (AccessToken $accessToken) use ($now): bool { + $this->accessTokens = array_filter($this->accessTokens, static function (AccessTokenInterface $accessToken) use ($now): bool { return $accessToken->getExpiry() >= $now; }); diff --git a/src/Manager/InMemory/AuthorizationCodeManager.php b/src/Manager/InMemory/AuthorizationCodeManager.php index bf9d619e..e90455dc 100644 --- a/src/Manager/InMemory/AuthorizationCodeManager.php +++ b/src/Manager/InMemory/AuthorizationCodeManager.php @@ -5,24 +5,24 @@ namespace League\Bundle\OAuth2ServerBundle\Manager\InMemory; use League\Bundle\OAuth2ServerBundle\Manager\AuthorizationCodeManagerInterface; -use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCode; +use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCodeInterface; final class AuthorizationCodeManager implements AuthorizationCodeManagerInterface { /** - * @var array + * @var array */ private $authorizationCodes = []; /** * @psalm-mutation-free */ - public function find(string $identifier): ?AuthorizationCode + public function find(string $identifier): ?AuthorizationCodeInterface { return $this->authorizationCodes[$identifier] ?? null; } - public function save(AuthorizationCode $authCode): void + public function save(AuthorizationCodeInterface $authCode): void { $this->authorizationCodes[$authCode->getIdentifier()] = $authCode; } @@ -32,7 +32,7 @@ public function clearExpired(): int $count = \count($this->authorizationCodes); $now = new \DateTimeImmutable(); - $this->authorizationCodes = array_filter($this->authorizationCodes, static function (AuthorizationCode $authorizationCode) use ($now): bool { + $this->authorizationCodes = array_filter($this->authorizationCodes, static function (AuthorizationCodeInterface $authorizationCode) use ($now): bool { return $authorizationCode->getExpiryDateTime() >= $now; }); diff --git a/src/Manager/InMemory/ClientManager.php b/src/Manager/InMemory/ClientManager.php index f2a270a5..3ccf5f22 100644 --- a/src/Manager/InMemory/ClientManager.php +++ b/src/Manager/InMemory/ClientManager.php @@ -7,7 +7,7 @@ use League\Bundle\OAuth2ServerBundle\Event\PreSaveClientEvent; use League\Bundle\OAuth2ServerBundle\Manager\ClientFilter; use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface; -use League\Bundle\OAuth2ServerBundle\Model\AbstractClient; +use League\Bundle\OAuth2ServerBundle\Model\ClientInterface; use League\Bundle\OAuth2ServerBundle\OAuth2Events; use League\Bundle\OAuth2ServerBundle\ValueObject\Grant; use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri; @@ -17,7 +17,7 @@ final class ClientManager implements ClientManagerInterface { /** - * @var array + * @var array */ private $clients = []; @@ -31,12 +31,12 @@ public function __construct(EventDispatcherInterface $dispatcher) $this->dispatcher = $dispatcher; } - public function find(string $identifier): ?AbstractClient + public function find(string $identifier): ?ClientInterface { return $this->clients[$identifier] ?? null; } - public function save(AbstractClient $client): void + public function save(ClientInterface $client): void { $event = $this->dispatcher->dispatch(new PreSaveClientEvent($client), OAuth2Events::PRE_SAVE_CLIENT); $client = $event->getClient(); @@ -44,13 +44,13 @@ public function save(AbstractClient $client): void $this->clients[$client->getIdentifier()] = $client; } - public function remove(AbstractClient $client): void + public function remove(ClientInterface $client): void { unset($this->clients[$client->getIdentifier()]); } /** - * @return list + * @return list */ public function list(?ClientFilter $clientFilter): array { @@ -58,7 +58,7 @@ public function list(?ClientFilter $clientFilter): array return array_values($this->clients); } - return array_values(array_filter($this->clients, static function (AbstractClient $client) use ($clientFilter): bool { + return array_values(array_filter($this->clients, static function (ClientInterface $client) use ($clientFilter): bool { if (!self::passesFilter($client->getGrants(), $clientFilter->getGrants())) { return false; } diff --git a/src/Manager/InMemory/RefreshTokenManager.php b/src/Manager/InMemory/RefreshTokenManager.php index 402984dd..e2060f1f 100644 --- a/src/Manager/InMemory/RefreshTokenManager.php +++ b/src/Manager/InMemory/RefreshTokenManager.php @@ -5,24 +5,24 @@ namespace League\Bundle\OAuth2ServerBundle\Manager\InMemory; use League\Bundle\OAuth2ServerBundle\Manager\RefreshTokenManagerInterface; -use League\Bundle\OAuth2ServerBundle\Model\RefreshToken; +use League\Bundle\OAuth2ServerBundle\Model\RefreshTokenInterface; final class RefreshTokenManager implements RefreshTokenManagerInterface { /** - * @var array + * @var array */ private $refreshTokens = []; /** * @psalm-mutation-free */ - public function find(string $identifier): ?RefreshToken + public function find(string $identifier): ?RefreshTokenInterface { return $this->refreshTokens[$identifier] ?? null; } - public function save(RefreshToken $refreshToken): void + public function save(RefreshTokenInterface $refreshToken): void { $this->refreshTokens[$refreshToken->getIdentifier()] = $refreshToken; } @@ -32,7 +32,7 @@ public function clearExpired(): int $count = \count($this->refreshTokens); $now = new \DateTimeImmutable(); - $this->refreshTokens = array_filter($this->refreshTokens, static function (RefreshToken $refreshToken) use ($now): bool { + $this->refreshTokens = array_filter($this->refreshTokens, static function (RefreshTokenInterface $refreshToken) use ($now): bool { return $refreshToken->getExpiry() >= $now; }); diff --git a/src/Manager/RefreshTokenManagerInterface.php b/src/Manager/RefreshTokenManagerInterface.php index e880c364..5cfcc274 100644 --- a/src/Manager/RefreshTokenManagerInterface.php +++ b/src/Manager/RefreshTokenManagerInterface.php @@ -4,13 +4,13 @@ namespace League\Bundle\OAuth2ServerBundle\Manager; -use League\Bundle\OAuth2ServerBundle\Model\RefreshToken; +use League\Bundle\OAuth2ServerBundle\Model\RefreshTokenInterface; interface RefreshTokenManagerInterface { - public function find(string $identifier): ?RefreshToken; + public function find(string $identifier): ?RefreshTokenInterface; - public function save(RefreshToken $refreshToken): void; + public function save(RefreshTokenInterface $refreshToken): void; public function clearExpired(): int; } diff --git a/src/Model/AbstractClient.php b/src/Model/AbstractClient.php index 5730587b..e98a9a0d 100644 --- a/src/Model/AbstractClient.php +++ b/src/Model/AbstractClient.php @@ -13,7 +13,7 @@ * * @author Mathias Arlaud */ -abstract class AbstractClient +abstract class AbstractClient implements ClientInterface { /** * @var string @@ -73,7 +73,7 @@ public function getName(): string return $this->name; } - public function setName(string $name): self + public function setName(string $name): ClientInterface { $this->name = $name; @@ -106,7 +106,7 @@ public function getRedirectUris(): array return $this->redirectUris; } - public function setRedirectUris(RedirectUri ...$redirectUris): self + public function setRedirectUris(RedirectUri ...$redirectUris): ClientInterface { /** @var list $redirectUris */ $this->redirectUris = $redirectUris; @@ -124,7 +124,7 @@ public function getGrants(): array return $this->grants; } - public function setGrants(Grant ...$grants): self + public function setGrants(Grant ...$grants): ClientInterface { /** @var list $grants */ $this->grants = $grants; @@ -142,7 +142,7 @@ public function getScopes(): array return $this->scopes; } - public function setScopes(Scope ...$scopes): self + public function setScopes(Scope ...$scopes): ClientInterface { /** @var list $scopes */ $this->scopes = $scopes; @@ -158,7 +158,7 @@ public function isActive(): bool return $this->active; } - public function setActive(bool $active): self + public function setActive(bool $active): ClientInterface { $this->active = $active; @@ -181,7 +181,7 @@ public function isPlainTextPkceAllowed(): bool return $this->allowPlainTextPkce; } - public function setAllowPlainTextPkce(bool $allowPlainTextPkce): self + public function setAllowPlainTextPkce(bool $allowPlainTextPkce): ClientInterface { $this->allowPlainTextPkce = $allowPlainTextPkce; diff --git a/src/Model/AccessToken.php b/src/Model/AccessToken.php index 6f7ab87a..ad034bfd 100644 --- a/src/Model/AccessToken.php +++ b/src/Model/AccessToken.php @@ -6,7 +6,7 @@ use League\Bundle\OAuth2ServerBundle\ValueObject\Scope; -class AccessToken +class AccessToken implements AccessTokenInterface { /** * @var string @@ -24,7 +24,7 @@ class AccessToken private $userIdentifier; /** - * @var AbstractClient + * @var ClientInterface */ private $client; @@ -46,7 +46,7 @@ class AccessToken public function __construct( string $identifier, \DateTimeInterface $expiry, - AbstractClient $client, + ClientInterface $client, ?string $userIdentifier, array $scopes ) { @@ -92,7 +92,7 @@ public function getUserIdentifier(): ?string /** * @psalm-mutation-free */ - public function getClient(): AbstractClient + public function getClient(): ClientInterface { return $this->client; } @@ -115,7 +115,7 @@ public function isRevoked(): bool return $this->revoked; } - public function revoke(): self + public function revoke(): AccessTokenInterface { $this->revoked = true; diff --git a/src/Model/AccessTokenInterface.php b/src/Model/AccessTokenInterface.php new file mode 100644 index 00000000..f1056830 --- /dev/null +++ b/src/Model/AccessTokenInterface.php @@ -0,0 +1,26 @@ +client; } @@ -115,7 +115,7 @@ public function isRevoked(): bool return $this->revoked; } - public function revoke(): self + public function revoke(): AuthorizationCodeInterface { $this->revoked = true; diff --git a/src/Model/AuthorizationCodeInterface.php b/src/Model/AuthorizationCodeInterface.php new file mode 100644 index 00000000..fc8abe86 --- /dev/null +++ b/src/Model/AuthorizationCodeInterface.php @@ -0,0 +1,26 @@ +accessToken; } @@ -76,7 +76,7 @@ public function isRevoked(): bool return $this->revoked; } - public function revoke(): self + public function revoke(): RefreshTokenInterface { $this->revoked = true; diff --git a/src/Model/RefreshTokenInterface.php b/src/Model/RefreshTokenInterface.php new file mode 100644 index 00000000..46ead423 --- /dev/null +++ b/src/Model/RefreshTokenInterface.php @@ -0,0 +1,22 @@ +