PHPackages                             samyoul/u2f-php-server - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. samyoul/u2f-php-server

ActiveLibrary[Security](/categories/security)

samyoul/u2f-php-server
======================

Server side handling class for FIDO U2F registration and authentication

v1.1.4(7y ago)36414.4k—9.9%7[5 issues](https://github.com/Samyoul/U2F-php-server/issues)1BSD-2-ClausePHP

Since Dec 12Pushed 4y ago3 watchersCompare

[ Source](https://github.com/Samyoul/U2F-php-server)[ Packagist](https://packagist.org/packages/samyoul/u2f-php-server)[ RSS](/packages/samyoul-u2f-php-server/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)DependenciesVersions (7)Used By (1)

U2F-php-server
==============

[](#u2f-php-server)

[![Latest Stable Version](https://camo.githubusercontent.com/3c2cc34e485b307dc2ed1f459afa9684e26a1df930b2a2bc98650595a684a3fb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73616d796f756c2f7532662d7068702d7365727665722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/samyoul/u2f-php-server)[![License](https://camo.githubusercontent.com/b73b5682e20e0cae8eedeafe2fbb9c0c33d89bcb67eb5277388bc468960700fe/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4253445f325f436c617573652d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENCE.md)

Server-side handling of FIDO U2F registration and authentication for PHP.

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.

The Family of U2F php Libraries
-------------------------------

[](#the-family-of-u2f-php-libraries)

**Base Library**

**Fido Test Suite (UTD)**

**Frameworks**

*Laravel*

*Yii*

*CodeIgniter*

Contents
--------

[](#contents)

1. [Installation](#installation)
2. [Requirements](#requirements)
    1. [OpenSSL](#openssl)
    2. [Clientside Magic](#client-side-the-magic-javascript-bit-of-talking-with-a-usb-device)
    3. [HTTPS and SSL](#https-and-ssl)
3. [Terminology](#terminology)
4. [Recommended Datastore Structure](#recommended-datastore-structure)
5. [Process Workflow](#process-workflow)
    1. [Registration Process Flow](#registration-process-flow)
    2. [Authentication Process Flow](#authentication-process-flow)
6. [Example Code](#example-code)
    1. [Compatibility Check](#compatibility-code)
    2. [Registration Code](#registration-code)
    3. [Authentication Code](#authentication-code)
7. [Frameworks](#frameworks)
    1. [Laravel](#laravel-framework)
    2. [Yii](#yii-framework)
    3. [CodeIgniter](#codeigniter-framework)
8. [Licence](#licence)
9. [Credits](#credits)

Installation
------------

[](#installation)

`composer require samyoul/u2f-php-server`

Requirements
------------

[](#requirements)

A few **things you need** to know before working with this:

1. [***OpenSSL***](#openssl) You need at least OpenSSL 1.0.0 or higher.
2. [***Client-side Handling***](#client-side) You need to be able to communicate with a some kind of device.
3. [***A HTTPS URL***](#https-and-ssl) This is very important, without HTTPS U2F simply will not work.
4. [***A Datastore***](#recommended-datastore-structure) 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

[](#openssl)

This repository requires OpenSSL 1.0.0 or higher. For further details on installing OpenSSL please refer to the php manual  .

Also see [Compatibility Code](#compatibility-code), to check if you have the correct version of OpenSSL installed, and are unsure how else to check.

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

[](#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.

1. Google already have this bit sorted :
2. [Mastahyeti](https://github.com/mastahyeti) has created a repo dedicated to Google's JavaScript Client-side API :

### HTTPS and SSL

[](#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. Basically encrypt everything.

Terminology
-----------

[](#terminology)

***HID*** : *Human Interface Device*, like A USB Device [like these things](https://www.google.co.uk/search?q=fido+usb+key&safe=off&tbm=isch)

Recommended Datastore Structure
-------------------------------

[](#recommended-datastore-structure)

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.

NameTypeDescriptionidinteger primary keyuser\_idintegerkey\_handlevarchar(255)public\_keyvarchar(255)certificatetextcounterintegerTODO the descriptions

Process Workflow
----------------

[](#process-workflow)

### Registration Process Flow

[](#registration-process-flow)

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

... TODO add the rest of the registration process flow ...

### Authentication Process Flow

[](#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
------------

[](#example-code)

For a full working code example for this repository please see [the dedicated example repository](https://github.com/Samyoul/U2F-php-server-examples)

You can also install it with the following:

```
$ git clone https://github.com/Samyoul/U2F-php-server-examples.git
$ cd u2f-php-server-examples
$ composer install
```

1. [Compatibility Code](#compatibility-code)
2. [Registration Code](#registration-code)
    1. [Step 1: Starting](#registration-step-1)
    2. [Step 2: Talking to the HID](#registration-step-2)
    3. [Step 3: Validation &amp; Storage](#registration-step-3)
3. Authentication Code
    1. Step 1: Starting
    2. Step 2: Talking to the HID
    3. Step 3: Validation

---

### Compatibility Code

[](#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 .

```
