<?php
 declare(strict_types=1); namespace Jose\Component\Encryption; use Jose\Component\Checker\HeaderCheckerManager; use Jose\Component\Core\JWK; use Jose\Component\Core\JWKSet; use Jose\Component\Encryption\Serializer\JWESerializerManager; use RuntimeException; use Throwable; class JWELoader { public function __construct( private readonly JWESerializerManager $serializerManager, private readonly JWEDecrypter $jweDecrypter, private readonly ?HeaderCheckerManager $headerCheckerManager ) { } public function getJweDecrypter(): JWEDecrypter { return $this->jweDecrypter; } public function getHeaderCheckerManager(): ?HeaderCheckerManager { return $this->headerCheckerManager; } public function getSerializerManager(): JWESerializerManager { return $this->serializerManager; } public function loadAndDecryptWithKey(string $token, JWK $key, ?int &$recipient): JWE { $keyset = new JWKSet([$key]); return $this->loadAndDecryptWithKeySet($token, $keyset, $recipient); } public function loadAndDecryptWithKeySet(string $token, JWKSet $keyset, ?int &$recipient): JWE { try { $jwe = $this->serializerManager->unserialize($token); $nbRecipients = $jwe->countRecipients(); for ($i = 0; $i < $nbRecipients; ++$i) { if ($this->processRecipient($jwe, $keyset, $i)) { $recipient = $i; return $jwe; } } } catch (Throwable) { } throw new RuntimeException('Unable to load and decrypt the token.'); } private function processRecipient(JWE &$jwe, JWKSet $keyset, int $recipient): bool { try { if ($this->headerCheckerManager !== null) { $this->headerCheckerManager->check($jwe, $recipient); } return $this->jweDecrypter->decryptUsingKeySet($jwe, $keyset, $recipient); } catch (Throwable) { return false; } } } 