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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.0] - 2026-10-13

### Changed

- `handleRequest()` no longer sends the RSL license `Link` header
(`Link: <…/license.xml>; rel="license"; type="application/rsl+xml"`) on
plain allowed responses: non-bot requests, valid tokens and `DISABLED`
mode. It is still sent on the `OBSERVE` missing-token signal and on every
blocked response, which matches the TypeScript SDK. Reverts #15.
`ResponseBuilder` method signatures are unchanged.

## [2.0.1] - 2026-08-27

Re-release of 2.0.0 with no code changes. Packagist crawled an early,
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ The `EnforcementMode` enum controls how `handleRequest()` responds to detected b

Default is `OBSERVE`.

The license is advertised with `Link: <…/license.xml>; rel="license"` on the `OBSERVE` signal and on blocked responses only. Plain allowed responses (non-bot, valid token, `DISABLED`) carry no RSL headers.

---

## Analytics
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "getsupertab/connect-sdk-php",
"description": "Supertab Connect PHP SDK",
"type": "library",
"version": "v2.0.1",
"version": "v2.1.0",
"license": "MIT",
"keywords": ["supertab", "connect", "license", "jwt", "rsl"],
"homepage": "https://github.com/getsupertab/connect-sdk-php",
Expand Down
14 changes: 7 additions & 7 deletions src/License/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
final class ResponseBuilder
{
/**
* Build an allowed result that advertises the governing RSL license via the
* Link header. Per the RSL Crawler Authorization Protocol, served responses
* must reference the license so clients can discover the terms — independent
* of enforcement mode or bot detection.
* Build an allowed result. A plain allow (non-bot, valid token, DISABLED)
* carries no RSL headers: the license is advertised only once the request
* is treated as a bot's, on the OBSERVE signal and on blocks. Matches the
* TypeScript SDK.
*
* `$requestUrl` is unused and kept for signature compatibility.
*/
public static function buildAllowResult(string $requestUrl): AllowResult
{
return new AllowResult([
'Link' => self::licenseLinkHeader($requestUrl),
]);
return new AllowResult;
}

/**
Expand Down
46 changes: 35 additions & 11 deletions tests/License/ResponseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Supertab\Connect\Tests\License;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Supertab\Connect\Enum\HandlerAction;
use Supertab\Connect\Enum\LicenseTokenInvalidReason;
Expand All @@ -19,24 +20,47 @@ public function test_build_signal_result(): void

$this->assertInstanceOf(AllowResult::class, $result);
$this->assertSame(HandlerAction::ALLOW, $result->action);
$this->assertSame('token_required', $result->headers['X-RSL-Status']);
$this->assertSame('missing', $result->headers['X-RSL-Reason']);
$this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']);
$this->assertStringContainsString('rel="license"', $result->headers['Link']);
// Matches the TypeScript SDK: the bot signal advertises the license.
$this->assertSame(
[
'Link' => '<https://example.com/license.xml>; rel="license"; type="application/rsl+xml"',
'X-RSL-Status' => 'token_required',
'X-RSL-Reason' => 'missing',
],
$result->headers,
);
}

public function test_build_allow_result_advertises_license_link(): void
public function test_build_allow_result_carries_no_headers(): void
{
$result = ResponseBuilder::buildAllowResult('https://example.com/article');

$this->assertInstanceOf(AllowResult::class, $result);
$this->assertSame(HandlerAction::ALLOW, $result->action);
$this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']);
$this->assertStringContainsString('rel="license"', $result->headers['Link']);
$this->assertStringContainsString('type="application/rsl+xml"', $result->headers['Link']);
// Discovery only — the request-state signal headers stay on the soft-signal path.
$this->assertArrayNotHasKey('X-RSL-Status', $result->headers);
$this->assertArrayNotHasKey('X-RSL-Reason', $result->headers);
$this->assertSame([], $result->headers);
}

#[DataProvider('blockReasonProvider')]
public function test_every_block_result_advertises_license_link(LicenseTokenInvalidReason|string $reason): void
{
$result = ResponseBuilder::buildBlockResult($reason, 'error', 'https://example.com/article');

$this->assertSame(
'<https://example.com/license.xml>; rel="license"; type="application/rsl+xml"',
$result->headers['Link'],
);
}

/**
* @return iterable<string, array{LicenseTokenInvalidReason|string}>
*/
public static function blockReasonProvider(): iterable
{
foreach (LicenseTokenInvalidReason::cases() as $reason) {
yield $reason->value => [$reason];
}

yield 'string reason' => ['custom_reason'];
}

public function test_build_block_result_missing_token(): void
Expand Down
89 changes: 79 additions & 10 deletions tests/SupertabConnectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ public function test_handle_request_ignores_non_license_auth_header_with_bot():
$this->assertArrayHasKey('X-RSL-Status', $result->headers);
}

// --- RSL discovery: Link header on allowed responses ---
// --- RSL discovery: Link header on bot signals and blocks, never on plain allows ---

