Skip to content
ProofAgePublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ProofAge PHP SDK

Framework-neutral PHP client for the ProofAge age and identity verification API: HMAC request signing, the resource methods, streaming media downloads, and inbound webhook verification. Runtime dependencies are php ^8.1, ext-curl, ext-json and the psr/http-message interfaces — nothing else, so it installs cleanly next to whatever your project already uses.

On Laravel, use proofage/laravel-client instead: it wraps this SDK with a service provider, a facade, the webhook middleware and the proofage:verify-setup command.

Install

composer require proofage/php-sdk

First request

use ProofAge\Sdk\Client;

$client = new Client([
    'api_key' => getenv('PROOFAGE_API_KEY'),
    'secret_key' => getenv('PROOFAGE_SECRET_KEY'),
    'base_url' => 'https://api.proofage.net',
]);

$workspace = $client->workspace()->get();

Every request is signed with X-API-Key, X-Timestamp and X-HMAC-Signature; you never touch any of them. The timestamp is read, and the request signed, again on every retry, so keep the host clock right: the API refuses a timestamp more than 300 seconds off (TIMESTAMP_OUT_OF_RANGE) and a repeated signature (REPLAYED_REQUEST). It also says which SDK sent it: X-ProofAge-Sdk: php/0.3.0 and User-Agent: ProofAge-PHP/0.3.0 (PHP 8.4.1) (see SDK identification).

Configuration

Key Default
api_key required Workspace API key
secret_key required Workspace secret key, used only to sign
base_url https://api.proofage.net The API origin. A trailing /v1 (the address the API reference prints) is ignored, since the client adds the version
version v1 API version segment
timeout 30 Seconds per attempt; a positive integer (sub-second timeouts are not supported)
retry_attempts 3 Attempts for interactive requests; see Retries
retry_delay 1000 Milliseconds between attempts, constant; an integer, 0 allowed
download_retry_attempts 1 Attempts for media downloads; only a transport failure is retried, never an HTTP status
sdk_tokens [] For a package wrapping this SDK: name/version tokens prepended to X-ProofAge-Sdk
user_agent_prefix '' For a package wrapping this SDK: Name/version products prepended to the default User-Agent

Retries

A GET gets another attempt after a transport failure, a 429, or any non-2xx status that is not a 4xx (a 3xx or a 5xx).

A DELETE (webhookSubscriptions()->delete()) is retried the same way; if the first attempt went through, the retry answers 404. erasePersonalData() is the exception: its DELETE is retried as a POST is, below.

A POST — acceptConsent(), uploadMedia(), submit(), blockFace(), setTestOutcome() — and the DELETE of erasePersonalData() are retried only when repeating it cannot make the server act twice: when the connection failed before the request was sent (DNS, connection refused, TLS handshake), or on a 429 that carries Retry-After. A 5xx or a timeout on a POST is thrown at once, because the server may already have created the verification, stored the upload or submitted it. Catch the exception and decide: get() tells you whether a submit went through.

A POST that carries an Idempotency-Key is the exception, and is retried like a GET: that is verifications()->create() and webhookSubscriptions()->create(). The SDK generates a UUID v4 key per call and reuses it on every attempt (with a fresh timestamp and signature), so a timeout or a 5xx can never create a second verification; a 409 IDEMPOTENCY_KEY_IN_PROGRESS is retried after its Retry-After. Pass your own key to make your own retry safe too:

$verification = $client->verifications()->create(['external_id' => 'order-42'], idempotencyKey: 'order-42');

The key is 1 to 255 printable ASCII characters. The same key and request within 24 hours returns the first response; the same key with another request throws 409 IDEMPOTENCY_KEY_REUSED.

A non-GET request the API refuses with 401 REPLAYED_REQUEST (an identical request was signed in the same second, and the signature covers whole seconds only) was never processed, so the SDK waits for the next whole second, signs it again with a new X-Timestamp and resends it once, keeping the Idempotency-Key. This applies to every POST and DELETE whatever the retry rules above say, and does not count against retry_attempts. A second REPLAYED_REQUEST throws ProofAgeException.

