Failed Conditions
Push — v7 ( 22f220...514019 )
by Florent
03:13
created

CompactSerializer::serialize()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 14
nc 8
nop 2
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\Signature\Serializer;
15
16
use Base64Url\Base64Url;
17
use Jose\Component\Signature\JWS;
18
19
/**
20
 * Class CompactSerializer.
21
 */
22
final class CompactSerializer extends AbstractSerializer
23
{
24
    public const NAME = 'jws_compact';
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function name(): string
30
    {
31
        return self::NAME;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function serialize(JWS $jws, ?int $signatureIndex = null): string
38
    {
39
        if (null === $signatureIndex) {
40
            $signatureIndex = 0;
41
        }
42
        $signature = $jws->getSignature($signatureIndex);
43
        if (!empty($signature->getHeaders())) {
44
            throw new \LogicException('The signature contains unprotected headers and cannot be converted into compact JSON.');
45
        }
46
        if (!$this->isPayloadEncoded($signature->getProtectedHeaders()) && !empty($jws->getEncodedPayload())) {
47
            if (1 !== preg_match('/^[\x{20}-\x{2d}|\x{2f}-\x{7e}]*$/u', $jws->getPayload())) {
48
                throw new \LogicException('Unable to convert the JWS with non-encoded payload.');
49
            }
50
        }
51
52
        return sprintf(
53
            '%s.%s.%s',
54
            $signature->getEncodedProtectedHeaders(),
55
            $jws->getEncodedPayload(),
56
            Base64Url::encode($signature->getSignature())
57
        );
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function unserialize(string $input): JWS
64
    {
65
        $parts = explode('.', $input);
66
        if (3 !== count($parts)) {
67
            throw new \InvalidArgumentException('Unsupported input');
68
        }
69
70
        $encodedProtectedHeaders = $parts[0];
71
        $protectedHeaders = json_decode(Base64Url::decode($parts[0]), true);
72
        if (empty($parts[1])) {
73
            $payload = null;
74
            $encodedPayload = null;
75
        } else {
76
            $encodedPayload = $parts[1];
77
            $payload = $this->isPayloadEncoded($protectedHeaders) ? Base64Url::decode($encodedPayload) : $encodedPayload;
78
        }
79
        $signature = Base64Url::decode($parts[2]);
80
81
        $jws = JWS::create($payload, $encodedPayload, empty($parts[1]));
82
        $jws = $jws->addSignature($signature, $protectedHeaders, $encodedProtectedHeaders);
83
84
        return $jws;
85
    }
86
}
87