blob: 2e13eb373c4e108f4f014b289d380f66afb2c79d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
<?php
namespace OAuth\OAuth1\Signature;
use OAuth\Common\Consumer\CredentialsInterface;
use OAuth\Common\Http\Uri\UriInterface;
use OAuth\OAuth1\Signature\Exception\UnsupportedHashAlgorithmException;
class Signature implements SignatureInterface
{
/**
* @var Credentials
*/
protected $credentials;
/**
* @var string
*/
protected $algorithm;
/**
* @var string
*/
protected $tokenSecret = null;
/**
* @param CredentialsInterface $credentials
*/
public function __construct(CredentialsInterface $credentials)
{
$this->credentials = $credentials;
}
/**
* @param string $algorithm
*/
public function setHashingAlgorithm($algorithm)
{
$this->algorithm = $algorithm;
}
/**
* @param string $token
*/
public function setTokenSecret($token)
{
$this->tokenSecret = $token;
}
/**
* @param UriInterface $uri
* @param array $params
* @param string $method
*
* @return string
*/
public function getSignature(UriInterface $uri, array $params, $method = 'POST')
{
parse_str($uri->getQuery(), $queryStringData);
foreach (array_merge($queryStringData, $params) as $key => $value) {
$signatureData[rawurlencode($key)] = rawurlencode($value);
}
ksort($signatureData);
// determine base uri
$baseUri = $uri->getScheme() . '://' . $uri->getRawAuthority();
if ('/' === $uri->getPath()) {
$baseUri .= $uri->hasExplicitTrailingHostSlash() ? '/' : '';
} else {
$baseUri .= $uri->getPath();
}
$baseString = strtoupper($method) . '&';
$baseString .= rawurlencode($baseUri) . '&';
$baseString .= rawurlencode($this->buildSignatureDataString($signatureData));
return base64_encode($this->hash($baseString));
}
/**
* @param array $signatureData
*
* @return string
*/
protected function buildSignatureDataString(array $signatureData)
{
$signatureString = '';
$delimiter = '';
foreach ($signatureData as $key => $value) {
$signatureString .= $delimiter . $key . '=' . $value;
$delimiter = '&';
}
return $signatureString;
}
/**
* @return string
*/
protected function getSigningKey()
{
$signingKey = rawurlencode($this->credentials->getConsumerSecret()) . '&';
if ($this->tokenSecret !== null) {
$signingKey .= rawurlencode($this->tokenSecret);
}
return $signingKey;
}
/**
* @param string $data
*
* @return string
*
* @throws UnsupportedHashAlgorithmException
*/
protected function hash($data)
{
switch (strtoupper($this->algorithm)) {
case 'HMAC-SHA1':
return hash_hmac('sha1', $data, $this->getSigningKey(), true);
default:
throw new UnsupportedHashAlgorithmException(
'Unsupported hashing algorithm (' . $this->algorithm . ') used.'
);
}
}
}
|