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 Base64Url\Base64Url; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class SignatureInstruction. |
18
|
|
|
*/ |
19
|
|
|
final class Signature implements SignatureInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var null|string |
23
|
|
|
*/ |
24
|
|
|
private $encoded_protected_headers = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $protected_headers = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $headers = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $signature; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function getProtectedHeaders() |
45
|
|
|
{ |
46
|
|
|
return $this->protected_headers; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function getHeaders() |
53
|
|
|
{ |
54
|
|
|
return $this->headers; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getEncodedProtectedHeaders() |
61
|
|
|
{ |
62
|
|
|
return $this->encoded_protected_headers; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function withEncodedProtectedHeaders($encoded_protected_headers) |
69
|
|
|
{ |
70
|
|
|
$signature = clone $this; |
71
|
|
|
$signature->encoded_protected_headers = $encoded_protected_headers; |
72
|
|
|
if (empty($encoded_protected_headers)) { |
73
|
|
|
$signature->protected_headers = []; |
74
|
|
|
} else { |
75
|
|
|
$decoded = json_decode(Base64Url::decode($encoded_protected_headers), true): |
|
|
|
|
76
|
|
|
if (!is_array($decoded)) { |
77
|
|
|
throw new \InvalidArgumentException('The argument does not contain valid encoded protected headers.'); |
78
|
|
|
} |
79
|
|
|
$signature->protected_headers = $decoded; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $signature; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function withProtectedHeaders(array $protected_headers) |
89
|
|
|
{ |
90
|
|
|
$signature = clone $this; |
91
|
|
|
$signature->protected_headers = $protected_headers; |
92
|
|
|
$signature->encoded_protected_headers = Base64Url::encode(json_encode($signature->protected_headers)); |
93
|
|
|
|
94
|
|
|
return $signature; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function withProtectedHeader($key, $value) |
101
|
|
|
{ |
102
|
|
|
$signature = clone $this; |
103
|
|
|
$signature->protected_headers[$key] = $value; |
104
|
|
|
$signature->encoded_protected_headers = Base64Url::encode(json_encode($signature->protected_headers)); |
105
|
|
|
|
106
|
|
|
return $signature; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getProtectedHeader($key) |
113
|
|
|
{ |
114
|
|
|
if ($this->hasProtectedHeader($key)) { |
115
|
|
|
return $this->protected_headers[$key]; |
116
|
|
|
} |
117
|
|
|
throw new \InvalidArgumentException(sprintf('The protected header "%s" does not exist', $key)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function hasProtectedHeader($key) |
124
|
|
|
{ |
125
|
|
|
return array_key_exists($key, $this->protected_headers); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function withHeaders(array $headers) |
132
|
|
|
{ |
133
|
|
|
$signature = clone $this; |
134
|
|
|
$signature->headers = $headers; |
135
|
|
|
|
136
|
|
|
return $signature; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function withHeader($key, $value) |
143
|
|
|
{ |
144
|
|
|
$signature = clone $this; |
145
|
|
|
$signature->headers[$key] = $value; |
146
|
|
|
|
147
|
|
|
return $signature; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function getHeader($key) |
154
|
|
|
{ |
155
|
|
|
if ($this->hasHeader($key)) { |
156
|
|
|
return $this->headers[$key]; |
157
|
|
|
} |
158
|
|
|
throw new \InvalidArgumentException(sprintf('The header "%s" does not exist', $key)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function hasHeader($key) |
165
|
|
|
{ |
166
|
|
|
return array_key_exists($key, $this->headers); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function getAllHeaders() |
170
|
|
|
{ |
171
|
|
|
return array_merge( |
172
|
|
|
$this->getProtectedHeaders(), |
173
|
|
|
$this->getHeaders() |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function getSignature() |
181
|
|
|
{ |
182
|
|
|
return $this->signature; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function withSignature($values) |
189
|
|
|
{ |
190
|
|
|
$signature = clone $this; |
191
|
|
|
$signature->signature = $values; |
192
|
|
|
|
193
|
|
|
return $signature; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|