JWE   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 0
dl 0
loc 259
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getPayload() 0 4 1
A withPayload() 0 7 1
A countRecipients() 0 4 1
A isEncrypted() 0 4 1
A getRecipients() 0 4 1
A getRecipient() 0 8 2
A getCiphertext() 0 4 1
A getAAD() 0 4 1
A getIV() 0 4 1
A getTag() 0 4 1
A getEncodedSharedProtectedHeader() 0 4 1
A getSharedProtectedHeader() 0 4 1
A getSharedProtectedHeaderParameter() 0 8 2
A hasSharedProtectedHeaderParameter() 0 4 1
A getSharedHeader() 0 4 1
A getSharedHeaderParameter() 0 8 2
A hasSharedHeaderParameter() 0 4 1
A split() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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;
15
16
use InvalidArgumentException;
17
use Jose\Component\Core\JWT;
18
19
class JWE implements JWT
20
{
21
    /**
22
     * @var Recipient[]
23
     */
24
    private $recipients = [];
25
26
    /**
27
     * @var null|string
28
     */
29
    private $ciphertext;
30
31
    /**
32
     * @var string
33
     */
34
    private $iv;
35
36
    /**
37
     * @var null|string
38
     */
39
    private $aad;
40
41
    /**
42
     * @var string
43
     */
44
    private $tag;
45
46
    /**
47
     * @var array
48
     */
49
    private $sharedHeader = [];
50
51
    /**
52
     * @var array
53
     */
54
    private $sharedProtectedHeader = [];
55
56
    /**
57
     * @var null|string
58
     */
59
    private $encodedSharedProtectedHeader;
60
61
    /**
62
     * @var null|string
63
     */
64
    private $payload;
65
66
    public function __construct(string $ciphertext, string $iv, string $tag, ?string $aad = null, array $sharedHeader = [], array $sharedProtectedHeader = [], ?string $encodedSharedProtectedHeader = null, array $recipients = [])
67
    {
68
        $this->ciphertext = $ciphertext;
69
        $this->iv = $iv;
70
        $this->aad = $aad;
71
        $this->tag = $tag;
72
        $this->sharedHeader = $sharedHeader;
73
        $this->sharedProtectedHeader = $sharedProtectedHeader;
74
        $this->encodedSharedProtectedHeader = $encodedSharedProtectedHeader;
75
        $this->recipients = $recipients;
76
    }
77
78
    public function getPayload(): ?string
79
    {
80
        return $this->payload;
81
    }
82
83
    /**
84
     * Set the payload.
85
     * This method is immutable and a new object will be returned.
86
     *
87
     * @return JWE
88
     */
89
    public function withPayload(string $payload): self
90
    {
91
        $clone = clone $this;
92
        $clone->payload = $payload;
93
94
        return $clone;
95
    }
96
97
    /**
98
     * Returns the number of recipients associated with the JWS.
99
     */
100
    public function countRecipients(): int
101
    {
102
        return \count($this->recipients);
103
    }
104
105
    /**
106
     * Returns true is the JWE has already been encrypted.
107
     */
108
    public function isEncrypted(): bool
109
    {
110
        return null !== $this->getCiphertext();
111
    }
112
113
    /**
114
     * Returns the recipients associated with the JWS.
115
     *
116
     * @return Recipient[]
117
     */
118
    public function getRecipients(): array
119
    {
120
        return $this->recipients;
121
    }
122
123
    /**
124
     * Returns the recipient object at the given index.
125
     *
126
     * @throws InvalidArgumentException if the recipient ID does not exist
127
     */
128
    public function getRecipient(int $id): Recipient
129
    {
130
        if (!isset($this->recipients[$id])) {
131
            throw new InvalidArgumentException('The recipient does not exist.');
132
        }
133
134
        return $this->recipients[$id];
135
    }
136
137
    /**
138
     * Returns the ciphertext. This method will return null is the JWE has not yet been encrypted.
139
     *
140
     * @return null|string The cyphertext
141
     */
142
    public function getCiphertext(): ?string
143
    {
144
        return $this->ciphertext;
145
    }
146
147
    /**
148
     * Returns the Additional Authentication Data if available.
149
     */
150
    public function getAAD(): ?string
151
    {
152
        return $this->aad;
153
    }
154
155
    /**
156
     * Returns the Initialization Vector if available.
157
     */
158
    public function getIV(): ?string
159
    {
160
        return $this->iv;
161
    }
162
163
    /**
164
     * Returns the tag if available.
165
     */
166
    public function getTag(): ?string
167
    {
168
        return $this->tag;
169
    }
170
171
    /**
172
     * Returns the encoded shared protected header.
173
     */
174
    public function getEncodedSharedProtectedHeader(): string
175
    {
176
        return $this->encodedSharedProtectedHeader ?? '';
177
    }
178
179
    /**
180
     * Returns the shared protected header.
181
     */
182
    public function getSharedProtectedHeader(): array
183
    {
184
        return $this->sharedProtectedHeader;
185
    }
186
187
    /**
188
     * Returns the shared protected header parameter identified by the given key.
189
     * Throws an exception is the the parameter is not available.
190
     *
191
     * @param string $key The key
192
     *
193
     * @throws InvalidArgumentException if the shared protected header parameter does not exist
194
     *
195
     * @return null|mixed
196
     */
197
    public function getSharedProtectedHeaderParameter(string $key)
198
    {
199
        if (!$this->hasSharedProtectedHeaderParameter($key)) {
200
            throw new InvalidArgumentException(sprintf('The shared protected header "%s" does not exist.', $key));
201
        }
202
203
        return $this->sharedProtectedHeader[$key];
204
    }
205
206
    /**
207
     * Returns true if the shared protected header has the parameter identified by the given key.
208
     *
209
     * @param string $key The key
210
     */
211
    public function hasSharedProtectedHeaderParameter(string $key): bool
212
    {
213
        return \array_key_exists($key, $this->sharedProtectedHeader);
214
    }
215
216
    /**
217
     * Returns the shared header.
218
     */
219
    public function getSharedHeader(): array
220
    {
221
        return $this->sharedHeader;
222
    }
223
224
    /**
225
     * Returns the shared header parameter identified by the given key.
226
     * Throws an exception is the the parameter is not available.
227
     *
228
     * @param string $key The key
229
     *
230
     * @throws InvalidArgumentException if the shared header parameter does not exist
231
     *
232
     * @return null|mixed
233
     */
234
    public function getSharedHeaderParameter(string $key)
235
    {
236
        if (!$this->hasSharedHeaderParameter($key)) {
237
            throw new InvalidArgumentException(sprintf('The shared header "%s" does not exist.', $key));
238
        }
239
240
        return $this->sharedHeader[$key];
241
    }
242
243
    /**
244
     * Returns true if the shared header has the parameter identified by the given key.
245
     *
246
     * @param string $key The key
247
     */
248
    public function hasSharedHeaderParameter(string $key): bool
249
    {
250
        return \array_key_exists($key, $this->sharedHeader);
251
    }
252
253
    /**
254
     * This method splits the JWE into a list of JWEs.
255
     * It is only useful when the JWE contains more than one recipient (JSON General Serialization).
256
     *
257
     * @return JWE[]
258
     */
259
    public function split(): array
260
    {
261
        $result = [];
262
        foreach ($this->recipients as $recipient) {
263
            $result[] = new self(
264
                $this->ciphertext,
265
                $this->iv,
266
                $this->tag,
267
                $this->aad,
268
                $this->sharedHeader,
269
                $this->sharedProtectedHeader,
270
                $this->encodedSharedProtectedHeader,
271
                [$recipient]
272
            );
273
        }
274
275
        return $result;
276
    }
277
}
278