| 1 |
<?php |
| 2 |
/*! |
| 3 |
* Hybridauth |
| 4 |
* https://hybridauth.github.io | https://github.com/hybridauth/hybridauth |
| 5 |
* (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Hybridauth\Adapter; |
| 9 |
|
| 10 |
use Hybridauth\Exception\NotImplementedException; |
| 11 |
use Hybridauth\Exception\InvalidArgumentException; |
| 12 |
use Hybridauth\Exception\HttpClientFailureException; |
| 13 |
use Hybridauth\Exception\HttpRequestFailedException; |
| 14 |
use Hybridauth\Storage\StorageInterface; |
| 15 |
use Hybridauth\Storage\Session; |
| 16 |
use Hybridauth\Logger\LoggerInterface; |
| 17 |
use Hybridauth\Logger\Logger; |
| 18 |
use Hybridauth\HttpClient\HttpClientInterface; |
| 19 |
use Hybridauth\HttpClient\Curl as HttpClient; |
| 20 |
use Hybridauth\Data; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class AbstractAdapter |
| 24 |
*/ |
| 25 |
abstract class AbstractAdapter implements AdapterInterface |
| 26 |
{ |
| 27 |
use DataStoreTrait; |
| 28 |
|
| 29 |
/** |
| 30 |
* Provider ID (unique name). |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $providerId = ''; |
| 35 |
|
| 36 |
/** |
| 37 |
* Specific Provider config. |
| 38 |
* |
| 39 |
* @var mixed |
| 40 |
*/ |
| 41 |
protected $config = []; |
| 42 |
|
| 43 |
/** |
| 44 |
* Extra Provider parameters. |
| 45 |
* |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
protected $params; |
| 49 |
|
| 50 |
/** |
| 51 |
* Callback url |
| 52 |
* |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
protected $callback = ''; |
| 56 |
|
| 57 |
/** |
| 58 |
* Storage. |
| 59 |
* |
| 60 |
* @var StorageInterface |
| 61 |
*/ |
| 62 |
public $storage; |
| 63 |
|
| 64 |
/** |
| 65 |
* HttpClient. |
| 66 |
* |
| 67 |
* @var HttpClientInterface |
| 68 |
*/ |
| 69 |
public $httpClient; |
| 70 |
|
| 71 |
/** |
| 72 |
* Logger. |
| 73 |
* |
| 74 |
* @var LoggerInterface |
| 75 |
*/ |
| 76 |
public $logger; |
| 77 |
|
| 78 |
/** |
| 79 |
* Whether to validate API status codes of http responses |
| 80 |
* |
| 81 |
* @var bool |
| 82 |
*/ |
| 83 |
protected $validateApiResponseHttpCode = true; |
| 84 |
|
| 85 |
/** |
| 86 |
* Common adapters constructor. |
| 87 |
* |
| 88 |
* @param array $config |
| 89 |
* @param HttpClientInterface $httpClient |
| 90 |
* @param StorageInterface $storage |
| 91 |
* @param LoggerInterface $logger |
| 92 |
*/ |
| 93 |
public function __construct( |
| 94 |
$config = [], |
| 95 |
HttpClientInterface $httpClient = null, |
| 96 |
StorageInterface $storage = null, |
| 97 |
LoggerInterface $logger = null |
| 98 |
) { |
| 99 |
$this->providerId = (new \ReflectionClass($this))->getShortName(); |
| 100 |
|
| 101 |
$this->config = new Data\Collection($config); |
| 102 |
|
| 103 |
$this->setHttpClient($httpClient); |
| 104 |
|
| 105 |
$this->setStorage($storage); |
| 106 |
|
| 107 |
$this->setLogger($logger); |
| 108 |
|
| 109 |
$this->configure(); |
| 110 |
|
| 111 |
$this->logger->debug(sprintf('Initialize %s, config: ', get_class($this)), $config); |
| 112 |
|
| 113 |
$this->initialize(); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Load adapter's configuration |
| 118 |
*/ |
| 119 |
abstract protected function configure(); |
| 120 |
|
| 121 |
/** |
| 122 |
* Adapter initializer |
| 123 |
*/ |
| 124 |
abstract protected function initialize(); |
| 125 |
|
| 126 |
/** |
| 127 |
* {@inheritdoc} |
| 128 |
*/ |
| 129 |
abstract public function isConnected(); |
| 130 |
|
| 131 |
/** |
| 132 |
* {@inheritdoc} |
| 133 |
*/ |
| 134 |
public function apiRequest($url, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
| 135 |
{ |
| 136 |
throw new NotImplementedException('Provider does not support this feature.'); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* {@inheritdoc} |
| 141 |
*/ |
| 142 |
public function maintainToken() |
| 143 |
{ |
| 144 |
// Nothing needed for most providers |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* {@inheritdoc} |
| 149 |
*/ |
| 150 |
public function getUserProfile() |
| 151 |
{ |
| 152 |
throw new NotImplementedException('Provider does not support this feature.'); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* {@inheritdoc} |
| 157 |
*/ |
| 158 |
public function getUserContacts() |
| 159 |
{ |
| 160 |
throw new NotImplementedException('Provider does not support this feature.'); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* {@inheritdoc} |
| 165 |
*/ |
| 166 |
public function getUserPages() |
| 167 |
{ |
| 168 |
throw new NotImplementedException('Provider does not support this feature.'); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* {@inheritdoc} |
| 173 |
*/ |
| 174 |
public function getUserActivity($stream) |
| 175 |
{ |
| 176 |
throw new NotImplementedException('Provider does not support this feature.'); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* {@inheritdoc} |
| 181 |
*/ |
| 182 |
public function setUserStatus($status) |
| 183 |
{ |
| 184 |
throw new NotImplementedException('Provider does not support this feature.'); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* {@inheritdoc} |
| 189 |
*/ |
| 190 |
public function setPageStatus($status, $pageId) |
| 191 |
{ |
| 192 |
throw new NotImplementedException('Provider does not support this feature.'); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* {@inheritdoc} |
| 197 |
*/ |
| 198 |
public function disconnect() |
| 199 |
{ |
| 200 |
$this->clearStoredData(); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* {@inheritdoc} |
| 205 |
*/ |
| 206 |
public function getAccessToken() |
| 207 |
{ |
| 208 |
$tokenNames = [ |
| 209 |
'access_token', |
| 210 |
'access_token_secret', |
| 211 |
'token_type', |
| 212 |
'refresh_token', |
| 213 |
'expires_in', |
| 214 |
'expires_at', |
| 215 |
]; |
| 216 |
|
| 217 |
$tokens = []; |
| 218 |
|
| 219 |
foreach ($tokenNames as $name) { |
| 220 |
if ($this->getStoredData($name)) { |
| 221 |
$tokens[$name] = $this->getStoredData($name); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
return $tokens; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* {@inheritdoc} |
| 230 |
*/ |
| 231 |
public function setAccessToken($tokens = []) |
| 232 |
{ |
| 233 |
$this->clearStoredData(); |
| 234 |
|
| 235 |
foreach ($tokens as $token => $value) { |
| 236 |
$this->storeData($token, $value); |
| 237 |
} |
| 238 |
|
| 239 |
// Re-initialize token parameters. |
| 240 |
$this->initialize(); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* {@inheritdoc} |
| 245 |
*/ |
| 246 |
public function setHttpClient(HttpClientInterface $httpClient = null) |
| 247 |
{ |
| 248 |
$this->httpClient = $httpClient ?: new HttpClient(); |
| 249 |
|
| 250 |
if ($this->config->exists('curl_options') && method_exists($this->httpClient, 'setCurlOptions')) { |
| 251 |
$this->httpClient->setCurlOptions($this->config->get('curl_options')); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* {@inheritdoc} |
| 257 |
*/ |
| 258 |
public function getHttpClient() |
| 259 |
{ |
| 260 |
return $this->httpClient; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* {@inheritdoc} |
| 265 |
*/ |
| 266 |
public function setStorage(StorageInterface $storage = null) |
| 267 |
{ |
| 268 |
$this->storage = $storage ?: new Session(); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* {@inheritdoc} |
| 273 |
*/ |
| 274 |
public function getStorage() |
| 275 |
{ |
| 276 |
return $this->storage; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* {@inheritdoc} |
| 281 |
*/ |
| 282 |
public function setLogger(LoggerInterface $logger = null) |
| 283 |
{ |
| 284 |
$this->logger = $logger ?: new Logger( |
| 285 |
$this->config->get('debug_mode'), |
| 286 |
$this->config->get('debug_file') |
| 287 |
); |
| 288 |
|
| 289 |
if (method_exists($this->httpClient, 'setLogger')) { |
| 290 |
$this->httpClient->setLogger($this->logger); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* {@inheritdoc} |
| 296 |
*/ |
| 297 |
public function getLogger() |
| 298 |
{ |
| 299 |
return $this->logger; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Set Adapter's API callback url |
| 304 |
* |
| 305 |
* @param string $callback |
| 306 |
* |
| 307 |
* @throws InvalidArgumentException |
| 308 |
*/ |
| 309 |
protected function setCallback($callback) |
| 310 |
{ |
| 311 |
if (!filter_var($callback, FILTER_VALIDATE_URL)) { |
| 312 |
throw new InvalidArgumentException('A valid callback url is required.'); |
| 313 |
} |
| 314 |
|
| 315 |
$this->callback = $callback; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Overwrite Adapter's API endpoints |
| 320 |
* |
| 321 |
* @param array|Data\Collection $endpoints |
| 322 |
*/ |
| 323 |
protected function setApiEndpoints($endpoints = null) |
| 324 |
{ |
| 325 |
if (empty($endpoints)) { |
| 326 |
return; |
| 327 |
} |
| 328 |
|
| 329 |
$collection = is_array($endpoints) ? new Data\Collection($endpoints) : $endpoints; |
| 330 |
|
| 331 |
$this->apiBaseUrl = $collection->get('api_base_url') ?: $this->apiBaseUrl; |
| 332 |
$this->authorizeUrl = $collection->get('authorize_url') ?: $this->authorizeUrl; |
| 333 |
$this->accessTokenUrl = $collection->get('access_token_url') ?: $this->accessTokenUrl; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
/** |
| 338 |
* Validate signed API responses Http status code. |
| 339 |
* |
| 340 |
* Since the specifics of error responses is beyond the scope of RFC6749 and OAuth Core specifications, |
| 341 |
* Hybridauth will consider any HTTP status code that is different than '200 OK' as an ERROR. |
| 342 |
* |
| 343 |
* @param string $error String to pre append to message thrown in exception |
| 344 |
* |
| 345 |
* @throws HttpClientFailureException |
| 346 |
* @throws HttpRequestFailedException |
| 347 |
*/ |
| 348 |
protected function validateApiResponse($error = '') |
| 349 |
{ |
| 350 |
$error .= !empty($error) ? '. ' : ''; |
| 351 |
|
| 352 |
if ($this->httpClient->getResponseClientError()) { |
| 353 |
throw new HttpClientFailureException( |
| 354 |
$error . 'HTTP client error: ' . $this->httpClient->getResponseClientError() . '.' |
| 355 |
); |
| 356 |
} |
| 357 |
|
| 358 |
// if validateApiResponseHttpCode is set to false, we by pass verification of http status code |
| 359 |
if (!$this->validateApiResponseHttpCode) { |
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
$status = $this->httpClient->getResponseHttpCode(); |
| 364 |
|
| 365 |
if ($status < 200 || $status > 299) { |
| 366 |
throw new HttpRequestFailedException( |
| 367 |
$error . 'HTTP error ' . $this->httpClient->getResponseHttpCode() . |
| 368 |
'. Raw Provider API response: ' . $this->httpClient->getResponseBody() . '.' |
| 369 |
); |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|