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
43
app/library/OAuth/Adapter/AdapterInterface.php
Normal file
43
app/library/OAuth/Adapter/AdapterInterface.php
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Httpcb\OAuth\Adapter;
|
||||||
|
|
||||||
|
interface AdapterInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Construct a adapter object.
|
||||||
|
*
|
||||||
|
* @param string $provider_name
|
||||||
|
* @param array $options
|
||||||
|
*/
|
||||||
|
public function __construct($provider_name, $options);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the provider name
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getProviderName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the authorization url.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getAuthorizationUrl();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the AccessToken using the temporary code returned from provider.
|
||||||
|
*
|
||||||
|
* @param $code
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public function fetchAccessToken($code);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get resource data from provider.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getResourceData();
|
||||||
|
}
|
||||||
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