100 lines
2.4 KiB
PHP
100 lines
2.4 KiB
PHP
<?php
|
|
|
|
//use Opauth;
|
|
use Phalcon\Mvc\User\Component;
|
|
|
|
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;
|
|
}
|
|
}
|