ceeram/google-authenticate

CakePHP 插件,为 AuthComponent 提供谷歌两步验证类。

安装: 11

依赖: 0

建议: 0

安全: 0

星标: 28

关注者: 2

分支: 6

开放问题: 1

类型:cakephp-plugin

dev-master 2012-09-13 15:48 UTC

This package is auto-updated.

Last update: 2024-09-13 09:59:11 UTC


README

包含用于 AuthComponent 的谷歌两步验证类的插件。

包含来自 https://github.com/chregu/GoogleAuthenticator.php 的库,并进行了一些小的调整

要求

  • PHP 5.3
  • CakePHP 2.x

安装

[手动]

  1. 下载此文件: http://github.com/ceeram/GoogleAuthenticate/zipball/master
  2. 解压下载文件。
  3. 将生成的文件夹复制到 app/Plugin
  4. 将您刚刚复制的文件夹重命名为 GoogleAuthenticate

[GIT 子模块]

在您的应用目录中键入

git submodule add git://github.com/ceeram/GoogleAuthenticate.git Plugin/GoogleAuthenticate
git submodule init
git submodule update

[GIT 克隆]

在您的插件目录中键入

git clone git://github.com/ceeram/GoogleAuthenticate.git GoogleAuthenticate

使用方法

在 app/Config/bootstrap.php 中添加: CakePlugin::load(‘GoogleAuthenticate’);

配置

设置认证类设置。
您不需要表单认证了,因为它只会在用户表的 secret 字段不为空时检查代码。
如果 secret 字段为空,它将像正常的表单认证一样运行。

如果您想将谷歌两步验证与表单结合使用,请确保您为表单认证设置了一个作用域

'scope' => array('User.secret' => null)

GoogleAuthenticate 示例


    //in $components
    public $components = array(
        'Auth' => array(
            'authenticate' => array(
                'GoogleAuthenticate.Google' => array(
                    'fields' => array(
			'username' => 'username',
			'password' => 'password',
			'code' => 'code',//fieldname in form
			'secret' => 'secret'//fieldname in table
		),
		'userModel' => 'User',
		'scope' => array()
                )
            )
        )
    );
    //Or in beforeFilter()
    $this->Auth->authenticate = array(
        'GoogleAuthenticate.Google' => array(
            'fields' => array(
			'username' => 'username',
			'password' => 'password',
			'code' => 'code',
			'secret' => 'secret'
		),
		'userModel' => 'User',
		'scope' => array()
        )
    );

使用方法

上述配置是使用适配器登录所需的所有配置。
您需要生成一个密钥代码并将其存储在用户表中。

您可以向用户显示密钥代码,或显示一个二维码。

以下是在 UsersController 中的示例操作,它处理所有这些


/**
 * Displays secret code and QRcode for the user account.
 * Will create a secret, if not set in users table
 *
 * Also handles removal of secret from database with Form->postLink(), to allow normal login again.
 *
 * To generate a different secret, browse to http://domain.com/users/secret/renew
 *
 * @return void
 */
	public function secret($renew = null) {
		$this->User->id = $this->Auth->user('id');
		if ($this->request->is('post')) {
			$this->User->saveField('secret', null);
			$this->redirect(array('action' => 'view', $this->User->id));
		}

		App::uses('GoogleAuthenticator', 'GoogleAuthenticate.Lib');
		$Google = new GoogleAuthenticator();

		$secret = $this->User->field('secret');
		if (!$secret || $renew == 'renew') {
			$secret = $Google->generateSecret();
			$this->User->saveField('secret', $secret);
		}

		$url = $Google->getUrl($secret, $this->Auth->user('username'), 'example.com');

		$this->set(compact('secret', 'url'));
	}

此操作的 secret.ctp 视图模板将是


<div class="users secret">
	<h2><?php echo __('Google secret'); ?></h2>
	<div>Enter this code manually: <?php echo $secret;?></div>
	<div>Or scan the QRcode: <?php echo $this->Html->image($url);?></div>
</div>
<div class="actions">
	<h3><?php echo __('Actions'); ?></h3>
	<ul>
		<li><?php echo $this->Html->link('Generate different secret', array('action' => 'secret', 'renew'));?></li>
		<li><?php echo $this->Form->postLink('Remove secret');?></li>
	</ul>
</div>

许可证(除非文件中另有说明)

版权 © 2012 Ceeram

特此授予任何获得此软件及其相关文档文件(“软件”)副本的人免费使用的权利
在软件中不受限制地处理,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件的副本,并允许软件的接受者为此目的进行处理,前提是符合以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于对适销性、适用性和非侵权的保证。
在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是否因合同、侵权或其他方式产生,无论是否与软件或软件的使用或其他交易有关。
软件。

软件。
软件。

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KINDEXPRESS OR
IMPLIEDINCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT。在 NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIMDAMAGES其他
LIABILITYWHETHERAN ACTIONOF CONTRACTTORT其他ARISING FROM
OUT OFIN CONNECTION WITH THE SOFTWARETHE USE其他 交易
软件

其他许可证

Lib/GoogleAuthenticator.php
Apache License, Version 2.0