Skip to content

Commit e45fe2b

Browse files
committed
When no provider supports a connection, store a null token to storage; this implicitly promotes the fallback behavior from the session authentication provider up a layer
1 parent 7620660 commit e45fe2b

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

src/Authentication/ProviderBackedAuthenticator.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use BabDev\WebSocketBundle\Authentication\Storage\TokenStorage;
99
use Psr\Log\LoggerAwareInterface;
1010
use Psr\Log\LoggerAwareTrait;
11+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
12+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1113

1214
final class ProviderBackedAuthenticator implements Authenticator, LoggerAwareInterface
1315
{
@@ -24,6 +26,8 @@ public function __construct(
2426
/**
2527
* Attempts to authenticate the current connection.
2628
*
29+
* When no provider supports authenticating the connection, the authenticator will store a null token to storage.
30+
*
2731
* @throws AuthenticationException if there was an error while trying to authenticate the user
2832
*/
2933
public function authenticate(Connection $connection): void
@@ -35,22 +39,29 @@ public function authenticate(Connection $connection): void
3539
continue;
3640
}
3741

38-
$token = $provider->authenticate($connection);
42+
$this->storeTokenToStorage($connection, $provider->authenticate($connection));
3943

40-
$id = $this->tokenStorage->generateStorageId($connection);
44+
return;
45+
}
4146

42-
$this->tokenStorage->addToken($id, $token);
47+
$this->logger?->debug('No authentication provider supported the connection, using a null token.');
4348

44-
$this->logger?->info(
45-
'User "{user}" authenticated to websocket server',
46-
[
47-
'resource_id' => $connection->getAttributeStore()->get('resource_id'),
48-
'storage_id' => $id,
49-
'user' => $token->getUserIdentifier() ?: 'Unknown User',
50-
],
51-
);
49+
$this->storeTokenToStorage($connection, new NullToken());
50+
}
5251

53-
break;
54-
}
52+
private function storeTokenToStorage(Connection $connection, TokenInterface $token): void
53+
{
54+
$id = $this->tokenStorage->generateStorageId($connection);
55+
56+
$this->tokenStorage->addToken($id, $token);
57+
58+
$this->logger?->info(
59+
'User "{user}" authenticated to websocket server',
60+
[
61+
'resource_id' => $connection->getAttributeStore()->get('resource_id'),
62+
'storage_id' => $id,
63+
'user' => $token->getUserIdentifier() ?: 'Unknown User',
64+
],
65+
);
5566
}
5667
}

tests/Authentication/ProviderBackedAuthenticatorTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use BabDev\WebSocketBundle\Authentication\Storage\TokenStorage;
99
use PHPUnit\Framework\MockObject\MockObject;
1010
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
1112
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1213

1314
final class ProviderBackedAuthenticatorTest extends TestCase
@@ -19,11 +20,13 @@ public function testTheAuthenticatorDoesNotAuthenticateAConnectionWhenItHasNoPro
1920

2021
/** @var MockObject&TokenStorage $tokenStorage */
2122
$tokenStorage = $this->createMock(TokenStorage::class);
22-
$tokenStorage->expects(self::never())
23-
->method('generateStorageId');
23+
$tokenStorage->expects(self::once())
24+
->method('generateStorageId')
25+
->willReturn('conn-123');
2426

25-
$tokenStorage->expects(self::never())
26-
->method('addToken');
27+
$tokenStorage->expects(self::once())
28+
->method('addToken')
29+
->with('conn-123', self::isInstanceOf(NullToken::class));
2730

2831
new ProviderBackedAuthenticator([], $tokenStorage)->authenticate($connection);
2932
}

0 commit comments

Comments
 (0)