From b0207b226628ddd6fc39ef3c1ebb48fb4273e01d Mon Sep 17 00:00:00 2001 From: AbhishekKr Date: Thu, 15 Dec 2022 21:28:20 +0530 Subject: [PATCH] Add Cloudflare API Token support for auth, domain optional if zone_id provided (#3406) Signed-off-by: AbhishekKr Signed-off-by: AbhishekKr --- contrib/cloudflare.php | 15 ++++++++++----- docs/contrib/cloudflare.md | 5 +++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/contrib/cloudflare.php b/contrib/cloudflare.php index e1c5b9b5a..4986654c9 100644 --- a/contrib/cloudflare.php +++ b/contrib/cloudflare.php @@ -7,7 +7,8 @@ - `service_key` – Cloudflare Service Key. If this is not provided, use api_key and email. - `api_key` – Cloudflare API key generated on the "My Account" page. - `email` – Cloudflare Email address associated with your account. - - `domain` – The domain you want to clear + - `api_token` – Cloudflare API Token generated on the "My Account" page. + - `domain` – The domain you want to clear (optional if zone_id is provided). - `zone_id` – Cloudflare Zone ID (optional). ### Usage @@ -33,16 +34,16 @@ 'X-Auth-Key' => $config['api_key'], 'X-Auth-Email' => $config['email'] ]; + } else if (!empty($config['api_token'])) { + $headers = [ + 'Authorization' => 'Bearer '.$config['api_token'] + ]; } else { throw new \RuntimeException("Set a service key or email / api key"); } $headers['Content-Type'] = 'application/json'; - if (empty($config['domain'])) { - throw new \RuntimeException("Set a domain"); - } - $makeRequest = function ($url, $opts = []) use ($headers) { $ch = curl_init("https://api.cloudflare.com/client/v4/$url"); @@ -71,6 +72,10 @@ $zoneId = $config['zone_id']; if (empty($zoneId)) { + if (empty($config['domain'])) { + throw new \RuntimeException("Set a domain"); + } + // get the mysterious zone id from Cloud Flare $zones = json_decode($makeRequest( "zones?name={$config['domain']}" diff --git a/docs/contrib/cloudflare.md b/docs/contrib/cloudflare.md index 1d414278c..47226c85d 100644 --- a/docs/contrib/cloudflare.md +++ b/docs/contrib/cloudflare.md @@ -19,7 +19,8 @@ require 'contrib/cloudflare.php'; - `service_key` – Cloudflare Service Key. If this is not provided, use api_key and email. - `api_key` – Cloudflare API key generated on the "My Account" page. - `email` – Cloudflare Email address associated with your account. - - `domain` – The domain you want to clear + - `api_token` – Cloudflare API Token generated on the "My Account" page. + - `domain` – The domain you want to clear (optional if zone_id is provided). - `zone_id` – Cloudflare Zone ID (optional). ### Usage @@ -33,7 +34,7 @@ if not the, last tasks before cleanup ## Tasks ### deploy:cloudflare -[Source](https://github.com/deployphp/deployer/blob/master/contrib/cloudflare.php#L22) +[Source](https://github.com/deployphp/deployer/blob/master/contrib/cloudflare.php#L23) Clears Cloudflare Cache.