2013-04-07 133 views
1

我正在學習Symfony2通過移動一些wordpress博客到Symfony。我被登錄程序卡住了。 Wordpress使用非標準密碼散列,如$P$....,我想在用戶登錄時檢查舊密碼散列,並在密碼正確時將其重新轉換爲bcrypt。到目前爲止,我創建了custome編碼器類來與symfony安全機制一起使用。Symfony2自定義密碼編碼器和密碼哈希更新

<?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不起作用。

我應該在哪裏放置密碼哈希更新邏輯?我在想,莫比剛密碼驗證這樣的事情後:

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); 
} 

,但它似乎在某種程度上就像錯了地方吧。那麼正確的方法是什麼?

回答

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']); 

    } 
} 

我發現我可以用我自己的身份驗證成功的處理程序,以便有一個好的地方可以放置密碼rehash邏輯。不幸的是,在使用自定義處理程序時,symfony2不會將配置傳遞給類構造函數,但我找到了一種使其工作的方法。我在這裏描述:

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