88use BabDev \WebSocketBundle \Authentication \Storage \TokenStorage ;
99use Psr \Log \LoggerAwareInterface ;
1010use Psr \Log \LoggerAwareTrait ;
11+ use Symfony \Component \Security \Core \Authentication \Token \NullToken ;
12+ use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
1113
1214final 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}
0 commit comments