Failed Conditions
Push — v7 ( b3d8c9...4e8990 )
by Florent
02:22
created

CompactSerializer::displayName()   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\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 displayName(): string
30
    {
31
        return 'JWS Compact';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function name(): string
38
    {
39
        return self::NAME;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function serialize(JWS $jws, ?int $signatureIndex = null): string
46
    {
47
        if (null === $signatureIndex) {
48
            $signatureIndex = 0;
49
        }
50
        $signature = $jws->getSignature($signatureIndex);
51
        if (!empty($signature->getHeaders())) {
52
            throw new \LogicException('The signature contains unprotected headers and cannot be converted into compact JSON.');
53
        }
54
        if (!$this->isPayloadEncoded($signature->getProtectedHeaders()) && !empty($jws->getEncodedPayload())) {
55
            if (1 !== preg_match('/^[\x{20}-\x{2d}|\x{2f}-\x{7e}]*$/u', $jws->getPayload())) {
56
                throw new \LogicException('Unable to convert the JWS with non-encoded payload.');
57
            }
58
        }
59
60
        return sprintf(
61
            '%s.%s.%s',
62
            $signature->getEncodedProtectedHeaders(),
63
            $jws->getEncodedPayload(),
64
            Base64Url::encode($signature->getSignature())
65
        );
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function unserialize(string $input): JWS
72
    {
73
        $parts = explode('.', $input);
74
        if (3 !== count($parts)) {
75
            throw new \InvalidArgumentException('Unsupported input');
76
        }
77
78
        try {
79
            $encodedProtectedHeaders = $parts[0];
80
            $protectedHeaders = json_decode(Base64Url::decode($parts[0]), true);
81
            if (empty($parts[1])) {
82
                $payload = null;
83
                $encodedPayload = null;
84
            } else {
85
                $encodedPayload = $parts[1];
86
                $payload = $this->isPayloadEncoded($protectedHeaders) ? Base64Url::decode($encodedPayload) : $encodedPayload;
87
            }
88
            $signature = Base64Url::decode($parts[2]);
89
90
            $jws = JWS::create($payload, $encodedPayload, empty($parts[1]));
91
            $jws = $jws->addSignature($signature, $protectedHeaders, $encodedProtectedHeaders);
92
93
            return $jws;
94
        } catch (\Exception $e) {
95
            throw new \InvalidArgumentException('Unsupported input');
96
        } catch (\Error $e) {
97
            throw new \InvalidArgumentException('Unsupported input');
98
        }
99
    }
100
}
101