vendor/web-token/jwt-bundle/DataCollector/JWECollector.php line 116

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Jose\Bundle\JoseFramework\DataCollector;
  4. use Jose\Bundle\JoseFramework\Event\JWEBuiltFailureEvent;
  5. use Jose\Bundle\JoseFramework\Event\JWEBuiltSuccessEvent;
  6. use Jose\Bundle\JoseFramework\Event\JWEDecryptionFailureEvent;
  7. use Jose\Bundle\JoseFramework\Event\JWEDecryptionSuccessEvent;
  8. use Jose\Component\Encryption\Compression\CompressionMethodManagerFactory;
  9. use Jose\Component\Encryption\JWEBuilder;
  10. use Jose\Component\Encryption\JWEDecrypter;
  11. use Jose\Component\Encryption\JWELoader;
  12. use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\VarDumper\Cloner\Data;
  17. use Symfony\Component\VarDumper\Cloner\VarCloner;
  18. use Throwable;
  19. class JWECollector implements CollectorEventSubscriberInterface
  20. {
  21.     /**
  22.      * @var array<Data>
  23.      */
  24.     private array $jweDecryptionSuccesses = [];
  25.     /**
  26.      * @var array<Data>
  27.      */
  28.     private array $jweDecryptionFailures = [];
  29.     /**
  30.      * @var array<Data>
  31.      */
  32.     private array $jweBuiltSuccesses = [];
  33.     /**
  34.      * @var array<Data>
  35.      */
  36.     private array $jweBuiltFailures = [];
  37.     /**
  38.      * @var array<JWEBuilder>
  39.      */
  40.     private array $jweBuilders = [];
  41.     /**
  42.      * @var array<JWEDecrypter>
  43.      */
  44.     private array $jweDecrypters = [];
  45.     /**
  46.      * @var array<JWELoader>
  47.      */
  48.     private array $jweLoaders = [];
  49.     public function __construct(
  50.         private readonly ?CompressionMethodManagerFactory $compressionMethodManagerFactory null,
  51.         private readonly ?JWESerializerManagerFactory $jweSerializerManagerFactory null
  52.     ) {
  53.     }
  54.     /**
  55.      * @param array<string, mixed> $data
  56.      */
  57.     public function collect(array &$dataRequest $requestResponse $response, ?Throwable $exception null): void
  58.     {
  59.         $this->collectSupportedCompressionMethods($data);
  60.         $this->collectSupportedJWESerializations($data);
  61.         $this->collectSupportedJWEBuilders($data);
  62.         $this->collectSupportedJWEDecrypters($data);
  63.         $this->collectSupportedJWELoaders($data);
  64.         $this->collectEvents($data);
  65.     }
  66.     public function addJWEBuilder(string $idJWEBuilder $jweBuilder): void
  67.     {
  68.         $this->jweBuilders[$id] = $jweBuilder;
  69.     }
  70.     public function addJWEDecrypter(string $idJWEDecrypter $jweDecrypter): void
  71.     {
  72.         $this->jweDecrypters[$id] = $jweDecrypter;
  73.     }
  74.     public function addJWELoader(string $idJWELoader $jweLoader): void
  75.     {
  76.         $this->jweLoaders[$id] = $jweLoader;
  77.     }
  78.     public static function getSubscribedEvents(): array
  79.     {
  80.         return [
  81.             JWEDecryptionSuccessEvent::class => ['catchJweDecryptionSuccess'],
  82.             JWEDecryptionFailureEvent::class => ['catchJweDecryptionFailure'],
  83.             JWEBuiltSuccessEvent::class => ['catchJweBuiltSuccess'],
  84.             JWEBuiltFailureEvent::class => ['catchJweBuiltFailure'],
  85.         ];
  86.     }
  87.     public function catchJweDecryptionSuccess(JWEDecryptionSuccessEvent $event): void
  88.     {
  89.         $cloner = new VarCloner();
  90.         $this->jweDecryptionSuccesses[] = $cloner->cloneVar($event);
  91.     }
  92.     public function catchJweDecryptionFailure(JWEDecryptionFailureEvent $event): void
  93.     {
  94.         $cloner = new VarCloner();
  95.         $this->jweDecryptionFailures[] = $cloner->cloneVar($event);
  96.     }
  97.     public function catchJweBuiltSuccess(JWEBuiltSuccessEvent $event): void
  98.     {
  99.         $cloner = new VarCloner();
  100.         $this->jweBuiltSuccesses[] = $cloner->cloneVar($event);
  101.     }
  102.     public function catchJweBuiltFailure(JWEBuiltFailureEvent $event): void
  103.     {
  104.         $cloner = new VarCloner();
  105.         $this->jweBuiltFailures[] = $cloner->cloneVar($event);
  106.     }
  107.     /**
  108.      * @param array<string, array<string, mixed>> $data
  109.      * @deprecated This method is deprecated and will be removed in v4.0. Compression is not recommended for JWE.
  110.      */
  111.     private function collectSupportedCompressionMethods(array &$data): void
  112.     {
  113.         $data['jwe']['compression_methods'] = [];
  114.         if ($this->compressionMethodManagerFactory === null) {
  115.             return;
  116.         }
  117.         $compressionMethods $this->compressionMethodManagerFactory->all();
  118.         foreach ($compressionMethods as $alias => $compressionMethod) {
  119.             $data['jwe']['compression_methods'][$alias] = $compressionMethod->name();
  120.         }
  121.     }
  122.     /**
  123.      * @param array<string, array<string, mixed>> $data
  124.      */
  125.     private function collectSupportedJWESerializations(array &$data): void
  126.     {
  127.         $data['jwe']['jwe_serialization'] = [];
  128.         if ($this->jweSerializerManagerFactory === null) {
  129.             return;
  130.         }
  131.         $serializers $this->jweSerializerManagerFactory->all();
  132.         foreach ($serializers as $serializer) {
  133.             $data['jwe']['jwe_serialization'][$serializer->name()] = $serializer->displayName();
  134.         }
  135.     }
  136.     /**
  137.      * @param array<string, array<string, mixed>> $data
  138.      */
  139.     private function collectSupportedJWEBuilders(array &$data): void
  140.     {
  141.         $data['jwe']['jwe_builders'] = [];
  142.         foreach ($this->jweBuilders as $id => $jweBuilder) {
  143.             $data['jwe']['jwe_builders'][$id] = [
  144.                 'key_encryption_algorithms' => $jweBuilder->getKeyEncryptionAlgorithmManager()
  145.                     ->list(),
  146.                 'content_encryption_algorithms' => $jweBuilder->getContentEncryptionAlgorithmManager()
  147.                     ->list(),
  148.                 'compression_methods' => $jweBuilder->getCompressionMethodManager()
  149.                     ->list(),
  150.             ];
  151.         }
  152.     }
  153.     /**
  154.      * @param array<string, array<string, mixed>> $data
  155.      */
  156.     private function collectSupportedJWEDecrypters(array &$data): void
  157.     {
  158.         $data['jwe']['jwe_decrypters'] = [];
  159.         foreach ($this->jweDecrypters as $id => $jweDecrypter) {
  160.             $data['jwe']['jwe_decrypters'][$id] = [
  161.                 'key_encryption_algorithms' => $jweDecrypter->getKeyEncryptionAlgorithmManager()
  162.                     ->list(),
  163.                 'content_encryption_algorithms' => $jweDecrypter->getContentEncryptionAlgorithmManager()
  164.                     ->list(),
  165.                 'compression_methods' => $jweDecrypter->getCompressionMethodManager()
  166.                     ->list(),
  167.             ];
  168.         }
  169.     }
  170.     /**
  171.      * @param array<string, array<string, mixed>> $data
  172.      */
  173.     private function collectSupportedJWELoaders(array &$data): void
  174.     {
  175.         $data['jwe']['jwe_loaders'] = [];
  176.         foreach ($this->jweLoaders as $id => $jweLoader) {
  177.             $data['jwe']['jwe_loaders'][$id] = [
  178.                 'serializers' => $jweLoader->getSerializerManager()
  179.                     ->names(),
  180.                 'key_encryption_algorithms' => $jweLoader->getJweDecrypter()
  181.                     ->getKeyEncryptionAlgorithmManager()
  182.                     ->list(),
  183.                 'content_encryption_algorithms' => $jweLoader->getJweDecrypter()
  184.                     ->getContentEncryptionAlgorithmManager()
  185.                     ->list(),
  186.                 'compression_methods' => $jweLoader->getJweDecrypter()
  187.                     ->getCompressionMethodManager()
  188.                     ->list(),
  189.             ];
  190.         }
  191.     }
  192.     /**
  193.      * @param array<string, array<string, mixed>> $data
  194.      */
  195.     private function collectEvents(array &$data): void
  196.     {
  197.         $data['jwe']['events'] = [
  198.             'decryption_success' => $this->jweDecryptionSuccesses,
  199.             'decryption_failure' => $this->jweDecryptionFailures,
  200.             'built_success' => $this->jweBuiltSuccesses,
  201.             'built_failure' => $this->jweBuiltFailures,
  202.         ];
  203.     }
  204. }