Failed Conditions
Push — v7 ( 3dace9...35cf1c )
by Florent
02:18
created

JSONFlattenedSerializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\Component\Encryption\Serializer;
15
16
use Base64Url\Base64Url;
17
use Jose\Component\Core\Converter\JsonConverterInterface;
18
use Jose\Component\Encryption\JWE;
19
use Jose\Component\Encryption\Recipient;
20
21
/**
22
 * Class JSONFlattenedSerializer.
23
 */
24
final class JSONFlattenedSerializer implements JWESerializerInterface
25
{
26
    public const NAME = 'jwe_json_flattened';
27
28
    /**
29
     * @var JsonConverterInterface
30
     */
31
    private $jsonConverter;
32
33
    /**
34
     * JSONFlattenedSerializer constructor.
35
     *
36
     * @param JsonConverterInterface $jsonConverter
37
     */
38
    public function __construct(JsonConverterInterface $jsonConverter)
39
    {
40
        $this->jsonConverter = $jsonConverter;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function displayName(): string
47
    {
48
        return 'JWE JSON Flattened';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function name(): string
55
    {
56
        return self::NAME;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function serialize(JWE $jwe, ?int $recipientIndex = null): string
63
    {
64
        if (null === $recipientIndex) {
65
            $recipientIndex = 0;
66
        }
67
        $recipient = $jwe->getRecipient($recipientIndex);
68
        $data = [
69
            'ciphertext' => Base64Url::encode($jwe->getCiphertext()),
70
            'iv' => Base64Url::encode($jwe->getIV()),
71
            'tag' => Base64Url::encode($jwe->getTag()),
72
        ];
73
        if (null !== $jwe->getAAD()) {
74
            $data['aad'] = Base64Url::encode($jwe->getAAD());
75
        }
76
        if (!empty($jwe->getSharedProtectedHeaders())) {
77
            $data['protected'] = $jwe->getEncodedSharedProtectedHeaders();
78
        }
79
        if (!empty($jwe->getSharedHeaders())) {
80
            $data['unprotected'] = $jwe->getSharedHeaders();
81
        }
82
        if (!empty($recipient->getHeaders())) {
83
            $data['header'] = $recipient->getHeaders();
84
        }
85
        if (null !== $recipient->getEncryptedKey()) {
86
            $data['encrypted_key'] = Base64Url::encode($recipient->getEncryptedKey());
87
        }
88
89
        return $this->jsonConverter->encode($data);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function unserialize(string $input): JWE
96
    {
97
        $data = $this->jsonConverter->decode($input);
98
        if (!is_array($data) || !array_key_exists('ciphertext', $data) || array_key_exists('recipients', $data)) {
99
            throw new \InvalidArgumentException('Unsupported input.');
100
        }
101
102
        $ciphertext = Base64Url::decode($data['ciphertext']);
103
        $iv = Base64Url::decode($data['iv']);
104
        $tag = Base64Url::decode($data['tag']);
105
        $aad = array_key_exists('aad', $data) ? Base64Url::decode($data['aad']) : null;
106
        $encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
107
        $sharedProtectedHeader = $encodedSharedProtectedHeader ? $this->jsonConverter->decode(Base64Url::decode($encodedSharedProtectedHeader)) : [];
108
        $sharedHeader = array_key_exists('unprotected', $data) ? $data['unprotected'] : [];
109
        $encryptedKey = array_key_exists('encrypted_key', $data) ? Base64Url::decode($data['encrypted_key']) : null;
110
        $header = array_key_exists('header', $data) ? $data['header'] : [];
111
112
        return JWE::create(
113
            $ciphertext,
114
            $iv,
115
            $tag,
116
            $aad,
117
            $sharedHeader,
118
            $sharedProtectedHeader,
119
            $encodedSharedProtectedHeader,
120
            [Recipient::create($header, $encryptedKey)]);
121
    }
122
}
123