rename app/library/OAuth.php to app/library/OAuth/Client.php and change implementation to use Adapter classes.
This commit is contained in:
parent
21c869a06a
commit
b3eb25d221
2 changed files with 59 additions and 103 deletions
|
|
@ -1,103 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Httpcb;
|
||||
|
||||
use Phalcon\Mvc\User\Component;
|
||||
|
||||
use Opauth;
|
||||
|
||||
class OAuth extends Component
|
||||
{
|
||||
/**
|
||||
* Configuration
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_config = array(
|
||||
'path' => '/oauth/',
|
||||
'callback_url' => '/oauth/callback'
|
||||
);
|
||||
|
||||
protected $_oauth;
|
||||
|
||||
protected $_callbackName = 'callback';
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$config = $this->objectToArray($config->oauth);
|
||||
|
||||
$this->_config = array_merge($this->_config, $config);
|
||||
|
||||
$this->_oauth = new Opauth($this->_config, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|null|void
|
||||
*/
|
||||
public function perform()
|
||||
{
|
||||
$strategy = $this->dispatcher->getParam('strategy', null, false);
|
||||
|
||||
if ($strategy == $this->_callbackName) {
|
||||
return $this->getResponse();
|
||||
}
|
||||
|
||||
$this->_oauth->run();
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|string
|
||||
*/
|
||||
public function getResponse()
|
||||
{
|
||||
$response = null;
|
||||
switch($this->_oauth->env['callback_transport']) {
|
||||
case 'session':
|
||||
$response = $this->session->get('opauth');
|
||||
$this->session->remove('opauth');
|
||||
break;
|
||||
case 'post':
|
||||
$response = unserialize(base64_decode( $_POST['opauth'] ));
|
||||
break;
|
||||
case 'get':
|
||||
$response = unserialize(base64_decode( $_GET['opauth'] ));
|
||||
break;
|
||||
}
|
||||
|
||||
$ret = $this->_validate($response, $reason);
|
||||
if ($ret === false) {
|
||||
return $reason;
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function objectToArray($object)
|
||||
{
|
||||
if(!is_object($object) && !is_array($object))
|
||||
{
|
||||
return $object;
|
||||
}
|
||||
if(is_object($object))
|
||||
{
|
||||
$object = get_object_vars( $object );
|
||||
}
|
||||
return array_map(array($this,"objectToArray"), $object );
|
||||
}
|
||||
|
||||
protected function _validate($response, &$reason)
|
||||
{
|
||||
if (isset($response['auth']) &&
|
||||
isset($response['timestamp']) &&
|
||||
isset($response['signature'])) {
|
||||
|
||||
$hash = sha1(print_r($response['auth'], true));
|
||||
|
||||
return $this->_oauth->validate($hash, $response['timestamp'],
|
||||
$response['signature'], $reason);
|
||||
}
|
||||
|
||||
$reason = "Invalid auth response";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
59
app/library/OAuth/Client.php
Normal file
59
app/library/OAuth/Client.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace Httpcb\OAuth;
|
||||
|
||||
use Httpcb\OAuth\Adapter\AdapterInterface;
|
||||
|
||||
class Client
|
||||
{
|
||||
/**
|
||||
* Adapter to use.
|
||||
*
|
||||
* @var AdapterInterface
|
||||
*/
|
||||
protected $_adapter;
|
||||
|
||||
/**
|
||||
* Access Token
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_token;
|
||||
|
||||
public function __construct(AdapterInterface $adapter)
|
||||
{
|
||||
$this->_adapter = $adapter;
|
||||
}
|
||||
|
||||
public function getAuthorizationUrl()
|
||||
{
|
||||
return $this->_adapter->getAuthorizationUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
* @return UserDataInterface
|
||||
*/
|
||||
public function authenticate($code)
|
||||
{
|
||||
$this->_adapter->fetchAccessToken($code);
|
||||
|
||||
return $this->getUserData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UserDataInterface
|
||||
*/
|
||||
public function getUserData()
|
||||
{
|
||||
$data = $this->_adapter->getResourceData();
|
||||
return $this->_createUserData($data);
|
||||
}
|
||||
|
||||
protected function _createUserData(array $data)
|
||||
{
|
||||
$name = $this->_adapter->getProviderName();
|
||||
$class = "Httpcb\\OAuth\\UserData\\{$name}";
|
||||
return new $class($data);
|
||||
}
|
||||
}
|
||||
Reference in a new issue