From 0bdf3febc34fd585fb8091e06c6d347639e08a42 Mon Sep 17 00:00:00 2001 From: e0ipso <e0ipso@550110.no-reply.drupal.org> Date: Tue, 23 Jul 2019 07:36:05 +0200 Subject: [PATCH] Issue #3069526 by e0ipso: Make the _api_proxy_uri parameter name configurable --- api_proxy.services.yml | 3 ++ src/Controller/Forwarder.php | 31 ++++++++++++++++--- .../OptionsRequestSubscriber.php | 14 +++++++-- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/api_proxy.services.yml b/api_proxy.services.yml index bca0caa..a877d08 100644 --- a/api_proxy.services.yml +++ b/api_proxy.services.yml @@ -1,3 +1,5 @@ +parameters: + api_proxy.uri_param_name: _api_proxy_uri services: Drupal\api_proxy\Plugin\HttpApiPluginManager: autowire: true @@ -11,5 +13,6 @@ services: arguments: - '@router.route_provider' - '@Drupal\api_proxy\EventSubscriber\OptionsRequestSubscriber.inner' + - '%api_proxy.uri_param_name%' tags: - { name: event_subscriber } diff --git a/src/Controller/Forwarder.php b/src/Controller/Forwarder.php index 554ba3c..4be6dd4 100644 --- a/src/Controller/Forwarder.php +++ b/src/Controller/Forwarder.php @@ -8,6 +8,7 @@ use Drupal\Core\Cache\CacheableResponse; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Http\Exception\CacheableAccessDeniedHttpException; use Drupal\Core\Http\Exception\CacheableBadRequestHttpException; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -16,7 +17,29 @@ use Symfony\Component\HttpFoundation\Response; */ final class Forwarder extends ControllerBase { - const QUERY_PARAM_URI = '_api_proxy_uri'; + /** + * The name of the query string parameter containing the URI. + * + * @param string + */ + private $uriParamName; + + /** + * Forwarder constructor. + * + * @param string $uri_param_name + * The name of the query string parameter containing the URI. + */ + public function __construct(string $uri_param_name) { + $this->uriParamName = $uri_param_name; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static($container->getParameter('api_proxy.uri_param_name')); + } /** * Forwards incoming requests to the connected API. @@ -33,7 +56,7 @@ final class Forwarder extends ControllerBase { // TODO: This belongs to the routing system. $account = $this->currentUser(); $cache_contexts = [ - 'url.query_args:' . Forwarder::QUERY_PARAM_URI, + 'url.query_args:' . $this->uriParamName, 'headers:Origin', 'user.permissions', ]; @@ -46,11 +69,11 @@ final class Forwarder extends ControllerBase { 'The current user does not have access to this proxy' ); } - $third_party_uri = $request->query->get(static::QUERY_PARAM_URI); + $third_party_uri = $request->query->get($this->uriParamName); if (empty($third_party_uri)) { throw new CacheableBadRequestHttpException( $cacheability, - sprintf('Unable to find a valid URI in the %s query parameter.', static::QUERY_PARAM_URI) + sprintf('Unable to find a valid URI in the %s query parameter.', $this->uriParamName) ); } $response = $api_proxy->forward($request, $third_party_uri); diff --git a/src/EventSubscriber/OptionsRequestSubscriber.php b/src/EventSubscriber/OptionsRequestSubscriber.php index 3826e4d..16a1361 100644 --- a/src/EventSubscriber/OptionsRequestSubscriber.php +++ b/src/EventSubscriber/OptionsRequestSubscriber.php @@ -33,6 +33,13 @@ class OptionsRequestSubscriber implements EventSubscriberInterface { */ protected $subject; + /** + * The name of the query string parameter containing the URI. + * + * @param string + */ + private $uriParamName; + /** * Creates a new OptionsRequestSubscriber instance. * @@ -40,10 +47,13 @@ class OptionsRequestSubscriber implements EventSubscriberInterface { * The route provider. * @param \Symfony\Component\EventDispatcher\EventSubscriberInterface * The decorated service. + * @param string $uri_param_name + * The name of the query string parameter containing the URI. */ - public function __construct(RouteProviderInterface $route_provider, EventSubscriberInterface $subject) { + public function __construct(RouteProviderInterface $route_provider, EventSubscriberInterface $subject, string $uri_param_name) { $this->routeProvider = $route_provider; $this->subject = $subject; + $this->uriParamName = $uri_param_name; } /** @@ -79,7 +89,7 @@ class OptionsRequestSubscriber implements EventSubscriberInterface { } $response = $proxy->corsResponse($request); $cache_contexts = [ - 'url.query_args:' . Forwarder::QUERY_PARAM_URI, + 'url.query_args:' . $this->uriParamName, 'headers:Origin', ]; $cacheability = (new CacheableMetadata()) -- GitLab