Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Cloudflare API Token #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,20 @@ php artisan vendor:publish --tag="cloudflare-cache-config"

Add environment variables to .env file

##### Using global api key:

```dotenv
[email protected] #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
Expand Down
5 changes: 5 additions & 0 deletions config/cloudflare-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
11 changes: 7 additions & 4 deletions src/CloudflareCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/CloudflareCacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
});

Expand Down
9 changes: 5 additions & 4 deletions src/Services/CloudflareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ public function __construct(
private readonly ?string $apiEmail,
private readonly ?string $apiKey,
private readonly ?string $identifier,
private readonly ?string $apiToken,
) {
// .
}

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
Expand Down