If the host clock is more than 300 seconds off, the API answers 401 TIMESTAMP_OUT_OF_RANGE with a Date header. The SDK then takes the server's time from it for this and every later request of the client, signs again and resends once (outside retry_attempts); without a Date header, or if it fails again, the ProofAgeException is thrown. Before any re-attempt of a POST or DELETE the SDK also waits until the clock is past the previous attempt's timestamp second, so a retry is never refused as a replay; that wait replaces the retry_delay pause when it is longer. Only the two creates the SDK sends an Idempotency-Key with are retried on a 5xx or a timeout: a header your own middleware adds elsewhere does not change that.

The four numeric settings must be integers (integer-valued strings such as getenv() returns are accepted); a float or anything else throws ProofAgeException at construction.

Resources

$verification = $client->verifications()->create([
    'callback_url' => 'https://example.com/verification/done',   // where the browser goes afterwards
    'external_id' => 'user-42',
]);

$verification['url'];                          // the link the person opens
\ProofAge\Sdk\Enums\VerificationStatus::tryFrom($verification['status']);

$v = $client->verifications($verification['id']);

$v->get();
$v->acceptConsent(['consent_version_id' => $consent['id'], 'text_sha256' => $consent['text_sha256']]);
$v->uploadMedia(['type' => 'document', 'side' => 'front', 'document' => 'passport', 'file' => '/tmp/front.jpg']);
$v->uploadMedia(['type' => 'selfie', 'file' => new \SplFileInfo('/tmp/selfie.jpg')]);
$v->submit();
$v->document();
$v->estimation();
$v->blockFace(['reason_code' => \ProofAge\Sdk\Enums\BlockFaceReasonCode::UNDERAGE->value]);

$client->workspace()->get();
$client->workspace()->getConsent();

Listing verifications

use ProofAge\Sdk\Enums\VerificationStatus;

$cursor = null;

do {
    $page = $client->verifications()->list([
        'status' => [VerificationStatus::APPROVED, VerificationStatus::DECLINED],   // or 'approved,declined'
        'external_id' => 'user-42',
        'limit' => 100,
        'cursor' => $cursor,
    ]);

    foreach ($page['data'] as $verification) {
        // the shape get() returns, newest first
    }

    $cursor = $page['next_cursor'];
} while ($cursor !== null);

A status list is sent comma-separated and null entries are left out. The query is signed as the API normalizes it (keys sorted, RFC 3986 encoding). Send the same filters with every cursor.

Test outcomes

In a test workspace, finish a verification with a chosen outcome to exercise your webhook handler end to end. A live workspace answers 403 with getErrorCode() TEST_WORKSPACE_ONLY; a verification already in a final status answers 422 INVALID_STATUS.

$client->verifications($id)->setTestOutcome(['status' => 'approved']);
$client->verifications($id)->setTestOutcome(['status' => 'resubmission_requested', 'reason' => 'Blurry photo']);

Erasing personal data

use ProofAge\Sdk\Enums\ErasureReasonCode;

$verification = $client->verifications($id)->erasePersonalData([
    'reason_code' => ErasureReasonCode::CUSTOMER_REQUEST->value,   // required
    'reason' => 'one-time public demo session',                     // required with OTHER
]);

$verification['erasure'];   // ['erased_at' => ..., 'scope' => 'personal_data', 'reason' => ..., 'requested_via' => 'customer']

Removes the images, recordings, document fields, analysis results and device data of an approved, declined, abandoned or expired verification (any other status answers 422 VERIFICATION_NOT_FINAL). The status, dates and the fact of the erasure stay, and no webhook is sent. A repeat call returns the verification again. A 409 (VERIFICATION_PROCESSING, ERASURE_IN_PROGRESS) or a 503 (ERASURE_FAILED, nothing erased) is not retried by the SDK: call again after the Retry-After, about 3 seconds.

