【发布时间】:2016-05-28 08:39:44
【问题描述】:
我正在尝试将我自己的旧密码服务插入 Symfony3 以被动地从旧数据库表迁移用户。
旧系统的密码散列具有相同的硬编码 $salt 变量在所有成员中使用(因此我的 FOSUserBundle 表当前对于要迁移的所有成员的 salt 列为空)。
遗留方法使用:
sha1($salt1.$password.$salt2)
新方法是 Symfony 的 FOSUserBundle 标准 bcrypt 哈希。
我正在尝试实现它,以便当旧用户首次登录时,Symfony 将尝试:
- 使用 FOSUserBundle 的标准 bcrypt 方法登录。
- 如果 #1 未成功,请尝试旧算法。
- 如果#2 成功,数据库表中的密码哈希和盐值将被更新以符合标准 FOSUserBundle 方法
我一直在阅读有关如何插入服务以使其正常工作的信息,我认为我所拥有的以下内容在理论上似乎是正确的 - 如果没有任何更正/指导,我将不胜感激,因为我无法做到测试一下!
但是,我不确定应该如何将它们全部连接到 Symfony 中,以便在第 1 步失败时正常的 FOSUserBundle 进程将执行第 2 步和第 3 步
services.yml:
parameters:
custom-password-encoder:
class: AppBundle\Security\LegacyPasswordEncoder
security.yml:
security:
encoders:
#FOS\UserBundle\Model\UserInterface: bcrypt Commented out to try the following alternative to give password migrating log in
FOS\UserBundle\Model\UserInterface: { id: custom-password-encoder }
BCryptPasswordEncoder(标准 FOSUserBundle):
class BCryptPasswordEncoder extends BasePasswordEncoder
{
/* .... */
/**
* {@inheritdoc}
*/
public function encodePassword($raw, $salt)
{
if ($this->isPasswordTooLong($raw)) {
throw new BadCredentialsException('Invalid password.');
}
$options = array('cost' => $this->cost);
if ($salt) {
// Ignore $salt, the auto-generated one is always the best
}
return password_hash($raw, PASSWORD_BCRYPT, $options);
}
/**
* {@inheritdoc}
*/
public function isPasswordValid($encoded, $raw, $salt)
{
return !$this->isPasswordTooLong($raw) && password_verify($raw, $encoded);
}
}
旧版密码编码器:
namespace AppBundle\Security;
use Symfony\Component\Security\Core\Encoder\BasePasswordEncoder;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
class LegacyPasswordEncoder extends BasePasswordEncoder
{
/**
* {@inheritdoc}
*/
public function encodePassword($raw,$salt)
{
if ($this->isPasswordTooLong($raw)) {
throw new BadCredentialsException('Invalid password.');
}
list($salt1,$salt2) = explode(",",$salt);
return sha1($salt1.$raw.$salt2);
}
/**
* {@inheritdoc}
*/
public function isPasswordValid($encoded, $raw, $salt)
{
list($salt1,$salt2) = explode(",",$salt);
return !$this->isPasswordTooLong($raw) && $this->comparePasswords($encoded,sha1($salt1.$raw.$salt2));
}
}
【问题讨论】:
标签: passwords fosuserbundle symfony