Server side handling class for FIDO U2F registration and authentication
Server-side handling of FIDO U2F registration and authentication for PHP., (*2)
Securing your online accounts and doing your bit to protect your data is extremely important and increasingly more so as hackers get more sophisticated. FIDO's U2F enables you to add a simple unobtrusive method of 2nd factor authentication, allowing users of your service and/or application to link a hardware key to their account., (*3)
Base Library, (*4)
https://github.com/Samyoul/U2F-php-server, (*5)
Fido Test Suite (UTD), (*6)
https://github.com/Samyoul/U2F-php-UTD, (*7)
Frameworks, (*8)
Laravel https://github.com/Samyoul/U2F-Laravel-server, (*9)
Yii https://github.com/Samyoul/U2F-Yii-server, (*10)
CodeIgniter https://github.com/Samyoul/U2F-CodeIgniter-server, (*11)
composer require samyoul/u2f-php-server
, (*12)
A few things you need to know before working with this:, (*13)
This repository requires OpenSSL 1.0.0 or higher. For further details on installing OpenSSL please refer to the php manual http://php.net/manual/en/openssl.installation.php ., (*14)
Also see Compatibility Code, to check if you have the correct version of OpenSSL installed, and are unsure how else to check., (*15)
My presumption is that if you are looking to add U2F authentication to a php system, then you'll probably are also looking for some client-side handling. You've got a U2F enabled USB device and you want to get the USB device speaking with the browser and then with your server running php., (*16)
For U2F to work your website/service must use a HTTPS URL. Without a HTTPS URL your code won't work, so get one for your localhost, get one for your production. https://letsencrypt.org/ Basically encrypt everything., (*17)
HID : Human Interface Device, like A USB Device like these things, (*18)
You don't need to follow this structure exactly, but you will need to associate your Registration data with a user. You'll also need to store the key handle, public key and the certificate, counter isn't 100% essential but it makes your application more secure., (*19)
Name | Type | Description |
---|---|---|
id | integer primary key | |
user_id | integer | |
key_handle | varchar(255) | |
public_key | varchar(255) | |
certificate | text | |
counter | integer |
TODO the descriptions, (*20)
... TODO add the rest of the registration process flow ..., (*21)
$registrations
.$appId
U2F::makeAuthentication($registrations, $appId)
call, the method returns an array of SignRequest
objects: $authenticationRequest
.u2f.sign(authenticationRequest, function(data){ // Callback logic })
functionU2F::authenticate($authenticationRequest, $registrations, $authenticationResponse)
methodFor a full working code example for this repository please see the dedicated example repository, (*22)
You can also install it with the following:, (*23)
$ git clone https://github.com/Samyoul/U2F-php-server-examples.git $ cd u2f-php-server-examples $ composer install
You'll only ever need to use this method call once per installation and only in the context of debugging if the class is giving you unexpected errors. This method call will check your OpenSSL version and ensure it is at least 1.0.0 ., (*24)
U2F Key Registration U2F Registration
Please enter your FIDO U2F device into your computer's USB port. Then confirm registration on the device.
Validation and Key Storage, (*25)
This is the last stage of registration. Validate the registration response data against the original request data., (*26)
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', compact('userMessage')); ``` --- ### Authentication Code #### Authentication Step 1: **Starting the authentication process:** We assume that user has successfully authenticated and has previously registered to use FIDO U2F. ```php 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', compact("authenticationRequest")); ``` #### Authentication Step 2: **Client-side, Talking To The USB** Non-AJAX client-side authentication of U2F key token. AJAX can of course be used in your application, but it is easier to demonstrate a linear process without AJAX and callbacks. ```htmlU2F Key Authentication U2F Authentication
Please enter your FIDO U2F device into your computer's USB port. Then confirm authentication on the device.
Validation, (*27)
This is the last stage of authentication. Validate the authentication response data against the original request data., (*28)
<?php require('vendor/autoload.php'); use Samyoul\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', compact('userMessage'));
Again, if you want to just download some example code to play with just install the full working code examples written for this repository please see the dedicated example repository, (*29)
You can also install it with the following:, (*30)
$ git clone https://github.com/Samyoul/U2F-php-server-examples.git $ cd u2f-php-server-examples $ composer install
See the dedicated repository : https://github.com/Samyoul/U2F-Laravel-server, (*31)
Installation:, (*32)
composer require u2f-laravel-server
, (*33)
See the dedicated repository : https://github.com/Samyoul/U2F-Yii-server, (*34)
Installation:, (*35)
composer require u2f-yii-server
, (*36)
See the dedicated repository : https://github.com/Samyoul/U2F-CodeIgniter-server, (*37)
Installation:, (*38)
composer require u2f-codeigniter-server
, (*39)
Your favourite php framework not in this list? Get coding and submit a pull request and get your framework extension included here., (*40)
The repository is licensed under a BSD license. Read details here, (*41)
This repo was originally based on the Yubico php-u2flib-server https://github.com/Yubico/php-u2flib-server, (*42)