PHP code example of jewei / app-attest-php

1. Go to this page and download the library: Download jewei/app-attest-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jewei / app-attest-php example snippets


use AppAttest\AppId;
use AppAttest\CategoryPolicy;
use AppAttest\Environment;
use AppAttest\ValidationCategory;
use AppAttest\Verifier;

$verifier = new Verifier(
    new AppId('ABCDE12345', 'com.example.app'), // App ID prefix (team ID) and bundle ID
    Environment::Production,                     // from your server configuration
    CategoryPolicy::onlyOrMissing(ValidationCategory::AppStore, ValidationCategory::TestFlight),
);

use AppAttest\Exception\VerificationException;

$attestation = base64_decode($request->attestation, true);
$keyId = base64_decode($request->keyId, true);

if ($attestation === false || $keyId === false) {
    return refuse(); // not Base64
}

try {
    $key = $verifier->verifyAttestation(
        attestation: $attestation,
        keyId: $keyId,
        clientDataHash: hash('sha256', $challenge, true), // your stored challenge, not a value from the request
    );
} catch (VerificationException $exception) {
    log_refusal($exception->failure->value); // for example "nonce_mismatch"

    return refuse();
}

// Store for this device (keyId, receipt, and certificate are raw bytes: use a binary column or Base64):
// $key->keyId, $key->publicKey (PEM), $key->environment->value, counter 0, and, if you use
// the fraud metric, $key->receipt (unverified: see below) and $key->certificate (each new receipt's field 3 must equal it).

try {
    $assertion = $verifier->verifyAssertion(
        assertion: $assertionBytes, // decoded from Base64 as above
        clientData: $clientData,
        publicKey: $stored->publicKey,
        previousCounter: $stored->counter,
        keyEnvironment: Environment::from($stored->environment), // the key's stored environment
    );
} catch (VerificationException $exception) {
    return refuse();
}

// Store $assertion->counter in the same transaction that read $stored->counter.

use AppAttest\Testing\TestAuthority;

$appId = new AppId('ABCDE12345', 'com.example.app');
$authority = TestAuthority::create();
$device = $authority->device($appId, Environment::Production, ValidationCategory::TestFlight);
$verifier = new Verifier($appId, Environment::Production, CategoryPolicy::only(ValidationCategory::TestFlight), $authority->rootPem());

$attestation = $device->attest(hash('sha256', $challenge, true)); // send with base64_encode($device->keyId)
$assertion = $device->assert($clientData);                        // the counter increases with each call
bash
composer