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.
- Host: GitHub
- URL: https://github.com/kelunik/two-factor
- Owner: kelunik
- License: mit
- Created: 2016-04-12T15:42:24.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2021-06-21T18:49:33.000Z (almost 4 years ago)
- Last Synced: 2025-03-12T01:05:25.299Z (3 months ago)
- Topics: 2fa, google-authenticator, oath-hotp, oath-totp, two-factor
- Language: PHP
- Size: 21.5 KB
- Stars: 37
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# two-factor
[](https://travis-ci.org/kelunik/two-factor)
[](https://coveralls.io/github/kelunik/two-factor?branch=master)
`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.
```