1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 François Kooman <[email protected]>. |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
namespace fkooman\OAuth\Client; |
18
|
|
|
|
19
|
|
|
use RuntimeException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Retrieve an access token using the cURL HTTP client. |
23
|
|
|
*/ |
24
|
|
|
class CurlHttpClient implements HttpClientInterface |
25
|
|
|
{ |
26
|
|
|
public function post(Provider $provider, array $postData) |
27
|
|
|
{ |
28
|
|
|
$ch = curl_init($provider->getTokenEndpoint()); |
29
|
|
|
|
30
|
|
|
$optionsSet = curl_setopt_array( |
31
|
|
|
$ch, |
32
|
|
|
[ |
33
|
|
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, |
34
|
|
|
CURLOPT_SSL_VERIFYPEER => true, |
35
|
|
|
CURLOPT_SSL_VERIFYHOST => 2, |
36
|
|
|
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
37
|
|
|
CURLOPT_USERPWD => sprintf('%s:%s', $provider->getId(), $provider->getSecret()), |
38
|
|
|
CURLOPT_POST => true, |
39
|
|
|
CURLOPT_POSTFIELDS => $postData, |
40
|
|
|
CURLOPT_RETURNTRANSFER => true, |
41
|
|
|
] |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
if (!$optionsSet) { |
45
|
|
|
throw new RuntimeException('unable to set all cURL options'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$jsonResponse = curl_exec($ch); |
49
|
|
|
curl_close($ch); |
50
|
|
|
if (false === $jsonResponse) { |
51
|
|
|
return []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$response = json_decode($jsonResponse, true); |
55
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
56
|
|
|
throw new OAuthException('malformed response from OAuth server token endpoint'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $response; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|