diff --git a/README.md b/README.md index df80f21..3861a2b 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,20 @@ php artisan vendor:publish --tag="cloudflare-cache-config" Add environment variables to .env file +##### Using global api key: + ```dotenv CLOUDFLARE_CACHE_EMAIL=info@example.com #Cloudflare account email address -CLOUDFLARE_CACHE_KEY=XXXXXXX #Cloudflare API_KEY +CLOUDFLARE_CACHE_KEY=XXXXXXX #Cloudflare global api key +CLOUDFLARE_CACHE_IDENTIFIER=XXXXXXX #ZONE_ID +CLOUDFLARE_DEFAULT_CACHE_TTL=600 #10 minutes +CLOUDFLARE_CACHE_DEBUG=false +``` + +##### Using fine-grained api token: + +```dotenv +CLOUDFLARE_CACHE_API_TOKEN=XXXXXXX CLOUDFLARE_CACHE_IDENTIFIER=XXXXXXX #ZONE_ID CLOUDFLARE_DEFAULT_CACHE_TTL=600 #10 minutes CLOUDFLARE_CACHE_DEBUG=false diff --git a/config/cloudflare-cache.php b/config/cloudflare-cache.php index ec50184..912ce68 100644 --- a/config/cloudflare-cache.php +++ b/config/cloudflare-cache.php @@ -17,6 +17,11 @@ */ 'api_key' => env('CLOUDFLARE_CACHE_KEY'), + /** + * Fine-grained api token. + */ + 'api_token' => env('CLOUDFLARE_CACHE_API_TOKEN'), + /** * zone_id of your site on cloudflare dashboard. */ diff --git a/src/CloudflareCache.php b/src/CloudflareCache.php index 19d4dd2..800596c 100644 --- a/src/CloudflareCache.php +++ b/src/CloudflareCache.php @@ -99,13 +99,16 @@ public function isActive(): bool return true; } - if (! config('cloudflare-cache.api_email') - || ! config('cloudflare-cache.api_key') - || ! config('cloudflare-cache.identifier') - ) { + if (! config('cloudflare-cache.identifier')) { return false; } + if (! config('cloudflare-cache.api_token')) { + if (! config('cloudflare-cache.api_email') || ! config('cloudflare-cache.api_key')) { + return false; + } + } + if (config('cloudflare-cache.debug')) { return true; } diff --git a/src/CloudflareCacheServiceProvider.php b/src/CloudflareCacheServiceProvider.php index a0b32f0..a4f6af6 100644 --- a/src/CloudflareCacheServiceProvider.php +++ b/src/CloudflareCacheServiceProvider.php @@ -47,6 +47,7 @@ public function registerClient(): static config('cloudflare-cache.api_email'), config('cloudflare-cache.api_key'), config('cloudflare-cache.identifier'), + config('cloudflare-cache.api_token'), ); }); diff --git a/src/Services/CloudflareService.php b/src/Services/CloudflareService.php index 9991b3f..943c32f 100644 --- a/src/Services/CloudflareService.php +++ b/src/Services/CloudflareService.php @@ -14,6 +14,7 @@ public function __construct( private readonly ?string $apiEmail, private readonly ?string $apiKey, private readonly ?string $identifier, + private readonly ?string $apiToken, ) { // . } @@ -21,10 +22,10 @@ public function __construct( private function request(): PendingRequest { /** @noinspection PhpIncompatibleReturnTypeInspection */ - return $this->client->withHeaders([ - 'X-Auth-Email' => $this->apiEmail, - 'X-Auth-Key' => $this->apiKey, - ]); + return $this->client->withHeaders($this->apiToken + ? ['Authorization' => 'Bearer ' . $this->apiToken] + : ['X-Auth-Email' => $this->apiEmail, 'X-Auth-Key' => $this->apiKey] + ); } protected function getBaseUrl(string $endpoint): string