From 820e1529531c8be0103b6956ab7ef1f72c2e8c7a Mon Sep 17 00:00:00 2001 From: hw Date: Tue, 30 Aug 2016 16:03:15 +0530 Subject: [PATCH] Fix OAuth2 authentication plugin based on review --- .../OAuth2ServerAuthentication.php | 57 ++++++++++++++----- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/src/Plugin/authentication/OAuth2ServerAuthentication.php b/src/Plugin/authentication/OAuth2ServerAuthentication.php index 8621ad02..558a655c 100644 --- a/src/Plugin/authentication/OAuth2ServerAuthentication.php +++ b/src/Plugin/authentication/OAuth2ServerAuthentication.php @@ -3,6 +3,7 @@ namespace Drupal\restful\Plugin\authentication; use Drupal\Component\Plugin\PluginBase; +use Drupal\restful\Exception\ServerConfigurationException; use Drupal\restful\Exception\UnauthorizedException; use Drupal\restful\Http\RequestInterface; use Drupal\restful\Plugin\ResourcePluginManager; @@ -11,18 +12,30 @@ * Authentication support for oauth2_server. * * @Authentication( - * id = "oauth2_auth", + * id = "oauth2", * label = "OAuth2 authentication", * description = "Authenticate requests based on oauth2_server auth.", * ) */ class OAuth2ServerAuthentication extends Authentication { + /** + * The resource manager. + * + * @var \Drupal\restful\Resource\ResourceManagerInterface + */ + protected $resourceManager; + + public function __construct(array $configuration, $plugin_id, $plugin_definition) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->resourceManager = restful()->getResourceManager(); + } + /** * {@inheritdoc} */ public function applies(RequestInterface $request) { - return module_exists('oauth2_server') && $this->getResourcePluginIdFromRequest(); + return module_exists('oauth2_server') && $this->getOAuth2Info($request); } /** @@ -31,38 +44,52 @@ public function applies(RequestInterface $request) { public function authenticate(RequestInterface $request) { $oauth2_info = $this->getOAuth2Info($request); if (!$oauth2_info) { - return NULL; + throw new ServerConfigurationException('The resource uses OAuth2 authentication but does not specify the OAuth2 server.'); } $result = oauth2_server_check_access($oauth2_info['server'], $oauth2_info['scope']); if ($result instanceof \OAuth2\Response) { throw new UnauthorizedException($result->getResponseBody(), $result->getStatusCode()); } - elseif (is_array($result) && !empty($result['user_id'])) { - return user_load($result['user_id']); + elseif (empty($result['user_id'])) { + return NULL; } + return user_load($result['user_id']); } -// protected function getOAuth2Info() { -// return [variable_get('oauth2_server_restful_server'), variable_get('oauth2_server_restful_scope')]; -// } - - protected function getOAuth2Info($request) { + /** + * Get OAuth2 information from the request. + * + * @param \Drupal\restful\Http\RequestInterface $request + * The request. + * + * @return array|null + * Simple associative array with the following keys: + * - server: The OAuth2 server to authenticate against. + * - scope: The scope required for the resource. + */ + protected function getOAuth2Info(RequestInterface $request) { $plugin_id = $this->getResourcePluginIdFromRequest(); - $plugin = ResourcePluginManager::create('cache', $request)->getDefinition($plugin_id); + $plugin_definition = ResourcePluginManager::create('cache', $request)->getDefinition($plugin_id); - $server = !empty($plugin['oauth2Server']) ? $plugin['oauth2Server'] : variable_get('oauth2_server_restful_server'); + $server = !empty($plugin_definition['oauth2Server']) ? $plugin_definition['oauth2Server'] : variable_get('oauth2_server_restful_server'); if (!$server) { return NULL; } - $scope = !empty($plugin['oauth2Scope']) ? $plugin['oauth2Scope'] : variable_get('oauth2_server_restful_scope'); + $scope = !empty($plugin_definition['oauth2Scope']) ? $plugin_definition['oauth2Scope'] : variable_get('oauth2_server_restful_scope'); return ['server' => $server, 'scope' =>$scope]; } + /** + * Get the resource plugin id requested. + * + * @return null|string + * The plugin id of the resource that was requested. + */ protected function getResourcePluginIdFromRequest() { - $resource_name = restful()->getResourceManager()->getResourceIdFromRequest(); - $version = restful()->getResourceManager()->getVersionFromRequest(); + $resource_name = $this->resourceManager->getResourceIdFromRequest(); + $version = $this->resourceManager->getVersionFromRequest(); if (!$resource_name || !$version) { return NULL;