PHP code example of code-lts / u2f-php-server

1. Go to this page and download the library: Download code-lts/u2f-php-server 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/ */

    

code-lts / u2f-php-server example snippets




odeLts\U2F\U2FServer\U2FServer as U2F;

var_dump(U2F::checkOpenSSLVersion());



odeLts\U2F\U2FServer\U2FServer as U2F;

session_start();

// This can be anything, but usually easier if you choose your applications domain and top level domain.
$appId = 'yourdomain.tld';

// Call the makeRegistration method, passing in the app ID
$registrationData = U2F::makeRegistration($appId);

// Store the request for later
$_SESSION['registrationRequest'] = $registrationData['request'];

// Extract the request and signatures, JSON encode them so we can give the data to our javaScript magic
$jsRequest = json_encode($registrationData['request']);
$jsSignatures = json_encode($registrationData['signatures']);

// now pass the data to a fictitious view.
echo View::make('template/location/u2f-registration.html', [
    'jsRequest' => $jsRequest,
    'jsSignatures' => $jsSignatures,
]);



deLts\U2F\U2FServer\U2FServer as U2F;

session_start();

// Fictitious function representing getting the authenticated user object
$user = getAuthenticatedUser();

try {

    // Validate the registration response against the registration request.
    // The output are the credentials you need to store for U2F authentication.
    $validatedRegistration = U2F::register(
        $_SESSION['registrationRequest'],
        json_decode($_POST['u2f_registration_response'])
    );

    // Fictitious function representing the storing of the validated U2F registration data against the authenticated user.
    $user->storeRegistration($validatedRegistration);

    // Then let your user know what happened
    $userMessage = 'Success';
} catch( Exception $e ) {
    $userMessage = 'We had an error: '. $e->getMessage();
}

//Fictitious view.
echo View::make('template/location/u2f-registration-result.html', [
    'userMessage' => $userMessage,
]);



deLts\U2F\U2FServer\U2FServer as U2F;

session_start();

// Fictitious function representing getting the authenticated user object
$user = getAuthenticatedUser();

// Fictitious function, get U2F registrations associated with the user
$registrations = $user->U2FRegistrations();

// This can be anything, but usually easier if you choose your applications domain and top level domain.
$appId = 'yourdomain.tld';

// Call the U2F makeAuthentication method, passing in the user's registration(s) and the app ID
$authenticationRequest = U2F::makeAuthentication($registrations, $appId);

// Store the request for later
$_SESSION['authenticationRequest'] = $authenticationRequest;

// now pass the data to a fictitious view.
echo View::make('template/location/u2f-authentication.html', [
    'authenticationRequest' => $authenticationRequest,
]);



deLts\U2F\U2FServer\U2FServer as U2F;

session_start();

// Fictitious function representing getting the authenticated user object
$user = authenticatedUser();

// Fictitious function, get U2F registrations associated with the user
$registrations = $user->U2FRegistrations();

try {

    // Validate the authentication response against the registration request.
    // The output are the credentials you need to store for U2F authentication.
    $validatedAuthentication = U2F::authenticate(
        $_SESSION['authenticationRequest'],
        $registrations,
        json_decode($_POST['u2f_authentication_response'])
    );

    // Fictitious function representing the updating of the U2F token count integer.
    $user->updateU2FRegistrationCount($validatedAuthentication);

    // Then let your user know what happened
    $userMessage = 'Success';
} catch( Exception $e ) {
    $userMessage = 'We had an error: '. $e->getMessage();
}

//Fictitious view.
echo View::make('template/location/u2f-authentication-result.html', [
    'userMessage' => $userMessage,
]);
sh
$ git clone https://github.com/code-lts/U2F-php-server-examples.git
$ cd u2f-php-server-examples
$ composer install
sh
$ git clone https://github.com/code-lts/U2F-php-server-examples.git
$ cd u2f-php-server-examples
$ composer install