1 | <?php |
||
21 | class JWS |
||
22 | { |
||
23 | /** |
||
24 | * Protected header. |
||
25 | * |
||
26 | * @var Header $_protectedHeader |
||
27 | */ |
||
28 | protected $_protectedHeader; |
||
29 | |||
30 | /** |
||
31 | * Payload. |
||
32 | * |
||
33 | * @var string $_payload |
||
34 | */ |
||
35 | protected $_payload; |
||
36 | |||
37 | /** |
||
38 | * Input value for the signature computation. |
||
39 | * |
||
40 | * @var string $_signatureInput |
||
41 | */ |
||
42 | protected $_signatureInput; |
||
43 | |||
44 | /** |
||
45 | * Signature. |
||
46 | * |
||
47 | * @var string $_signature |
||
48 | */ |
||
49 | protected $_signature; |
||
50 | |||
51 | /** |
||
52 | * Constructor |
||
53 | * |
||
54 | * @param Header $protected_header JWS Protected Header |
||
55 | * @param string $payload JWS Payload |
||
56 | * @param string $signature_input Input value for the signature computation |
||
57 | * @param string $signature JWS Signature |
||
58 | */ |
||
59 | 32 | protected function __construct(Header $protected_header, $payload, |
|
66 | |||
67 | /** |
||
68 | * Initialize from a compact serialization. |
||
69 | * |
||
70 | * @param string $data |
||
71 | * @return self |
||
72 | */ |
||
73 | 6 | public static function fromCompact($data) { |
|
76 | |||
77 | /** |
||
78 | * Initialize from the parts of a compact serialization. |
||
79 | * |
||
80 | * @param array $parts |
||
81 | * @throws \UnexpectedValueException |
||
82 | * @return self |
||
83 | */ |
||
84 | 18 | public static function fromParts(array $parts) { |
|
96 | |||
97 | /** |
||
98 | * Initialize by signing the payload with given algorithm. |
||
99 | * |
||
100 | * @param string $payload JWS Payload |
||
101 | * @param SignatureAlgorithm $algo Signature algorithm |
||
102 | * @param Header|null $header Desired header. Algorithm specific |
||
103 | * parameters are added automatically. |
||
104 | * @throws \RuntimeException If signature computation fails |
||
105 | * @return self |
||
106 | */ |
||
107 | 15 | public static function sign($payload, SignatureAlgorithm $algo, |
|
127 | |||
128 | /** |
||
129 | * Get JOSE header. |
||
130 | * |
||
131 | * @return JOSE |
||
132 | */ |
||
133 | 22 | public function header() { |
|
136 | |||
137 | /** |
||
138 | * Get the signature algorithm name. |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | 20 | public function algorithmName() { |
|
147 | |||
148 | /** |
||
149 | * Check whether JWS is unsecured, that is, contains no signature. |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 10 | public function isUnsecured() { |
|
156 | |||
157 | /** |
||
158 | * Get the payload. |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 6 | public function payload() { |
|
165 | |||
166 | /** |
||
167 | * Get the signature. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 5 | public function signature() { |
|
174 | |||
175 | /** |
||
176 | * Get the payload encoded for serialization. |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | 12 | protected function _encodedPayload() { |
|
187 | |||
188 | /** |
||
189 | * Validate the signature using explicit algorithm. |
||
190 | * |
||
191 | * @param SignatureAlgorithm $algo |
||
192 | * @throws \UnexpectedValueException If using different signature algorithm |
||
193 | * then specified by the header |
||
194 | * @throws \RuntimeException If signature computation fails |
||
195 | * @return bool True if signature is valid |
||
196 | */ |
||
197 | 15 | public function validate(SignatureAlgorithm $algo) { |
|
204 | |||
205 | /** |
||
206 | * Validate the signature using the given JWK. |
||
207 | * |
||
208 | * Signature algorithm is determined from the header. |
||
209 | * |
||
210 | * @param JWK $jwk JSON Web Key |
||
211 | * @return bool True if signature is valid |
||
212 | */ |
||
213 | 4 | public function validateWithJWK(JWK $jwk) { |
|
217 | |||
218 | /** |
||
219 | * Validate the signature using a key from the given JWK set. |
||
220 | * |
||
221 | * Correct key shall be sought by the key ID indicated by the header. |
||
222 | * |
||
223 | * @param JWKSet $set Set of JSON Web Keys |
||
224 | * @return bool True if signature is valid |
||
225 | */ |
||
226 | 4 | public function validateWithJWKSet(JWKSet $set) { |
|
231 | |||
232 | /** |
||
233 | * Convert to compact serialization. |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | 12 | public function toCompact() { |
|
242 | |||
243 | /** |
||
244 | * Convert to compact serialization with payload detached. |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | 2 | public function toCompactDetached() { |
|
252 | |||
253 | /** |
||
254 | * Generate input for the signature computation. |
||
255 | * |
||
256 | * @param string $payload Payload |
||
257 | * @param Header $header Protected header |
||
258 | * @return string |
||
259 | */ |
||
260 | 15 | protected static function _generateSignatureInput($payload, Header $header) { |
|
266 | |||
267 | /** |
||
268 | * Convert JWS to string. |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | 1 | public function __toString() { |
|
275 | } |
||
276 |