2017 © Pedro Peláez
 

library u2f-php-server

Server side handling class for FIDO U2F registration and authentication

image

samyoul/u2f-php-server

Server side handling class for FIDO U2F registration and authentication

  • Saturday, February 11, 2017
  • by Samyoul
  • Repository
  • 3 Watchers
  • 11 Stars
  • 36,885 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 1 Forks
  • 1 Open issues
  • 6 Versions
  • 63 % Grown

The README.md

U2F-php-server

Latest Stable Version License, (*1)

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)

The Family of U2F php Libraries

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)

Contents

  1. Installation
  2. Requirements
    1. OpenSSL
    2. Clientside Magic
    3. HTTPS and SSL
  3. Terminology
  4. Recommended Datastore Structure
  5. Process Workflow
    1. Registration Process Flow
    2. Authentication Process Flow
  6. Example Code
    1. Compatibility Check
    2. Registration Code
    3. Authentication Code
  7. Frameworks
    1. Laravel
    2. Yii
    3. CodeIgniter
  8. Licence
  9. Credits

Installation

composer require samyoul/u2f-php-server, (*12)

Requirements

A few things you need to know before working with this:, (*13)

  1. OpenSSL You need at least OpenSSL 1.0.0 or higher.
  2. Client-side Handling You need to be able to communicate with a some kind of device.
  3. A HTTPS URL This is very important, without HTTPS U2F simply will not work.
  4. A Datastore You need some kind of datastore for all your U2F registered users (although if you have a system with user authentication I'm presuming you've got this one sorted).

OpenSSL

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)

Client-side (The magic JavaScript Bit of talking with a USB device)

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)

  1. Google already have this bit sorted : https://github.com/google/u2f-ref-code/blob/master/u2f-gae-demo/war/js/u2f-api.js
  2. Mastahyeti has created a repo dedicated to Google's JavaScript Client-side API : https://github.com/mastahyeti/u2f-api

HTTPS and SSL

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)

Terminology

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)

Process Workflow

Registration Process Flow

  1. User navigates to a 2nd factor authentication page in your application.

... TODO add the rest of the registration process flow ..., (*21)

Authentication Process Flow

  1. User navigates to their login page as they usually would, submits username and password.
  2. Server received POST request authentication data, normal username + password validation occurs
  3. On successful authentication, the application checks 2nd factor authentication is required. We're going to presume it is, otherwise the user would just be logged in at this stage.
  4. Application gets the user's registered signatures from the application datastore: $registrations.
  5. Application gets its ID, usually the domain the application is accessible from: $appId
  6. Application makes a U2F::makeAuthentication($registrations, $appId) call, the method returns an array of SignRequest objects: $authenticationRequest.
  7. Application JSON encodes the array and passes the data to the view
  8. When the browser loads the page the JavaScript fires the u2f.sign(authenticationRequest, function(data){ // Callback logic }) function
  9. The view will use JavaScript / Browser to poll the host machine's ports for a FIDO U2F device
  10. Once the HID has been found the JavaScript / Browser will send the sign request with data.
  11. The HID will prompt the user to authorise the sign request
  12. On success the HID returns authentication data
  13. The JavaScript receives the HID's returned data and passes it to the server
  14. The application takes the returned data passes it to the U2F::authenticate($authenticationRequest, $registrations, $authenticationResponse) method
  15. If the method returns a registration and doesn't throw an Exception, authentication is complete.
  16. Set the user's session, inform the user of the success, and redirect them.

Example Code

For 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 
  1. Compatibility Code
  2. Registration Code
    1. Step 1: Starting
    2. Step 2: Talking to the HID
    3. Step 3: Validation & Storage
  3. Authentication Code
    1. Step 1: Starting
    2. Step 2: Talking to the HID
    3. Step 3: Validation

Compatibility Code

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.

Registration Step 3:

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. 


```html


    U2F Key Authentication


    

U2F Authentication

Please enter your FIDO U2F device into your computer's USB port. Then confirm authentication on the device.

Authentication Step 3:

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 

Frameworks

Laravel Framework

See the dedicated repository : https://github.com/Samyoul/U2F-Laravel-server, (*31)

Installation:, (*32)

composer require u2f-laravel-server, (*33)

Yii Framework

See the dedicated repository : https://github.com/Samyoul/U2F-Yii-server, (*34)

Installation:, (*35)

composer require u2f-yii-server, (*36)

CodeIgniter Framework

See the dedicated repository : https://github.com/Samyoul/U2F-CodeIgniter-server, (*37)

Installation:, (*38)

composer require u2f-codeigniter-server, (*39)

Can't see yours?

Your favourite php framework not in this list? Get coding and submit a pull request and get your framework extension included here., (*40)

Licence

The repository is licensed under a BSD license. Read details here, (*41)

Credits

This repo was originally based on the Yubico php-u2flib-server https://github.com/Yubico/php-u2flib-server, (*42)

The Versions

11/02 2017

dev-master

9999999-dev

Server side handling class for FIDO U2F registration and authentication

  Sources   Download

BSD-2-Clause

The Requires

  • ext-openssl *

 

by Samuel Hawksby-Robinson

14/12 2016

v1.1.3

1.1.3.0

Server side handling class for FIDO U2F registration and authentication

  Sources   Download

BSD-2-Clause

The Requires

  • ext-openssl *

 

by Samuel Hawksby-Robinson

13/12 2016

v1.1.2

1.1.2.0

Server side handling class for FIDO U2F registration and authentication

  Sources   Download

BSD-2-Clause

The Requires

  • ext-openssl *

 

by Samuel Hawksby-Robinson

13/12 2016

v1.1.1

1.1.1.0

Server side handling class for FIDO U2F registration and authentication

  Sources   Download

BSD-2-Clause

The Requires

  • ext-openssl *

 

by Samuel Hawksby-Robinson

13/12 2016

v1.1.0

1.1.0.0

Server side handling class for FIDO U2F registration and authentication

  Sources   Download

BSD-2-Clause

The Requires

  • ext-openssl *

 

by Samuel Hawksby-Robinson

12/12 2016

v1.0.0

1.0.0.0

Server side handling class for FIDO U2F registration and authentication

  Sources   Download

BSD-2-Clause

The Requires

  • ext-openssl *

 

by Samuel Hawksby-Robinson