|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2017 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace fkooman\OAuth\Client; |
|
20
|
|
|
|
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
|
|
23
|
|
|
class CurlHttpClient implements HttpClientInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var resource */ |
|
26
|
|
|
private $curlChannel; |
|
27
|
|
|
|
|
28
|
|
|
/** @var bool */ |
|
29
|
|
|
private $httpsOnly = true; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
if (false === $this->curlChannel = curl_init()) { |
|
34
|
|
|
throw new RuntimeException('unable to create cURL channel'); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function __destruct() |
|
39
|
|
|
{ |
|
40
|
|
|
curl_close($this->curlChannel); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function setHttpsOnly($httpsOnly) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->httpsOnly = (bool) $httpsOnly; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function post(Provider $provider, array $postData) |
|
49
|
|
|
{ |
|
50
|
|
|
$curlOptions = [ |
|
51
|
|
|
CURLOPT_URL => $provider->getTokenEndpoint(), |
|
52
|
|
|
CURLOPT_POSTFIELDS => http_build_query($postData), |
|
53
|
|
|
CURLOPT_USERPWD => sprintf('%s:%s', $provider->getId(), $provider->getSecret()), |
|
54
|
|
|
CURLOPT_HEADER => 0, |
|
55
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
|
56
|
|
|
CURLOPT_FOLLOWLOCATION => 0, |
|
57
|
|
|
CURLOPT_PROTOCOLS => $this->httpsOnly ? CURLPROTO_HTTPS : CURLPROTO_HTTPS | CURLPROTO_HTTP, |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
if (false === curl_setopt_array($this->curlChannel, $curlOptions)) { |
|
61
|
|
|
throw new RuntimeException('unable to set cURL options'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (false === $responseData = curl_exec($this->curlChannel)) { |
|
65
|
|
|
$curlError = curl_error($this->curlChannel); |
|
66
|
|
|
throw new RuntimeException(sprintf('failure performing the HTTP request: "%s"', $curlError)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$decodedResponseData = json_decode($responseData, true); |
|
70
|
|
|
if (is_null($decodedResponseData) && JSON_ERROR_NONE !== json_last_error()) { |
|
71
|
|
|
throw new RuntimeException('unable to decode JSON'); |
|
72
|
|
|
} |
|
73
|
|
|
if (!is_array($decodedResponseData)) { |
|
74
|
|
|
throw new RuntimeException('unexpected JSON data'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $decodedResponseData; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|