PHP code example of particle-academy / fancy-passkeys

1. Go to this page and download the library: Download particle-academy/fancy-passkeys 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/ */

    

particle-academy / fancy-passkeys example snippets


use FancyPasskeys\Laravel\Concerns\HasPasskeys;

class User extends Authenticatable
{
    use HasPasskeys;
}

use FancyPasskeys\{PasskeyServer, PasskeyPolicy, PasskeyUser, RelyingParty};

$server = new PasskeyServer(
    new RelyingParty('example.com', 'Example App', ['https://example.com']),
    PasskeyPolicy::default(),
    $challengeStore,   // your ChallengeStore
    $credentialStore,  // your CredentialStore
);

// Registration
['state' => $state, 'publicKey' => $options] = $server->startRegistration(
    new PasskeyUser($userHandle, '[email protected]', 'Ada Lovelace')
);
['credential' => $credential] = $server->finishRegistration($state, $responseJson, 'MacBook Touch ID');

// Authentication (discoverable: pass no handle)
['state' => $state, 'publicKey' => $options] = $server->startAuthentication();
['userHandle' => $handle] = $server->finishAuthentication($state, $responseJson);

interface ChallengeStore
{
    public function put(string $handle, ChallengeRecord $record): void;
    public function pull(string $handle): ?ChallengeRecord;   // reads AND deletes
}

interface CredentialStore
{
    public function findById(string $id): ?StoredCredential;
    public function findByUserHandle(string $userHandle): array;
    public function save(StoredCredential $credential): void;              // throws on a duplicate ID
    public function updateAfterAuthentication(string $id, int $signCount, string $lastUsedAt): void;
    public function flagCloned(string $id, string $clonedAt): void;
    public function delete(string $id): void;
}
bash
php artisan vendor:publish --tag=passkeys-config
php artisan vendor:publish --tag=passkeys-migrations   # optional; they auto-load
php artisan migrate