EN · ÖZGÜN DEPO BELGESİ
İmzalı istemci protokolü
Özgün İngilizce metin. Komutlar ve kanıtlar, belgede belirtilen revizyona ve ortama aittir.
product/licensing/README.client.md
Bu sayfada
Standalone PHP entitlement client prototype
client.php is original PHP 7.2-compatible code requiring OpenSSL. It is separate
from the WordPress object cache. It gates premium update access only; no cache
operation, Redis connection or runtime correctness depends on licensing.
Create OriginalProduct\Licensing\EntitlementClient with license and installation
secrets, an HTTPS site origin, a configured kid => RSA public PEM map and a private
cache-file path. status() is strictly local. Only an explicit administrative
refresh($transport) invokes your callable, with /entitlement and the request
array. The entitlement transport must use the configured trusted server, verified TLS,
bounded timeouts, POST JSON and a decoded response envelope. The separate WordPress admin adapter supplies
explicit permission/nonce-checked actions and local-only page status. This client
supplies the standalone update helpers described below.
Protocol: {payload, sig, kid}, unpadded canonical base64url. The RSA-SHA256 signature
covers the decoded JSON payload bytes. Claims bind audience
original-product-entitlement, normalized HTTPS origin, SHA256 license digest and
SHA256 installation digest. Accepted signed statuses: active/revoked/inactive/expired.
The timestamps are nonnegative integers with
issued_at <= refresh_after <= expires_at <= grace_until, fresh lifetime at most
3600 seconds, refresh interval at most 300 seconds and grace at most 3600 additional seconds. Future issuance tolerance
is 60 seconds. The server must cap grace at the actual license expiry; that expiry
is not independently present in this envelope. The client cannot reconstruct it.
Local results: fresh/grace permit premium updates; expired/revoked/inactive/unknown do not. Expiry and grace boundaries are exclusive. A signed denial immediately replaces cached active entitlement. Older issuance or a same-time active replay following denial is rejected. Outages, malformed responses and bad signatures never extend verified deadlines. Rotation is explicit: keep old and new public keys in the configured map during transition, then remove retired key IDs.
The persisted JSON contains the signed envelope plus local observed_at high-water
metadata; it stores no raw license or installation key. Atomic same-directory rename
and mode 0600 protect against partial writes. New directories use 0700. Choose an
existing private directory when possible, outside web-served paths.
peek() is lock-free and never creates or writes anything. status() is peek() plus
the high-water: when the verified state advances past the stored observed time (refresh
due, grace, expiry) it stores the later observed time, once per advance, so a subsequent
ordinary clock rollback cannot renew access; inability to store that mark fails closed.
Reads within the same state neither lock nor write. Use status(), not peek(), for
access decisions.
Copies of the record. The client always has the file copy and may be given a second copy
(EntitlementReplica, constructor argument $fallback). Every read merges the verified
copies: the higher issued_at wins, a signed denial wins a tie with an active record, and
the high-water is the highest observed_at. A writer (refresh, an advance) re-reads every
copy without memo, applies its change to the merged record and stores the result in the
file copy, or in the second copy when the file copy cannot be written, with
compare-and-swap against what that copy held at the re-read; on a conflict it starts over
(at most 8 times, then it fails closed). Because every stored record is the merge of all
copies plus the change, no copy ever orders below what another writer stored, so a write
cannot roll back a denial or the high-water, and a reader that sees both copies always gets
the newest state. A copy that exists but cannot be read fails the read closed (it might hold
a denial). The file copy serializes writers with flock on <path>.lock, compares under
the lock and replaces the file by an atomic same-directory rename (a tempnam() fallback
outside the directory is refused).
The WordPress adapter passes a database copy (WordPressAdmin\OptionReplica): a row of the
options table of the current network’s main site, named original_entitlement_store_<sha256 of license, installation, site origin> (the file name’s digest; the key map is bound by
verification, so a key rotation keeps recorded denials). It is accessed with direct
single-row statements, not the options API or the object cache: compare-and-swap is an
UPDATE ... WHERE option_name = ... AND CAST(option_value AS BINARY) = CAST(<expected> AS BINARY) (byte-exact, collation-independent) or an INSERT IGNORE when there was no row, and one affected row
means stored. A single-row statement is atomic, so a reader never sees half a record. Each
network has its own main-site table and its own origin in the name, so one network’s copy
never answers for another. Lock-free reads of the row are memoized per process for at most
the current second. So on a read-only entitlement directory (image) WordPress keeps access
past the first deadline, records signed denials and the high-water in the database, and a
clock rollback cannot renew access. Outside WordPress (no $wpdb) the file copy is the
only one, and an advance it cannot store fails closed. Uninstall removes the stored identity,
which orphans the row (it can no longer be addressed); the file copy is likewise left in place.
Within one PHP process verification results are memoized per binding (license, installation,
site, key map) and record bytes, only when every configured key is an inline PEM string
(-----BEGIN); a file:// key or key object can change behind the same value and is
verified again. The file copy’s bytes are memoized by device, inode, size and mtime; a file
whose mtime is not older than the second it was read is read again, and writers always read
the file. This metadata is not signed, so a party controlling the client/files/database/
system clock can alter enforcement. No offline entitlement system is uncrackable. Network
transport runs outside any lock. Separate client objects observe a stored denial on their
next local status call; stale active responses cannot overwrite it. Distributed filesystems
with unreliable advisory locks and externally rewritten clock metadata remain outside this
prototype’s evidence. Lock or persistence failures of every writable copy deny local access.
When a signed denial cannot be stored in any copy, the client fails closed without ever removing
a stored denial: if the copies already merge to a denial nothing is removed; otherwise each copy
holding a verified active record is removed by compare-and-delete (file: under the lock; database:
DELETE ... AND CAST(option_value AS BINARY) = CAST(<loaded> AS BINARY)). A copy that cannot be
removed (a read-only file) keeps its active record, as a single file did before. Writers read the
database copy on the primary when a read/write-split wpdb offers send_reads_to_masters(); a
lock-free read from a lagging replica can show an older state for the replication lag, and a
writer running inside an outer database transaction is outside this design.
Tests use generated ephemeral RSA keys; no private key is committed:
docker run --rm --network none -v "$PWD:/opt/plugin:ro" -w /opt/plugin \
php:8.3-apache-bookworm php product/licensing/client-test.php
PHP 8.3 tests do not establish PHP 7.2 runtime coverage; the code intentionally avoids newer syntax. Cross-language compatibility with the Go replacement must pass before adopting it as the actual backend. Owner-confirmed reuse authorization is recorded in ../../PROVENANCE.md; entitlement signatures grant no upstream rights.
Explicit update helpers
checkUpdates($transport) checks the local premium entitlement, then calls
$transport('POST', '/updates', identityArray, []). It requires decoded metadata
with a semantic version string, lowercase SHA256 digest, bounded ASCII bearer token,
active: true and the bound normalized site. download($transport, $metadata)
rechecks local access and metadata, calls
$transport('GET', '/download', [], ['Authorization' => 'Bearer ' . token]), then
returns bytes only if their SHA256 matches. Transport must throw on non-success
HTTP status, including signed-server policy denial, and use the same configured
trusted HTTPS origin. No arbitrary download URL is accepted. Download bytes must
be bounded by the transport to the intended package size. These helpers do not
install/extract packages or modify WordPress.
Transport failures propagate; local grace cannot force a server to approve a revoked license or unavailable download. TLS/server authenticity protects unsigned update metadata; its digest detects corrupted/mismatched bytes, not a malicious issuer. No updater endpoint is configured by default.
Usable explicit HTTP transport
transport.php supplies HttpTransport, an operator-configured fixed HTTPS origin
with verified TLS peer and hostname, redirects disabled, a 5-second default time
budget, 64-KiB JSON and 8-MiB ZIP bounds. Configurable ceilings are 30 seconds,
1 MiB JSON and 64 MiB ZIP. Unsupported methods/paths and arbitrary headers are
rejected. Non-200 responses, wrong content types, invalid JSON, TLS failures and
oversized responses throw generic errors without displaying request secrets.
require_once 'product/licensing/transport.php';
use OriginalProduct\Licensing\EntitlementClient;
use OriginalProduct\Licensing\HttpTransport;
$transport = new HttpTransport(getenv('PRODUCT_LICENSE_ORIGIN'));
$keys = ['prod-v1' => file_get_contents(getenv('PRODUCT_PUBLIC_KEY_PATH'))];
$client = new EntitlementClient(
getenv('PRODUCT_LICENSE_KEY'), getenv('PRODUCT_INSTALLATION_KEY'),
getenv('PRODUCT_SITE_ORIGIN'), $keys, getenv('PRODUCT_ENTITLEMENT_CACHE_PATH')
);
// Invoke these only through an explicitly authorized administrative action.
$entitlement = $client->refresh([$transport, 'entitlement']);
if ($entitlement['premium_update_access']) {
$metadata = $client->checkUpdates($transport);
$verifiedZipBytes = $client->download($transport, $metadata);
}
// A local status read makes no service request.
$local = $client->status();
For activation/deactivation/check, explicitly call
$transport('POST', '/activate', $identity, []) (or /deactivate, /check) with
license_key, installation_key, site. No lifecycle call happens automatically.
Only HttpTransport::loopbackFixture('http://127.0.0.1:PORT') permits HTTP, for
local fixtures. It rejects localhost names, other IPs, credentials and URL suffixes;
production configuration never falls back to HTTP. Run focused validation with
php product/licensing/transport-test.php; live HTTP behavior is exercised by the
cross-language integration runner. PHP stream connection/header timeout behavior
follows the host runtime; response-body reads also enforce the remaining time budget.