Created
September 10, 2012 16:02
-
-
Save alexbilbie/3691756 to your computer and use it in GitHub Desktop.
CodeIgniter OAuth controller example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Oauth extends CI_Controller { | |
private $oauth; | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->load->model('authdb'); | |
$this->authServer = new Oauth2\Authentication\Server(); | |
$this->authServer->registerDbAbstractor($this->authdb); | |
} | |
public function index() | |
{ | |
try { | |
$params = $this->authServer->checkClientAuthoriseParams(); | |
} | |
catch (Oauth2\Authentication\ClientException $e) | |
{ | |
show_error('<strong>Client exception (' . $this->authServer->exceptionCodes[$e->getCode()] . '):</strong> ' . $e->getMessage()); | |
} | |
catch (Oauth2\Authentication\UserException $e) | |
{ | |
show_error('<strong>User exception (' . $this->authServer->exceptionCodes[$e->getCode()] . '):</strong> ' . $e); | |
} | |
catch (Oauth2\Authentication\ServerException $e) | |
{ | |
show_error('<strong>Server exception (' . $this->authServer->exceptionCodes[$e->getCode()] . '):</strong> ' . $e); | |
} | |
catch (Exception $e) | |
{ | |
show_error('<strong>General exception:</strong> ' . $e->getMessage()); | |
} | |
$this->session->set_userdata('oauth', $params); | |
redirect(site_url('oauth/signin')); | |
} | |
public function signin() | |
{ | |
$user_id = $this->session->userdata('user_id'); | |
$oauth = $this->session->userdata('oauth'); | |
// If the user is already signed in then redirect them | |
if ($user_id !== FALSE && $oauth !== FALSE) | |
{ | |
redirect(site_url('oauth/authorise')); | |
} | |
if ($oauth === FALSE) | |
{ | |
show_error('No OAuth session initiated or the request parameters haven\'t been saved properly'); | |
} | |
$data = array(); | |
if ($this->input->post('username') && $this->input->post('password')) | |
{ | |
$u = $this->input->post('username'); | |
$p = $this->input->post('password'); | |
if ($user_id = $this->auth->check($u, $p)) | |
{ | |
$this->session->set_userdata('user_id', $user_id); | |
redirect(site_url('oauth/authorise')); | |
} | |
else | |
{ | |
$data['error'] = 'Invalid username/password'; | |
} | |
} | |
$this->load->view('oauth/signin', $data); | |
} | |
public function authorise() | |
{ | |
$user_id = $this->session->userdata('user_id'); | |
$oauth = $this->session->userdata('oauth'); | |
if ($user_id === FALSE) | |
{ | |
redirect(site_url('oauth/signin')); | |
} | |
if ($oauth === FALSE) | |
{ | |
show_error('No OAuth session initiated or the request parameters haven\'t been saved properly'); | |
} | |
if ($state = $this->input->post('state')) | |
{ | |
switch ($state) | |
{ | |
case 'approve': | |
$code = $authserver->newAuthoriseRequest('user', $user_id, $oauth); | |
// Redirect the user back to the client with the authorization code | |
redirect($authserver->redirectUri($oauth['redirect_uri'], array( | |
'code' => $code, | |
'state' => isset($oauth['state']) ? $oauth['state'] : '' | |
))); | |
break; | |
case 'deny': | |
redirect($authserver->redirectUri($oauth['redirect_uri'], array( | |
'error' => $authserver->exceptionCodes[2], | |
'error_message' => $authserver->errors[$authserver->exceptionCodes[2]], | |
'state' => isset($oauth['state']) ? $oauth['state'] : '' | |
))); | |
break; | |
} | |
} | |
else | |
{ | |
$this->load->view('oauth/authorise'); | |
} | |
} | |
public function access_token() | |
{ | |
try { | |
$response = $authserver->issueAccessToken(); | |
} | |
catch (Oauth2\Authentication\ClientException $e) | |
{ | |
$response = array( | |
'error' => $authserver->exceptionCodes[$e->getCode()], | |
'error_description' => $e->getMessage() | |
); | |
} | |
catch (Oauth2\Authentication\ServerException $e) | |
{ | |
$response = array( | |
'error' => $authserver->exceptionCodes[$e->getCode()], | |
'error_description' => $e->getMessage() | |
); | |
} | |
catch (Oauth2\Authentication\Exception $e) | |
{ | |
$response = array( | |
'error' => $authserver->exceptionCodes[$e->getCode()], | |
'error_description' => $e->getMessage() | |
); | |
} | |
catch (Exception $e) | |
{ | |
$response = array( | |
'error' => 'undefined_error', | |
'error_description' => $e->getMessage() | |
); | |
} | |
header('Content-type: application/json'); | |
echo json_encode($response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment