Skip to content
Merged
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
9 changes: 7 additions & 2 deletions src/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use FriendlyCaptcha\SDK\{ClientConfig, VerifyResult, ErrorCodes};

const VERSION = "0.1.1";
const VERSION = "0.1.2";
const EU_API_ENDPOINT = "https://eu.frcapi.com/api/v2/captcha/siteverify";
const GLOBAL_API_ENDPOINT = "https://global.frcapi.com/api/v2/captcha/siteverify";

Expand Down Expand Up @@ -54,6 +54,11 @@ public function verifyCaptchaResponse(?string $response): VerifyResult
$requestFields["sitekey"] = $this->config->sitekey;
}

$frcSdk = 'friendly-captcha-php@' . VERSION;
if ($this->config->sdkTrailer != "") {
$frcSdk = $frcSdk . "; " . $this->config->sdkTrailer;
}

$payload = json_encode($requestFields);
if ($payload === false) {
// TODO: should we put `json_last_error()` somewhere on the object?
Expand All @@ -76,7 +81,7 @@ public function verifyCaptchaResponse(?string $response): VerifyResult
'Content-Type: application/json',
'Content-Length: ' . strlen($payload),
'X-Api-Key: ' . $this->config->apiKey,
'Frc-Sdk: ' . 'friendly-captcha-php@' . VERSION
'Frc-Sdk: ' . $frcSdk,
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
Expand Down
16 changes: 16 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ClientConfig
{
public $apiKey = "";
public $sitekey = "";
public $sdkTrailer = "";
public $siteverifyEndpoint = "global";
public $strict = false;
public $timeout = 30;
Expand Down Expand Up @@ -37,6 +38,21 @@ public function setSitekey(string $sitekey): self
return $this;
}

/**
* An "Frc-Sdk" HTTP header is sent as part of the API request to identify the SDK being used to initiate the request.
* A downstream SDK might depend on *this* SDK, so this function is provided to allow the downstream SDK to append an
* identifier to uniquely identify it. For example, this library sends an "Frc-Sdk" header of
* `friendly-captcha-php@1.2.3`. If `friendly-captcha-wordpress` depends on it, it can add a trailer so that requests
* will use an "Frc-Sdk" header of `friendly-captcha-php@1.2.3; friendly-captcha-wordpress@4.5.6`.
*
* @param string $sdk an identifier that describes the component that depends on this SDK.
*/
public function setSDKTrailer(string $sdkTrailer): self
{
$this->sdkTrailer = $sdkTrailer;
return $this;
}

/**
* @param string $siteverifyEndpoint a full URL, or the shorthands `"global"` or `"eu"`.
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/verifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FriendlyCaptcha\SDK\{Client, ClientConfig};
use Exception;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

const MOCK_SERVER_URL = "http://localhost:1090";
Expand Down Expand Up @@ -86,6 +87,7 @@ public static function sdkMockTestsProvider(): array
/**
* @dataProvider sdkMockTestsProvider
*/
#[DataProvider('sdkMockTestsProvider')]
public function testSDKTestServerCase($test): void
{
$opts = new ClientConfig();
Expand Down