From aa8c86e5c22879d73c9d1b4524a627d638209aa8 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Wed, 8 Oct 2014 21:37:22 -0400 Subject: [PATCH] Working on oauth --- composer.json | 2 +- src/Auth.php | 11 ++++++++--- src/Resource.php | 49 ++++++++++++++++++++++++------------------------ src/Router.php | 2 ++ 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index 33c66af..dd8cc94 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ }, "require": { "php": ">=5.4", - "league/oauth2-server": "3.*" + "league/oauth2-server": "4.*" }, "autoload": { "psr-4": { diff --git a/src/Auth.php b/src/Auth.php index aaf757e..8bbadfd 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -27,14 +27,19 @@ class Auth extends Object * Rather use an API key and not worry about the password? Do it. Return * true when authentication is successful and false when it is not. */ - public function basic() + public static function basic() { return false; } - public function oauth2() + /** + * OAuth2 + * + * Handles authentication of the access token. + */ + final static public function oauth2() { - return false; + } } diff --git a/src/Resource.php b/src/Resource.php index 9a5dcd2..7bb02d1 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -95,47 +95,46 @@ class Resource extends Object if ($this->auth === true || (isset($this->auth[$method]) && $this->auth[$method])) { - if (!$this->config['pickles']['auth']) + if (!$this->config['auth'][$_SERVER['__version']]) { throw new \Exception('Authentication is not configured properly.', 401); } - // This class should be in the classes directory of the service - $auth = '\\' . $this->config['pickles']['namespace'] . '\\Classes\\Auth'; - - // Strips preceding slashs when there is no namespace - if (strpos($auth, '\\\\') === 0) - { - $auth = substr($auth, 2); - } - - $auth = new $auth(); - - // @todo Remove when switch is implemented - if (!$auth->basic()) - { - throw new \Exception('Invalid authentication credentials.', 401); - } - - // @todo Not yet implemented - /* - switch ($this->config['pickles']['auth']) + switch ($this->config['auth'][$_SERVER['__version']]['strategy']) { case 'basic': - if (!$auth->basic()) + // @todo Check if Auth class has been implemented, if + // not, fallback to the parent + + // This class should be in the classes directory of the service + $auth = '\\' . $this->config['pickles']['namespace'] . '\\Classes\\Auth'; + + // Strips preceding slashs when there is no namespace + if (strpos($auth, '\\\\') === 0) + { + $auth = substr($auth, 2); + } + + // @todo Custom method + if (!$auth::basic()) { throw new \Exception('Invalid authentication credentials.', 401); } break; + case 'oauth2': - $auth->oauth2(); + /* + if (!Auth::oauth2()) + { + throw new \Exception('Invalid access token.', 401); + } + */ break; default: - throw new \Exception('Invalid authentication scheme.', 401); + throw new \Exception('Invalid authentication strategy.', 401); break; } - */ } // Hack together some new globals diff --git a/src/Router.php b/src/Router.php index 5eb9e76..45cb0e6 100644 --- a/src/Router.php +++ b/src/Router.php @@ -46,6 +46,8 @@ class Router extends Object $nouns = []; $uids = []; + $_SERVER['__version'] = substr($version, 1); + // Loops through the components to determine nouns and IDs foreach ($components as $index => $component) {