Service-account authentication for PHP clients of the Blerify platform.
It signs a short-lived JWT client assertion with a service-account private
key and exchanges it at the Blerify IAM token endpoint for an access token,
which is then sent as Authorization: Bearer <token> on API calls. This is the
RFC 7523 private_key_jwt client
authentication flow.
The token-acquisition logic was extracted from blerify/mdl (php-mdl) so it
can be reused by any PHP service that calls Blerify — without depending on the
mDL library.
- PHP >= 8.1 with
ext-curl,ext-openssl,ext-json - A Blerify service-account credentials JSON file (issued by the Blerify portal when a service account is created)
composer require blerify/auth-php-clientuse Blerify\Auth\ServiceAccountTokenProvider;
$tokens = ServiceAccountTokenProvider::fromFile(__DIR__ . '/credentials.json');
// Returns a cached token until shortly before it expires.
$accessToken = $tokens->getAccessToken();
// Use it on any Blerify API request (e.g. via the gateway):
// Authorization: Bearer {$accessToken}A complete runnable example lives in index.php:
composer install
# Generate the service-account credentials JSON from the Blerify portal and
# save it as config/credentials.json
php index.phpIt loads the credentials file, obtains a token, and shows how to attach it as a
Bearer header. The credentials JSON is generated from the Blerify portal;
config/credentials.example.json documents its shape, and real credentials
files under config/ are git-ignored.
The service-account JSON is generated from the Blerify portal (create a service account, then download its credentials file). It contains (other fields are ignored):
| Field | Used for |
|---|---|
client_id |
iss / sub of the assertion, and the client_id form field |
organization_id |
sent as the organization_id form field |
private_key |
PEM key that signs the assertion (RS256) |
token_uri |
the Blerify IAM token endpoint |
iam_audience |
the aud of the assertion |
The provider builds and signs this assertion:
and POSTs it as application/x-www-form-urlencoded to token_uri:
POST {token_uri}
Content-Type: application/x-www-form-urlencoded
client_id={client_id}&organization_id={organization_id}&client_assertion={signed_jwt}
The endpoint responds with a standard OAuth2 token response; the access_token
field is what gets used as the Bearer token.
getAccessToken() uses cURL by default. To use a different HTTP stack (or to
test without a network), implement Blerify\Auth\TokenEndpointClient and pass
it in:
$tokens = new ServiceAccountTokenProvider($credentials, $myTokenEndpointClient);composer install
composer test # runs PHPUnitUnit tests use a throwaway RSA keypair fixture (tests/fixtures/) to verify the
signed assertion's claims and signature, the form sent to the endpoint, token
caching and renewal, and error handling — no network or real credentials
required.