Skip to content

Instantly share code, notes, and snippets.

@devi
Created December 15, 2012 18:01
Show Gist options
  • Select an option

  • Save devi/4297692 to your computer and use it in GitHub Desktop.

Select an option

Save devi/4297692 to your computer and use it in GitHub Desktop.
Google Oauth2 Provider for https://github.com/kemo/oauth
<?php defined('SYSPATH') or die('No direct script access.');
/**
* OAuth2 sample controller
*
*/
class Controller_Auth extends Controller {
protected $session;
protected $provider;
protected $client;
protected $token;
protected $content;
public function before()
{
$this->session = Session::instance('cookie');
$this->provider = OAuth2_Provider::factory('Google');
$config = Kohana::$config->load('oauth')->get('google');
$this->client = OAuth2_Client::factory($config);
if ($token = $this->session->get('GOOG_TOKEN'))
{
// Make the access token available
$this->token = $token;
}
}
public function after()
{
$this->response->body($this->content);
}
public function action_index()
{
// Attempt to complete signin
if ($code = Arr::get($_REQUEST, 'code'))
{
// We will need a callback URL for the user to return to
$callback = $this->request->url(TRUE);
// Add the callback URL to the consumer
$this->client->callback($callback);
// Exchange the authorization code for an access token
$token = $this->provider->access_token($this->client, $code);
// Store the access token
$this->session->set('GOOG_TOKEN', $token);
// Refresh the page to prevent errors
$this->redirect($this->request->uri());
}
if ($this->token)
{
// Login succesful
//$this->content = Debug::vars('Access token granted:', $this->token);
// Get userinfo
$user = $this->provider->access_resource($this->token, 'https://www.googleapis.com/oauth2/v2/userinfo');
$this->content = Debug::vars('Userinfo: ', $user);
}
else
{
// We will need a callback URL for the user to return to
$callback = $this->request->url(TRUE);
// Add the callback URL to the consumer
$this->client->callback($callback);
// Get the login URL from the provider
$url = $this->provider->authorize_url($this->client, array(
'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
));
// Redirect to the twitter login page
$this->content = HTML::anchor($url, "Login to Google");
}
}
public function action_logout()
{
if (Arr::get($_GET, 'confirm'))
{
// Delete the access token
$this->session->delete('GOOG_TOKEN');
// Redirect to the demo list
$this->redirect('auth');//$this->request->uri(array('action' => FALSE, 'id' => FALSE)));
}
$this->content = HTML::anchor($this->request->uri().'?confirm=yes', "Logout of Google");
}
}
<?php defined('SYSPATH') or die('No direct script access.');
class OAuth2_Provider_Google extends OAuth2_Provider {
public $name = 'google';
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 authorize_url(OAuth2_Client $client, array $params = NULL)
{
if ( ! isset($params['scope']))
{
// All request tokens must specify the data scope to access
// https://developers.google.com/accounts/docs/OAuth2#basicsteps
throw new Kohana_OAuth_Exception('Required parameter to not passed: :param', array(
':param' => 'scope',
));
}
return parent::authorize_url($client, $params);
}
public function access_token(OAuth2_Client $client, $code, array $params = NULL, array $options = 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($options);
return OAuth2_Token::factory('access', array(
'token' => $response->param('access_token'),
'expires_in' => $response->param('expires_in'),
'refresh_token' => $response->param('refresh_token'),
));
}
public function refresh_token(OAuth2_Client $client, OAuth2_Token_Access $token)
{
$request = OAuth2_Request::factory('token', 'POST', $this->url_access_token(), array(
'grant_type' => 'refresh_token',
'client_id' => $client->id,
'client_secret' => $client->secret,
'refresh_token' => $token->refresh_token,
));
$response = $request->execute($options);
return OAuth2_Token::factory('access', array(
'token' => $response->param('access_token'),
'expires_in' => $response->param('expires_in'),
'refresh_token' => $response->param('refresh_token'),
));
}
public function access_resource(OAuth2_Token_Access $token, $url, array $params = NULL)
{
$request = OAuth2_Request::factory('resource', 'GET', $url, array(
'access_token' => $token->token,
))
->required('access_token', TRUE);
if ($params)
{
$request->params($params);
}
return $this->execute($request);
}
} // End OAuth2_Provider_Google
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment