|
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; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class JWSConverter |
|
18
|
|
|
*/ |
|
19
|
|
|
final class JWSConverter |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $input |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function convert(string $input): array |
|
27
|
|
|
{ |
|
28
|
|
|
$data = json_decode($input, true); |
|
29
|
|
|
if (is_array($data)) { |
|
30
|
|
|
if (array_key_exists('signatures', $data)) { |
|
31
|
|
|
return $data; |
|
32
|
|
|
} elseif (array_key_exists('signature', $data)) { |
|
33
|
|
|
return self::fromFlattenedSerializationSignatureToSerialization($data); |
|
34
|
|
|
} |
|
35
|
|
|
} elseif (is_string($input)) { |
|
36
|
|
|
return self::fromCompactSerializationToSerialization($input); |
|
37
|
|
|
} |
|
38
|
|
|
throw new \InvalidArgumentException('Unsupported input'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $input |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
private static function fromFlattenedSerializationSignatureToSerialization(array $input): array |
|
47
|
|
|
{ |
|
48
|
|
|
$signature = [ |
|
49
|
|
|
'signature' => $input['signature'], |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
foreach (['protected', 'header'] as $key) { |
|
53
|
|
|
if (array_key_exists($key, $input)) { |
|
54
|
|
|
$signature[$key] = $input[$key]; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$temp = []; |
|
59
|
|
|
if (!empty($input['payload'])) { |
|
60
|
|
|
$temp['payload'] = $input['payload']; |
|
61
|
|
|
} |
|
62
|
|
|
$temp['signatures'] = [$signature]; |
|
63
|
|
|
|
|
64
|
|
|
return $temp; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $input |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
private static function fromCompactSerializationToSerialization(string $input): array |
|
73
|
|
|
{ |
|
74
|
|
|
$parts = explode('.', $input); |
|
75
|
|
|
switch (count($parts)) { |
|
76
|
|
|
case 3: |
|
77
|
|
|
return self::fromCompactSerializationSignatureToSerialization($parts); |
|
78
|
|
|
default: |
|
79
|
|
|
throw new \InvalidArgumentException('Unsupported input'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array $parts |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
private static function fromCompactSerializationSignatureToSerialization(array $parts): array |
|
89
|
|
|
{ |
|
90
|
|
|
$temp = []; |
|
91
|
|
|
|
|
92
|
|
|
if (!empty($parts[1])) { |
|
93
|
|
|
$temp['payload'] = $parts[1]; |
|
94
|
|
|
} |
|
95
|
|
|
$temp['signatures'] = [[ |
|
96
|
|
|
'protected' => $parts[0], |
|
97
|
|
|
'signature' => $parts[2], |
|
98
|
|
|
]]; |
|
99
|
|
|
|
|
100
|
|
|
return $temp; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|