From 70022b6d6f911ae6a5b8784ae6d88e88f52f633c Mon Sep 17 00:00:00 2001 From: Michael Babker Date: Thu, 14 Sep 2023 10:54:01 -0400 Subject: [PATCH] Only provide the in-memory storage driver --- config/services.php | 11 +-- docs/authentication.md | 37 +-------- docs/default-configuration.md | 10 --- .../Storage/Driver/PsrCacheStorageDriver.php | 75 ------------------- .../BabDevWebSocketExtension.php | 26 ------- src/DependencyInjection/Configuration.php | 35 --------- .../Driver/PsrCacheStorageDriverTest.php | 57 -------------- .../BabDevWebSocketExtensionTest.php | 50 ------------- .../DependencyInjection/ConfigurationTest.php | 2 +- 9 files changed, 5 insertions(+), 298 deletions(-) delete mode 100644 src/Authentication/Storage/Driver/PsrCacheStorageDriver.php delete mode 100644 tests/Authentication/Storage/Driver/PsrCacheStorageDriverTest.php diff --git a/config/services.php b/config/services.php index d7f51f3..b644387 100644 --- a/config/services.php +++ b/config/services.php @@ -26,7 +26,7 @@ use BabDev\WebSocketBundle\Authentication\DefaultAuthenticator; use BabDev\WebSocketBundle\Authentication\Provider\SessionAuthenticationProvider; use BabDev\WebSocketBundle\Authentication\Storage\Driver\InMemoryStorageDriver; -use BabDev\WebSocketBundle\Authentication\Storage\Driver\PsrCacheStorageDriver; +use BabDev\WebSocketBundle\Authentication\Storage\Driver\StorageDriver; use BabDev\WebSocketBundle\Authentication\Storage\DriverBackedTokenStorage; use BabDev\WebSocketBundle\Authentication\Storage\TokenStorage; use BabDev\WebSocketBundle\Authentication\StorageBackedConnectionRepository; @@ -105,16 +105,11 @@ ; $services->set('babdev_websocket_server.authentication.storage.driver.in_memory', InMemoryStorageDriver::class); - - $services->set('babdev_websocket_server.authentication.storage.driver.psr_cache', PsrCacheStorageDriver::class) - ->args([ - abstract_arg('cache pool'), - ]) - ; + $services->alias(StorageDriver::class, 'babdev_websocket_server.authentication.storage.driver.in_memory'); $services->set('babdev_websocket_server.authentication.token_storage.driver', DriverBackedTokenStorage::class) ->args([ - service('babdev_websocket_server.authentication.storage.driver'), + service(StorageDriver::class), ]) ->call('setLogger', [ service('logger'), diff --git a/docs/authentication.md b/docs/authentication.md index 47a7f83..2f32cb1 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -102,42 +102,7 @@ class Kernel extends BaseKernel After authentication is complete, the token is stored in a `BabDev\WebSocketBundle\Authentication\Storage\TokenStorage` instance. The default implementation uses a `BabDev\WebSocketBundle\Authentication\Storage\Driver\StorageDriver` as an abstraction layer for where authentication tokens are stored. -By default, the bundle uses an in-memory storage driver. The storage driver can be configured with the `storage` section of the authentication configuration. - -### In-Memory Storage - -The below example represents the default configuration for the in-memory storage driver. - -```yaml -babdev_websocket: - authentication: - storage: - type: in_memory -``` - -### Cache Storage - -A cache pool can be used as a storage driver by setting the storage type to `psr_cache` and specifying the cache pool that should be used. - -```yaml -babdev_websocket: - authentication: - storage: - type: psr_cache - pool: 'cache.websocket' -``` - -### Service Storage - -You can create your own implementation of the storage driver interface and use that service by setting the storage type to `service` and specifying the container service ID to use. - -```yaml -babdev_websocket: - authentication: - storage: - type: storage - id: 'app.websocket.storage.driver' -``` +By default, the bundle provides and uses an in-memory storage driver. You can provide your own driver implementation by creating a class implementing the driver interface and updating the service container to point the '`BabDev\WebSocketBundle\Authentication\Storage\Driver\StorageDriver`' alias to your implementation. ## Fetching Tokens diff --git a/docs/default-configuration.md b/docs/default-configuration.md index c885dca..ed3876f 100644 --- a/docs/default-configuration.md +++ b/docs/default-configuration.md @@ -8,16 +8,6 @@ babdev_websocket: # The firewalls from which the session token can be used; can be an array, a string, or null to allow all firewalls. firewalls: null - storage: - - # The type of storage for the websocket server authentication tokens. - type: in_memory # One of "in_memory"; "psr_cache"; "service", Required - - # The cache pool to use when using the PSR cache storage. - pool: null - - # The service ID to use when using the service storage. - id: null server: # An identifier for the websocket server, disclosed in the response to the WELCOME message from a WAMP client. diff --git a/src/Authentication/Storage/Driver/PsrCacheStorageDriver.php b/src/Authentication/Storage/Driver/PsrCacheStorageDriver.php deleted file mode 100644 index 2044ba5..0000000 --- a/src/Authentication/Storage/Driver/PsrCacheStorageDriver.php +++ /dev/null @@ -1,75 +0,0 @@ -cache->clear(); - } - - /** - * @throws StorageError if the token could not be deleted from storage - */ - public function delete(string $id): bool - { - try { - return $this->cache->deleteItem($id); - } catch (CacheException $exception) { - throw new StorageError(sprintf('Could not delete token for ID "%s" from storage.', $id), $exception->getCode(), $exception); - } - } - - /** - * @throws StorageError if the token could not be read from storage - * @throws TokenNotFound if a token for the given ID is not found - */ - public function get(string $id): TokenInterface - { - try { - $item = $this->cache->getItem($id); - - if (!$item->isHit()) { - throw new TokenNotFound(sprintf('Token for ID "%s" not found.', $id)); - } - - /** @var TokenInterface */ - return $item->get(); - } catch (CacheException $exception) { - throw new StorageError(sprintf('Could not load token for ID "%s" from storage.', $id), $exception->getCode(), $exception); - } - } - - /** - * @throws StorageError if the storage could not be checked for token presence - */ - public function has(string $id): bool - { - try { - return $this->cache->hasItem($id); - } catch (CacheException $exception) { - throw new StorageError(sprintf('Could not check storage for token with ID "%s".', $id), $exception->getCode(), $exception); - } - } - - public function store(string $id, TokenInterface $token): bool - { - try { - $item = $this->cache->getItem($id); - $item->set($token); - - return $this->cache->save($item); - } catch (CacheException $exception) { - throw new StorageError(sprintf('Could not store token for ID "%s" to storage.', $id), $exception->getCode(), $exception); - } - } -} diff --git a/src/DependencyInjection/BabDevWebSocketExtension.php b/src/DependencyInjection/BabDevWebSocketExtension.php index 8c73dbf..ac06eb0 100644 --- a/src/DependencyInjection/BabDevWebSocketExtension.php +++ b/src/DependencyInjection/BabDevWebSocketExtension.php @@ -3,7 +3,6 @@ namespace BabDev\WebSocketBundle\DependencyInjection; use BabDev\WebSocketBundle\Attribute\AsMessageHandler; -use BabDev\WebSocketBundle\Authentication\Storage\Driver\StorageDriver; use BabDev\WebSocketBundle\DependencyInjection\Factory\Authentication\AuthenticationProviderFactory; use BabDev\WebSocketBundle\PeriodicManager\PeriodicManager; use Doctrine\DBAL\Connection; @@ -77,31 +76,6 @@ private function registerAuthenticationConfiguration(array $mergedConfig, Contai $container->getDefinition('babdev_websocket_server.authentication.authenticator') ->replaceArgument(0, new IteratorArgument($authenticators)); - - $storageId = null; - - switch ($mergedConfig['authentication']['storage']['type']) { - case Configuration::AUTHENTICATION_STORAGE_TYPE_IN_MEMORY: - $storageId = 'babdev_websocket_server.authentication.storage.driver.in_memory'; - - break; - - case Configuration::AUTHENTICATION_STORAGE_TYPE_PSR_CACHE: - $storageId = 'babdev_websocket_server.authentication.storage.driver.psr_cache'; - - $container->getDefinition($storageId) - ->replaceArgument(0, new Reference($mergedConfig['authentication']['storage']['pool'])); - - break; - - case Configuration::AUTHENTICATION_STORAGE_TYPE_SERVICE: - $storageId = $mergedConfig['authentication']['storage']['id']; - - break; - } - - $container->setAlias('babdev_websocket_server.authentication.storage.driver', $storageId); - $container->setAlias(StorageDriver::class, $storageId); } private function registerServerConfiguration(array $mergedConfig, ContainerBuilder $container): void diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 2fd3546..cdb9b6b 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -14,10 +14,6 @@ final class Configuration implements ConfigurationInterface { - public const AUTHENTICATION_STORAGE_TYPE_IN_MEMORY = 'in_memory'; - public const AUTHENTICATION_STORAGE_TYPE_PSR_CACHE = 'psr_cache'; - public const AUTHENTICATION_STORAGE_TYPE_SERVICE = 'service'; - /** * @param list $authenticationProviderFactories */ @@ -42,37 +38,6 @@ private function addAuthenticationSection(ArrayNodeDefinition $rootNode): void ->addDefaultsIfNotSet(); $this->addAuthenticationProvidersSection($authenticationNode); - - $authenticationNode->children() - ->arrayNode('storage') - ->addDefaultsIfNotSet() - ->children() - ->enumNode('type') - ->isRequired() - ->defaultValue(self::AUTHENTICATION_STORAGE_TYPE_IN_MEMORY) - ->info('The type of storage for the websocket server authentication tokens.') - ->values([self::AUTHENTICATION_STORAGE_TYPE_IN_MEMORY, self::AUTHENTICATION_STORAGE_TYPE_PSR_CACHE, self::AUTHENTICATION_STORAGE_TYPE_SERVICE]) - ->end() - ->scalarNode('pool') - ->defaultNull() - ->info('The cache pool to use when using the PSR cache storage.') - ->end() - ->scalarNode('id') - ->defaultNull() - ->info('The service ID to use when using the service storage.') - ->end() - ->end() - ->validate() - ->ifTrue(static fn (array $config): bool => ('' === $config['pool'] || null === $config['pool']) && self::AUTHENTICATION_STORAGE_TYPE_PSR_CACHE === $config['type']) - ->thenInvalid('A cache pool must be set when using the PSR cache storage') - ->end() - ->validate() - ->ifTrue(static fn (array $config): bool => ('' === $config['id'] || null === $config['id']) && self::AUTHENTICATION_STORAGE_TYPE_SERVICE === $config['type']) - ->thenInvalid('A service ID must be set when using the service storage') - ->end() - ->end() - ->end() - ->end(); } private function addAuthenticationProvidersSection(ArrayNodeDefinition $authenticationNode): void diff --git a/tests/Authentication/Storage/Driver/PsrCacheStorageDriverTest.php b/tests/Authentication/Storage/Driver/PsrCacheStorageDriverTest.php deleted file mode 100644 index 4315bdc..0000000 --- a/tests/Authentication/Storage/Driver/PsrCacheStorageDriverTest.php +++ /dev/null @@ -1,57 +0,0 @@ -cache = new ArrayAdapter(); - - $this->driver = new PsrCacheStorageDriver($this->cache); - } - - public function testTokenIsManagedInStorage(): void - { - $user = new InMemoryUser('user', 'password'); - $token = new UsernamePasswordToken($user, 'main', ['ROLE_USER']); - - self::assertFalse($this->driver->has('abc')); - self::assertTrue($this->driver->store('abc', $token)); - self::assertTrue($this->driver->has('abc')); - - $storedToken = $this->driver->get('abc'); - - self::assertSame($token->getUserIdentifier(), $storedToken->getUserIdentifier(), 'The token retrieved from storage should be comparable to the originally saved token.'); - - self::assertTrue($this->driver->delete('abc')); - - try { - $this->driver->get('abc'); - - self::fail('The get() method should throw an exception when the ID is not present.'); - } catch (TokenNotFound) { - // Successful test case - } - - self::assertTrue($this->driver->store('abc', $token)); - self::assertTrue($this->driver->has('abc')); - - $this->driver->clear(); - - self::assertFalse($this->driver->has('abc')); - } -} diff --git a/tests/DependencyInjection/BabDevWebSocketExtensionTest.php b/tests/DependencyInjection/BabDevWebSocketExtensionTest.php index 28412fa..f0c1415 100644 --- a/tests/DependencyInjection/BabDevWebSocketExtensionTest.php +++ b/tests/DependencyInjection/BabDevWebSocketExtensionTest.php @@ -4,7 +4,6 @@ use BabDev\WebSocketBundle\Authentication\Storage\Driver\StorageDriver; use BabDev\WebSocketBundle\DependencyInjection\BabDevWebSocketExtension; -use BabDev\WebSocketBundle\DependencyInjection\Configuration; use BabDev\WebSocketBundle\DependencyInjection\Factory\Authentication\SessionAuthenticationProviderFactory; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerHasParameterConstraint; @@ -80,7 +79,6 @@ public function testContainerIsLoadedWithValidConfiguration(): void } self::assertThat($this->container->findDefinition('babdev_websocket_server.server.server_middleware.establish_websocket_connection'), new LogicalNot(new DefinitionHasMethodCallConstraint('enableKeepAlive'))); - $this->assertContainerBuilderHasAlias('babdev_websocket_server.authentication.storage.driver', 'babdev_websocket_server.authentication.storage.driver.in_memory'); $this->assertContainerBuilderHasAlias(StorageDriver::class, 'babdev_websocket_server.authentication.storage.driver.in_memory'); $this->assertContainerBuilderNotHasService('babdev_websocket_server.periodic_manager.ping_doctrine_dbal_connections'); $this->assertContainerBuilderNotHasService('babdev_websocket_server.server.server_middleware.initialize_session'); @@ -164,54 +162,6 @@ public function testContainerIsLoadedWithSessionAuthenticationProviderConfigured ); } - public function testContainerIsLoadedWithPsrCacheAuthenticationStorageConfigured(): void - { - $this->load([ - 'authentication' => [ - 'storage' => [ - 'type' => Configuration::AUTHENTICATION_STORAGE_TYPE_PSR_CACHE, - 'pool' => 'cache.websocket', - ], - ], - 'server' => [ - 'uri' => 'tcp://127.0.0.1:8080', - 'router' => [ - 'resource' => '%kernel.project_dir%/config/websocket_router.php', - ], - ], - ]); - - $this->assertContainerBuilderHasAlias('babdev_websocket_server.authentication.storage.driver', 'babdev_websocket_server.authentication.storage.driver.psr_cache'); - $this->assertContainerBuilderHasAlias(StorageDriver::class, 'babdev_websocket_server.authentication.storage.driver.psr_cache'); - - $this->assertContainerBuilderHasServiceDefinitionWithArgument( - 'babdev_websocket_server.authentication.storage.driver.psr_cache', - 0, - new Reference('cache.websocket') - ); - } - - public function testContainerIsLoadedWithServiceAuthenticationStorageConfigured(): void - { - $this->load([ - 'authentication' => [ - 'storage' => [ - 'type' => Configuration::AUTHENTICATION_STORAGE_TYPE_SERVICE, - 'id' => 'app.authentication.storage.driver.custom', - ], - ], - 'server' => [ - 'uri' => 'tcp://127.0.0.1:8080', - 'router' => [ - 'resource' => '%kernel.project_dir%/config/websocket_router.php', - ], - ], - ]); - - $this->assertContainerBuilderHasAlias('babdev_websocket_server.authentication.storage.driver', 'app.authentication.storage.driver.custom'); - $this->assertContainerBuilderHasAlias(StorageDriver::class, 'app.authentication.storage.driver.custom'); - } - public function testContainerIsLoadedWithConfiguredSessionFactory(): void { $this->load([ diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index f600ec0..c7e0540 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -33,7 +33,7 @@ public function testConfigurationIsValidWithServerConfiguration(): void ], [ 'server' => ['identity' => Server::VERSION, 'uri' => 'tcp://127.0.0.1:8080', 'context' => ['tls' => ['verify_peer' => false]], 'allowed_origins' => ['example.com'], 'blocked_ip_addresses' => ['192.168.1.1'], 'keepalive' => ['enabled' => true, 'interval' => 60], 'periodic' => ['dbal' => ['connections' => ['database_connection'], 'interval' => 60]], 'router' => ['resource' => '%kernel.project_dir%/config/websocket_router.php'], 'session' => ['handler_service_id' => 'session.handler.test']], - 'authentication' => ['storage' => ['type' => Configuration::AUTHENTICATION_STORAGE_TYPE_IN_MEMORY, 'pool' => null, 'id' => null]], + 'authentication' => [], ], ); }