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
|
|
|
public function __construct(ConfigInterface $config, SignerInterface $signer, NonceGeneratorInterface $nonceGenerator) |
45
|
|
|
{ |
46
|
|
|
$this->config = $config; |
47
|
|
|
$this->signer = $signer; |
48
|
|
|
$this->nonceGenerator = $nonceGenerator; |
49
|
|
|
|
50
|
|
|
if ($this->signer->isKeyBased()) { |
51
|
|
|
$this->signer->setClientCredentials($config->getClientCredentials()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
|
|
public function getConfig() |
59
|
|
|
{ |
60
|
|
|
return $this->config; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function getSigner() |
67
|
|
|
{ |
68
|
|
|
return $this->signer; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
|
|
public function getNonceGenerator() |
75
|
|
|
{ |
76
|
|
|
return $this->nonceGenerator; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
*/ |
82
|
|
|
public function getCurrentTimestamp() |
83
|
|
|
{ |
84
|
|
|
return (new DateTime)->getTimestamp(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
|
|
public function getTemporaryCredentialsUrl() |
91
|
|
|
{ |
92
|
|
|
return $this->config->getTemporaryCredentialsUrl(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function getTemporaryCredentialsAuthorizationHeader() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$parameters = $this->getBaseProtocolParameters(); |
101
|
|
|
|
102
|
|
|
if ($this->config->hasCallbackUri()) { |
103
|
|
|
$parameters['oauth_callback'] = $this->config->getCallbackUri(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->addSignatureParameter($parameters, $this->getTemporaryCredentialsUrl()); |
107
|
|
|
|
108
|
|
|
return $this->normalizeProtocolParameters($parameters); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritDoc} |
113
|
|
|
*/ |
114
|
|
|
public function buildAuthorizationUrl(TemporaryCredentials $temporaryCredentials) |
115
|
|
|
{ |
116
|
|
|
return $this->appendQueryParametersToUri($this->config->getAuthorizationUrl(), [ |
117
|
|
|
'oauth_token' => $temporaryCredentials->getIdentifier(), |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
*/ |
124
|
|
|
public function getTokenCredentialsUrl() |
125
|
|
|
{ |
126
|
|
|
return $this->config->getTokenCredentialsUrl(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
|
|
public function getTokenCredentialsAuthorizationHeader(TemporaryCredentials $temporaryCredentials, $verificationCode) |
133
|
|
|
{ |
134
|
|
|
$parameters = $this->getBaseProtocolParameters(); |
135
|
|
|
|
136
|
|
|
$parameters['oauth_token'] = $temporaryCredentials->getIdentifier(); |
137
|
|
|
|
138
|
|
|
$this->addSignatureParameter( |
139
|
|
|
$parameters, |
140
|
|
|
$this->getTokenCredentialsUrl(), |
141
|
|
|
$temporaryCredentials, |
142
|
|
|
[ |
143
|
|
|
'form_params' => [ |
144
|
|
|
'oauth_verifier' => $verificationCode, |
145
|
|
|
], |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
return $this->normalizeProtocolParameters($parameters); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get base protocol parameters for the authorization header. |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function getBaseProtocolParameters() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
return [ |
160
|
|
|
'oauth_consumer_key' => $this->config->getClientCredentialsIdentifier(), |
161
|
|
|
'oauth_nonce' => $this->nonceGenerator->generate(), |
162
|
|
|
'oauth_signature_method' => $this->signer->getMethod(), |
163
|
|
|
'oauth_timestamp' => "{$this->getCurrentTimestamp()}", |
164
|
|
|
'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
|
|
|
public function addSignatureParameter(array &$parameters, $uri, ServerIssuedCredentials $serverIssuedCredentials = null, array $requestOptions = [], $httpMethod = 'POST') |
178
|
|
|
{ |
179
|
|
|
$requestParameters = $parameters; |
180
|
|
|
|
181
|
|
|
if ($this->requestOptionsHas($requestOptions, 'query')) { |
182
|
|
|
$requestParameters = array_merge($requestParameters, $requestOptions['query']); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($this->requestOptionsHas($requestOptions, 'form_params')) { |
186
|
|
|
$requestParameters = array_merge($requestParameters, $requestOptions['form_params']); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($this->signer->isKeyBased() && $serverIssuedCredentials instanceof ServerIssuedCredentials) { |
190
|
|
|
$this->signer->setServerIssuedCredentials($setServerIssuedCredentials); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$parameters['oauth_signature'] = $this->signer->sign($uri, $requestParameters, $httpMethod); |
194
|
|
|
|
195
|
|
|
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
|
|
|
public function requestOptionsHas(array $requestOptions, $key) |
206
|
|
|
{ |
207
|
|
|
return isset($requestOptions[$key]) && |
208
|
|
|
is_array($requestOptions[$key]) && |
209
|
|
|
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
|
|
|
public function appendQueryParametersToUri($uri, array $parameters) |
220
|
|
|
{ |
221
|
|
|
$uri = new Uri($uri); |
222
|
|
|
|
223
|
|
|
parse_str($uri->getQuery(), $queryParameters); |
224
|
|
|
|
225
|
|
|
$mergedParameters = array_merge($queryParameters, $parameters); |
226
|
|
|
|
227
|
|
|
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
|
|
|
array_walk($parameters, function (&$value, $key) { |
239
|
|
|
$value = rawurlencode($key) . '="' . rawurlencode($value) . '"'; |
240
|
|
|
}); |
241
|
|
|
|
242
|
|
|
return 'OAuth ' . implode(', ', $parameters); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.