API keys for the Bleeding Deacons suite. A Composer library, not a WordPress plugin — it is never activated. Integrity, Promises and Sentinel each require it and load it through their own Composer autoloaders.
It exists because all three carried their own copy of this code, and the copies drifted. Only Integrity refused a malformed key before spending 64 MiB of Argon2id on it. None of them noticed that WordPress calls a REST route's permission_callback a second time to build the Allow header, so every request was authenticated two or three times — two audit rows, two rate-limit increments, sixteen Argon2id verifies in Integrity's case (integrity#90). Code that decides whether a stranger holds a valid key should exist once.
| Class | What it does |
|---|---|
KeyFormat |
A consumer's key shape: its prefix (int_, prm_, snt_), random bytes as lower-case hex, and how much of a key an admin screen may show. generate(), and looksLikeKey() to refuse junk for free before any hashing. |
KeyHasher |
Argon2id at 64 MiB, time cost 4 — the parameters every stored hash in the suite already uses. |
KeyVerifier |
Checks a key against candidate hashes at a fixed cost: at least n password_verify() calls, real or padding, and no early exit on a match, so timing says nothing about whether a key exists or how many share a prefix. |
PresentedKey |
The key a client sent: Authorization: Bearer <key>, else X-API-Key. |
OncePerRequest |
Wraps a permission check so it runs once per request object, however many times WordPress asks. Pass it as the permission_callback. |
FailureThrottle |
From 1.1. Bounds the Argon2id work one address can make a server do with wrong keys: counts failures per REMOTE_ADDR in a fixed window (20 per 15 minutes by default), in transients, and says when to refuse before verifying. Integrity's PreAuthThrottle, made general. |
SingleKey\SingleKey |
One key guarding one endpoint: generate() (shown once, stored as a hash), revoke(), verify(). With no key issued, everything is refused. |
SingleKey\SingleKeyStorage |
The interface a consumer's settings class implements to keep that key's hash, display prefix and creation time — in its own row, so adopting Warden moves no data. |
SingleKey\KeyLifetime |
From 1.2. How long a single key stays valid after it is issued: 3 months by default, any number of months up to 120, or never (0). Applied to the key's stored creation time when checked, so changing it moves the expiry of the key already issued. |
SingleKey\SingleKeyAdminSection |
The key panel for a consumer's own admin page: which key is live, the new key shown once, Generate / Regenerate / Revoke. register() hooks its admin-post.php handlers; render() prints it. |
KeySet\KeySet |
From 1.3. Several labelled keys for one endpoint, each with its own auto-disable period (seconds; 0 never) and an on/off switch. issue() (shown once), enable() — which restarts the full period — disable(), setAutoDisableAfter(), revoke(), verify() (one Argon2id check, picked by display prefix), touch() for "last used", and import() to bring in an old single key. |
KeySet\KeyRecord |
One stored key: id, label, hash, display prefix, created, period, when the running countdown ends, disabled, last used. status($now): active, disabled or timed_out; secondsLeft($now). |
KeySet\KeySetStorage |
The interface a consumer's settings class implements to keep the records — a list of arrays in one field of its option. |
KeySet\KeySetAdminSection |
The keys panel: a table with each key's status and time left, its period, Enable / Disable / Revoke, and a form to add a key with a label and a period. |
What it deliberately does not ship: key tables, permissions, rate limits on callers that hold a key, IP allowlists, audit logging, or logging of any kind. (FailureThrottle limits only failed checks — the work a stranger can cause — and leaves logging to the consumer: penalise() says which failure shut a client out, so that can be logged once.) A consumer with several keys of different reach — Integrity — keeps its own table and uses KeyFormat, KeyHasher and KeyVerifier directly. Warden returns verdicts; the consumer decides what to log.
use Warden\KeyFormat;
use Warden\OncePerRequest;
use Warden\PresentedKey;
use Warden\SingleKey\SingleKey;
use Warden\SingleKey\SingleKeyAdminSection;
// The consumer's settings class implements Warden\SingleKey\SingleKeyStorage
// over the row it already has.
$key = new SingleKey($settings, new KeyFormat('snt_'));
// REST: check once per request.
$permission = new OncePerRequest(function (WP_REST_Request $request) use ($key) {
return $key->verify(PresentedKey::fromRequest($request))
? true
: new WP_Error('unauthorized', 'A valid API key is required.', ['status' => 401]);
});
register_rest_route('sentinel/v1', '/mcp', [
'methods' => 'POST',
'callback' => [$controller, 'handle'],
'permission_callback' => $permission,
]);
// Admin: one panel, two handlers.
$section = new SingleKeyAdminSection($key, 'sentinel_mcp', admin_url('admin.php?page=sentinel-mcp'));
$section->register(); // once, from the admin boot
$section->render(); // where the panel belongs on the pageuse Warden\FailureThrottle;
$throttle = new FailureThrottle('sentinel_mcp'); // names its transients
$ip = FailureThrottle::clientIp(); // REMOTE_ADDR only, never a forwarding header
if ($throttle->isBlocked($ip)) {
// Before verifying: this is the work the throttle exists to save.
return new WP_Error('too_many_attempts', 'Too many failed attempts.', ['status' => 429]);
}
if (!$key->verify(PresentedKey::fromRequest($request))) {
if ($throttle->penalise($ip)) {
// This failure spent the budget: log it once, not every refusal after.
}
return new WP_Error('unauthorized', 'A valid API key is required.', ['status' => 401]);
}retryAfter() gives the seconds until the window turns, for a Retry-After header. A consumer that may load alongside Warden 1.0 checks class_exists(FailureThrottle::class) first — see Versions.
use Warden\SingleKey\KeyLifetime;
$lifetime = new KeyLifetime($settings->keyLifetimeMonths()); // 3 by default; 0 never expires
// Verify first: the Argon2id cost stays the same whether or not the key has
// expired, and only someone holding the key learns that it expired.
if ($key->verify($presented)) {
if ($lifetime->hasExpired($key->createdAt())) {
return new WP_Error('key_expired', 'This API key has expired.', ['status' => 401]);
}
return true;
}expiresAt($key->createdAt()) gives the expiry, "Y-m-d H:i:s" UTC, for the admin screen; '' when keys never expire. It is a class of its own rather than a new parameter on SingleKey, so a consumer that may load alongside an older Warden checks class_exists(KeyLifetime::class) and knows whether expiry is enforced — see Versions.
use Warden\KeyFormat;
use Warden\KeySet\KeySet;
use Warden\KeySet\KeySetAdminSection;
$keys = new KeySet($settings, new KeyFormat('snt_')); // $settings implements KeySetStorage
// Verify first: one Argon2id check whatever is presented, and only someone
// holding a key learns that it is switched off.
$record = $keys->verify($presented);
if ($record === null) {
return new WP_Error('unauthorized', 'Unauthorised.', ['status' => 401]);
}
if (!$record->isActive($keys->now())) {
return new WP_Error('key_disabled', 'This API key is disabled.', ['status' => 401]);
}
$keys->touch($record);
return true;
// On the admin page:
$section = new KeySetAdminSection($keys, 'sentinel_mcp', admin_url('admin.php?page=sentinel-mcp'));
$section->register(); // from admin boot
$section->render(); // where the panel belongsEach key counts down from when it was added or last enabled. When the period runs out the key is off (timed_out) and stays off until an administrator presses Enable, which starts the full period again; lengthening the period of a key that is already off does not bring it back. A consumer moving from SingleKey calls import() once with the old hash, display prefix and creation time; the imported key's countdown starts then, so the move never switches off a key that was working.
use Warden\KeyFormat;
use Warden\KeyHasher;
use Warden\KeyVerifier;
$format = new KeyFormat('int_', 32, 8); // 8: the prefix Integrity's table looks keys up by
if (!$format->looksLikeKey($presented)) {
return null; // free: no query, no hashing
}
$rows = /* active rows whose api_key_prefix = $format->displayPrefix($presented) */;
$index = (new KeyVerifier())->verify($presented, array_column($rows, 'api_key_hash'), 8);
return $index === null ? null : $rows[$index];Minor releases only. Warden is v1 and stays v1: every change is additive — a new class, a new method, a new optional parameter. Nothing is removed or has its signature changed; something superseded is deprecated and left working. Consumers require ^1.0.
That rule exists because a site loads one copy of Warden per request, whichever consumer's autoloader asks first, and every consumer gets that copy. A breaking change would therefore break whichever consumers had not moved yet.
Minor releases cannot prevent the opposite problem: an older copy may be the one loaded. If Sentinel ships Warden 1.2 and calls something new in it, but Integrity ships 1.0 and its autoloader runs first, Sentinel gets 1.0 and the call fails. So:
A consumer starts using a Warden feature only once every consumer that ships Warden has been released with at least the version that added it.
Until then, a consumer that needs the feature checks for it (method_exists(), class_exists()) and falls back. Adding a feature as a new class rather than a new method on an existing one makes that check trivial — Guardian does the same for the same reason.
By hand, like Guardian: tag vX.Y.Z on main once its CI is green, then run composer update bleedingdeacons/warden in each consumer. There is no release job, no zip and no GitHub Release asset. CI runs the gates on pull requests and on main.
.gitattributes export-ignores everything but src/, composer.json, LICENSE and README.md: consumers ship vendor/ inside their plugin zips, and Composer downloads GitHub's archive of a tag, which honours it. Without it the test suite would ride onto web servers.
composer install
composer test # Pest, over wp-mocks
composer phpstan # level 8
composer phpcs # PSR-12The suite hashes for real. Argon2id is slow on purpose, so the tests take a few seconds.
MIT License (Modified) — Copyright © 2025 The Bleeding Deacons. The licensee may not sell the Software, alone or as part of an aggregate software distribution containing the Software. See LICENSE.