1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Risan\OAuth1\Request; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Risan\OAuth1\Config\ConfigInterface; |
7
|
|
|
use Risan\OAuth1\Signature\SignerInterface; |
8
|
|
|
use Risan\OAuth1\Credentials\TokenCredentials; |
9
|
|
|
use Risan\OAuth1\Credentials\TemporaryCredentials; |
10
|
|
|
use Risan\OAuth1\Credentials\ServerIssuedCredentials; |
11
|
|
|
|
12
|
|
|
class ProtocolParameter implements ProtocolParameterInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The ConfigInterface instance. |
16
|
|
|
* |
17
|
|
|
* @var \Risan\OAuth1\Config\ConfigInterface |
18
|
|
|
*/ |
19
|
|
|
protected $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The SignerInterface instance. |
23
|
|
|
* |
24
|
|
|
* @var \Risan\OAuth1\Signature\SignerInterface |
25
|
|
|
*/ |
26
|
|
|
protected $signer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The NonceGeneratorInterface instance. |
30
|
|
|
* |
31
|
|
|
* @var \Risan\OAuth1\Request\NonceGeneratorInterface |
32
|
|
|
*/ |
33
|
|
|
protected $nonceGenerator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create ProtocolParameter instance. |
37
|
|
|
* |
38
|
|
|
* @param \Risan\OAuth1\Config\ConfigInterface $config |
39
|
|
|
* @param \Risan\OAuth1\Signature\SignerInterface $signer |
40
|
|
|
* @param \Risan\OAuth1\Request\NonceGeneratorInterface $nonceGenerator |
41
|
|
|
*/ |
42
|
22 |
|
public function __construct(ConfigInterface $config, SignerInterface $signer, NonceGeneratorInterface $nonceGenerator) |
43
|
|
|
{ |
44
|
22 |
|
$this->config = $config; |
45
|
22 |
|
$this->signer = $signer; |
46
|
22 |
|
$this->nonceGenerator = $nonceGenerator; |
47
|
22 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
public function getConfig() |
53
|
|
|
{ |
54
|
1 |
|
return $this->config; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
3 |
|
public function getSigner() |
61
|
|
|
{ |
62
|
3 |
|
return $this->signer; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function getNonceGenerator() |
69
|
|
|
{ |
70
|
1 |
|
return $this->nonceGenerator; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
1 |
|
public function getCurrentTimestamp() |
77
|
|
|
{ |
78
|
1 |
|
return (new DateTime())->getTimestamp(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
2 |
|
public function getVersion() |
85
|
|
|
{ |
86
|
2 |
|
return '1.0'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function getBase() |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
1 |
|
'oauth_consumer_key' => $this->config->getClientCredentialsIdentifier(), |
96
|
1 |
|
'oauth_nonce' => $this->nonceGenerator->generate(), |
97
|
1 |
|
'oauth_signature_method' => $this->signer->getMethod(), |
98
|
1 |
|
'oauth_timestamp' => "{$this->getCurrentTimestamp()}", |
99
|
1 |
|
'oauth_version' => $this->getVersion(), |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function forTemporaryCredentials() |
107
|
|
|
{ |
108
|
1 |
|
$parameters = $this->getBase(); |
109
|
|
|
|
110
|
1 |
|
if ($this->config->hasCallbackUri()) { |
111
|
1 |
|
$parameters['oauth_callback'] = $this->config->getCallbackUri(); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
$parameters['oauth_signature'] = $this->getSignature($parameters, $this->config->getTemporaryCredentialsUri()); |
115
|
|
|
|
116
|
1 |
|
return $parameters; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
1 |
|
public function forTokenCredentials(TemporaryCredentials $temporaryCredentials, $verificationCode) |
123
|
|
|
{ |
124
|
1 |
|
$parameters = $this->getBase(); |
125
|
|
|
|
126
|
1 |
|
$parameters['oauth_token'] = $temporaryCredentials->getIdentifier(); |
127
|
|
|
|
128
|
|
|
$requestOptions = [ |
129
|
1 |
|
'form_params' => ['oauth_verifier' => $verificationCode], |
130
|
|
|
]; |
131
|
|
|
|
132
|
1 |
|
$parameters['oauth_signature'] = $this->getSignature( |
133
|
1 |
|
$parameters, |
134
|
1 |
|
$this->config->getTokenCredentialsUri(), |
135
|
1 |
|
$temporaryCredentials, |
136
|
1 |
|
$requestOptions |
137
|
|
|
); |
138
|
|
|
|
139
|
1 |
|
return $parameters; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
1 |
|
public function forProtectedResource(TokenCredentials $tokenCredentials, $httpMethod, $uri, array $requestOptions = []) |
146
|
|
|
{ |
147
|
1 |
|
$parameters = $this->getBase(); |
148
|
|
|
|
149
|
1 |
|
$parameters['oauth_token'] = $tokenCredentials->getIdentifier(); |
150
|
|
|
|
151
|
1 |
|
$parameters['oauth_signature'] = $this->getSignature( |
152
|
1 |
|
$parameters, |
153
|
1 |
|
$this->config->buildUri($uri), |
154
|
1 |
|
$tokenCredentials, |
155
|
1 |
|
$requestOptions, |
156
|
1 |
|
$httpMethod |
157
|
|
|
); |
158
|
|
|
|
159
|
1 |
|
return $parameters; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
1 |
|
public function getSignature(array $protocolParameters, $uri, ServerIssuedCredentials $serverIssuedCredentials = null, array $requestOptions = [], $httpMethod = 'POST') |
166
|
|
|
{ |
167
|
1 |
|
$signatureParameters = $this->signatureParameters($protocolParameters, $requestOptions); |
168
|
|
|
|
169
|
1 |
|
return $this->setupSigner($serverIssuedCredentials) |
170
|
1 |
|
->sign($uri, $signatureParameters, $httpMethod); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Build the signature parameters to be signed. |
175
|
|
|
* |
176
|
|
|
* @param array $protocolParameters |
177
|
|
|
* @param array $requestOptions |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
1 |
|
public function signatureParameters(array $protocolParameters, array $requestOptions = []) |
182
|
|
|
{ |
183
|
1 |
|
$parameters = $protocolParameters; |
184
|
|
|
|
185
|
1 |
|
if ($this->requestOptionsHas($requestOptions, 'query')) { |
186
|
1 |
|
$parameters = array_merge($parameters, $requestOptions['query']); |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
if ($this->requestOptionsHas($requestOptions, 'form_params')) { |
190
|
1 |
|
$parameters = array_merge($parameters, $requestOptions['form_params']); |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
return $parameters; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Setup the signer. |
198
|
|
|
* |
199
|
|
|
* @param \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials |
200
|
|
|
* |
201
|
|
|
* @return \Risan\OAuth1\Signature\SignerInterface |
202
|
|
|
*/ |
203
|
1 |
|
public function setupSigner(ServerIssuedCredentials $serverIssuedCredentials = null) |
204
|
|
|
{ |
205
|
1 |
|
if ($this->shouldSignWithClientCredentials()) { |
206
|
1 |
|
$this->signer->setClientCredentials($this->config->getClientCredentials()); |
207
|
|
|
} |
208
|
|
|
|
209
|
1 |
|
if ($this->shouldSignWithServerIssuedCredentials($serverIssuedCredentials)) { |
210
|
1 |
|
$this->signer->setServerIssuedCredentials($serverIssuedCredentials); |
211
|
|
|
} |
212
|
|
|
|
213
|
1 |
|
return $this->signer; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Should sign with the client credentials. |
218
|
|
|
* |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
2 |
|
public function shouldSignWithClientCredentials() |
222
|
|
|
{ |
223
|
2 |
|
return $this->signer->isKeyBased(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Should sign with the server issued credentials. |
228
|
|
|
* |
229
|
|
|
* @param \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials |
230
|
|
|
* |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
2 |
|
public function shouldSignWithServerIssuedCredentials(ServerIssuedCredentials $serverIssuedCredentials = null) |
234
|
|
|
{ |
235
|
2 |
|
return $this->signer->isKeyBased() && null !== $serverIssuedCredentials; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Check if request options has the given key option. |
240
|
|
|
* |
241
|
|
|
* @param array $requestOptions |
242
|
|
|
* @param string $key |
243
|
|
|
* |
244
|
|
|
* @return bool |
245
|
|
|
*/ |
246
|
2 |
|
public function requestOptionsHas(array $requestOptions, $key) |
247
|
|
|
{ |
248
|
2 |
|
return isset($requestOptions[$key]) && |
249
|
2 |
|
is_array($requestOptions[$key]) && |
250
|
2 |
|
count($requestOptions[$key]) > 0; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|