如何访问注入服务中的用户令牌以重新编码密码?

How to access the user Token in an injected service to reencode passwords?

我有以下代码,我在其中尝试在用户登录时重新编码密码(数据库已从遗留网站迁移过来)。但是,我不确定自己做错了什么,因为我不断收到错误消息:

Attempted to call an undefined method named "forward" of class "AppBundle\Service\HubAuthenticator".

我设置如下:

security.yml

security:
    encoders:
        AppBundle\Entity\Member:
            id: club.hub_authenticator

services.yml

services:
    //This should be central service than then calls the second
    club.hub_authenticator:
        class: AppBundle\Service\HubAuthenticator

    club.password_rehash:
        class: AppBundle\Service\PasswordRehash

Hubauthenticator.php

namespace AppBundle\Service;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;

class HubAuthenticator extends \Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder implements PasswordEncoderInterface
{
    function __construct($cost=13)
    {
        parent::__construct($cost);
    }

    function isPasswordValid($encoded, $raw, $salt)
    {
        // Test for legacy authentication (and conditionally rehash the password stored in the database if true)
        if ($this->comparePasswords($encoded, sha1("saltA".$raw."saltB"))) {
            $this->forward('club.password_rehash:rehash');
        }

        // Test for Symfony's Bcrypt authentication (any passwords just rehashed in previous step should work here)
        if (parent::isPasswordValid($cost=13, $encoded,$raw,$salt)) return true ;
    }
}

PasswordRehash.php

namespace AppBundle\Service;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;

class PasswordRehash extends \Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder
{
    // Customises BCryptPasswordEncoder class to use legacy SHA method
    function rehash($member, $raw, $salt)
    {
        //Salt is null as Symfony documentation says it is better to generate a new one
        parent::encodePassword($member->getPlainPassword, $salt=null ) ;
    }
}

之前的其他一些完整性尝试:

我的 猜测 问题是我误解了我可以使用哪些对象。我的理解是此时用户尚未通过身份验证,因此尝试并删除了以下尝试:

正在尝试将 $member 注入 HubAuthenticator 服务:

function __construct($cost=13)
{
    parent::__construct($cost, \Member $member);
}

尝试获取要重新散列的明文密码时:

$this->get('security.context')->getToken()->getUser()->getPlainPassword();

在您的服务中,您只能访问您注入的依赖项。

因此,要访问当前用户对象,您需要将其作为参数传递:

服务:

club.password_rehash:
    class: AppBundle\Service\PasswordRehash
    arguments: [ "@security.token_storage" ]

构造函数:

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class HubAuthenticator extends \Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder implements PasswordEncoderInterface
{
    private $storage;

    function __construct($cost = 13, TokenStorageInterface $storage)
    {
        parent::__construct($cost);

        $this->storage = $storage;
        // Now you can use:
        // $user = $this->storage->getToken()->getUser();
    }
}

然后,访问第二个服务,同样的方法,注入它。

将其添加到服务参数中:

club.password_rehash:
    class: AppBundle\Service\PasswordRehash
    arguments: [ "@security.token_storage", "@club.password_rehash" ]

将其添加到您的构造函数中:

private $storage;
private $passwordRehash

function __construct($cost = 13, TokenStorageInterface $storage, PasswordRehash $passwordRehash)
{
    parent::__construct($cost);

    $this->storage = $storage;
    $this->passwordRehash = $passwordRehash;
    // Now you can use:
    // $this->passwordRehash->rehash(...);
}

希望对您有所帮助。