An open API service indexing awesome lists of open source software.

https://github.com/kelunik/two-factor

Two factor authentication.
https://github.com/kelunik/two-factor

2fa google-authenticator oath-hotp oath-totp two-factor

Last synced: 3 months ago
JSON representation

Two factor authentication.

Awesome Lists containing this project

README

        

# two-factor

[![Build Status](https://img.shields.io/travis/kelunik/two-factor/master.svg?style=flat-square)](https://travis-ci.org/kelunik/two-factor)
[![CoverageStatus](https://img.shields.io/coveralls/kelunik/two-factor/master.svg?style=flat-square)](https://coveralls.io/github/kelunik/two-factor?branch=master)
![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)

`kelunik/two-factor` is a Google Authenticator compatible OATH implementation.

## Requirements

- PHP 5.5+

## Installation

```bash
composer require kelunik/two-factor
```

## Demo

There's a [runnable demo](./examples/demo.php) contained in this repository.

## Usage

### Generate a secret per user

```php
$oath = new Oath;

// this generates a key in binary format
$key = $oath->generateKey();

// store key for user
```

### Let user setup two factor device

```php
$oath = new Oath;
$key = "..."; // load user key from storage

// Use the URI to provide an easy to scan QR code
$uri = $oath->getUri($key);

// Alternatively display the key for manual input
$secret = $oath->encodeKey($key);
```

You can use your favourite JavaScript or PHP library to generate the QR code. For a working example, we're using [`qr.js`](http://neocotic.com/qr.js/).

```html

Scan the following QR code and click continue once you're ready.




qr.canvas({
canvas: document.getElementById("qr-code"),
value: document.getElementById("2fa-uri").value
});

Continue

```

### Validate TOTP value

```php
$oath = new Oath;
$key = "..."; // load user key from storage
$isValid = $oath->verifyTotp($key, $totpValue);
// If the token is valid, ensure that it can't be used again.
// Because we use the default grace window size of two,
// we have to store the used TOTP value for at least 90 seconds,
// to prevent its usage explicitly.
```