Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Adding a component that acts as a wrapper for the oauth2 lib and take…
Browse files Browse the repository at this point in the history
…s care of loading all required files
  • Loading branch information
Florian Krämer committed Jan 11, 2012
1 parent ada1497 commit 3404b32
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
66 changes: 66 additions & 0 deletions Controller/Component/Oauth2Component.php
@@ -0,0 +1,66 @@
<?php
App::uses('Component', 'Controller');

class Oauth2Component extends Component {

/**
* Constructor
*
* @param Component Collection
* @param array $settings
* @return void
*/
public function __construct(ComponentCollection $collection, $settings = array()) {
parent::__construct($collection, $settings);
$this->__loadLibs();

$defaults = array(
'storage' => 'OAuth2StorageCake');

$this->settings = Set::merge($defaults, $settings);
}

/**
* Proxy for the oauth2 lib methods
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments) {
if (isset($this->Oauth2) && method_exists($this->Oauth2, $name)) {
return call_user_func_array(array($this->Oauth2, $name), $arguments);
}
}

public function initialize(Controller $Controller) {
$this->Oauth2 = $this->getOauthInstance();
}

/**
* Creates a new Oauth2 lib instance with the configured storage adapter
*
* @return Oauth instance
*/
public function getOauthInstance() {
if ($this->settings['storage'] == 'OAuth2StorageCake') {
App::uses('OAuth2StorageCake', 'Oauth2.Lib');
}
$this->Storage = new $this->settings['storage'];
$this->Oauth2 = new OAuth2($this->Storage);
}

/**
* Loads all required libs
*
* @return void
*/
protected function __loadLibs() {
$basePath = CakePlugin::path('Oauth2') . 'Vendor' . DS . 'oauth2-php' . DS . 'lib'. DS;
require_once($basePath . 'Oauth2.php');
require_once($basePath . 'IOAuth2Storage.php');
require_once($basePath . 'IOAuth2GrantCode.php');
require_once($basePath . 'IOAuth2RefreshTokens.php');
}

}
19 changes: 10 additions & 9 deletions Controller/ServerController.php
@@ -1,6 +1,4 @@
<?php
require_once(CakePlugin::path('Oauth2') . 'Vendor' . DS . 'oauth2-php' . DS . 'lib' . DS . 'OAuth2.php');
App::uses('OAuth2StorageCake', 'Oauth2.Lib');
App::uses('Oauth2AppController', 'Oauth2.Controller');

class ServerController extends Oauth2AppController {
Expand All @@ -11,14 +9,20 @@ class ServerController extends Oauth2AppController {
*/
public $name = 'Server';

/**
* Components
*
* @var array
*/
public $components = array('Oauth2.Oauth2');

/**
* beforeFilter callback
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
$this->Storage = new OAuth2StorageCake();
if (isset($this->Auth)) {
$this->Auth->allow('*');
}
Expand All @@ -30,16 +34,14 @@ public function beforeFilter() {
public function authorize() {
// Clickjacking prevention (supported by IE8+, FF3.6.9+, Opera10.5+, Safari4+, Chrome 4.1.249.1042+)
header('X-Frame-Options: DENY');

$oauth = new OAuth2($this->Storage);

if ($this->request->is('post')) {
$userId = 42;
$oauth->finishClientAuthorization($this->request->data["accept"] == "Yep", $userId, $this->request->data);
$this->Oauth2->finishClientAuthorization($this->request->data["accept"] == "Yep", $userId, $this->request->data);
}

try {
$authParams = $oauth->getAuthorizeParams();
$authParams = $this->Oauth2->getAuthorizeParams();
$this->set(compact('authParams'));
} catch (OAuth2ServerException $oauthError) {
$oauthError->sendHttpResponse();
Expand All @@ -52,9 +54,8 @@ public function authorize() {
*
*/
public function token() {
$oauth = new OAuth2($this->Storage);
try {
$oauth->grantAccessToken();
$this->Oauth2->grantAccessToken();
} catch (OAuth2ServerException $oauthError) {
$oauthError->sendHttpResponse();
}
Expand Down

0 comments on commit 3404b32

Please sign in to comment.