1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acquia\Rest; |
4
|
|
|
|
5
|
|
|
abstract class SignatureAbstract |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
protected $secretKey; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var \Acquia\Rest\NoncerInterface |
14
|
|
|
*/ |
15
|
|
|
private $noncer; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
private $requestTime = 0; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $noncerLength = NoncerAbstract::DEFAULT_LENGTH; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected static $defaultNoncerClass = 'Acquia\Rest\RandomStringNoncer'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $secretKey |
34
|
|
|
*/ |
35
|
87 |
|
public function __construct($secretKey) |
36
|
|
|
{ |
37
|
87 |
|
$this->secretKey = $secretKey; |
38
|
87 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed $data |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
abstract public function generate($data); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the shared secret. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
54 |
|
public function getSecretKey() |
53
|
|
|
{ |
54
|
54 |
|
return $this->secretKey; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $class |
59
|
|
|
*/ |
60
|
30 |
|
public static function setDefaultNoncerClass($class) |
61
|
|
|
{ |
62
|
30 |
|
self::$defaultNoncerClass = $class; |
63
|
30 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
3 |
|
public static function getDefaultNoncerClass() |
69
|
|
|
{ |
70
|
3 |
|
return self::$defaultNoncerClass; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns a noncer, instantiates it if it doesn't exist. |
75
|
|
|
* |
76
|
|
|
* @return \Acquia\Rest\NoncerInterface |
77
|
|
|
* |
78
|
|
|
* @throws \UnexpectedValueException |
79
|
|
|
*/ |
80
|
57 |
|
public function getNoncer() |
81
|
|
|
{ |
82
|
57 |
|
if (!isset($this->noncer)) { |
83
|
57 |
|
$this->noncer = new self::$defaultNoncerClass($this->noncerLength); |
84
|
57 |
|
if (!$this->noncer instanceof NoncerInterface) { |
85
|
3 |
|
throw new \UnexpectedValueException('Noncer must implement Acquia\Rest\NoncerInterface'); |
86
|
|
|
} |
87
|
54 |
|
} |
88
|
54 |
|
return $this->noncer; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
51 |
|
public function generateNonce() |
95
|
|
|
{ |
96
|
51 |
|
return $this->getNoncer()->generate(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns the last nonce that was generated. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
42 |
|
public function getNonce() |
105
|
|
|
{ |
106
|
42 |
|
return $this->getNoncer()->getLastNonce(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param int $requestTime |
111
|
|
|
* |
112
|
|
|
* @return \Acquia\Rest\SignatureAbstract |
113
|
|
|
*/ |
114
|
21 |
|
public function setRequestTime($requestTime) |
115
|
|
|
{ |
116
|
21 |
|
$this->requestTime = $requestTime; |
117
|
21 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return \Acquia\Rest\SignatureAbstract |
122
|
|
|
*/ |
123
|
12 |
|
public function unsetRequestTime() |
124
|
|
|
{ |
125
|
12 |
|
$this->requestTime = 0; |
126
|
12 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
54 |
|
public function getRequestTime() |
133
|
|
|
{ |
134
|
54 |
|
$this->requestTime = $this->requestTime ?: time(); |
135
|
54 |
|
return $this->requestTime; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|