public function test_handle_request_disabled_no_token_advertises_license_link(): void
public function test_handle_request_disabled_no_token_sends_no_license_link(): void
{
$stc = new SupertabConnect(
apiKey: 'test-key',
Expand All @@ -298,11 +298,10 @@ public function test_handle_request_disabled_no_token_advertises_license_link():
));

$this->assertInstanceOf(AllowResult::class, $result);
$this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']);
$this->assertStringContainsString('rel="license"', $result->headers['Link']);
$this->assertArrayNotHasKey('Link', $result->headers);
}

public function test_handle_request_disabled_with_token_advertises_license_link(): void
public function test_handle_request_disabled_with_token_sends_no_license_link(): void
{
$stc = new SupertabConnect(
apiKey: 'test-key',
Expand All @@ -315,10 +314,10 @@ public function test_handle_request_disabled_with_token_advertises_license_link(
));

$this->assertInstanceOf(AllowResult::class, $result);
$this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']);
$this->assertArrayNotHasKey('Link', $result->headers);
}

public function test_handle_request_non_bot_allow_advertises_license_link(): void
public function test_handle_request_non_bot_allow_sends_no_license_link(): void
{
$botDetector = $this->createMock(BotDetectorInterface::class);
$botDetector->method('isBot')->willReturn(false);
Expand All @@ -335,10 +334,10 @@ public function test_handle_request_non_bot_allow_advertises_license_link(): voi
));

$this->assertInstanceOf(AllowResult::class, $result);
$this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']);
$this->assertArrayNotHasKey('Link', $result->headers);
}

public function test_handle_request_bot_disabled_allow_advertises_license_link(): void
public function test_handle_request_bot_disabled_allow_sends_no_license_link(): void
{
$botDetector = $this->createMock(BotDetectorInterface::class);
$botDetector->method('isBot')->willReturn(true);
Expand All @@ -355,7 +354,7 @@ public function test_handle_request_bot_disabled_allow_advertises_license_link()
));

$this->assertInstanceOf(AllowResult::class, $result);
$this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']);
$this->assertArrayNotHasKey('Link', $result->headers);
}

public function test_handle_request_observe_signal_keeps_link_and_custom_headers(): void
Expand All @@ -380,6 +379,76 @@ public function test_handle_request_observe_signal_keeps_link_and_custom_headers
$this->assertSame('missing', $result->headers['X-RSL-Reason']);
}

public function test_handle_request_enforce_bot_without_token_block_advertises_license_link(): void
{
$botDetector = $this->createMock(BotDetectorInterface::class);
$botDetector->method('isBot')->willReturn(true);

$stc = new SupertabConnect(
apiKey: 'test-key',
enforcement: EnforcementMode::ENFORCE,
botDetector: $botDetector,
);

$result = $stc->handleRequest(new RequestContext(
url: 'https://example.com/article',
authorizationHeader: null,
));

$this->assertInstanceOf(BlockResult::class, $result);
$this->assertStringContainsString('<https://example.com/license.xml>; rel="license"', $result->headers['Link']);
}

public function test_handle_request_invalid_token_block_advertises_license_link(): void
{
$stc = new SupertabConnect(
apiKey: 'test-key',
enforcement: EnforcementMode::OBSERVE,
httpClient: $this->createEventSinkHttpClient(),
);

$result = $stc->handleRequest(new RequestContext(
url: 'https://example.com/article',
authorizationHeader: 'License not-a-real-jwt',
));

$this->assertInstanceOf(BlockResult::class, $result);
$this->assertSame(401, $result->status);
$this->assertStringContainsString('<https://example.com/license.xml>; rel="license"', $result->headers['Link']);
}

public function test_handle_request_empty_license_token_block_advertises_license_link(): void
{
$stc = new SupertabConnect(
apiKey: 'test-key',
enforcement: EnforcementMode::OBSERVE,
httpClient: $this->createEventSinkHttpClient(),
);

// "License " with nothing after it takes the token-present path as MISSING_TOKEN.
$result = $stc->handleRequest(new RequestContext(
url: 'https://example.com/article',
authorizationHeader: 'License ',
));

$this->assertInstanceOf(BlockResult::class, $result);
$this->assertSame(401, $result->status);
$this->assertStringContainsString('invalid_request', $result->headers['WWW-Authenticate']);
$this->assertStringContainsString('<https://example.com/license.xml>; rel="license"', $result->headers['Link']);
}

/**
* HTTP client that accepts the fire-and-forget billing event POST that the
* token-present path records, so these tests never touch the network.
*/
private function createEventSinkHttpClient(): HttpClientInterface
{
$httpClient = $this->createStub(HttpClientInterface::class);
$httpClient->method('post')->willReturn(['statusCode' => 200, 'body' => '']);

return $httpClient;
}

// --- Bot Detection Integration ---

public function test_handle_request_allows_non_bot_without_token(): void
Expand Down
Loading