项目作者: zekang

项目描述 :
Use the Google Authenticator App and check the code with php extension
高级语言: C
项目地址: git://github.com/zekang/php_google_authenticator.git
创建时间: 2017-08-18T08:46:06Z
项目社区:https://github.com/zekang/php_google_authenticator

开源协议:

下载


php_google_authenticator

php_google_authenticator是google身份验证器服务端php实现,可用于网站后台动态令牌验证。

从源码安装

  1. 解压缩源码包

    1. tar -xf php_google_authenticator.tar.gz
    2. cd php_google_authenticator
  2. 编译

    PHP扩展

    1. {php_bin_dir}/phpize
    2. ./configure --with-php-config={php_bin_dir}/php-config
    3. make
  3. 安装配置

    安装PHP扩展至PHP目录

    1. make install

    编辑配置文件php.ini,增加下面配置信息。

    1. extension=php_google_authenticator.so

使用

1.函数原型

  1. /**
  2. * Class GoogleAuthenticator
  3. */
  4. class GoogleAuthenticator
  5. {
  6. /**
  7. * 校验验证码
  8. * @param string $secretKey 用户密钥
  9. * @param int $code 用户输入的验证码
  10. * @return bool
  11. */
  12. public function validate($secretKey,$code)
  13. {
  14. return false;
  15. }
  16. /**
  17. * @param $encodeStr
  18. * @return string
  19. */
  20. public function base32_encode($encodeStr)
  21. {
  22. return '';
  23. }
  24. /**
  25. * @param $decodeStr
  26. * @return string
  27. */
  28. public function base32_decode($decodeStr)
  29. {
  30. return '';
  31. }
  32. /**
  33. * 生成密钥
  34. * $token为null时,随机生成密钥,传入固定的字符串时 可以生成固定的密钥,如:根据用户名生成
  35. * @param null $token
  36. * @return string
  37. */
  38. public function generateSecretKey($token=null)
  39. {
  40. return '';
  41. }
  42. /**
  43. * 根据输入的密钥生成验证码,同google身份验证器客户端功能
  44. * @param $secretKey
  45. * @return int
  46. */
  47. public function generateCode($secretKey)
  48. {
  49. return 0;
  50. }
  51. /**
  52. * 生成一个google身份验证器,识别的字符串,只需要把该方法返回值生成二维码扫描就可以了
  53. * @param string $user 用户名
  54. * @param string $secretKey 用户密钥
  55. * @return string
  56. */
  57. public function getQRBarcode($user,$secretKey)
  58. {
  59. return '';
  60. }
  61. /**
  62. * 生成二维码链接,访问此链接用google身份验证器扫描即可
  63. * @param string $host 用于验证的网站域名
  64. * @param string $user 用户名
  65. * @param string $secretKey 用户密钥
  66. * @return string
  67. */
  68. public function getQRBarcodeURL($host,$user,$secretKey)
  69. {
  70. return '';
  71. }
  72. }

2.生成用户密钥

  1. <?php
  2. $googleAuth = new GoogleAuthenticator();
  3. //随机生成
  4. echo $googleAuth->generateSecretKey();
  5. echo PHP_EOL;
  6. //根据固定串生成
  7. echo $googleAuth->generateSecretKey('465708481@qq.com');
  8. echo PHP_EOL;

Output:

  1. [root@localhost ~]# phpdev test.php
  2. OQ3BASVCUUTM2VNH
  3. 6AEUMY5VUJSV6ZI3
  4. [root@localhost ~]# phpdev test.php
  5. UVD6Y5R4OLE72SUD
  6. 6AEUMY5VUJSV6ZI3

3.验证输入

  1. <?php
  2. $key = "6AEUMY5VUJSV6ZI3";
  3. $code = 781844 ;
  4. $googleAuth = new GoogleAuthenticator();
  5. $flag = $googleAuth->validate($key,$code);
  6. var_dump($flag);

Output:

  1. [root@localhost ~]# phpdev test.php
  2. bool(false)