CompactSerializer::serialize()   B
last analyzed

Complexity

Conditions 7
Paths 14

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.6186
c 0
b 0
f 0
cc 7
nc 14
nop 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\Signature\Serializer;
15
16
use Base64Url\Base64Url;
17
use InvalidArgumentException;
18
use Jose\Component\Core\Util\JsonConverter;
19
use Jose\Component\Signature\JWS;
20
use LogicException;
21
use Throwable;
22
23
final class CompactSerializer extends Serializer
24
{
25
    public const NAME = 'jws_compact';
26
27
    public function displayName(): string
28
    {
29
        return 'JWS Compact';
30
    }
31
32
    public function name(): string
33
    {
34
        return self::NAME;
35
    }
36
37
    /**
38
     * @throws LogicException if the JWS has unprotected header (invalid for compact JSON)
39
     * @throws LogicException if the payload is not encoded but contains unauthorized characters
40
     */
41
    public function serialize(JWS $jws, ?int $signatureIndex = null): string
42
    {
43
        if (null === $signatureIndex) {
44
            $signatureIndex = 0;
45
        }
46
        $signature = $jws->getSignature($signatureIndex);
47
        if (0 !== \count($signature->getHeader())) {
48
            throw new LogicException('The signature contains unprotected header parameters and cannot be converted into compact JSON.');
49
        }
50
        $isEmptyPayload = null === $jws->getEncodedPayload() || '' === $jws->getEncodedPayload();
51
        if (!$this->isPayloadEncoded($signature->getProtectedHeader()) && !$isEmptyPayload) {
52
            if (1 !== preg_match('/^[\x{20}-\x{2d}|\x{2f}-\x{7e}]*$/u', $jws->getPayload())) {
53
                throw new LogicException('Unable to convert the JWS with non-encoded payload.');
54
            }
55
        }
56
57
        return sprintf(
58
            '%s.%s.%s',
59
            $signature->getEncodedProtectedHeader(),
60
            $jws->getEncodedPayload(),
61
            Base64Url::encode($signature->getSignature())
62
        );
63
    }
64
65
    /**
66
     * @throws InvalidArgumentException if the input is invalid
67
     */
68
    public function unserialize(string $input): JWS
69
    {
70
        $parts = explode('.', $input);
71
        if (3 !== \count($parts)) {
72
            throw new InvalidArgumentException('Unsupported input');
73
        }
74
75
        try {
76
            $encodedProtectedHeader = $parts[0];
77
            $protectedHeader = JsonConverter::decode(Base64Url::decode($parts[0]));
78
            $hasPayload = '' !== $parts[1];
79
            if (!$hasPayload) {
80
                $payload = null;
81
                $encodedPayload = null;
82
            } else {
83
                $encodedPayload = $parts[1];
84
                $payload = $this->isPayloadEncoded($protectedHeader) ? Base64Url::decode($encodedPayload) : $encodedPayload;
85
            }
86
            $signature = Base64Url::decode($parts[2]);
87
88
            $jws = new JWS($payload, $encodedPayload, !$hasPayload);
89
90
            return $jws->addSignature($signature, $protectedHeader, $encodedProtectedHeader);
91
        } catch (Throwable $throwable) {
92
            throw new InvalidArgumentException('Unsupported input', $throwable->getCode(), $throwable);
93
        }
94
    }
95
}
96