OAuth2ServerAuthentication.php in RESTful 7.2
File
src/Plugin/authentication/OAuth2ServerAuthentication.php
View source
<?php
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;
class OAuth2ServerAuthentication extends Authentication {
protected $resourceManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->resourceManager = restful()
->getResourceManager();
}
public function applies(RequestInterface $request) {
return module_exists('oauth2_server') && $this
->getOAuth2Info($request);
}
public function authenticate(RequestInterface $request) {
$oauth2_info = $this
->getOAuth2Info($request);
if (!$oauth2_info) {
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 (empty($result['user_id'])) {
return NULL;
}
return user_load($result['user_id']);
}
protected function getOAuth2Info(RequestInterface $request) {
$plugin_id = $this
->getResourcePluginIdFromRequest();
if (!$plugin_id) {
return NULL;
}
$plugin_definition = ResourcePluginManager::create('cache', $request)
->getDefinition($plugin_id);
if (empty($plugin_definition['oauth2Server'])) {
return NULL;
}
$server = $plugin_definition['oauth2Server'];
$scope = !empty($plugin_definition['oauth2Scope']) ? $plugin_definition['oauth2Scope'] : '';
return [
'server' => $server,
'scope' => $scope,
];
}
protected function getResourcePluginIdFromRequest() {
$resource_name = $this->resourceManager
->getResourceIdFromRequest();
$version = $this->resourceManager
->getVersionFromRequest();
if (!$resource_name || !$version) {
return NULL;
}
return $resource_name . PluginBase::DERIVATIVE_SEPARATOR . $version[0] . '.' . $version[1];
}
}