1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
7
|
|
|
* |
8
|
|
|
* This software may be modified and distributed under the terms |
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jose\Util; |
13
|
|
|
|
14
|
|
|
use Assert\Assertion; |
15
|
|
|
use Base64Url\Base64Url; |
16
|
|
|
use Jose\Object\JWS; |
17
|
|
|
use Jose\Object\JWSInterface; |
18
|
|
|
use Jose\Object\SignatureInterface; |
19
|
|
|
|
20
|
|
|
final class JWSLoader |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param array $data |
24
|
|
|
* |
25
|
|
|
* @return \Jose\Object\JWSInterface |
26
|
|
|
*/ |
27
|
|
|
public static function loadSerializedJsonJWS(array $data) |
28
|
|
|
{ |
29
|
|
|
$jws = new JWS(); |
30
|
|
|
|
31
|
|
|
foreach ($data['signatures'] as $signature) { |
32
|
|
|
$bin_signature = Base64Url::decode($signature['signature']); |
33
|
|
|
$protected_headers = self::getProtectedHeaders($signature); |
34
|
|
|
$headers = self::getHeaders($signature); |
35
|
|
|
|
36
|
|
|
$jws = $jws->addSignatureFromLoadedData($bin_signature, $protected_headers, $headers); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
self::populatePayload($jws, $data); |
40
|
|
|
|
41
|
|
|
return $jws; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $data |
46
|
|
|
* |
47
|
|
|
* @return string|null |
48
|
|
|
*/ |
49
|
|
|
private static function getProtectedHeaders(array $data) |
50
|
|
|
{ |
51
|
|
|
if (array_key_exists('protected', $data)) { |
52
|
|
|
return $data['protected']; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $data |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
private static function getHeaders(array $data) |
62
|
|
|
{ |
63
|
|
|
if (array_key_exists('header', $data)) { |
64
|
|
|
return $data['header']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \Jose\Object\JWSInterface $jws |
72
|
|
|
* @param array $data |
73
|
|
|
*/ |
74
|
|
|
private static function populatePayload(JWSInterface &$jws, array $data) |
75
|
|
|
{ |
76
|
|
|
$is_encoded = null; |
77
|
|
|
foreach ($jws->getSignatures() as $signature) { |
78
|
|
|
if (null === $is_encoded) { |
79
|
|
|
$is_encoded = self::isPayloadEncoded($signature); |
80
|
|
|
} |
81
|
|
|
Assertion::eq($is_encoded, self::isPayloadEncoded($signature), 'Foreign payload encoding detected. The JWS cannot be loaded.'); |
82
|
|
|
} |
83
|
|
|
if (array_key_exists('payload', $data)) { |
84
|
|
|
$payload = $data['payload']; |
85
|
|
|
if (false !== $is_encoded) { |
86
|
|
|
$payload = Base64Url::decode($payload); |
87
|
|
|
} |
88
|
|
|
$json = json_decode($payload, true); |
89
|
|
|
if (null !== $json && !empty($payload)) { |
90
|
|
|
$payload = $json; |
91
|
|
|
} |
92
|
|
|
$jws = $jws->withPayload($payload); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param \Jose\Object\SignatureInterface $signature |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
private static function isPayloadEncoded(SignatureInterface $signature) |
102
|
|
|
{ |
103
|
|
|
return !$signature->hasProtectedHeader('b64') || true === $signature->getProtectedHeader('b64'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|