Failed Conditions
Push — v7 ( 0b8993...932046 )
by Florent
03:54
created

JSONGeneralSerializer::name()   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 extends AbstractSerializer
24
{
25
    public const NAME = 'jwe_json_general';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function name(): string
31
    {
32
        return self::NAME;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function serialize(JWE $jwe, ?int $recipientIndex = null): string
39
    {
40
        if (0 === $jwe->countRecipients()) {
41
            throw new \LogicException('No recipient.');
42
        }
43
44
        $data = [
45
            'ciphertext' => Base64Url::encode($jwe->getCiphertext()),
46
            'iv' => Base64Url::encode($jwe->getIV()),
47
            'tag' => Base64Url::encode($jwe->getTag()),
48
        ];
49
        if (null !== $jwe->getAAD()) {
50
            $data['aad'] = Base64Url::encode($jwe->getAAD());
51
        }
52
        if (!empty($jwe->getSharedProtectedHeaders())) {
53
            $data['protected'] = $jwe->getEncodedSharedProtectedHeaders();
54
        }
55
        if (!empty($jwe->getSharedHeaders())) {
56
            $data['unprotected'] = $jwe->getSharedHeaders();
57
        }
58
        $data['recipients'] = [];
59
        foreach ($jwe->getRecipients() as $recipient) {
60
            $temp = [];
61
            if (!empty($recipient->getHeaders())) {
62
                $temp['header'] = $recipient->getHeaders();
63
            }
64
            if (null !== $recipient->getEncryptedKey()) {
65
                $temp['encrypted_key'] = Base64Url::encode($recipient->getEncryptedKey());
66
            }
67
            $data['recipients'][] = $temp;
68
        }
69
70
        return json_encode($data);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function unserialize(string $input): JWE
77
    {
78
        $data = json_decode($input, true);
79
        if (!is_array($data) || !array_key_exists('ciphertext', $data) || !array_key_exists('recipients', $data)) {
80
            throw new \InvalidArgumentException('Unsupported input.');
81
        }
82
83
        $ciphertext = Base64Url::decode($data['ciphertext']);
84
        $iv = Base64Url::decode($data['iv']);
85
        $tag = Base64Url::decode($data['tag']);
86
        $aad = array_key_exists('aad', $data) ? Base64Url::decode($data['aad']) : null;
87
        $encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
88
        $sharedProtectedHeader = $encodedSharedProtectedHeader ? json_decode(Base64Url::decode($encodedSharedProtectedHeader), true) : [];
89
        $sharedHeader = array_key_exists('unprotected', $data) ? $data['unprotected'] : [];
90
        $recipients = [];
91
        foreach ($data['recipients'] as $recipient) {
92
            $encryptedKey = array_key_exists('encrypted_key', $recipient) ? Base64Url::decode($recipient['encrypted_key']) : null;
93
            $header = array_key_exists('header', $recipient) ? $recipient['header'] : [];
94
            $recipients[] = Recipient::create($header, $encryptedKey);
95
        }
96
97
        return JWE::create(
98
            $ciphertext,
99
            $iv,
100
            $tag,
101
            $aad,
102
            $sharedHeader,
103
            $sharedProtectedHeader,
104
            $encodedSharedProtectedHeader,
105
            $recipients);
106
    }
107
}
108