PHP code example of trilbdev / two-factor-library

1. Go to this page and download the library: Download trilbdev/two-factor-library 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/ */

    

trilbdev / two-factor-library example snippets




rilbdev\TwoFactorLibrary\TwoFactorAuthentication;

$auth = new TwoFactorAuthentication('Example', '[email protected]');

$totp = $auth->totp();
$qrCode = $auth->qrCode();
$passkey = $auth->passkey();
$recovery = $auth->recoveryCodes();

use Trilbdev\TwoFactorLibrary\TwoFactor;

$auth = new TwoFactor('Example', '[email protected]');



rilbdev\TwoFactorLibrary\TwoFactorAuthentication;

$auth = new TwoFactorAuthentication('Example', '[email protected]');
$secret = $auth->totp()->generateSecret();
$code = $auth->totp()->createCode($secret);

if ($auth->totp()->validateCode($secret, $code)) {
    echo "Code is valid.";
}



rilbdev\TwoFactorLibrary\TwoFactorAuthentication;

$auth = new TwoFactorAuthentication('Example', '[email protected]');
$secret = 'JBSWY3DPEHPK3PXP';
$uri = $auth->totp()->createProvisioningUri($secret, '[email protected]', 'Example');

$png = $auth->qrCode()->generateFromUri($uri, 220);
file_put_contents('totp-qr.png', $png);



rilbdev\TwoFactorLibrary\TwoFactorAuthentication;

$auth = new TwoFactorAuthentication('Example', '[email protected]');
$codes = $auth->recoveryCodes()->createBatch(5, 12, 'alphanumeric');

if ($auth->recoveryCodes()->validate($codes[0], $codes)) {
    echo "Recovery code is valid.";
}



rilbdev\TwoFactorLibrary\TwoFactorAuthentication;

$auth = new TwoFactorAuthentication('Example', '[email protected]');

$options = $auth->passkey()->createRegistrationOptions(
    'user-123',
    '[email protected]',
    'Example User',
    ['rpId' => 'example.com', 'origin' => 'https://example.com']
);

$challenge = $options['challenge'];

$credential = $auth->passkey()->createCredential(
    'user-123',
    '[email protected]',
    'challenge-123',
    ['rpId' => 'example.com']
);

$valid = $auth->passkey()->validateCredential(
    $credential['id'],
    '[email protected]',
    'challenge-123',
    $credential['signature'],
    'example.com'
);