|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\restful\Plugin\authentication; |
| 4 | + |
| 5 | +use Drupal\Component\Plugin\PluginBase; |
| 6 | +use Drupal\restful\Exception\ServerConfigurationException; |
| 7 | +use Drupal\restful\Exception\UnauthorizedException; |
| 8 | +use Drupal\restful\Http\RequestInterface; |
| 9 | +use Drupal\restful\Plugin\ResourcePluginManager; |
| 10 | + |
| 11 | +/** |
| 12 | + * Authentication support for oauth2_server. |
| 13 | + * |
| 14 | + * @Authentication( |
| 15 | + * id = "oauth2", |
| 16 | + * label = "OAuth2 authentication", |
| 17 | + * description = "Authenticate requests based on oauth2_server auth.", |
| 18 | + * ) |
| 19 | + */ |
| 20 | +class OAuth2ServerAuthentication extends Authentication { |
| 21 | + |
| 22 | + /** |
| 23 | + * The resource manager. |
| 24 | + * |
| 25 | + * @var \Drupal\restful\Resource\ResourceManagerInterface |
| 26 | + */ |
| 27 | + protected $resourceManager; |
| 28 | + |
| 29 | + public function __construct(array $configuration, $plugin_id, $plugin_definition) { |
| 30 | + parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 31 | + $this->resourceManager = restful()->getResourceManager(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritdoc} |
| 36 | + */ |
| 37 | + public function applies(RequestInterface $request) { |
| 38 | + return module_exists('oauth2_server') && $this->getOAuth2Info($request); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + public function authenticate(RequestInterface $request) { |
| 45 | + $oauth2_info = $this->getOAuth2Info($request); |
| 46 | + if (!$oauth2_info) { |
| 47 | + throw new ServerConfigurationException('The resource uses OAuth2 authentication but does not specify the OAuth2 server.'); |
| 48 | + } |
| 49 | + |
| 50 | + $result = oauth2_server_check_access($oauth2_info['server'], $oauth2_info['scope']); |
| 51 | + if ($result instanceof \OAuth2\Response) { |
| 52 | + throw new UnauthorizedException($result->getResponseBody(), $result->getStatusCode()); |
| 53 | + } |
| 54 | + elseif (empty($result['user_id'])) { |
| 55 | + return NULL; |
| 56 | + } |
| 57 | + return user_load($result['user_id']); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get OAuth2 information from the request. |
| 62 | + * |
| 63 | + * @param \Drupal\restful\Http\RequestInterface $request |
| 64 | + * The request. |
| 65 | + * |
| 66 | + * @return array|null |
| 67 | + * Simple associative array with the following keys: |
| 68 | + * - server: The OAuth2 server to authenticate against. |
| 69 | + * - scope: The scope required for the resource. |
| 70 | + */ |
| 71 | + protected function getOAuth2Info(RequestInterface $request) { |
| 72 | + $plugin_id = $this->getResourcePluginIdFromRequest(); |
| 73 | + if (!$plugin_id) { |
| 74 | + // If the plugin can't be determined, it is probably not a request to the |
| 75 | + // resource but something else that is just loading all the plugins. |
| 76 | + return NULL; |
| 77 | + } |
| 78 | + |
| 79 | + $plugin_definition = ResourcePluginManager::create('cache', $request)->getDefinition($plugin_id); |
| 80 | + |
| 81 | + if (empty($plugin_definition['oauth2Server'])) { |
| 82 | + return NULL; |
| 83 | + } |
| 84 | + |
| 85 | + $server = $plugin_definition['oauth2Server']; |
| 86 | + $scope = !empty($plugin_definition['oauth2Scope']) ? $plugin_definition['oauth2Scope'] : ''; |
| 87 | + return ['server' => $server, 'scope' => $scope]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get the resource plugin id requested. |
| 92 | + * |
| 93 | + * @return null|string |
| 94 | + * The plugin id of the resource that was requested. |
| 95 | + */ |
| 96 | + protected function getResourcePluginIdFromRequest() { |
| 97 | + $resource_name = $this->resourceManager->getResourceIdFromRequest(); |
| 98 | + $version = $this->resourceManager->getVersionFromRequest(); |
| 99 | + |
| 100 | + if (!$resource_name || !$version) { |
| 101 | + return NULL; |
| 102 | + } |
| 103 | + |
| 104 | + return $resource_name . PluginBase::DERIVATIVE_SEPARATOR . $version[0] . '.' . $version[1]; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments