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

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.      */
  110.     private function collectSupportedCompressionMethods(array &$data): void
  111.     {
  112.         $data['jwe']['compression_methods'] = [];
  113.         if ($this->compressionMethodManagerFactory === null) {
  114.             return;
  115.         }
  116.         $compressionMethods $this->compressionMethodManagerFactory->all();
  117.         foreach ($compressionMethods as $alias => $compressionMethod) {
  118.             $data['jwe']['compression_methods'][$alias] = $compressionMethod->name();
  119.         }
  120.     }
  121.     /**
  122.      * @param array<string, array<string, mixed>> $data
  123.      */
  124.     private function collectSupportedJWESerializations(array &$data): void
  125.     {
  126.         $data['jwe']['jwe_serialization'] = [];
  127.         if ($this->jweSerializerManagerFactory === null) {
  128.             return;
  129.         }
  130.         $serializers $this->jweSerializerManagerFactory->all();
  131.         foreach ($serializers as $serializer) {
  132.             $data['jwe']['jwe_serialization'][$serializer->name()] = $serializer->displayName();
  133.         }
  134.     }
  135.     /**
  136.      * @param array<string, array<string, mixed>> $data
  137.      */
  138.     private function collectSupportedJWEBuilders(array &$data): void
  139.     {
  140.         $data['jwe']['jwe_builders'] = [];
  141.         foreach ($this->jweBuilders as $id => $jweBuilder) {
  142.             $data['jwe']['jwe_builders'][$id] = [
  143.                 'key_encryption_algorithms' => $jweBuilder->getKeyEncryptionAlgorithmManager()
  144.                     ->list(),
  145.                 'content_encryption_algorithms' => $jweBuilder->getContentEncryptionAlgorithmManager()
  146.                     ->list(),
  147.                 'compression_methods' => $jweBuilder->getCompressionMethodManager()
  148.                     ->list(),
  149.             ];
  150.         }
  151.     }
  152.     /**
  153.      * @param array<string, array<string, mixed>> $data
  154.      */
  155.     private function collectSupportedJWEDecrypters(array &$data): void
  156.     {
  157.         $data['jwe']['jwe_decrypters'] = [];
  158.         foreach ($this->jweDecrypters as $id => $jweDecrypter) {
  159.             $data['jwe']['jwe_decrypters'][$id] = [
  160.                 'key_encryption_algorithms' => $jweDecrypter->getKeyEncryptionAlgorithmManager()
  161.                     ->list(),
  162.                 'content_encryption_algorithms' => $jweDecrypter->getContentEncryptionAlgorithmManager()
  163.                     ->list(),
  164.                 'compression_methods' => $jweDecrypter->getCompressionMethodManager()
  165.                     ->list(),
  166.             ];
  167.         }
  168.     }
  169.     /**
  170.      * @param array<string, array<string, mixed>> $data
  171.      */
  172.     private function collectSupportedJWELoaders(array &$data): void
  173.     {
  174.         $data['jwe']['jwe_loaders'] = [];
  175.         foreach ($this->jweLoaders as $id => $jweLoader) {
  176.             $data['jwe']['jwe_loaders'][$id] = [
  177.                 'serializers' => $jweLoader->getSerializerManager()
  178.                     ->names(),
  179.                 'key_encryption_algorithms' => $jweLoader->getJweDecrypter()
  180.                     ->getKeyEncryptionAlgorithmManager()
  181.                     ->list(),
  182.                 'content_encryption_algorithms' => $jweLoader->getJweDecrypter()
  183.                     ->getContentEncryptionAlgorithmManager()
  184.                     ->list(),
  185.                 'compression_methods' => $jweLoader->getJweDecrypter()
  186.                     ->getCompressionMethodManager()
  187.                     ->list(),
  188.             ];
  189.         }
  190.     }
  191.     /**
  192.      * @param array<string, array<string, mixed>> $data
  193.      */
  194.     private function collectEvents(array &$data): void
  195.     {
  196.         $data['jwe']['events'] = [
  197.             'decryption_success' => $this->jweDecryptionSuccesses,
  198.             'decryption_failure' => $this->jweDecryptionFailures,
  199.             'built_success' => $this->jweBuiltSuccesses,
  200.             'built_failure' => $this->jweBuiltFailures,
  201.         ];
  202.     }
  203. }