vendor/gos/web-socket-bundle/src/Client/Auth/WebsocketAuthenticationProvider.php line 13

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Gos\Bundle\WebSocketBundle\Client\Auth;
  3. use Gos\Bundle\WebSocketBundle\Client\ClientStorageInterface;
  4. use Psr\Log\LoggerAwareInterface;
  5. use Psr\Log\LoggerAwareTrait;
  6. use Ratchet\ConnectionInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  8. use Symfony\Component\Security\Core\Authentication\Token\NullToken;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. trigger_deprecation('gos/web-socket-bundle''3.11''The "%s" class is deprecated and will be removed in 4.0, use the new websocket authentication API instead.'WebsocketAuthenticationProvider::class);
  11. /**
  12.  * @deprecated to be removed in 4.0, use the new websocket authentication API instead
  13.  */
  14. final class WebsocketAuthenticationProvider implements WebsocketAuthenticationProviderInterfaceLoggerAwareInterface
  15. {
  16.     use LoggerAwareTrait;
  17.     private ClientStorageInterface $clientStorage;
  18.     /**
  19.      * @var string[]
  20.      */
  21.     private array $firewalls = [];
  22.     /**
  23.      * @param string[] $firewalls
  24.      */
  25.     public function __construct(ClientStorageInterface $clientStorage, array $firewalls = [])
  26.     {
  27.         $this->clientStorage $clientStorage;
  28.         $this->firewalls $firewalls;
  29.     }
  30.     public function authenticate(ConnectionInterface $conn): TokenInterface
  31.     {
  32.         if (=== \count($this->firewalls) && 'ws_firewall' === $this->firewalls[0]) {
  33.             if (null !== $this->logger) {
  34.                 $this->logger->warning(
  35.                     sprintf(
  36.                         'User firewall is not configured, we have set %s by default',
  37.                         $this->firewalls[0]
  38.                     )
  39.                 );
  40.             }
  41.         }
  42.         $token $this->getToken($conn);
  43.         $identifier $this->clientStorage->getStorageId($conn);
  44.         $this->clientStorage->addClient($identifier$token);
  45.         if (null !== $this->logger) {
  46.             $this->logger->info(
  47.                 sprintf(
  48.                     '%s connected',
  49.                     method_exists($token'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername()
  50.                 ),
  51.                 [
  52.                     'connection_id' => $conn->resourceId,
  53.                     'session_id' => $conn->WAMP->sessionId,
  54.                     'storage_id' => $identifier,
  55.                 ]
  56.             );
  57.         }
  58.         return $token;
  59.     }
  60.     private function getToken(ConnectionInterface $connection): TokenInterface
  61.     {
  62.         $token null;
  63.         if (isset($connection->Session) && $connection->Session) {
  64.             foreach ($this->firewalls as $firewall) {
  65.                 if (false !== $serializedToken $connection->Session->get('_security_'.$firewallfalse)) {
  66.                     /** @var TokenInterface $token */
  67.                     $token unserialize($serializedToken);
  68.                     break;
  69.                 }
  70.             }
  71.         }
  72.         if (null === $token) {
  73.             if (class_exists(NullToken::class)) {
  74.                 $token = new NullToken();
  75.             } else {
  76.                 $token = new AnonymousToken($this->firewalls[0], 'anon-'.$connection->WAMP->sessionId);
  77.             }
  78.         }
  79.         return $token;
  80.     }
  81. }