diff --git a/api_proxy.services.yml b/api_proxy.services.yml index bca0caa951b05ef65cbb7d5af2569757ccde1b48..a877d08e53f2135ac28ba090c72dc53cdc9d583b 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 554ba3c37b9dbf33e5a9d1d86f3e308e3a241062..4be6dd4f0c28aebc1ec1db7533a6d26138b0f360 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 3826e4d0bddb37a63ce6731c9393b32bad42e356..16a136159ec547d4fab2cf788377b0899ca4e91a 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())