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

JSONFlattenedSerializer::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 JSONFlattenedSerializer.
21
 */
22
final class JSONFlattenedSerializer extends AbstractSerializer
23
{
24
    public const NAME = 'jws_json_flattened';
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function displayName(): string
30
    {
31
        return 'JWS JSON Flattened';
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
52
        $data = [];
53
        $values = [
54
            'payload' => $jws->getEncodedPayload(),
55
            'protected' => $signature->getEncodedProtectedHeaders(),
56
            'header' => $signature->getHeaders(),
57
        ];
58
59
        foreach ($values as $key => $value) {
60
            if (!empty($value)) {
61
                $data[$key] = $value;
62
            }
63
        }
64
        $data['signature'] = Base64Url::encode($signature->getSignature());
65
66
        return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function unserialize(string $input): JWS
73
    {
74
        $data = json_decode($input, true);
75
        if (!is_array($data) || !array_key_exists('signature', $data)) {
76
            throw new \InvalidArgumentException('Unsupported input.');
77
        }
78
79
        $signature = Base64Url::decode($data['signature']);
80
81
        if (array_key_exists('protected', $data)) {
82
            $encodedProtectedHeaders = $data['protected'];
83
            $protectedHeaders = json_decode(Base64Url::decode($data['protected']), true);
84
        } else {
85
            $encodedProtectedHeaders = null;
86
            $protectedHeaders = [];
87
        }
88
        if (array_key_exists('header', $data)) {
89
            if (!is_array($data['header'])) {
90
                throw new \InvalidArgumentException('Bad header.');
91
            }
92
            $headers = $data['header'];
93
        } else {
94
            $headers = [];
95
        }
96
97
        if (array_key_exists('payload', $data)) {
98
            $encodedPayload = $data['payload'];
99
            $payload = $this->isPayloadEncoded($protectedHeaders) ? Base64Url::decode($encodedPayload) : $encodedPayload;
100
        } else {
101
            $payload = null;
102
            $encodedPayload = null;
103
        }
104
105
        $jws = JWS::create($payload, $encodedPayload, null === $encodedPayload);
106
        $jws = $jws->addSignature($signature, $protectedHeaders, $encodedProtectedHeaders, $headers);
107
108
        return $jws;
109
    }
110
}
111