Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a component that acts as a wrapper for the oauth2 lib and take…

…s care of loading all required files
  • Loading branch information...
commit 3404b32c5bbc0da19782b99b0e02a5947cab8821 1 parent ada1497
@burzum burzum authored
View
66 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');
+ }
+
+}
View
19 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 {
@@ -12,13 +10,19 @@ 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('*');
}
@@ -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();
@@ -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();
}
Please sign in to comment.
Something went wrong with that request. Please try again.