Skip to content

Commit

Permalink
added support for api token
Browse files Browse the repository at this point in the history
  • Loading branch information
harryqt committed Apr 1, 2024
1 parent 2d2dd78 commit 408e832
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
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

0 comments on commit 408e832

Please sign in to comment.