1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ApiVideo\Client\Buzz; |
4
|
|
|
|
5
|
|
|
use ApiVideo\Client\Exception\AuthenticationFailed; |
6
|
|
|
use Buzz\Browser; |
7
|
|
|
use Buzz\Client\ClientInterface; |
8
|
|
|
use Buzz\Message\Factory\FactoryInterface; |
9
|
|
|
use Buzz\Message\MessageInterface; |
10
|
|
|
use Buzz\Message\RequestInterface; |
11
|
|
|
use Buzz\Message\Response; |
12
|
|
|
|
13
|
|
|
class OAuthBrowser extends Browser |
14
|
|
|
{ |
15
|
|
|
/** @var array */ |
16
|
|
|
private $authPayload = array(); |
17
|
|
|
|
18
|
|
|
/** @var bool */ |
19
|
|
|
private $isAuthenticated = false; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
private $headers; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $baseUri; |
26
|
|
|
|
27
|
|
|
const SDK_VERSION = "1.8.9"; |
28
|
|
|
|
29
|
|
|
public function __construct(ClientInterface $client = null, FactoryInterface $factory = null, $applicationName = "") |
30
|
|
|
{ |
31
|
|
|
parent::__construct($client, $factory); |
32
|
|
|
|
33
|
|
|
$this->headers = array('User-Agent' => 'api.video SDK (php; v:' . self::SDK_VERSION . '; ' . $applicationName . ')'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @param string $baseUri |
39
|
|
|
*/ |
40
|
|
|
public function setBaseUri($baseUri) |
41
|
|
|
{ |
42
|
|
|
$this->baseUri = $baseUri; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* @param $apiKey |
48
|
|
|
*/ |
49
|
|
|
public function authenticate($apiKey) |
50
|
|
|
{ |
51
|
|
|
$this->authPayload = array( |
52
|
|
|
'apiKey' => $apiKey, |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$this->getAccessToken(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
* @throws AuthenticationFailed |
61
|
|
|
*/ |
62
|
|
|
private function getAccessToken() |
63
|
|
|
{ |
64
|
|
|
if (empty($this->authPayload)) { |
65
|
|
|
throw new AuthenticationFailed('Authentication failed'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* @var $response Response */ |
69
|
|
|
$response = |
70
|
|
|
parent::post( |
71
|
|
|
$this->baseUri.'/auth/api-key', |
72
|
|
|
array(), |
73
|
|
|
json_encode($this->authPayload) |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
if ($response->getStatusCode() >= 400) { |
77
|
|
|
throw new AuthenticationFailed('Authentication failed'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->headers['Authorization'] = 'Bearer '.json_decode($response->getContent())->access_token; |
81
|
|
|
$this->isAuthenticated = true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
* @param Response|MessageInterface $response |
87
|
|
|
* @return Response |
88
|
|
|
*/ |
89
|
|
|
private function check(Response $response) |
90
|
|
|
{ |
91
|
|
|
$status = $response->getStatusCode(); |
92
|
|
|
|
93
|
|
|
// Refresh access token automatically and transparently |
94
|
|
|
if ( |
95
|
|
|
401 === $status && |
96
|
|
|
array('application/problem+json') === $response->getHeader('Content-Type', false) && |
97
|
|
|
false !== strpos($response->getContent(), 'access_denied') |
98
|
|
|
) { |
99
|
|
|
$lastRequest = $this->getLastRequest(); |
100
|
|
|
|
101
|
|
|
// Refresh access token |
102
|
|
|
$this->getAccessToken(); |
103
|
|
|
|
104
|
|
|
// Re issue the request |
105
|
|
|
$headers = $lastRequest->getHeaders(); |
106
|
|
|
foreach ($headers as $key => $header) { |
107
|
|
|
if (0 === stripos($header, 'Authorization:')) { |
108
|
|
|
unset($headers[$key]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
$headers[] = 'Authorization: '.$this->headers['Authorization']; |
112
|
|
|
$lastRequest->setHeaders($headers); |
113
|
|
|
/** @var Response $response */ |
114
|
|
|
$response = $this->send($lastRequest); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $response; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* |
122
|
|
|
* @param string $path |
123
|
|
|
* @param array $headers |
124
|
|
|
* @return Response |
125
|
|
|
*/ |
126
|
|
|
public function get($path, $headers = array()) |
127
|
|
|
{ |
128
|
|
|
return $this->check( |
129
|
|
|
parent::get( |
130
|
|
|
$this->baseUri.$path, |
131
|
|
|
array_merge($this->headers, $headers) |
132
|
|
|
) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* |
138
|
|
|
* @param string $path |
139
|
|
|
* @param array $headers |
140
|
|
|
* @param string $content |
141
|
|
|
* @return Response |
142
|
|
|
*/ |
143
|
|
|
public function post($path, $headers = array(), $content = '') |
144
|
|
|
{ |
145
|
|
|
return $this->check( |
146
|
|
|
parent::post( |
147
|
|
|
$this->baseUri.$path, |
148
|
|
|
array_merge($this->headers, $headers), |
149
|
|
|
$content |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Submit a form (upload file for example) |
156
|
|
|
* |
157
|
|
|
* @param string $path |
158
|
|
|
* @param array $fields |
159
|
|
|
* @param string $method |
160
|
|
|
* @param array $headers |
161
|
|
|
* @return Response |
162
|
|
|
*/ |
163
|
|
|
public function submit($path, array $fields = array(), $method = RequestInterface::METHOD_POST, $headers = array()) |
164
|
|
|
{ |
165
|
|
|
return $this->check( |
166
|
|
|
parent::submit( |
167
|
|
|
$this->baseUri.$path, |
168
|
|
|
$fields, |
169
|
|
|
$method, |
170
|
|
|
array_merge($this->headers, $headers) |
171
|
|
|
) |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* |
177
|
|
|
* @param string $path |
178
|
|
|
* @param array $headers |
179
|
|
|
* @param string $content |
180
|
|
|
* @return Response |
181
|
|
|
*/ |
182
|
|
|
public function put($path, $headers = array(), $content = '') |
183
|
|
|
{ |
184
|
|
|
return $this->check( |
185
|
|
|
parent::put( |
186
|
|
|
$this->baseUri.$path, |
187
|
|
|
array_merge($this->headers, $headers), |
188
|
|
|
$content |
189
|
|
|
) |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* |
195
|
|
|
* @param string $path |
196
|
|
|
* @param array $headers |
197
|
|
|
* @param string $content |
198
|
|
|
* @return Response |
199
|
|
|
*/ |
200
|
|
|
public function patch($path, $headers = array(), $content = '') |
201
|
|
|
{ |
202
|
|
|
return $this->check( |
203
|
|
|
parent::patch( |
204
|
|
|
$this->baseUri.$path, |
205
|
|
|
array_merge($this->headers, $headers), |
206
|
|
|
$content |
207
|
|
|
) |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* |
213
|
|
|
* @param string $path |
214
|
|
|
* @param array $headers |
215
|
|
|
* @param string $content |
216
|
|
|
* @return Response |
217
|
|
|
*/ |
218
|
|
|
public function delete($path, $headers = array(), $content = '') |
219
|
|
|
{ |
220
|
|
|
return $this->check( |
221
|
|
|
parent::delete( |
222
|
|
|
$this->baseUri.$path, |
223
|
|
|
array_merge($this->headers, $headers), |
224
|
|
|
$content |
225
|
|
|
) |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* |
231
|
|
|
* @param string $path |
232
|
|
|
* @param array $headers |
233
|
|
|
* @return Response |
234
|
|
|
*/ |
235
|
|
|
public function head($path, $headers = array()) |
236
|
|
|
{ |
237
|
|
|
return $this->check( |
238
|
|
|
parent::head( |
239
|
|
|
$this->baseUri.$path, |
240
|
|
|
array_merge($this->headers, $headers) |
241
|
|
|
) |
242
|
|
|
); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* |
247
|
|
|
* @param string $path |
248
|
|
|
* @param array $headers |
249
|
|
|
* @return Response |
250
|
|
|
*/ |
251
|
|
|
public function options($path, array $headers = array()) |
252
|
|
|
{ |
253
|
|
|
return $this->check( |
254
|
|
|
$this->call( |
255
|
|
|
$this->baseUri.$path, |
256
|
|
|
'OPTIONS', |
257
|
|
|
array_merge($this->headers, $headers) |
258
|
|
|
) |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|