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\Object; |
13
|
|
|
|
14
|
|
|
use Assert\Assertion; |
15
|
|
|
use Base64Url\Base64Url; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class SignatureInstruction. |
19
|
|
|
*/ |
20
|
|
|
final class Signature implements SignatureInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var null|string |
24
|
|
|
*/ |
25
|
|
|
private $encoded_protected_headers = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $protected_headers = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $headers = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $signature; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Jose\Object\JWKInterface |
44
|
|
|
*/ |
45
|
|
|
private $signature_key; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public static function createSignatureFromLoadedData($signature, $encoded_protected_headers, array $headers) |
51
|
|
|
{ |
52
|
|
|
$object = new self(); |
53
|
|
|
$object->encoded_protected_headers = $encoded_protected_headers; |
54
|
|
|
if (null !== $encoded_protected_headers) { |
55
|
|
|
$protected_headers = json_decode(Base64Url::decode($encoded_protected_headers), true); |
56
|
|
|
Assertion::isArray($protected_headers, 'Unable to decode the protected headers.'); |
57
|
|
|
$object->protected_headers = $protected_headers; |
58
|
|
|
} |
59
|
|
|
$object->signature = $signature; |
60
|
|
|
$object->headers = $headers; |
61
|
|
|
|
62
|
|
|
return $object; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public static function createSignature(JWKInterface $signature_key, array $protected_headers, array $headers) |
69
|
|
|
{ |
70
|
|
|
$object = new self(); |
71
|
|
|
$object->protected_headers = $protected_headers; |
72
|
|
|
if (!empty($protected_headers)) { |
73
|
|
|
$object->encoded_protected_headers = Base64Url::encode(json_encode($protected_headers)); |
74
|
|
|
} |
75
|
|
|
$object->signature_key = $signature_key; |
76
|
|
|
$object->headers = $headers; |
77
|
|
|
|
78
|
|
|
return $object; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function getProtectedHeaders() |
85
|
|
|
{ |
86
|
|
|
return $this->protected_headers; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function getHeaders() |
93
|
|
|
{ |
94
|
|
|
return $this->headers; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function getEncodedProtectedHeaders() |
101
|
|
|
{ |
102
|
|
|
return $this->encoded_protected_headers; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function getProtectedHeader($key) |
109
|
|
|
{ |
110
|
|
|
if ($this->hasProtectedHeader($key)) { |
111
|
|
|
return $this->getProtectedHeaders()[$key]; |
112
|
|
|
} |
113
|
|
|
throw new \InvalidArgumentException(sprintf('The protected header "%s" does not exist', $key)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function hasProtectedHeader($key) |
120
|
|
|
{ |
121
|
|
|
return array_key_exists($key, $this->getProtectedHeaders()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function getHeader($key) |
128
|
|
|
{ |
129
|
|
|
if ($this->hasHeader($key)) { |
130
|
|
|
return $this->headers[$key]; |
131
|
|
|
} |
132
|
|
|
throw new \InvalidArgumentException(sprintf('The header "%s" does not exist', $key)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function hasHeader($key) |
139
|
|
|
{ |
140
|
|
|
return array_key_exists($key, $this->headers); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getAllHeaders() |
144
|
|
|
{ |
145
|
|
|
return array_merge( |
146
|
|
|
$this->getProtectedHeaders(), |
147
|
|
|
$this->getHeaders() |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function getSignature() |
155
|
|
|
{ |
156
|
|
|
return $this->signature; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return \Jose\Object\JWKInterface |
161
|
|
|
*/ |
162
|
|
|
public function getSignatureKey() |
163
|
|
|
{ |
164
|
|
|
return $this->signature_key; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|