Skip to content
Snippets Groups Projects
Unverified Commit 0bdf3feb authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch Committed by Mateu Aguiló Bosch
Browse files

Issue #3069526 by e0ipso: Make the _api_proxy_uri parameter name configurable

parent 257f920d
Branches
Tags 8.x-1.0 8.x-1.0-beta3
No related merge requests found
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 }
......@@ -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);
......
......@@ -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())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment