Symfony2自定义密码编码器和密码哈希更新

时间:2013-04-07 17:43:04

标签: wordpress symfony passwords bcrypt encoder

我正在通过将一些wordpress博客转移到Symfony来学习Symfony2。我坚持使用登录程序。 Wordpress使用非标准密码哈希,如$P$....,我想在用户登录时检查旧密码哈希,当密码正确时,请将其重新分配给bcrypt。到目前为止,我创建了使用symfony安全机制的custome编码器类。

<?php
namespace Pkr\BlogUserBundle\Service\Encoder;

use PHPassLib\Application\Context;
use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Util\SecureRandom;

class WpTransitionalEncoder implements PasswordEncoderInterface
{

    public function __construct($cost = 13)
    {
        $secure = new SecureRandom();
        $this->_bcryptEncoder = new BCryptPasswordEncoder($secure, $cost);
    }

    public function isPasswordValid($encoded, $raw, $salt)
    {
        if (preg_match('^\$P\$', $encoded)) {
            $context = new Context();
            $context->addConfig('portable');
            return $context->verify($raw, $encoded);
        }
        return $this->_bcryptEncoder->isPasswordValid($encoded, $raw, $salt);
    }

    public function encodePassword($raw, $salt)
    {
        return $this->_bcryptEncoder->encodePassword($raw, $salt);
    }
}

我正在使用它作为服务:

#/src/Pkr/BlogUserBundle/Resources/config/services.yml
services:
    pkr_blog_user.wp_transitional_encoder:
        class: Pkr\BlogUserBundle\Service\Encoder\WpTransitionalEncoder

在security.yml:

#/app/config/security.yml
security:
encoders:
    Pkr\BlogUserBoundle\Entity\User:
        id:   pkr_blog_user.wp_transitional_encoder
        cost: 15

我的问题是:

如何在security.yml内将参数传递到我的编码器服务表单?

我问,因为cost: 15不起作用。

我应该在哪里放置密码哈希更新逻辑?在密码验证之后,我认为maby是这样的:

public function isPasswordValid($encoded, $raw, $salt)
{
    if (preg_match('^\$P\$', $encoded)) {
        $context = new Context();
        $context->addConfig('portable');
        $isValid = $context->verify($raw, $encoded);
        if ($isValid) {
            // put logic here...
        }
        return $isValid;
    }
    return $this->_bcryptEncoder->isPasswordValid($encoded, $raw, $salt);
}

但它似乎有点像错误的地方。那么正确的方法是什么?

1 个答案:

答案 0 :(得分:2)

我会回答我自己的问题。

我在config.yml

中放置了编码器服务的参数
pkr_blog_user:
    password_encoder:
        cost: 17

它们将被传递给我的包扩展类:

# /src/Pkr/BlogUserBundle/DependencyInjection/PkrBlogUserExtension.php
namespace Pkr\BlogUserBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class PkrBlogUserExtension extends Extension
{
    /**
    * {@inheritDoc}
    */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');

        if ($config['password_encoder']['cost'] < 10) {
            $config['password_encoder']['cost'] = sprintf('%02d', $config['password_encoder']['cost']);
        }
        $container->setParameter('pkr_blog_user.wp_transitional_encoder.cost', $config['password_encoder']['cost']);

    }
}

我发现我可以使用自己的身份验证成功处理程序,因此有一个放置密码重写逻辑的好地方。不幸的是,当使用自定义处理程序时,symfony2不会将配置传递给类构造函数,但我找到了一种方法使其工作。我在这里描述了它:

https://stackoverflow.com/a/15988399/1089412