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

Allow tokens which have hyphens and underscores #1

Open
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"php": ">=5.5.9",
"guzzlehttp/guzzle": "~6.1"
"guzzlehttp/guzzle": "~6.1|^7.0"
},
"require-dev": {
"drupal/coder": "8.2.*",
Expand Down
26 changes: 13 additions & 13 deletions src/ApiEndpoints/CloudFlareAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class CloudFlareAPI {
*
* @var \GuzzleHttp\Client
*/
private $client;
protected $client;

/**
* Last raw response returned from the API. Intended for debugging only.
Expand Down Expand Up @@ -52,8 +52,8 @@ abstract class CloudFlareAPI {
// length is greater than 37. If the key is invalid but the expected length
// the Api will return a more informative http code of 403.
const GLOBAL_API_KEY_LENGTH = 37;
// The length of the Api key.
const API_KEY_LENGTH = 40;
// The length of an Api token.
const API_TOKEN_LENGTH = 40;

// The CloudFlare API sets a maximum of 1,200 requests in a 5-minute period.
const API_RATE_LIMIT = 1200;
Expand Down Expand Up @@ -97,7 +97,7 @@ public function __construct($apikey, $email, MockHandler $mock_handler = NULL) {
$headers = [
'Content-Type' => 'application/json',
];
if (strlen($apikey) === self::API_KEY_LENGTH) {
if (strlen($apikey) === self::API_TOKEN_LENGTH) {
$headers['Authorization'] = 'Bearer ' . $apikey;
}
else {
Expand Down Expand Up @@ -149,20 +149,20 @@ protected function makeRequest($request_type, $api_end_point, $request_params =
// This check seems superfluous. However, the Api only returns a http 400
// code. This proactive check gives us more information.
$api_key_length = strlen($this->apikey);
$is_api_key_valid = $api_key_length == self::API_KEY_LENGTH || $api_key_length == self::GLOBAL_API_KEY_LENGTH;
$is_api_key_alpha_numeric = ctype_alnum($this->apikey);
$is_api_key_lower_case = !(preg_match('/[A-Z]/', $this->apikey));
$is_valid_length = $api_key_length == self::API_TOKEN_LENGTH || $api_key_length == self::GLOBAL_API_KEY_LENGTH;
$is_valid_token_chars = !preg_match('/[^A-Za-z0-9_-]/', $this->apikey);
$is_valid_key_chars = !preg_match('/[^a-z0-9]/', $this->apikey);

if (!$is_api_key_valid) {
throw new CloudFlareInvalidCredentialException("Invalid Api Key: Key should be 37 chars long.", 403);
if (!$is_valid_length) {
throw new CloudFlareInvalidCredentialException("Invalid Api Key: Should be 37 character global key, or 40 character token.", 403);
}

if (!$is_api_key_alpha_numeric) {
throw new CloudFlareInvalidCredentialException('Invalid Api Key: Key can only contain alphanumeric characters.', 403);
if ($api_key_length == self::API_TOKEN_LENGTH && !$is_valid_token_chars) {
throw new CloudFlareInvalidCredentialException('Invalid Api Key: 40-character token can only contain alphanumeric characters, hyphens, and underscores.', 403);
}

if ($api_key_length == self::GLOBAL_API_KEY_LENGTH && !$is_api_key_lower_case) {
throw new CloudFlareInvalidCredentialException('Invalid Api Key: Key can only contain lowercase or numerical characters.', 403);
if ($api_key_length == self::GLOBAL_API_KEY_LENGTH && !$is_valid_key_chars) {
throw new CloudFlareInvalidCredentialException('Invalid Api Key: 37-character global key can only contain lowercase and numeric characters.', 403);
}

try {
Expand Down