Enabled OAuth/OAuth2 logins

This commit is contained in:
Deon George
2013-05-27 22:10:41 +10:00
parent 8edac5ad4a
commit ab895eab93
82 changed files with 3052 additions and 154 deletions

View File

@@ -0,0 +1,30 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth API
*
* @package OAuth
* @category Classes
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class Kohana_OAuth2_API {
/**
* Create a new API object.
*
* $API = OAuth2_API::factory($name);
*
* @param string API type
* @param array API options
* @return OAuth2_API
*/
public static function factory(OAuth2_Provider $provider, $name, array $options = NULL)
{
$class = 'OAuth2_API_'.ucfirst($name).'_'.ucfirst($provider->name);
return new $class($options);
}
} // End OAuth2_API
?>

View File

@@ -0,0 +1,17 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth Profile for Facebook
*
* @package OAuth
* @category Classes
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Kohana_OAuth2_API_Profile_Facebook extends OAuth_API {
public function id() {
return $this->profile->id;
}
}
?>

View File

@@ -0,0 +1,17 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth Profile for Google
*
* @package OAuth
* @category Classes
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Kohana_OAuth2_API_Profile_Google extends OAuth_API {
public function id() {
return $this->profile->id;
}
}
?>

View File

@@ -0,0 +1,98 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth Client
*
* @package Kohana/OAuth2
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Kohana_OAuth2_Client {
/**
* Create a new consumer object.
*
* $consumer = OAuth2_Client::factory($options);
*
* @param array consumer options, key and secret are required
* @return OAuth_Consumer
*/
public static function factory(array $options = NULL)
{
return new OAuth2_Client($options);
}
/**
* @var string client id
*/
protected $id;
/**
* @var string client secret
*/
protected $secret;
/**
* @var string callback URL for OAuth authorization completion
*/
protected $callback;
/**
* Sets the consumer key and secret.
*
* @param array consumer options, key and secret are required
* @return void
*/
public function __construct(array $options = NULL)
{
if ( ! isset($options['id']))
{
throw new Kohana_OAuth_Exception('Required option not passed: :option',
array(':option' => 'id'));
}
if ( ! isset($options['secret']))
{
throw new Kohana_OAuth_Exception('Required option not passed: :option',
array(':option' => 'secret'));
}
$this->id = $options['id'];
$this->secret = $options['secret'];
if (isset($options['callback']))
{
$this->callback = $options['callback'];
}
}
/**
* Return the value of any protected class variable.
*
* // Get the client key
* $key = $client->key;
*
* @param string variable name
* @return mixed
*/
public function __get($key)
{
return $this->$key;
}
/**
* Change the client callback.
*
* @param string new consumer callback
* @return $this
*/
public function callback($callback)
{
$this->callback = $callback;
return $this;
}
}

View File

@@ -0,0 +1,127 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth Provider
*
* @package Kohana/OAuth2
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class Kohana_OAuth2_Provider {
private $response;
public static function factory($name, array $options = NULL)
{
$class = 'OAuth2_Provider_'.ucfirst($name);
return new $class($options);
}
/**
* Return the value of any protected class variable.
*
* // Get the provider signature
* $signature = $provider->signature;
*
* @param string variable name
* @return mixed
*/
public function __get($key)
{
return $this->$key;
}
abstract public function url_authorize();
abstract public function url_access_token();
public $name;
protected $scope;
public function url_refresh_token()
{
// By default its the same as access token URL
return $this->url_access_token();
}
public function authorize_url(OAuth2_Client $client, array $params = NULL)
{
// Create a new GET request for a request token with the required parameters
$request = OAuth2_Request::factory('authorize', 'GET', $this->url_authorize(), array(
'response_type' => 'code',
'client_id' => $client->id,
'redirect_uri' => $client->callback,
'scope' => $this->scope,
));
if ($params)
{
// Load user parameters
$request->params($params);
}
return $request->as_url();
}
public function access_token(OAuth2_Client $client, $code, array $params = NULL)
{
$request = OAuth2_Request::factory('token', 'POST', $this->url_access_token(), array(
'grant_type' => 'authorization_code',
'code' => $code,
'client_id' => $client->id,
'client_secret' => $client->secret,
));
if ($client->callback)
{
$request->param('redirect_uri', $client->callback);
}
if ($params)
{
// Load user parameters
$request->params($params);
}
$response = $request->execute();
return OAuth2_Token::factory('access', array(
'token' => $response->param('access_token')
));
}
public function user_details(OAuth2_Client $client, array $params = NULL) {
$request = OAuth2_Request::factory('resource', 'GET', $this->url_user_details(), array(
));
if ($params)
{
// Load user parameters
$request->params($params);
}
// Create a response from the request
$response = $request->execute();
// Store these user details useful
return OAuth2_API::factory($this, 'profile', array(
'provider' => $this->name,
'profile' => json_decode($response),
));
}
/**
* Execute an OAuth2 request, apply any provider-specfic options to the request.
*
* @param object request object
* @param array request options
* @return mixed
*/
public function execute(OAuth2_Request $request, array $options = NULL)
{
return $request->execute($options);
}
}

