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

JSONGeneralSerializer::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 JSONGeneralSerializer.
22
 */
23
final class JSONGeneralSerializer implements JWESerializerInterface
24
{
25
    public const NAME = 'jwe_json_general';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function displayName(): string
31
    {
32
        return 'JWE JSON General';
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 (0 === $jwe->countRecipients()) {
49
            throw new \LogicException('No recipient.');
50
        }
51
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
        $data['recipients'] = [];
67
        foreach ($jwe->getRecipients() as $recipient) {
68
            $temp = [];
69
            if (!empty($recipient->getHeaders())) {
70
                $temp['header'] = $recipient->getHeaders();
71
            }
72
            if (null !== $recipient->getEncryptedKey()) {
73
                $temp['encrypted_key'] = Base64Url::encode($recipient->getEncryptedKey());
74
            }
75
            $data['recipients'][] = $temp;
76
        }
77
78
        return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function unserialize(string $input): JWE
85
    {
86
        $data = json_decode($input, true);
87
        if (!is_array($data) || !array_key_exists('ciphertext', $data) || !array_key_exists('recipients', $data)) {
88
            throw new \InvalidArgumentException('Unsupported input.');
89
        }
90
91
        $ciphertext = Base64Url::decode($data['ciphertext']);
92
        $iv = Base64Url::decode($data['iv']);
93
        $tag = Base64Url::decode($data['tag']);
94
        $aad = array_key_exists('aad', $data) ? Base64Url::decode($data['aad']) : null;
95
        $encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
96
        $sharedProtectedHeader = $encodedSharedProtectedHeader ? json_decode(Base64Url::decode($encodedSharedProtectedHeader), true) : [];
97
        $sharedHeader = array_key_exists('unprotected', $data) ? $data['unprotected'] : [];
98
        $recipients = [];
99
        foreach ($data['recipients'] as $recipient) {
100
            $encryptedKey = array_key_exists('encrypted_key', $recipient) ? Base64Url::decode($recipient['encrypted_key']) : null;
101
            $header = array_key_exists('header', $recipient) ? $recipient['header'] : [];
102
            $recipients[] = Recipient::create($header, $encryptedKey);
103
        }
104
105
        return JWE::create(
106
            $ciphertext,
107
            $iv,
108
            $tag,
109
            $aad,
110
            $sharedHeader,
111
            $sharedProtectedHeader,
112
            $encodedSharedProtectedHeader,
113
            $recipients);
114
    }
115
}
116