|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * The MIT License (MIT) |
| 7 | + * |
| 8 | + * Copyright (c) 2014-2018 Spomky-Labs |
| 9 | + * |
| 10 | + * This software may be modified and distributed under the terms |
| 11 | + * of the MIT license. See the LICENSE file for details. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Jose\Bundle\JoseFramework\Serializer; |
| 15 | + |
| 16 | +use Jose\Component\Encryption\Serializer\JWESerializerManager; |
| 17 | +use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory; |
| 18 | +use Symfony\Component\Serializer\Encoder\DecoderInterface; |
| 19 | +use Symfony\Component\Serializer\Encoder\EncoderInterface; |
| 20 | +use Symfony\Component\Serializer\Exception\NotEncodableValueException; |
| 21 | +use Symfony\Component\Serializer\Exception\UnexpectedValueException; |
| 22 | + |
| 23 | +class JWEEncoder implements EncoderInterface, DecoderInterface |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var JWESerializerManager |
| 27 | + */ |
| 28 | + protected $serializerManager; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + JWESerializerManagerFactory $serializerManagerFactory, |
| 32 | + ?JWESerializerManager $serializerManager = null |
| 33 | + ) { |
| 34 | + if (null === $serializerManager) { |
| 35 | + $serializerManager = $serializerManagerFactory->create($serializerManagerFactory->names()); |
| 36 | + } |
| 37 | + |
| 38 | + $this->serializerManager = $serializerManager; |
| 39 | + } |
| 40 | + |
| 41 | + public function supportsEncoding($format) |
| 42 | + { |
| 43 | + return \in_array(mb_strtolower($format), $this->serializerManager->list(), true); |
| 44 | + } |
| 45 | + |
| 46 | + public function supportsDecoding($format) |
| 47 | + { |
| 48 | + return $this->supportsEncoding($format); |
| 49 | + } |
| 50 | + |
| 51 | + public function encode($data, $format, array $context = []) |
| 52 | + { |
| 53 | + try { |
| 54 | + return $this->serializerManager->serialize(mb_strtolower($format), $data, $this->getRecipientIndex($context)); |
| 55 | + } catch (\Exception $ex) { |
| 56 | + $message = sprintf('Cannot encode JWE to %s format.', $format); |
| 57 | + |
| 58 | + if (\class_exists('Symfony\Component\Serializer\Exception\NotEncodableValueException')) { |
| 59 | + throw new NotEncodableValueException($message, 0, $ex); |
| 60 | + } |
| 61 | + |
| 62 | + throw new UnexpectedValueException($message, 0, $ex); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public function decode($data, $format, array $context = []) |
| 67 | + { |
| 68 | + try { |
| 69 | + return $this->serializerManager->unserialize($data); |
| 70 | + } catch (\Exception $ex) { |
| 71 | + $message = sprintf('Cannot decode JWE from %s format.', $format); |
| 72 | + |
| 73 | + if (\class_exists('Symfony\Component\Serializer\Exception\NotEncodableValueException')) { |
| 74 | + throw new NotEncodableValueException($message, 0, $ex); |
| 75 | + } |
| 76 | + |
| 77 | + throw new UnexpectedValueException($message, 0, $ex); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Get JWE recipient index from context. |
| 83 | + */ |
| 84 | + protected function getRecipientIndex(array $context): int |
| 85 | + { |
| 86 | + $recipientIndex = 0; |
| 87 | + |
| 88 | + if (isset($context['recipient_index']) && \is_int($context['recipient_index'])) { |
| 89 | + $recipientIndex = $context['recipient_index']; |
| 90 | + } |
| 91 | + |
| 92 | + return $recipientIndex; |
| 93 | + } |
| 94 | +} |
0 commit comments