Skip to content
Snippets Groups Projects
Commit de81220a authored by David Barratt's avatar David Barratt
Browse files

Issue #3467496 by davidwbarratt: Cannot use a Cloudflare API Token

parent f5bbe1e2
No related branches found
No related tags found
1 merge request!5Remove dependency on cloudflare and optionally use key
......@@ -2,6 +2,4 @@ name: Cloudflare Worker Purge
type: module
description: Purge with a Cloudflare Worker
package: Purge - reverse proxies & CDNs
core_version_requirement: ^9 || ^10 || ^11
dependencies:
- cloudflare:cloudflare
core_version_requirement: ^10.3 || ^11
......@@ -7,3 +7,7 @@ cloudflare_worker_purge.settings:
label: 'URL'
type: uri
translatable: false
token:
label: 'API Token'
type: string
translatable: false
......@@ -2,8 +2,11 @@
namespace Drupal\cloudflare_worker_purge\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\purge_ui\Form\PurgerConfigFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Cloudflare Worker Purge Configuration Form.
......@@ -17,6 +20,28 @@ class CloudflareWorkerPurgeForm extends PurgerConfigFormBase {
*/
private const CONFIG_NAME = 'cloudflare_worker_purge.settings';
/**
* {@inheritdoc}
*/
public function __construct(
ConfigFactoryInterface $config_factory,
protected ModuleHandlerInterface $moduleHandler,
protected $typedConfigManager = NULL,
) {
parent::__construct($config_factory, $typedConfigManager);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('module_handler'),
$container->get('config.typed'),
);
}
/**
* {@inheritdoc}
*/
......@@ -42,11 +67,22 @@ class CloudflareWorkerPurgeForm extends PurgerConfigFormBase {
'#title' => $this->t('URL'),
'#type' => 'url',
'#default_value' => $config->get('url'),
'#description' => $this->t('The purger with make a request to the URL in the same format as the <a href="@cache_api">Purge Cache API</a> with the addition of a <code>CF-Zone</code> header that contains the zone id.', [
'#description' => $this->t('The purger with make a request to the URL in the same format as the <a href="@cache_api">Purge Cache API</a>.', [
"@cache_api" => 'https://api.cloudflare.com/#zone-purge-files-by-cache-tags,-host-or-prefix',
]),
];
if ($this->moduleHandler->moduleExists('key')) {
$form['token'] = [
'#title' => $this->t('API Token'),
'#type' => 'key_select',
'#default_value' => $config->get('token'),
'#description' => $this->t('This could be a <a href="@api_token">Cloudflare API Token</a>, or any authorization your Cloudflare Worker accepts.', [
'@api_token' => 'https://developers.cloudflare.com/fundamentals/api/get-started/create-token/',
]),
];
}
return parent::buildForm($form, $form_state);
}
......@@ -56,6 +92,7 @@ class CloudflareWorkerPurgeForm extends PurgerConfigFormBase {
public function submitFormSuccess(array &$form, FormStateInterface $form_state) {
$this->config(self::CONFIG_NAME)
->set('url', $form_state->getValue('url'))
->set('token', $form_state->getValue('token'))
->save();
}
......
......@@ -3,11 +3,13 @@
namespace Drupal\cloudflare_worker_purge\Plugin\Purge\Purger;
use Drupal\Core\Config\Config;
use Drupal\key\KeyRepositoryInterface;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface;
use Drupal\purge\Plugin\Purge\Purger\PurgerBase;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Promise\Utils;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -26,34 +28,6 @@ class CloudflareWorkerPurger extends PurgerBase {
public const MAX_TAG_PURGES_PER_REQUEST = 30;
/**
* HTTP Client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $client;
/**
* Cloudflare Worker Purge Config.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* Cloudflare Config.
*
* @var \Drupal\Core\Config\Config
*/
protected $cloudflareConfig;
/**
* A logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* {@inheritdoc}
*/
......@@ -61,17 +35,13 @@ class CloudflareWorkerPurger extends PurgerBase {
array $configuration,
$plugin_id,
$plugin_definition,
Config $cloudflareConfig,
Config $config,
ClientInterface $client,
protected Config $config,
protected ClientInterface $client,
LoggerInterface $logger,
protected ?KeyRepositoryInterface $keyRepository = NULL,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->cloudflareConfig = $cloudflareConfig;
$this->config = $config;
$this->client = $client;
$this->logger = $logger;
$this->setLogger($logger);
}
/**
......@@ -82,10 +52,10 @@ class CloudflareWorkerPurger extends PurgerBase {
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory')->get('cloudflare.settings'),
$container->get('config.factory')->get('cloudflare_worker_purge.settings'),
$container->get('http_client'),
$container->get('logger.factory')->get('cloudflare')
$container->get('logger.factory')->get('cloudflare_worker_purge'),
$container->get('key.repository', Container::NULL_ON_INVALID_REFERENCE)
);
}
......@@ -147,14 +117,11 @@ class CloudflareWorkerPurger extends PurgerBase {
/** @var array<string,string> */
$headers = [];
if ($this->cloudflareConfig->get('zone_id')) {
$headers['CF-Zone'] = $this->cloudflareConfig->get('zone_id');
}
if ($this->cloudflareConfig->get('email')) {
$headers['X-Auth-Email'] = $this->cloudflareConfig->get('email');
}
if ($this->cloudflareConfig->get('apikey')) {
$headers['X-Auth-Key'] = $this->cloudflareConfig->get('apikey');
if ($this->keyRepository && $this->config->get('token')) {
$token = $this->keyRepository->getKey($this->config->get('token'))->getKeyValue();
if ($token) {
$headers['Authorization'] = "Bearer $token";
}
}
$promises = array_map(function ($chunk) use ($url, $headers) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment