Skip to content

Instantly share code, notes, and snippets.

@kiall
Created August 22, 2011 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiall/1162955 to your computer and use it in GitHub Desktop.
Save kiall/1162955 to your computer and use it in GitHub Desktop.
<?php defined('SYSPATH') or die('No direct script access.');
/**
*
*
* @package OAuth2
* @category Library
* @author Managed I.T.
* @copyright (c) 2011 Managed I.T.
*/
class Kohana_Controller_OAuth2_Endpoints extends Controller {
/**
* @var OAuth2_Provider
*/
protected $_oauth;
public function before()
{
parent::before();
$this->_oauth = OAuth2_Provider::factory($this->request);
}
public function action_authorize()
{
try
{
Auth::instance()->force_login(ORM::factory('user', 1));
/**
* Check if the user is logged in
*/
if (Auth::instance()->logged_in())
{
$user = Auth::instance()->get_user();
$auth_params = $this->_oauth->validate_authorize_params();
// Form has been submitted
if ($this->request->method() == Request::POST)
{
$accepted = ($this->request->post('accepted') == 'Yes');
$accepted = TRUE;
// Validate custom form stuff .. whatever
$redirect_url = $this->_oauth->authorize($accepted, $user->pk());
// Redirect the user back to the application
$this->request->redirect($redirect_url);
}
$client = Model_OAuth2_Client::find_client($auth_params['client_id']);
$this->response->body(View::factory('oauth2/authorize', array(
'auth_params' => $auth_params,
'client' => $client,
'user' => $user,
)));
}
else
{
$this->request->redirect(Route::url('login'));
}
}
catch (OAuth2_Exception_InvalidClient $e)
{
throw new HTTP_Exception_401($e->getMessage());
}
catch (OAuth2_Exception_InvalidRequest $e)
{
throw new HTTP_Exception_400($e->getMessage());
}
catch (OAuth2_Exception_InvalidGrant $e)
{
throw new HTTP_Exception_400($e->getMessage());
}
catch (OAuth2_Exception $e)
{
// We should never actually get here..
Kohana::$log->add(Log::ERROR, "Unknown OAuth2_Exception thrown in authorize. Class: :class", array(
':class' => get_class($e),
));
throw new HTTP_Exception_400($e->getMessage());
}
}
public function action_token()
{
$this->response->headers('Content-Type', File::mime_by_ext('json'));
$this->response->body($this->_oauth->token());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment