如何在注入的服务中访问用户令牌以重新编码密码? [英] How to access the user Token in an injected service to reencode passwords?
问题描述
我有下面的代码,当用户登录时我试图重新编码密码(数据库已从旧网站迁移).但是,我不确定自己在做什么错,因为我不断出错:
I have the below code where I am trying to re-encode passwords as users log in (the database has bee migrated form a legacy website). However, I'm not sure what I'm doing wrong as I keep getting errors:
试图调用类"AppBundle \ Service \ HubAuthenticator"的名为"forward"的未定义方法.
Attempted to call an undefined method named "forward" of class "AppBundle\Service\HubAuthenticator".
我将事情设置如下:
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 ) ;
}
}
以前进行过其他一些完整性检查:
我的猜测是,问题在于我误解了哪些对象对我可用.我的理解是,此时用户尚未通过身份验证,因此尝试并删除了以下尝试:
Some other previous attempts for completeness:
My guess is that the problem is that I am misunderstanding what objects are available to me. My understanding is that the user hasn't been authenticated at this point so have tried and removed the below attempts:
尝试将 $ member
注入到 HubAuthenticator
服务中:
function __construct($cost=13)
{
parent::__construct($cost, \Member $member);
}
尝试获取普通密码进行重新哈希处理时:
When trying to get the plainpassword to rehash:
$this->get('security.context')->getToken()->getUser()->getPlainPassword();
推荐答案
在您的服务中,您只能访问已注入的依赖项.
In your services, you can only access what dependencies you've injected.
因此,要访问当前用户对象,您需要将其作为参数传递:
So, to access the current user object, you need to pass it as argument:
服务:
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();
}
}
然后,以相同的方式访问第二个服务,将其注入.
Then, to access the second service, same way, inject it.
将其添加到服务参数中:
Add it to the service arguments:
club.password_rehash:
class: AppBundle\Service\PasswordRehash
arguments: [ "@security.token_storage", "@club.password_rehash" ]
将其添加到您的构造函数中:
Add it to your constructor:
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(...);
}
希望这对您有所帮助.
这篇关于如何在注入的服务中访问用户令牌以重新编码密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!