Skip to content

Commit 9cc23e8

Browse files
authored
Merge pull request #6 from FriendlyCaptcha/add-sdk-trailer-method
Add a method that enables adding an Frc-Sdk component
2 parents f4470f6 + ac4374c commit 9cc23e8

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/client.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

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

9-
const VERSION = "0.1.1";
9+
const VERSION = "0.1.2";
1010
const EU_API_ENDPOINT = "https://eu.frcapi.com/api/v2/captcha/siteverify";
1111
const GLOBAL_API_ENDPOINT = "https://global.frcapi.com/api/v2/captcha/siteverify";
1212

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

57+
$frcSdk = 'friendly-captcha-php@' . VERSION;
58+
if ($this->config->sdkTrailer != "") {
59+
$frcSdk = $frcSdk . "; " . $this->config->sdkTrailer;
60+
}
61+
5762
$payload = json_encode($requestFields);
5863
if ($payload === false) {
5964
// TODO: should we put `json_last_error()` somewhere on the object?
@@ -76,7 +81,7 @@ public function verifyCaptchaResponse(?string $response): VerifyResult
7681
'Content-Type: application/json',
7782
'Content-Length: ' . strlen($payload),
7883
'X-Api-Key: ' . $this->config->apiKey,
79-
'Frc-Sdk: ' . 'friendly-captcha-php@' . VERSION
84+
'Frc-Sdk: ' . $frcSdk,
8085
)
8186
);
8287
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

src/config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ClientConfig
1010
{
1111
public $apiKey = "";
1212
public $sitekey = "";
13+
public $sdkTrailer = "";
1314
public $siteverifyEndpoint = "global";
1415
public $strict = false;
1516
public $timeout = 30;
@@ -37,6 +38,21 @@ public function setSitekey(string $sitekey): self
3738
return $this;
3839
}
3940

41+
/**
42+
* An "Frc-Sdk" HTTP header is sent as part of the API request to identify the SDK being used to initiate the request.
43+
* A downstream SDK might depend on *this* SDK, so this function is provided to allow the downstream SDK to append an
44+
* identifier to uniquely identify it. For example, this library sends an "Frc-Sdk" header of
45+
* `[email protected]`. If `friendly-captcha-wordpress` depends on it, it can add a trailer so that requests
46+
* will use an "Frc-Sdk" header of `[email protected]; [email protected]`.
47+
*
48+
* @param string $sdk an identifier that describes the component that depends on this SDK.
49+
*/
50+
public function setSDKTrailer(string $sdkTrailer): self
51+
{
52+
$this->sdkTrailer = $sdkTrailer;
53+
return $this;
54+
}
55+
4056
/**
4157
* @param string $siteverifyEndpoint a full URL, or the shorthands `"global"` or `"eu"`.
4258
*/

tests/verifyTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use FriendlyCaptcha\SDK\{Client, ClientConfig};
88
use Exception;
99

10+
use PHPUnit\Framework\Attributes\DataProvider;
1011
use PHPUnit\Framework\TestCase;
1112

1213
const MOCK_SERVER_URL = "http://localhost:1090";
@@ -86,6 +87,7 @@ public static function sdkMockTestsProvider(): array
8687
/**
8788
* @dataProvider sdkMockTestsProvider
8889
*/
90+
#[DataProvider('sdkMockTestsProvider')]
8991
public function testSDKTestServerCase($test): void
9092
{
9193
$opts = new ClientConfig();

0 commit comments

Comments
 (0)