Skip to content

Commit

Permalink
Only provide the in-memory storage driver
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Sep 14, 2023
1 parent b17a66e commit 70022b6
Show file tree
Hide file tree
Showing 9 changed files with 5 additions and 298 deletions.
11 changes: 3 additions & 8 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'),
Expand Down
37 changes: 1 addition & 36 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 0 additions & 10 deletions docs/default-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
75 changes: 0 additions & 75 deletions src/Authentication/Storage/Driver/PsrCacheStorageDriver.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/DependencyInjection/BabDevWebSocketExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
35 changes: 0 additions & 35 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthenticationProviderFactory> $authenticationProviderFactories
*/
Expand All @@ -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
Expand Down
57 changes: 0 additions & 57 deletions tests/Authentication/Storage/Driver/PsrCacheStorageDriverTest.php

This file was deleted.

50 changes: 0 additions & 50 deletions tests/DependencyInjection/BabDevWebSocketExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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([
Expand Down
2 changes: 1 addition & 1 deletion tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
],
);
}
Expand Down

0 comments on commit 70022b6

Please sign in to comment.