项目作者: enygma

项目描述 :
Google Authenticator Code Validation and Generation
高级语言: PHP
项目地址: git://github.com/enygma/gauth.git
创建时间: 2013-01-07T20:58:20Z
项目社区:https://github.com/enygma/gauth

开源协议:MIT License

下载


GAuth : Google Authenticator Code Generator/Validation

Total Downloads

The GAuth library is designed to generate and validate codes compatible with the
Google Authenticator tools.

Installation via Composer:

Include in your composer.json file:

  1. {
  2. "require": {
  3. "enygma/gauth": "dev-master"
  4. }
  5. }

Getting Started

To get started using the Google Authenticator with your application, you’ll need to make an
initialization key (using generateCode) and save that to your app’s settings. This is the
code you’ll share with your users when they’re trying to set up their client for your system.

Then, when they log in you have them enter in the latest code listed for your application for
thier account.

NOTE: This tool offers a “window of opportunity” for the codes of 2 seconds forward and
backward of the current timestamp, just in case things are a bit off. You can change this with
the setRange method:

  1. <?php
  2. $g = new \GAuth\Auth();
  3. // set it to 3 seconds
  4. $g->setRange(3);
  5. ?>

To generate a new code:

  1. <?php
  2. require_once 'vendor/autoload.php';
  3. // Useful for creating a new Initialization key if needed
  4. $g = new \GAuth\Auth();
  5. $code = $g->generateCode();
  6. var_dump($code);
  7. ?>

To validate a code

  1. <?php
  2. $code = 'code-inputted-from-user';
  3. $g = new \GAuth\Auth('your-initialization-code');
  4. $verify = $g->validateCode($code);
  5. if ($verify == true) {
  6. echo 'User code verified!';
  7. } else {
  8. echo 'User code invalid!';
  9. }
  10. ?>

To get the QR code for the application

You can also use the tool to get the URL for a QR code users can scan to add your application to their Authenticator client. The call to generateQrImage returns the actual image data for you to use as you wish, either to embed in an img tag or save to a file:

  1. <?php
  2. $holder = 'foo@bar.com';
  3. $name = 'my-app-name';
  4. $g = new \GAuth\Auth('your-initialization-code');
  5. $qrCodeImageData = $g->generateQrImage($holder, $name, 200);
  6. // To use in an image tag:
  7. echo '<img src="data:image/png;base64,'.base64_encode($qrCodeImageData).'"/><br/><hr/>';
  8. // Or just save to a file
  9. file_put_contents('/path/to/qr-file.png', $qrCodeImageData);
  10. ?>

The library uses internal QR code generation, not the Google Charts API many similar libraries use.

More info: