Failed Conditions
Push — v7 ( b3d8c9...4e8990 )
by Florent
02:22
created

JSONFlattenedSerializer::displayName()   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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Encryption\JWE;
18
use Jose\Component\Encryption\Recipient;
19
20
/**
21
 * Class JSONFlattenedSerializer.
22
 */
23
final class JSONFlattenedSerializer implements JWESerializerInterface
24
{
25
    public const NAME = 'jwe_json_flattened';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function displayName(): string
31
    {
32
        return 'JWE JSON Flattened';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function name(): string
39
    {
40
        return self::NAME;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function serialize(JWE $jwe, ?int $recipientIndex = null): string
47
    {
48
        if (null === $recipientIndex) {
49
            $recipientIndex = 0;
50
        }
51
        $recipient = $jwe->getRecipient($recipientIndex);
52
        $data = [
53
            'ciphertext' => Base64Url::encode($jwe->getCiphertext()),
54
            'iv' => Base64Url::encode($jwe->getIV()),
55
            'tag' => Base64Url::encode($jwe->getTag()),
56
        ];
57
        if (null !== $jwe->getAAD()) {
58
            $data['aad'] = Base64Url::encode($jwe->getAAD());
59
        }
60
        if (!empty($jwe->getSharedProtectedHeaders())) {
61
            $data['protected'] = $jwe->getEncodedSharedProtectedHeaders();
62
        }
63
        if (!empty($jwe->getSharedHeaders())) {
64
            $data['unprotected'] = $jwe->getSharedHeaders();
65
        }
66
        if (!empty($recipient->getHeaders())) {
67
            $data['header'] = $recipient->getHeaders();
68
        }
69
        if (null !== $recipient->getEncryptedKey()) {
70
            $data['encrypted_key'] = Base64Url::encode($recipient->getEncryptedKey());
71
        }
72
73
        return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function unserialize(string $input): JWE
80
    {
81
        $data = json_decode($input, true);
82
        if (!is_array($data) || !array_key_exists('ciphertext', $data) || array_key_exists('recipients', $data)) {
83
            throw new \InvalidArgumentException('Unsupported input.');
84
        }
85
86
        $ciphertext = Base64Url::decode($data['ciphertext']);
87
        $iv = Base64Url::decode($data['iv']);
88
        $tag = Base64Url::decode($data['tag']);
89
        $aad = array_key_exists('aad', $data) ? Base64Url::decode($data['aad']) : null;
90
        $encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
91
        $sharedProtectedHeader = $encodedSharedProtectedHeader ? json_decode(Base64Url::decode($encodedSharedProtectedHeader), true) : [];
92
        $sharedHeader = array_key_exists('unprotected', $data) ? $data['unprotected'] : [];
93
        $encryptedKey = array_key_exists('encrypted_key', $data) ? Base64Url::decode($data['encrypted_key']) : null;
94
        $header = array_key_exists('header', $data) ? $data['header'] : [];
95
96
        return JWE::create(
97
            $ciphertext,
98
            $iv,
99
            $tag,
100
            $aad,
101
            $sharedHeader,
102
            $sharedProtectedHeader,
103
            $encodedSharedProtectedHeader,
104
            [Recipient::create($header, $encryptedKey)]);
105
    }
106
}
107