1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Risan\OAuth1\Request; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use GuzzleHttp\Psr7\Uri; |
7
|
|
|
use Risan\OAuth1\ConfigInterface; |
8
|
|
|
use Risan\OAuth1\Signature\HmacSha1Signer; |
9
|
|
|
use Risan\OAuth1\Signature\SignerInterface; |
10
|
|
|
use Risan\OAuth1\Credentials\TemporaryCredentials; |
11
|
|
|
use Risan\OAuth1\Signature\KeyBasedSignerInterface; |
12
|
|
|
use Risan\OAuth1\Credentials\ServerIssuedCredentials; |
13
|
|
|
|
14
|
|
|
class RequestConfig implements RequestConfigInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The ConfigInterface instance. |
18
|
|
|
* |
19
|
|
|
* @var \Risan\OAuth1\ConfigInterface |
20
|
|
|
*/ |
21
|
|
|
protected $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The SignerInterface instance. |
25
|
|
|
* |
26
|
|
|
* @var \Risan\OAuth1\Signature\SignerInterface |
27
|
|
|
*/ |
28
|
|
|
protected $signer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The NonceGeneratorInterface instance. |
32
|
|
|
* |
33
|
|
|
* @var \Risan\OAuth1\Request\NonceGeneratorInterface |
34
|
|
|
*/ |
35
|
|
|
protected $nonceGenerator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create RequestBuilder instance. |
39
|
|
|
* |
40
|
|
|
* @param \Risan\OAuth1\ConfigInterface $config |
41
|
|
|
* @param \Risan\OAuth1\Signature\SignerInterface $signer |
42
|
|
|
* @param \Risan\OAuth1\Request\NonceGeneratorInterface $nonceGenerator |
43
|
|
|
*/ |
44
|
17 |
|
public function __construct(ConfigInterface $config, SignerInterface $signer, NonceGeneratorInterface $nonceGenerator) |
45
|
|
|
{ |
46
|
17 |
|
$this->config = $config; |
47
|
17 |
|
$this->signer = $signer; |
48
|
17 |
|
$this->nonceGenerator = $nonceGenerator; |
49
|
|
|
|
50
|
17 |
|
if ($this->signer->isKeyBased()) { |
51
|
2 |
|
$this->signer->setClientCredentials($config->getClientCredentials()); |
52
|
|
|
} |
53
|
17 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
1 |
|
public function getConfig() |
59
|
|
|
{ |
60
|
1 |
|
return $this->config; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
1 |
|
public function getSigner() |
67
|
|
|
{ |
68
|
1 |
|
return $this->signer; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
1 |
|
public function getNonceGenerator() |
75
|
|
|
{ |
76
|
1 |
|
return $this->nonceGenerator; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
*/ |
82
|
2 |
|
public function getCurrentTimestamp() |
83
|
|
|
{ |
84
|
2 |
|
return (new DateTime)->getTimestamp(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function getTemporaryCredentialsUrl() |
91
|
|
|
{ |
92
|
1 |
|
return $this->config->getTemporaryCredentialsUrl(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function getTemporaryCredentialsAuthorizationHeader() |
99
|
|
|
{ |
100
|
1 |
|
$parameters = $this->getBaseProtocolParameters(); |
101
|
|
|
|
102
|
1 |
|
if ($this->config->hasCallbackUri()) { |
103
|
1 |
|
$parameters['oauth_callback'] = $this->config->getCallbackUri(); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
$this->addSignatureParameter($parameters, $this->getTemporaryCredentialsUrl()); |
107
|
|
|
|
108
|
1 |
|
return $this->normalizeProtocolParameters($parameters); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritDoc} |
113
|
|
|
*/ |
114
|
1 |
|
public function buildAuthorizationUrl(TemporaryCredentials $temporaryCredentials) |
115
|
|
|
{ |
116
|
1 |
|
return $this->appendQueryParametersToUri($this->config->getAuthorizationUrl(), [ |
117
|
1 |
|
'oauth_token' => $temporaryCredentials->getIdentifier(), |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
*/ |
124
|
1 |
|
public function getTokenCredentialsUrl() |
125
|
|
|
{ |
126
|
1 |
|
return $this->config->getTokenCredentialsUrl(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
1 |
|
public function getTokenCredentialsAuthorizationHeader(TemporaryCredentials $temporaryCredentials, $verificationCode) |
133
|
|
|
{ |
134
|
1 |
|
$parameters = $this->getBaseProtocolParameters(); |
135
|
|
|
|
136
|
1 |
|
$parameters['oauth_token'] = $temporaryCredentials->getIdentifier(); |
137
|
|
|
|
138
|
1 |
|
$this->addSignatureParameter( |
139
|
1 |
|
$parameters, |
140
|
1 |
|
$this->getTokenCredentialsUrl(), |
141
|
1 |
|
$temporaryCredentials, |
142
|
|
|
[ |
143
|
|
|
'form_params' => [ |
144
|
1 |
|
'oauth_verifier' => $verificationCode, |
145
|
|
|
], |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
|
149
|
1 |
|
return $this->normalizeProtocolParameters($parameters); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get base protocol parameters for the authorization header. |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
1 |
|
public function getBaseProtocolParameters() |
158
|
|
|
{ |
159
|
|
|
return [ |
160
|
1 |
|
'oauth_consumer_key' => $this->config->getClientCredentialsIdentifier(), |
161
|
1 |
|
'oauth_nonce' => $this->nonceGenerator->generate(), |
162
|
1 |
|
'oauth_signature_method' => $this->signer->getMethod(), |
163
|
1 |
|
'oauth_timestamp' => "{$this->getCurrentTimestamp()}", |
164
|
1 |
|
'oauth_version' => '1.0', |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Add signature parameter to the given protocol parameters. |
170
|
|
|
* |
171
|
|
|
* @param array &$parameters |
172
|
|
|
* @param string $uri |
173
|
|
|
* @param \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials |
174
|
|
|
* @param array $requestOptions |
175
|
|
|
* @param string $httpMethod |
176
|
|
|
*/ |
177
|
1 |
|
public function addSignatureParameter(array &$parameters, $uri, ServerIssuedCredentials $serverIssuedCredentials = null, array $requestOptions = [], $httpMethod = 'POST') |
178
|
|
|
{ |
179
|
1 |
|
$requestParameters = $parameters; |
180
|
|
|
|
181
|
1 |
|
if ($this->requestOptionsHas($requestOptions, 'query')) { |
182
|
1 |
|
$requestParameters = array_merge($requestParameters, $requestOptions['query']); |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
if ($this->requestOptionsHas($requestOptions, 'form_params')) { |
186
|
1 |
|
$requestParameters = array_merge($requestParameters, $requestOptions['form_params']); |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
if ($this->signer->isKeyBased() && $serverIssuedCredentials instanceof ServerIssuedCredentials) { |
190
|
|
|
$this->signer->setServerIssuedCredentials($setServerIssuedCredentials); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
$parameters['oauth_signature'] = $this->signer->sign($uri, $requestParameters, $httpMethod); |
194
|
|
|
|
195
|
1 |
|
return $parameters; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Check if request options has the given key option. |
200
|
|
|
* |
201
|
|
|
* @param array $requestOptions |
202
|
|
|
* @param string $key |
203
|
|
|
* @return boolean |
204
|
|
|
*/ |
205
|
2 |
|
public function requestOptionsHas(array $requestOptions, $key) |
206
|
|
|
{ |
207
|
2 |
|
return isset($requestOptions[$key]) && |
208
|
2 |
|
is_array($requestOptions[$key]) && |
209
|
2 |
|
count($requestOptions[$key]) > 0; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Append query parameters to URI. |
214
|
|
|
* |
215
|
|
|
* @param string $uri |
216
|
|
|
* @param array $parameters |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
2 |
|
public function appendQueryParametersToUri($uri, array $parameters) |
220
|
|
|
{ |
221
|
2 |
|
$uri = new Uri($uri); |
222
|
|
|
|
223
|
2 |
|
parse_str($uri->getQuery(), $queryParameters); |
224
|
|
|
|
225
|
2 |
|
$mergedParameters = array_merge($queryParameters, $parameters); |
226
|
|
|
|
227
|
2 |
|
return (string) $uri->withQuery(http_build_query($mergedParameters)); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Normalize protocol parameters to be used as authorization header. |
232
|
|
|
* |
233
|
|
|
* @param array $parameters |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public function normalizeProtocolParameters(array $parameters) |
237
|
|
|
{ |
238
|
1 |
|
array_walk($parameters, function (&$value, $key) { |
239
|
1 |
|
$value = rawurlencode($key) . '="' . rawurlencode($value) . '"'; |
240
|
1 |
|
}); |
241
|
|
|
|
242
|
|
|
return 'OAuth ' . implode(', ', $parameters); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.