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

checkHasSharedProtectedHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
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\Encryption\JWE;
18
use Jose\Component\Encryption\Recipient;
19
20
/**
21
 * Class CompactSerializer.
22
 */
23
final class CompactSerializer extends AbstractSerializer
24
{
25
    public const NAME = 'jwe_compact';
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 (null === $recipientIndex) {
41
            $recipientIndex = 0;
42
        }
43
        $recipient = $jwe->getRecipient($recipientIndex);
44
45
        $this->checkHasNoAAD($jwe);
46
        $this->checkHasSharedProtectedHeaders($jwe);
47
        $this->checkRecipientHasNoHeaders($jwe, $recipientIndex);
48
49
        return sprintf(
50
            '%s.%s.%s.%s.%s',
51
            $jwe->getEncodedSharedProtectedHeaders(),
52
            Base64Url::encode(null === $recipient->getEncryptedKey() ? '' : $recipient->getEncryptedKey()),
53
            Base64Url::encode(null === $jwe->getIV() ? '' : $jwe->getIV()),
54
            Base64Url::encode($jwe->getCiphertext()),
55
            Base64Url::encode(null === $jwe->getTag() ? '' : $jwe->getTag())
56
        );
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function unserialize(string $input): JWE
63
    {
64
        $parts = explode('.', $input);
65
        if (5 !== count($parts)) {
66
            throw new \InvalidArgumentException('Unsupported input');
67
        }
68
69
        $encodedSharedProtectedHeader = $parts[0];
70
        $sharedProtectedHeader = json_decode(Base64Url::decode($encodedSharedProtectedHeader));
71
        $encryptedKey = empty($parts[1]) ? null : Base64Url::decode($parts[1]);
72
        $iv = Base64Url::decode($parts[2]);
73
        $ciphertext = Base64Url::decode($parts[3]);
74
        $tag = Base64Url::decode($parts[4]);
75
76
        return JWE::create(
77
            $ciphertext,
78
            $iv,
79
            $tag,
80
            null,
81
            [],
82
            $sharedProtectedHeader,
83
            $encodedSharedProtectedHeader,
84
            [Recipient::create([], $encryptedKey)]);
85
    }
86
87
    /**
88
     * @param JWE $jwe
89
     */
90
    private function checkHasNoAAD(JWE $jwe)
91
    {
92
        if (!empty($jwe->getAAD())) {
93
            throw new \LogicException('This JWE has AAD and cannot be converted into Compact JSON.');
94
        }
95
    }
96
97
    /**
98
     * @param JWE $jwe
99
     * @param int $id
100
     */
101
    private function checkRecipientHasNoHeaders(JWE $jwe, int $id)
102
    {
103
        if (!empty($jwe->getSharedHeaders()) || !empty($jwe->getRecipient($id)->getHeaders())) {
104
            throw new \LogicException('This JWE has shared headers or recipient headers and cannot be converted into Compact JSON.');
105
        }
106
    }
107
108
    /**
109
     * @param JWE $jwe
110
     */
111
    private function checkHasSharedProtectedHeaders(JWE $jwe)
112
    {
113
        if (empty($jwe->getSharedProtectedHeaders())) {
114
            throw new \LogicException('This JWE does not have shared protected headers and cannot be converted into Compact JSON.');
115
        }
116
    }
117
}
118