Skip to content

Commit a657dba

Browse files
committed
Close the connection on an authentication exception
1 parent 94f9e52 commit a657dba

6 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/Authentication/Authenticator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace BabDev\WebSocketBundle\Authentication;
44

55
use BabDev\WebSocket\Server\Connection;
6+
use BabDev\WebSocketBundle\Authentication\Exception\AuthenticationException;
67

78
interface Authenticator
89
{
910
/**
1011
* Attempts to authenticate the current connection.
12+
*
13+
* @throws AuthenticationException if there was an error while trying to authenticate the user
1114
*/
1215
public function authenticate(Connection $connection): void;
1316
}

src/Authentication/DefaultAuthenticator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BabDev\WebSocketBundle\Authentication;
44

55
use BabDev\WebSocket\Server\Connection;
6+
use BabDev\WebSocketBundle\Authentication\Exception\AuthenticationException;
67
use BabDev\WebSocketBundle\Authentication\Provider\AuthenticationProvider;
78
use BabDev\WebSocketBundle\Authentication\Storage\TokenStorage;
89
use Psr\Log\LoggerAwareInterface;
@@ -22,6 +23,8 @@ public function __construct(
2223

2324
/**
2425
* Attempts to authenticate the current connection.
26+
*
27+
* @throws AuthenticationException if there was an error while trying to authenticate the user
2528
*/
2629
public function authenticate(Connection $connection): void
2730
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BabDev\WebSocketBundle\Authentication\Exception;
4+
5+
use BabDev\WebSocket\Server\WebSocketException;
6+
7+
class AuthenticationException extends \RuntimeException implements WebSocketException {}

src/Authentication/Provider/AuthenticationProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BabDev\WebSocketBundle\Authentication\Provider;
44

55
use BabDev\WebSocket\Server\Connection;
6+
use BabDev\WebSocketBundle\Authentication\Exception\AuthenticationException;
67
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
78

89
interface AuthenticationProvider
@@ -16,6 +17,8 @@ public function supports(Connection $connection): bool;
1617
* Attempts to authenticate the current connection.
1718
*
1819
* Implementations can assume this method will only be executed when supports() is true.
20+
*
21+
* @throws AuthenticationException if there was an error while trying to authenticate the user
1922
*/
2023
public function authenticate(Connection $connection): TokenInterface;
2124
}

src/Authentication/Provider/SessionAuthenticationProvider.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace BabDev\WebSocketBundle\Authentication\Provider;
44

55
use BabDev\WebSocket\Server\Connection;
6+
use BabDev\WebSocket\Server\WebSocketException;
7+
use BabDev\WebSocketBundle\Authentication\Exception\AuthenticationException;
68
use BabDev\WebSocketBundle\Authentication\Storage\TokenStorage;
79
use Psr\Log\LoggerAwareInterface;
810
use Psr\Log\LoggerAwareTrait;
@@ -36,9 +38,19 @@ public function supports(Connection $connection): bool
3638
return $attributeStore->has('session') && $attributeStore->get('session') instanceof SessionInterface;
3739
}
3840

41+
/**
42+
* @throws AuthenticationException if there was an error while trying to authenticate the user
43+
*/
3944
public function authenticate(Connection $connection): TokenInterface
4045
{
41-
$token = $this->getToken($connection);
46+
try {
47+
$token = $this->getToken($connection);
48+
} catch (WebSocketException $exception) {
49+
// Out-of-the-box, we'll get a WebSocketException from our read-only session handler if there was an issue grabbing the session data, so focus only on the component's exceptions
50+
$this->logger?->error('Could not authenticate user.', ['exception' => $exception]);
51+
52+
throw new AuthenticationException('Could not authenticate user.', previous: $exception);
53+
}
4254

4355
$storageId = $this->tokenStorage->generateStorageId($connection);
4456

src/Server/Middleware/AuthenticateUser.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace BabDev\WebSocketBundle\Server\Middleware;
44

55
use BabDev\WebSocket\Server\Connection;
6+
use BabDev\WebSocket\Server\Connection\ClosesConnectionWithResponse;
67
use BabDev\WebSocket\Server\ServerMiddleware;
78
use BabDev\WebSocketBundle\Authentication\Authenticator;
9+
use BabDev\WebSocketBundle\Authentication\Exception\AuthenticationException;
810
use BabDev\WebSocketBundle\Authentication\Storage\Exception\StorageError;
911
use BabDev\WebSocketBundle\Authentication\Storage\Exception\TokenNotFound;
1012
use BabDev\WebSocketBundle\Authentication\Storage\TokenStorage;
@@ -16,6 +18,7 @@
1618
*/
1719
final class AuthenticateUser implements ServerMiddleware, LoggerAwareInterface
1820
{
21+
use ClosesConnectionWithResponse;
1922
use LoggerAwareTrait;
2023

2124
public function __construct(
@@ -29,7 +32,13 @@ public function __construct(
2932
*/
3033
public function onOpen(Connection $connection): void
3134
{
32-
$this->authenticator->authenticate($connection);
35+
try {
36+
$this->authenticator->authenticate($connection);
37+
} catch (AuthenticationException $exception) {
38+
$this->close($connection, 401);
39+
40+
throw $exception;
41+
}
3342

3443
$this->middleware->onOpen($connection);
3544
}

0 commit comments

Comments
 (0)