From 193916c7e731401de10248688f643f82dd62534b Mon Sep 17 00:00:00 2001 From: Vragov Roman Date: Mon, 24 Jun 2024 13:44:33 +0300 Subject: [PATCH] Change \OAuth\Server\Storage\ClientStorageInterface --- composer.json | 1 - src/Command/CreateClientCommand.php | 3 ++- src/DependencyInjection/OAuthServerExtension.php | 5 +++++ .../Repository/ClientRepositoryInterface.php | 2 +- src/Doctrine/Storage/ClientStorage.php | 4 ++-- src/Model/ClientInterface.php | 2 ++ src/OAuthServerBundle.php | 7 +++++++ src/Resources/routing/token.php | 15 --------------- src/Server/Storage/ClientStorageInterface.php | 2 +- tests/Stub/Entity/Client.php | 7 +++++++ tests/Stub/Repository/ClientRepositoryStub.php | 7 +++---- 11 files changed, 30 insertions(+), 25 deletions(-) delete mode 100644 src/Resources/routing/token.php diff --git a/composer.json b/composer.json index 0664f31..e4b2a8a 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,6 @@ "symfony/event-dispatcher": "^5.4|^6.0|^7.0", "symfony/http-foundation": "^5.4|^6.0|^7.0", "symfony/http-kernel": "^5.4|^6.0|^7.0", - "symfony/routing": "^5.4|^6.0|^7.0", "symfony/security-bundle": "^5.4|^6.0|^7.0" }, "require-dev": { diff --git a/src/Command/CreateClientCommand.php b/src/Command/CreateClientCommand.php index b85ed64..b278171 100644 --- a/src/Command/CreateClientCommand.php +++ b/src/Command/CreateClientCommand.php @@ -5,6 +5,7 @@ namespace OAuth\Command; use OAuth\Server\Storage\ClientStorageInterface; +use OAuth\Utils\Random; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -52,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->title('Client Credentials'); - $client = $this->clientStorage->createClient(); + $client = $this->clientStorage->createClient(Random::generateToken(), Random::generateToken()); $client->setRedirectUris($input->getOption('redirect-uri')); $client->setGrantTypes($input->getOption('grant-type')); diff --git a/src/DependencyInjection/OAuthServerExtension.php b/src/DependencyInjection/OAuthServerExtension.php index a97407c..1413d8a 100644 --- a/src/DependencyInjection/OAuthServerExtension.php +++ b/src/DependencyInjection/OAuthServerExtension.php @@ -37,4 +37,9 @@ public function load(array $configs, ContainerBuilder $container): void $loader->load('command.php'); $loader->load('security.php'); } + + public function getAlias(): string + { + return 'oauth_server'; + } } diff --git a/src/Doctrine/Repository/ClientRepositoryInterface.php b/src/Doctrine/Repository/ClientRepositoryInterface.php index d2717dd..c41395e 100644 --- a/src/Doctrine/Repository/ClientRepositoryInterface.php +++ b/src/Doctrine/Repository/ClientRepositoryInterface.php @@ -8,7 +8,7 @@ interface ClientRepositoryInterface { - public function createClient(): ClientInterface; + public function createClient(string $publicId, ?string $secret): ClientInterface; public function updateClient(ClientInterface $client): void; diff --git a/src/Doctrine/Storage/ClientStorage.php b/src/Doctrine/Storage/ClientStorage.php index d5c6e4b..afcdfd4 100644 --- a/src/Doctrine/Storage/ClientStorage.php +++ b/src/Doctrine/Storage/ClientStorage.php @@ -26,9 +26,9 @@ public function __construct( $this->repository = $repository; } - public function createClient(): ClientInterface + public function createClient(string $publicId, ?string $secret): ClientInterface { - return $this->repository->createClient(); + return $this->repository->createClient($publicId, $secret); } public function updateClient(ClientInterface $client): void diff --git a/src/Model/ClientInterface.php b/src/Model/ClientInterface.php index dd283d6..593dbec 100644 --- a/src/Model/ClientInterface.php +++ b/src/Model/ClientInterface.php @@ -8,6 +8,8 @@ interface ClientInterface extends UserInterface { + public function setPublicId(string $publicId): self; + public function getPublicId(): string; public function setSecret(?string $secret): self; diff --git a/src/OAuthServerBundle.php b/src/OAuthServerBundle.php index 9b50f28..4980fe1 100644 --- a/src/OAuthServerBundle.php +++ b/src/OAuthServerBundle.php @@ -5,9 +5,11 @@ namespace OAuth; use OAuth\DependencyInjection\Compiler\GrantExtensionsCompilerPass; +use OAuth\DependencyInjection\OAuthServerExtension; use OAuth\DependencyInjection\Security\Factory\OAuthFactory; use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\HttpKernel\Bundle\Bundle; class OAuthServerBundle extends Bundle @@ -21,4 +23,9 @@ public function build(ContainerBuilder $container): void $extension->addAuthenticatorFactory(new OAuthFactory()); $container->addCompilerPass(new GrantExtensionsCompilerPass()); } + + public function getContainerExtension(): ?ExtensionInterface + { + return new OAuthServerExtension(); + } } diff --git a/src/Resources/routing/token.php b/src/Resources/routing/token.php deleted file mode 100644 index ce7174e..0000000 --- a/src/Resources/routing/token.php +++ /dev/null @@ -1,15 +0,0 @@ -add('oauth_server_token', '/oauth/v2/token') - ->controller([TokenController::class, 'token']) - ->methods([Request::METHOD_GET, Request::METHOD_HEAD]) - ; -}; diff --git a/src/Server/Storage/ClientStorageInterface.php b/src/Server/Storage/ClientStorageInterface.php index d11f40a..298b134 100644 --- a/src/Server/Storage/ClientStorageInterface.php +++ b/src/Server/Storage/ClientStorageInterface.php @@ -8,7 +8,7 @@ interface ClientStorageInterface { - public function createClient(): ClientInterface; + public function createClient(string $publicId, ?string $secret): ClientInterface; public function updateClient(ClientInterface $client): void; diff --git a/tests/Stub/Entity/Client.php b/tests/Stub/Entity/Client.php index 287140c..3c14cfb 100644 --- a/tests/Stub/Entity/Client.php +++ b/tests/Stub/Entity/Client.php @@ -18,6 +18,13 @@ public function __construct(string $publicId) $this->randomId = $publicId; } + public function setPublicId(string $publicId): self + { + $this->randomId = $publicId; + + return $this; + } + public function getPublicId(): string { return $this->randomId; diff --git a/tests/Stub/Repository/ClientRepositoryStub.php b/tests/Stub/Repository/ClientRepositoryStub.php index 6d07122..781bb0e 100644 --- a/tests/Stub/Repository/ClientRepositoryStub.php +++ b/tests/Stub/Repository/ClientRepositoryStub.php @@ -7,17 +7,16 @@ use OAuth\Doctrine\Repository\ClientRepositoryInterface; use OAuth\Model\ClientInterface; use OAuth\Tests\Stub\Entity\Client; -use OAuth\Utils\Random; class ClientRepositoryStub implements ClientRepositoryInterface { /** @var array */ private array $tokens = []; - public function createClient(): ClientInterface + public function createClient(string $publicId, ?string $secret): ClientInterface { - $client = new Client(Random::generateToken()); - $client->setSecret(Random::generateToken()); + $client = new Client($publicId); + $client->setSecret($secret); $this->tokens[$client->getPublicId()] = $client;