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\Core\Converter\JsonConverterInterface; |
18
|
|
|
use Jose\Component\Signature\JWS; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class JSONFlattenedSerializer. |
22
|
|
|
*/ |
23
|
|
|
final class JSONFlattenedSerializer extends AbstractSerializer |
24
|
|
|
{ |
25
|
|
|
public const NAME = 'jws_json_flattened'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var JsonConverterInterface |
29
|
|
|
*/ |
30
|
|
|
private $jsonConverter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* JSONFlattenedSerializer constructor. |
34
|
|
|
* |
35
|
|
|
* @param JsonConverterInterface $jsonConverter |
36
|
|
|
*/ |
37
|
|
|
public function __construct(JsonConverterInterface $jsonConverter) |
38
|
|
|
{ |
39
|
|
|
$this->jsonConverter = $jsonConverter; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function displayName(): string |
46
|
|
|
{ |
47
|
|
|
return 'JWS JSON Flattened'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function name(): string |
54
|
|
|
{ |
55
|
|
|
return self::NAME; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function serialize(JWS $jws, ?int $signatureIndex = null): string |
62
|
|
|
{ |
63
|
|
|
if (null === $signatureIndex) { |
64
|
|
|
$signatureIndex = 0; |
65
|
|
|
} |
66
|
|
|
$signature = $jws->getSignature($signatureIndex); |
67
|
|
|
|
68
|
|
|
$data = []; |
69
|
|
|
$values = [ |
70
|
|
|
'payload' => $jws->getEncodedPayload(), |
71
|
|
|
'protected' => $signature->getEncodedProtectedHeaders(), |
72
|
|
|
'header' => $signature->getHeaders(), |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
foreach ($values as $key => $value) { |
76
|
|
|
if (!empty($value)) { |
77
|
|
|
$data[$key] = $value; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
$data['signature'] = Base64Url::encode($signature->getSignature()); |
81
|
|
|
|
82
|
|
|
return $this->jsonConverter->encode($data); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function unserialize(string $input): JWS |
89
|
|
|
{ |
90
|
|
|
$data = $this->jsonConverter->decode($input); |
91
|
|
|
if (!is_array($data) || !array_key_exists('signature', $data)) { |
92
|
|
|
throw new \InvalidArgumentException('Unsupported input.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$signature = Base64Url::decode($data['signature']); |
96
|
|
|
|
97
|
|
|
if (array_key_exists('protected', $data)) { |
98
|
|
|
$encodedProtectedHeaders = $data['protected']; |
99
|
|
|
$protectedHeaders = $this->jsonConverter->decode(Base64Url::decode($data['protected'])); |
100
|
|
|
} else { |
101
|
|
|
$encodedProtectedHeaders = null; |
102
|
|
|
$protectedHeaders = []; |
103
|
|
|
} |
104
|
|
|
if (array_key_exists('header', $data)) { |
105
|
|
|
if (!is_array($data['header'])) { |
106
|
|
|
throw new \InvalidArgumentException('Bad header.'); |
107
|
|
|
} |
108
|
|
|
$headers = $data['header']; |
109
|
|
|
} else { |
110
|
|
|
$headers = []; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (array_key_exists('payload', $data)) { |
114
|
|
|
$encodedPayload = $data['payload']; |
115
|
|
|
$payload = $this->isPayloadEncoded($protectedHeaders) ? Base64Url::decode($encodedPayload) : $encodedPayload; |
116
|
|
|
} else { |
117
|
|
|
$payload = null; |
118
|
|
|
$encodedPayload = null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$jws = JWS::create($payload, $encodedPayload, null === $encodedPayload); |
122
|
|
|
$jws = $jws->addSignature($signature, $protectedHeaders, $encodedProtectedHeaders, $headers); |
123
|
|
|
|
124
|
|
|
return $jws; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|