adding app/library/OAuth/Adapter classes (interface and League adapter)
This commit is contained in:
parent
205b8477f2
commit
556a62b34b
2 changed files with 116 additions and 0 deletions
73
app/library/OAuth/Adapter/League.php
Normal file
73
app/library/OAuth/Adapter/League.php
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in a new issue