Face re-verification

Check that the person in front of the camera is the one who passed an earlier ID verification, with a selfie only:

$reverification = $client->verifications()->create([
    'external_id' => 'student-42',
    'reference_verification_id' => $kycVerificationId,   // an approved document verification of the same workspace
]);

// Send the person to $reverification['url']; the decision arrives by webhook.

$verification = $client->verifications()->find($reverification['id']);
$verification['reauth'];   // ['reference_verification_id' => ..., 'reference_verified_at' => ...]

The selfie is compared 1:1 with the reference's face and decided on the spot: approved, or declined with reference.face.mismatch (reference.unavailable if the reference was erased in the meantime). It is not billed, the person takes it in the hosted page (the headless consent, media and submit calls answer 409 REAUTH_HOSTED_ONLY), and reauth is on re-verifications only. A refusal throws: 404 REFERENCE_VERIFICATION_NOT_FOUND, 422 REFERENCE_NOT_APPROVED, REFERENCE_NOT_SUPPORTED or REFERENCE_UNAVAILABLE (a ValidationException), 429 REAUTH_RATE_LIMITED (more than 10 for one reference in 24 hours; it does not clear within seconds and is not retried). See the Face re-verification guide.

Webhook subscriptions

REST hooks (Zapier and the like) subscribe a URL to the decision webhooks, in addition to the workspace webhook URL. A workspace can have up to 50 (WEBHOOK_SUBSCRIPTION_LIMIT past that).

$subscription = $client->webhookSubscriptions()->create([
    'url' => 'https://hooks.zapier.com/hooks/standard/12345678/abcdef/',
    'statuses' => ['approved', 'declined'],   // omit or null for every decision status
    'include_document_data' => false,         // the default: no document, fingerprint signals or moderator
]);

$client->webhookSubscriptions()->list();      // ['data' => [...]], newest first
$client->webhookSubscriptions()->delete($subscription['id']);

Deliveries carry the workspace webhook's headers and body and are signed with the secret key that created the subscription (the active key once that one is deleted). A delivery answered with 410 Gone deletes the subscription.

Methods return the decoded JSON as array|null; uploadMedia(), submit() and blockFace() return null and webhookSubscriptions()->delete() returns nothing, because the API answers them with an empty body. Every request and response shape is documented in AGENTS.md and in the @param/@return PHPDoc on src/Resources/.

uploadMedia() accepts a path, any \SplFileInfo (Symfony's and Laravel's UploadedFile included) or a ProofAge\Sdk\Http\Body\FilePart. A path that does not exist throws \InvalidArgumentException before anything is sent. Form fields are sent as the server will read them back: a null field is left out and a boolean is sent as 1 / 0, so the signature matches whichever transport sends the body.

makeRequest($method, $endpoint, $data, $files) takes the endpoint relative to the version segment, with raw (not pre-encoded) path segments: each segment is percent-encoded once, so the signed path is exactly the path sent. An endpoint containing . or .. segments, a #fragment, whitespace or control characters — things a transport rewrites before sending — throws \InvalidArgumentException rather than producing a 401 "HMAC signature is invalid".

Media downloads

$stream = $v->downloadMedia($mediaId);          // Psr\Http\Message\StreamInterface
$path = $v->downloadMediaTo($mediaId, '/var/media/front.jpg');

