|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OAuth1\Flows; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use OAuth1\Contracts\Tokens\AccessTokenInterface; |
|
7
|
|
|
|
|
8
|
|
|
trait GrantedFlow |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Granted access token. |
|
12
|
|
|
* |
|
13
|
|
|
* @var \OAuth1\Contracts\Tokens\AccessTokenInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $grantedAccessToken; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get resource base url. |
|
19
|
|
|
* |
|
20
|
|
|
* @return string|null |
|
21
|
|
|
*/ |
|
22
|
14 |
|
public function resourceBaseUrl() |
|
23
|
|
|
{ |
|
24
|
14 |
|
return $this->config()->resourceBaseUrl(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Set resource base url. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $url |
|
31
|
|
|
* |
|
32
|
|
|
* @return \OAuth1\Contracts\GrantedFlowInterface |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function setResourceBaseUrl($url) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$this->config()->setResourceBaseUrl($url); |
|
37
|
|
|
|
|
38
|
1 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Build resource url. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $url |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
12 |
|
public function resourceUrl($url) |
|
49
|
|
|
{ |
|
50
|
12 |
|
if (is_null($this->resourceBaseUrl())) { |
|
51
|
1 |
|
return $url; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
11 |
|
return $this->resourceBaseUrl().$url; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Send HTTP request to protected resource. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $method |
|
61
|
|
|
* @param string $url |
|
62
|
|
|
* @param array $options |
|
63
|
|
|
* |
|
64
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
65
|
|
|
*/ |
|
66
|
9 |
|
public function request($method, $url, $options = []) |
|
67
|
|
|
{ |
|
68
|
9 |
|
if (!$this->grantedAccessToken() instanceof AccessTokenInterface) { |
|
69
|
1 |
|
throw new InvalidArgumentException('No access token has been set.'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
8 |
|
$resourceUrl = $this->resourceUrl($url); |
|
73
|
|
|
|
|
74
|
|
|
$headers = [ |
|
75
|
8 |
|
'headers' => $this->grantedRequestHeaders($this->grantedAccessToken(), $resourceUrl, $method, $options), |
|
76
|
8 |
|
]; |
|
77
|
|
|
|
|
78
|
8 |
|
return $this->httpClient()->request($method, $resourceUrl, array_merge($options, $headers)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Send HTTP GET request to protected resource. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $url |
|
85
|
|
|
* @param array $options |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function get($url, $options = []) |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->request('GET', $url, $options); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Send POST DELETE request to protected resource. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $url |
|
98
|
|
|
* @param array $options |
|
99
|
|
|
* |
|
100
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function post($url, $options = []) |
|
103
|
|
|
{ |
|
104
|
1 |
|
return $this->request('POST', $url, $options); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Send HTTP PUT request to protected resource. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $url |
|
111
|
|
|
* @param array $options |
|
112
|
|
|
* |
|
113
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function put($url, $options = []) |
|
116
|
|
|
{ |
|
117
|
1 |
|
return $this->request('PUT', $url, $options); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Send HTTP PATCH request to protected resource. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $url |
|
124
|
|
|
* @param array $options |
|
125
|
|
|
* |
|
126
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function patch($url, $options = []) |
|
129
|
|
|
{ |
|
130
|
1 |
|
return $this->request('PATCH', $url, $options); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Send HTTP DELETE request to protected resource. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $url |
|
137
|
|
|
* @param array $options |
|
138
|
|
|
* |
|
139
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function delete($url, $options = []) |
|
142
|
|
|
{ |
|
143
|
1 |
|
return $this->request('DELETE', $url, $options); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Send HTTP HEAD request to protected resource. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $url |
|
150
|
|
|
* @param array $options |
|
151
|
|
|
* |
|
152
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
153
|
|
|
*/ |
|
154
|
1 |
|
public function head($url, $options = []) |
|
155
|
|
|
{ |
|
156
|
1 |
|
return $this->request('HEAD', $url, $options); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Send HTTP OPTIONS request to protected resource. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $url |
|
163
|
|
|
* @param array $options |
|
164
|
|
|
* |
|
165
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function options($url, $options = []) |
|
168
|
|
|
{ |
|
169
|
1 |
|
return $this->request('OPTIONS', $url, $options); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Get granted access token. |
|
174
|
|
|
* |
|
175
|
|
|
* @return \OAuth1\Contracts\Tokens\AccessTokenInterface |
|
176
|
|
|
*/ |
|
177
|
10 |
|
public function grantedAccessToken() |
|
178
|
|
|
{ |
|
179
|
10 |
|
return $this->grantedAccessToken; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Set granted access token. |
|
184
|
|
|
* |
|
185
|
|
|
* @param \OAuth1\Contracts\Tokens\AccessTokenInterface $accessToken |
|
186
|
|
|
* |
|
187
|
|
|
* @return \OAuth1\Contracts\GrantedFlowInterface |
|
188
|
|
|
*/ |
|
189
|
9 |
|
public function setGrantedAccessToken(AccessTokenInterface $accessToken) |
|
190
|
|
|
{ |
|
191
|
9 |
|
$this->grantedAccessToken = $accessToken; |
|
192
|
|
|
|
|
193
|
9 |
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Get granted request headers. |
|
198
|
|
|
* |
|
199
|
|
|
* @param \OAuth1\Contracts\Tokens\AccessTokenInterface $accessToken |
|
200
|
|
|
* @param string $url |
|
201
|
|
|
* @param string $httpVerb |
|
202
|
|
|
* @param array $options |
|
203
|
|
|
* |
|
204
|
|
|
* @return array |
|
205
|
|
|
*/ |
|
206
|
10 |
|
public function grantedRequestHeaders(AccessTokenInterface $accessToken, $url, $httpVerb, $options = []) |
|
207
|
|
|
{ |
|
208
|
10 |
|
$parameters = $this->baseProtocolParameters(); |
|
209
|
|
|
|
|
210
|
10 |
|
if (isset($options['query'])) { |
|
211
|
1 |
|
$parameters = array_merge($parameters, $options['query']); |
|
212
|
1 |
|
} |
|
213
|
|
|
|
|
214
|
10 |
|
$parameters['oauth_token'] = $accessToken->key(); |
|
215
|
10 |
|
$parameters['oauth_signature'] = $this->signer()->setTokenSecret($accessToken->secret())->sign($url, $parameters, $httpVerb); |
|
216
|
|
|
|
|
217
|
|
|
return [ |
|
218
|
10 |
|
'Authorization' => $this->authorizationHeaders($parameters), |
|
219
|
10 |
|
]; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Get http client instance. |
|
224
|
|
|
* |
|
225
|
|
|
* @return \OAuth1\Contracts\HttpClientInterface |
|
226
|
|
|
*/ |
|
227
|
|
|
abstract public function httpClient(); |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Get client configuration. |
|
231
|
|
|
* |
|
232
|
|
|
* @return \OAuth1\Contracts\ConfigInterface |
|
233
|
|
|
*/ |
|
234
|
|
|
abstract public function config(); |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get signer. |
|
238
|
|
|
* |
|
239
|
|
|
* @return \OAuth1\Contracts\Signers\SignerInterface |
|
240
|
|
|
*/ |
|
241
|
|
|
abstract public function signer(); |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Get OAuth base protocol parameters. |
|
245
|
|
|
* |
|
246
|
|
|
* @return array |
|
247
|
|
|
*/ |
|
248
|
|
|
abstract public function baseProtocolParameters(); |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Build authorization headers. |
|
252
|
|
|
* |
|
253
|
|
|
* @param array $parameters |
|
254
|
|
|
* |
|
255
|
|
|
* @return string |
|
256
|
|
|
*/ |
|
257
|
|
|
abstract public function authorizationHeaders(array $parameters); |
|
258
|
|
|
} |
|
259
|
|
|
|