73 lines
1.6 KiB
PHP
73 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Httpcb\OAuth\Adapter;
|
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider,
|
|
League\OAuth2\Client\Token\AccessToken;
|
|
|
|
class League implements AdapterInterface
|
|
{
|
|
/**
|
|
* @var AbstractProvider
|
|
*/
|
|
protected $_provider;
|
|
|
|
/**
|
|
* @var AccessToken
|
|
*/
|
|
protected $_accessToken;
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
* @throws Exception
|
|
*/
|
|
public function __construct($provider_name, $options)
|
|
{
|
|
$provider_name = ucfirst($provider_name);
|
|
|
|
$className = 'League\OAuth2\Client\Provider\\' . $provider_name;
|
|
$provider = new $className($options);
|
|
|
|
if (!($provider instanceof AbstractProvider)) {
|
|
// TODO: Throw a better exception class :)
|
|
throw new Exception("Provider object must be an instance of League\\OAuth2\\Client\\Provider\\AbstractProvider");
|
|
}
|
|
|
|
$this->_provider = $provider;
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function getProviderName()
|
|
{
|
|
return (new \ReflectionClass($this->_provider))->getShortName();
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function getAuthorizationUrl()
|
|
{
|
|
return $this->_provider->getAuthorizationUrl();
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function fetchAccessToken($code)
|
|
{
|
|
$this->_accessToken = $this->_provider->getAccessToken('authorization_code', [
|
|
'code' => $code
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function getResourceData()
|
|
{
|
|
$resource = $this->_provider->getResourceOwner($this->_accessToken);
|
|
return $resource->toArray();
|
|
}
|
|
}
|