Downloads send Accept: application/json, */*;q=0.8: the endpoint answers with the file's bytes regardless, and the header makes the server render a 403 or a 404 as a JSON error the exception can read instead of an HTML page.

With the bundled cURL transport the body is received into php://temp, which spills to disk past 2 MB, and is never held as a PHP string. Downloads never retry an HTTP status (429 included): they usually run from a queue whose own backoff owns the wait. Raise download_retry_attempts to retry connection failures only.

downloadMediaTo() writes to a temporary file next to the destination and renames it into place only after a 2xx; a 404 or a timeout leaves nothing at the destination (and a file already there untouched), and the exception still carries the error body.

Errors

use ProofAge\Sdk\Exceptions\AuthenticationException;   // 401
use ProofAge\Sdk\Exceptions\ValidationException;       // 422, getErrors()
use ProofAge\Sdk\Exceptions\TransportException;        // connection refused, DNS, TLS, timeout
use ProofAge\Sdk\Exceptions\ProofAgeException;         // every other non-2xx, and the base class
use ProofAge\Sdk\Exceptions\ExceptionInterface;        // marker: catch the whole family

try {
    $client->verifications()->create($data);
} catch (ValidationException $e) {
    $errors = $e->getErrors();
} catch (TransportException $e) {
    // no response: $e->getResponse() is null, $e->getCode() is the cURL errno
} catch (ProofAgeException $e) {
    $e->getCode();       // HTTP status
    $e->getErrorCode();  // e.g. MEDIA_NOT_FOUND, PAYMENT_METHOD_REQUIRED, FACE_NOT_FOUND; null when the body has none
    $e->getErrorData();  // the `error` object, or the whole body when it is not nested
    $e->getResponse();   // ProofAge\Sdk\Http\Response
}

The API nests most errors as {"error": {"code", "message"}} but answers 402 and the upload quality checks with a flat {"code", "message"}, request validation with {"message", "errors"} and 403 with {"message"}; the exception reads all of them. A 422 from an upload quality check is a ValidationException whose getErrors() is empty and whose getErrorCode() says what failed.

Missing verification IDs and missing files throw \InvalidArgumentException.

Dumping SDK objects

print_r() and var_dump() of a client, a request, a response or an SDK exception — including the exception's trace with zend.exception_ignore_args=0 — never show the secret key, show the API key masked to its last four characters, and show request bodies and uploaded files as sizes and sha256 hashes. So error_log(print_r($e, true)) in a catch block is safe.

That protection comes from __debugInfo(), which var_export(), (array) casts, reflection and Symfony's VarDumper (Laravel's dd() / dump()) do not honour or only merge with the real properties. Do not point those at a Client, Signer, Request or WebhookVerifier. On PHP 8.1, where #[\SensitiveParameter] does not exist, a failure inside the Client constructor still leaves the config array in the trace unless zend.exception_ignore_args=1.

Webhooks

Status webhooks go to the workspace's webhook URL (set in the console; workspace()->get() returns it as webhook_url), not to the callback_url given to create(), which is only where the person's browser is sent afterwards. ProofAge signs every delivery with X-Auth-Client, X-Timestamp and X-HMAC-Signature (HMAC-SHA256 of {timestamp}.{rawBody}) using the workspace's active secret key; API requests accept any key that has not been deleted, so give the verifier the active one. The body is documented in AGENTS.md.

use ProofAge\Sdk\Webhooks\WebhookVerifier;
use ProofAge\Sdk\Exceptions\WebhookVerificationException;

$verifier = new WebhookVerifier(getenv('PROOFAGE_API_KEY'), getenv('PROOFAGE_SECRET_KEY'));

try {
    $verifier->verifyHeaders(getallheaders(), file_get_contents('php://input'));
} catch (WebhookVerificationException $e) {
    http_response_code($e->statusCode);
    echo json_encode($e->toArray());   // {"error": {"code": "INVALID_SIGNATURE", "message": "..."}}
    exit;
}

Codes, in the order they are checked: MISSING_SIGNATURE, MISSING_TIMESTAMP, MISSING_AUTH_CLIENT, INVALID_AUTH_CLIENT, TIMESTAMP_TOO_OLD, INVALID_SIGNATURE. The timestamp tolerance defaults to 300 seconds (third constructor argument).

Read event before status. It is status.updated for a decision, and data.updated when someone on your team corrected document fields the reader got wrong: status is then the current one, unchanged, document has the corrected values and changed_fields the names of the fields that changed. A body without event (a retry of an older delivery) is status.updated. The SDK has no webhook model; ProofAge\Sdk\Enums\WebhookEvent names the two events:

use ProofAge\Sdk\Enums\WebhookEvent;

$body = json_decode($rawBody, true);

if (WebhookEvent::tryFrom($body['event'] ?? 'status.updated') === WebhookEvent::DATA_UPDATED) {
    // refresh the stored document fields; this is not a new decision
    http_response_code(200);
    exit;
}

Middleware

A middleware is callable(Request $request, callable $next): Response. It runs once per HTTP attempt and before signing, so whatever it changes is what gets signed — a middleware can add a header or rewrite the body and the signature stays valid. It never sees X-API-Key, X-HMAC-Signature, X-ProofAge-Sdk or the default User-Agent; those are added below it. The first middleware pushed is the outermost.

use ProofAge\Sdk\Http\Request;
use ProofAge\Sdk\Http\Response;

$client->pushMiddleware(function (Request $request, callable $next): Response {
    return $next($request->withHeader('X-Request-Id', bin2hex(random_bytes(8))));
}, 'request-id');

// Once per logical call rather than per attempt:
$client->pushMiddleware(function (Request $request, callable $next): Response {
    if ($request->attempt === 1) {
        $quota->consume();
    }

    return $next($request);
});

$client->removeMiddleware('request-id');

A middleware that returns a Response without calling $next short-circuits: nothing is signed, no event fires, nothing is sent.

SDK identification

Every request carries X-ProofAge-Sdk: name/version tokens separated by single spaces, the outermost wrapper first and this SDK's php/{Client::VERSION} last. On its own the SDK sends php/0.3.0; proofage/laravel-client sends laravel/0.9.0 php/0.3.0 (versions here are examples). The User-Agent is ProofAge-PHP/0.3.0 (PHP 8.4.1) unless the request already has one. Neither header is part of the HMAC signature.

Both are set below the middleware, on every attempt, so a middleware cannot remove or replace X-ProofAge-Sdk (whatever it sets there is overwritten); it can set its own User-Agent, which is then sent as is. A package that wraps the SDK adds itself with two options, validated at construction:

$client = new Client($config + [
    'sdk_tokens' => ['acme-shop/2.1.0'],              // lowercase name, outermost first
    'user_agent_prefix' => 'AcmeShop/2.1.0',          // User-Agent products, space-separated
]);
// X-ProofAge-Sdk: acme-shop/2.1.0 php/0.3.0
// User-Agent: AcmeShop/2.1.0 ProofAge-PHP/0.3.0 (PHP 8.4.1)

A PSR-18 client's own default User-Agent (Guzzle's GuzzleHttp/7, or one set in its headers option) is replaced, because the SDK puts its User-Agent on the request itself; set yours through a middleware instead.

Events

Events observe the signed request going down and the response or transport failure coming back, once per attempt.

use ProofAge\Sdk\Events\{RequestEvent, ResponseEvent, ErrorEvent};

$client->onRequest(fn (RequestEvent $e) => $log->info('proofage.request', [
    'method' => $e->method(),
    'url' => $e->url(),
    'attempt' => $e->attempt(),
    'headers' => $e->headers(),   // ['X-API-Key' => '****7f2a', 'X-HMAC-Signature' => '4c6daa63...', ...]
    'body' => $e->body(),         // ['kind' => 'multipart', 'fields' => [...], 'files' => [['name' => 'file', 'filename' => 'front.jpg', 'bytes' => 183422, 'sha256' => '...']]]
]));

$client->onResponse(fn (ResponseEvent $e) => $metrics->timing('proofage.request_ms', $e->durationMs(), [
    'status' => $e->status(),
    'attempt' => $e->attempt(),
]));

$client->onError(fn (ErrorEvent $e) => $log->warning('proofage.transport', [
    'attempt' => $e->attempt(),
    'error' => $e->exception()->getMessage(),
]));

An HTTP error status is a response and arrives through onResponse; onError fires only for transport failures. A listener that throws aborts the request.

What the events redact, and what raw() exposes

Events are views built for logging. RequestEvent::headers() masks X-API-Key to its last four characters and X-HMAC-Signature to its first eight; RequestEvent::body() reduces a JSON body to its byte count and sha256 and each file to name, filename, size and sha256 (scalar form fields such as type and side are shown verbatim); ResponseEvent has no body accessor at all.

raw() on any event returns the underlying Request or Response with everything in it: the API key, a signature that replays the request until the API's 300-second timestamp window closes, document photos and selfies in multipart bodies, and response bodies carrying names, dates of birth and document numbers. Call it deliberately, and do not log what it returns.

Transports

The default transport is a bundled cURL client (ProofAge\Sdk\Http\Curl\CurlHttpClient): one handle per request, TLS verification on, no redirects, 10 s connect timeout.

PSR-18

If you already have a PSR-18 client and PSR-17 factories:

use ProofAge\Sdk\Http\Psr18\Psr18HttpClient;

$factory = new \GuzzleHttp\Psr7\HttpFactory;
$transport = new Psr18HttpClient(new \GuzzleHttp\Client(['timeout' => 30]), $factory, $factory);

$client = new Client($config, $transport);

PSR-18 has no per-request timeout, so timeout from the config is not applied there; configure it on your client. Any Psr\Http\Client\ClientExceptionInterface surfaces as TransportException with the original as getPrevious().

Your own

Implement ProofAge\Sdk\Http\HttpClient — one method, send(Request): Response — and pass it as the second constructor argument. A transport sends exactly what it is given and never retries or throws on an HTTP status; the SDK owns both. When it fails below HTTP it throws TransportException; pass requestMayHaveBeenSent: false only when it knows the request never left (the connection was never made), which is what allows a POST to be retried.

Testing your integration

ProofAge\Sdk\Testing\FakeHttpClient ships in the package and needs no network:

use ProofAge\Sdk\Testing\FakeHttpClient;

$fake = new FakeHttpClient([
    'api.proofage.net/v1/workspace' => FakeHttpClient::json(['id' => 'ws_1', 'name' => 'Acme']),
    'api.proofage.net/v1/verifications/*' => [               // a sequence
        FakeHttpClient::json(['error' => ['code' => 'RATE_LIMIT']], 429, ['Retry-After' => '1']),
        FakeHttpClient::json(['id' => 'ver_1', 'status' => 'created']),
    ],
    '*' => FakeHttpClient::failedConnection(),
]);

$client = new Client($config, $fake);

$client->workspace()->get();

$fake->assertSent(fn ($request) => $request->method === 'GET' && str_ends_with($request->url, '/v1/workspace'));
$fake->assertSentCount(1);

Patterns use * wildcards and are tried in order. FakeHttpClient::failedConnection() fails before sending (a POST is retried after it); FakeHttpClient::timeout() fails after the request may have arrived (a POST is not). sent() returns the requests as the transport received them — signed, one per attempt — so you can assert X-HMAC-Signature and $request->body->bytes directly. An unmatched URL throws \LogicException.

The wait between retry attempts is usleep() unless you pass a fourth constructor argument, callable(int $microseconds): void; a test that exercises retries passes a recorder or static fn () => null so it does not sleep for real (the Laravel package passes Sleep::usleep(...) so Sleep::fake() covers it).

Contract

resources/openapi.json is the bundled API spec and resources/hmac-vectors.json the golden signature vectors this SDK executes in its test suite. The fixture ships in the dist so the ProofAge server's tests can execute the same file; until they do, the two implementations agree by inspection. See AGENTS.md.

License

MIT. See LICENSE.md.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages