1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\JWS; |
4
|
|
|
|
5
|
|
|
use JWX\JWK\JWK; |
6
|
|
|
use JWX\JWS\Algorithm\SignatureAlgorithmFactory; |
7
|
|
|
use JWX\JWT\Header\Header; |
8
|
|
|
use JWX\JWT\Header\HeaderParameters; |
9
|
|
|
use JWX\JWT\Parameter\AlgorithmParameterValue; |
10
|
|
|
use JWX\JWT\Parameter\KeyIDParameter; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base class for algorithms usable for signing and validating JWS's. |
15
|
|
|
*/ |
16
|
|
|
abstract class SignatureAlgorithm implements |
17
|
|
|
AlgorithmParameterValue, |
|
|
|
|
18
|
|
|
HeaderParameters |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* ID of the key used by the algorithm. |
22
|
|
|
* |
23
|
|
|
* If set, KeyID parameter shall be automatically inserted into JWS's |
24
|
|
|
* header. |
25
|
|
|
* |
26
|
|
|
* @var string|null $_keyID |
27
|
|
|
*/ |
28
|
|
|
protected $_keyID; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Compute signature. |
32
|
|
|
* |
33
|
|
|
* @param string $data Data for which the signature is computed |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
abstract public function computeSignature($data); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Validate signature. |
40
|
|
|
* |
41
|
|
|
* @param string $data Data to validate |
42
|
|
|
* @param string $signature Signature to compare |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
abstract public function validateSignature($data, $signature); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initialize signature algorithm from a JWK and a header. |
49
|
|
|
* |
50
|
|
|
* @param JWK $jwk JSON Web Key |
51
|
|
|
* @param Header $header Header |
52
|
|
|
* @return SignatureAlgorithm |
53
|
|
|
*/ |
54
|
5 |
|
public static function fromJWK(JWK $jwk, Header $header) { |
55
|
5 |
|
$factory = new SignatureAlgorithmFactory($header); |
56
|
5 |
|
return $factory->algoByKey($jwk); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get self with key ID. |
61
|
|
|
* |
62
|
|
|
* @param string|null $id Key ID or null to remove |
63
|
|
|
* @return self |
64
|
|
|
*/ |
65
|
2 |
|
public function withKeyID($id) { |
66
|
2 |
|
$obj = clone $this; |
67
|
2 |
|
$obj->_keyID = $id; |
68
|
2 |
|
return $obj; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
74
|
|
|
* @return JWTParameter[] |
75
|
|
|
*/ |
76
|
18 |
View Code Duplication |
public function headerParameters() { |
|
|
|
|
77
|
18 |
|
$params = array(); |
78
|
18 |
|
if (isset($this->_keyID)) { |
79
|
2 |
|
$params[] = new KeyIDParameter($this->_keyID); |
80
|
2 |
|
} |
81
|
18 |
|
return $params; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|