nod软件/yii2-password

此包已被弃用,不再维护。没有建议替代包。

Yii框架的密码策略。

安装数: 2,248

依赖者: 0

建议者: 0

安全性: 0

星标: 1

关注者: 12

分支: 29

类型:yii2-extension

1.0.3 2014-06-04 11:26 UTC

This package is not auto-updated.

Last update: 2022-04-02 03:12:58 UTC


README

密码策略是指定密码如何编码和验证,以及用户提供的密码应该有多复杂的规范。默认情况下,它包含bcrypt策略以及多个轮次的哈希函数,例如sha1,还支持旧的密码哈希,如未加盐的md5和未加盐的sha1。目标是允许多种不同的密码策略共存,并在用户登录时从旧哈希升级到新哈希。

为什么我需要这个?

想象一下,你有一个使用简单、未加盐的md5密码哈希的旧应用程序,这在2012年被认为完全不安全。你想要升级你的密码哈希,但你无法访问明文密码。在这种情况下,你可以配置两个密码策略,一个是你的旧的遗留策略,它使用md5,另一个是你的新的、闪亮的策略,它使用bcrypt。然后当用户登录到他们的账户时,他们的密码将使用旧策略进行验证,如果匹配,他们将被无缝升级到新的bcrypt密码策略。例如

class User extends \yii\db\ActiveRecord
{
  public function behaviors()
  {
    return [
      [
					"class" => "YiiPassword\Behavior",
				"defaultStrategyName" => "bcrypt",
				"strategies" => array(
					"bcrypt" => array(
						"class" => "YiiPassword\Strategies\Bcrypt",
						"workFactor" => 14
					),
					"legacy" => array(
						"class" => "YiiPassword\Strategies\LegacyMd5",
					)
				),
			)
		);
	}

	....
}

$user = User::model()->findByPK(1); // a user using the legacy password strategy
echo $user->password; // unsalted md5, horrible
$user->verifyPassword("password"); // verifies the password using the legacy strategy, and rehashes based on bcrypt strategy
echo $user->password; // now hashed with bcrpt

但这对于现代应用程序也有用,比如说,你有一个新的web应用,你正在做正确的事,使用bcrypt进行密码哈希。你最初使用工作因子12,但几个月后你决定你想将其增加到15。通常这会因为所有已经使用不安全哈希的注册用户而变得相当困难,但有了密码策略,你可以简单地添加另一个具有期望工作因子的bcrypt策略,将其设置为默认值,然后你的用户将在他们下次登录时升级到新策略。

默认情况下,YiiPassword\Behavior假定你的模型包含以下字段

* *salt* - holds the per user salt used for hashing passwords
* *username* - holds the username
* *password* - holds the hashed password
* *passwordStrategy* - holds the name of the current password strategy for this user
* *requiresNewPassword* - a boolean field that determines whether the user should change their password or not

你可以在行为上配置字段名称。

另外信息:在新的应用程序中使用Bcrypt策略? - phpnode#10