View File

@@ -0,0 +1,32 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth Facebook Provider
*
* @package Kohana/OAuth2
* @category Provider
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class Kohana_OAuth2_Provider_Facebook extends OAuth2_Provider {
public $name = 'facebook';
protected $scope = 'email';
public function url_authorize()
{
return 'https://www.facebook.com/dialog/oauth';
}
public function url_access_token()
{
return 'https://graph.facebook.com/oauth/access_token';
}
public function url_user_details()
{
return 'https://graph.facebook.com/me';
}
} // End OAuth_Provider_Facebook

View File

@@ -0,0 +1,44 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* OAuth Google Provider
*
* Documents for implementing Google OAuth can be found at
* <http://code.google.com/apis/accounts/docs/OAuth.html>.
* Individual Google APIs have separate documentation. A complete list is
* available at <http://code.google.com/more/>.
*
* [!!] This class does not implement any Google API. It is only an
* implementation of standard OAuth with Google as the service provider.
*
* @package Kohana/OAuth2
* @category Provider
* @author Kohana Team
* @copyright (c) 2010 Kohana Team
* @license http://kohanaframework.org/license
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
* @since 3.0.7
*/
class Kohana_OAuth2_Provider_Google extends OAuth2_Provider {
public $name = 'google';
protected $scope = 'openid email';
public function url_authorize()
{
return 'https://accounts.google.com/o/oauth2/auth';
}
public function url_access_token()
{
return 'https://accounts.google.com/o/oauth2/token';
}
public function url_user_details()
{
return 'https://www.googleapis.com/oauth2/v2/userinfo';
}
} // End OAuth_Provider_Google

View File

@@ -0,0 +1,67 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth v2 Request
*
* @package Kohana/OAuth2
* @category Request
* @author Kohana Team
* @copyright (c) 2010 Kohana Team
* @license http://kohanaframework.org/license
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
* @since 3.0.7
*/
abstract class Kohana_OAuth2_Request extends OAuth_Request {
/**
* @static
* @param string $type
* @param string $method
* @param string $url
* @param array $params
* @return OAuth2_Request
*/
public static function factory($type, $method, $url = NULL, array $params = NULL)
{
$class = 'OAuth2_Request_'.ucfirst($type);
return new $class($method, $url, $params);
}
/**
* @var boolean send Authorization header?
*/
public $send_header = TRUE;
protected $auth_params = '/^access_token$/';
/**
* Convert the request parameters into an `Authorization` header.
*
* $header = $request->as_header();
*
* [!!] This method implements [OAuth 2.0 v22 Spec 7.1](http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-7.1).
*
* @return string
*/
public function as_header()
{
if ($access = Arr::get($this->params, 'access_token'))
{
if (is_string($this->send_header))
{
$header = $this->send_header;
}
else
{
$header = 'Bearer';
}
$access = $header.' '.$access;
}
return $access ? $access : NULL;
}
}

View File

@@ -0,0 +1,20 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth Request Authorize
*
* @package Kohana/OAuth2
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class Kohana_OAuth2_Request_Authorize extends OAuth2_Request {
protected $name = 'authorize';
public function execute(array $options = NULL)
{
return Request::current()->redirect($this->as_url());
}
}

View File

@@ -0,0 +1,19 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth Request Resource
*
* @package Kohana/OAuth2
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class Kohana_OAuth2_Request_Resource extends OAuth2_Request {
protected $name = 'resource';
protected $auth_params = '/^(access_token|redirect_uri)/';
public $send_header = 'OAuth';
public $body = '';
}

View File

@@ -0,0 +1,22 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth Request Token
*
* @package Kohana/OAuth2
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class Kohana_OAuth2_Request_Token extends OAuth2_Request {
protected $auth_params = '/^(grant_type|code|client_id|client_secret|redirect_uri)$/';
protected $name = 'token';
public function execute(array $options = NULL)
{
return OAuth_Response::factory(parent::execute($options));
}
}

View File

@@ -0,0 +1,29 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth Provider
*
* @package Kohana/OAuth2
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class Kohana_OAuth2_Token extends OAuth_Token {
/**
* Create a new token object.
*
* $token = OAuth2_Token::factory($name);
*
* @param string token type
* @param array token options
* @return OAuth2_Token
*/
public static function factory($name, array $options = NULL)
{
$class = 'OAuth2_Token_'.ucfirst($name);
return new $class($options);
}
}

View File

@@ -0,0 +1,7 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
abstract class Kohana_OAuth2_Token_Access extends OAuth2_Token {
protected $name = 'access';
}