1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth1\Signature; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Consumer\CredentialsInterface; |
6
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
7
|
|
|
use OAuth\OAuth1\Signature\Exception\UnsupportedHashAlgorithmException; |
8
|
|
|
|
9
|
|
|
class Signature implements SignatureInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Credentials |
13
|
|
|
*/ |
14
|
|
|
protected $credentials; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $algorithm; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $tokenSecret = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param CredentialsInterface $credentials |
28
|
|
|
*/ |
29
|
|
|
public function __construct(CredentialsInterface $credentials) |
30
|
|
|
{ |
31
|
|
|
$this->credentials = $credentials; |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $algorithm |
36
|
|
|
*/ |
37
|
|
|
public function setHashingAlgorithm($algorithm) |
38
|
|
|
{ |
39
|
|
|
$this->algorithm = $algorithm; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $token |
44
|
|
|
*/ |
45
|
|
|
public function setTokenSecret($token) |
46
|
|
|
{ |
47
|
|
|
$this->tokenSecret = $token; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param UriInterface $uri |
52
|
|
|
* @param array $params |
53
|
|
|
* @param string $method |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getSignature(UriInterface $uri, array $params, $method = 'POST') |
58
|
|
|
{ |
59
|
|
|
parse_str($uri->getQuery(), $queryStringData); |
60
|
|
|
|
61
|
|
|
$signatureData = $this->prepareParameters(array_merge($queryStringData, $params)); |
62
|
|
|
|
63
|
|
|
// determine base uri |
64
|
|
|
$baseUri = $uri->getScheme() . '://' . $uri->getRawAuthority(); |
65
|
|
|
|
66
|
|
|
if ('/' === $uri->getPath()) { |
67
|
|
|
$baseUri .= $uri->hasExplicitTrailingHostSlash() ? '/' : ''; |
68
|
|
|
} else { |
69
|
|
|
$baseUri .= $uri->getPath(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$baseString = strtoupper($method) . '&'; |
73
|
|
|
$baseString .= rawurlencode($baseUri) . '&'; |
74
|
|
|
$baseString .= rawurlencode($this->buildSignatureDataString($signatureData)); |
75
|
|
|
|
76
|
|
|
return base64_encode($this->hash($baseString)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $signatureData |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function buildSignatureDataString(array $signatureData) |
85
|
|
|
{ |
86
|
|
|
return http_build_query($signatureData, null, '&', PHP_QUERY_RFC3986); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Convert booleans to strings, removed unset parameters, and sorts the array |
91
|
|
|
* |
92
|
|
|
* @param array $data Data array |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function prepareParameters($data) |
97
|
|
|
{ |
98
|
|
|
uksort($data, 'strcmp'); |
99
|
|
|
foreach ($data as $key => &$value) { |
100
|
|
|
switch (gettype($value)) { |
101
|
|
|
case 'NULL': |
102
|
|
|
unset($data[$key]); |
103
|
|
|
break; |
104
|
|
|
case 'array': |
105
|
|
|
$data[$key] = self::prepareParameters($value); |
106
|
|
|
break; |
107
|
|
|
case 'boolean': |
108
|
|
|
$data[$key] = $value ? 'true' : 'false'; |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return $data; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
protected function getSigningKey() |
119
|
|
|
{ |
120
|
|
|
$signingKey = rawurlencode($this->credentials->getConsumerSecret()) . '&'; |
121
|
|
|
if ($this->tokenSecret !== null) { |
122
|
|
|
$signingKey .= rawurlencode($this->tokenSecret); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $signingKey; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $data |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
* |
133
|
|
|
* @throws UnsupportedHashAlgorithmException |
134
|
|
|
*/ |
135
|
|
|
protected function hash($data) |
136
|
|
|
{ |
137
|
|
|
switch (strtoupper($this->algorithm)) { |
138
|
|
|
case 'HMAC-SHA1': |
139
|
|
|
return hash_hmac('sha1', $data, $this->getSigningKey(), true); |
140
|
|
|
default: |
141
|
|
|
throw new UnsupportedHashAlgorithmException( |
142
|
|
|
'Unsupported hashing algorithm (' . $this->algorithm . ') used.' |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..