|
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; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(CredentialsInterface $credentials) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->credentials = $credentials; |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $algorithm |
|
33
|
|
|
*/ |
|
34
|
|
|
public function setHashingAlgorithm($algorithm): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->algorithm = $algorithm; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $token |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setTokenSecret($token): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->tokenSecret = $token; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $method |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getSignature(UriInterface $uri, array $params, $method = 'POST') |
|
53
|
|
|
{ |
|
54
|
|
|
parse_str($uri->getQuery(), $queryStringData); |
|
55
|
|
|
|
|
56
|
|
|
foreach (array_merge($queryStringData, $params) as $key => $value) { |
|
57
|
|
|
$signatureData[rawurlencode($key)] = rawurlencode($value); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
ksort($signatureData); |
|
61
|
|
|
|
|
62
|
|
|
// determine base uri |
|
63
|
|
|
$baseUri = $uri->getScheme() . '://' . $uri->getRawAuthority(); |
|
64
|
|
|
|
|
65
|
|
|
if ('/' === $uri->getPath()) { |
|
66
|
|
|
$baseUri .= $uri->hasExplicitTrailingHostSlash() ? '/' : ''; |
|
67
|
|
|
} else { |
|
68
|
|
|
$baseUri .= $uri->getPath(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$baseString = strtoupper($method) . '&'; |
|
72
|
|
|
$baseString .= rawurlencode($baseUri) . '&'; |
|
73
|
|
|
$baseString .= rawurlencode($this->buildSignatureDataString($signatureData)); |
|
74
|
|
|
|
|
75
|
|
|
return base64_encode($this->hash($baseString)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function buildSignatureDataString(array $signatureData) |
|
82
|
|
|
{ |
|
83
|
|
|
$signatureString = ''; |
|
84
|
|
|
$delimiter = ''; |
|
85
|
|
|
foreach ($signatureData as $key => $value) { |
|
86
|
|
|
$signatureString .= $delimiter . $key . '=' . $value; |
|
87
|
|
|
|
|
88
|
|
|
$delimiter = '&'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $signatureString; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getSigningKey() |
|
98
|
|
|
{ |
|
99
|
|
|
$signingKey = rawurlencode($this->credentials->getConsumerSecret()) . '&'; |
|
100
|
|
|
if ($this->tokenSecret !== null) { |
|
101
|
|
|
$signingKey .= rawurlencode($this->tokenSecret); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $signingKey; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $data |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function hash($data) |
|
113
|
|
|
{ |
|
114
|
|
|
switch (strtoupper($this->algorithm)) { |
|
115
|
|
|
case 'HMAC-SHA1': |
|
116
|
|
|
return hash_hmac('sha1', $data, $this->getSigningKey(), true); |
|
117
|
|
|
default: |
|
118
|
|
|
throw new UnsupportedHashAlgorithmException( |
|
119
|
|
|
'Unsupported hashing algorithm (' . $this->algorithm . ') used.' |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
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..